Commit | Line | Data |
---|---|---|
ddc64d0a | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * net/sunrpc/cache.c | |
4 | * | |
5 | * Generic code for various authentication-related caches | |
6 | * used by sunrpc clients and servers. | |
7 | * | |
8 | * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au> | |
1da177e4 LT |
9 | */ |
10 | ||
11 | #include <linux/types.h> | |
12 | #include <linux/fs.h> | |
13 | #include <linux/file.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/signal.h> | |
16 | #include <linux/sched.h> | |
17 | #include <linux/kmod.h> | |
18 | #include <linux/list.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/ctype.h> | |
1b2e122d | 21 | #include <linux/string_helpers.h> |
7c0f6ba6 | 22 | #include <linux/uaccess.h> |
1da177e4 LT |
23 | #include <linux/poll.h> |
24 | #include <linux/seq_file.h> | |
25 | #include <linux/proc_fs.h> | |
26 | #include <linux/net.h> | |
27 | #include <linux/workqueue.h> | |
4a3e2f71 | 28 | #include <linux/mutex.h> |
da77005f | 29 | #include <linux/pagemap.h> |
1da177e4 LT |
30 | #include <asm/ioctls.h> |
31 | #include <linux/sunrpc/types.h> | |
32 | #include <linux/sunrpc/cache.h> | |
33 | #include <linux/sunrpc/stats.h> | |
8854e82d | 34 | #include <linux/sunrpc/rpc_pipe_fs.h> |
78a947f5 | 35 | #include <trace/events/sunrpc.h> |
37324e6b | 36 | |
4f42d0d5 | 37 | #include "netns.h" |
37324e6b | 38 | #include "fail.h" |
1da177e4 LT |
39 | |
40 | #define RPCDBG_FACILITY RPCDBG_CACHE | |
41 | ||
d76d1815 | 42 | static bool cache_defer_req(struct cache_req *req, struct cache_head *item); |
1da177e4 LT |
43 | static void cache_revisit_request(struct cache_head *item); |
44 | ||
77862036 | 45 | static void cache_init(struct cache_head *h, struct cache_detail *detail) |
1da177e4 | 46 | { |
f559935e | 47 | time64_t now = seconds_since_boot(); |
129e5824 | 48 | INIT_HLIST_NODE(&h->cache_list); |
1da177e4 | 49 | h->flags = 0; |
baab935f | 50 | kref_init(&h->ref); |
1da177e4 | 51 | h->expiry_time = now + CACHE_NEW_EXPIRY; |
77862036 NB |
52 | if (now <= detail->flush_time) |
53 | /* ensure it isn't already expired */ | |
54 | now = detail->flush_time + 1; | |
1da177e4 LT |
55 | h->last_refresh = now; |
56 | } | |
57 | ||
4ecd55ea VA |
58 | static void cache_fresh_unlocked(struct cache_head *head, |
59 | struct cache_detail *detail); | |
60 | ||
ae74136b TM |
61 | static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail, |
62 | struct cache_head *key, | |
63 | int hash) | |
64 | { | |
65 | struct hlist_head *head = &detail->hash_table[hash]; | |
66 | struct cache_head *tmp; | |
67 | ||
68 | rcu_read_lock(); | |
69 | hlist_for_each_entry_rcu(tmp, head, cache_list) { | |
277f27e2 TM |
70 | if (!detail->match(tmp, key)) |
71 | continue; | |
72 | if (test_bit(CACHE_VALID, &tmp->flags) && | |
73 | cache_is_expired(detail, tmp)) | |
74 | continue; | |
75 | tmp = cache_get_rcu(tmp); | |
76 | rcu_read_unlock(); | |
77 | return tmp; | |
ae74136b TM |
78 | } |
79 | rcu_read_unlock(); | |
80 | return NULL; | |
81 | } | |
82 | ||
809fe3c5 TM |
83 | static void sunrpc_begin_cache_remove_entry(struct cache_head *ch, |
84 | struct cache_detail *cd) | |
85 | { | |
86 | /* Must be called under cd->hash_lock */ | |
87 | hlist_del_init_rcu(&ch->cache_list); | |
88 | set_bit(CACHE_CLEANED, &ch->flags); | |
89 | cd->entries --; | |
90 | } | |
91 | ||
92 | static void sunrpc_end_cache_remove_entry(struct cache_head *ch, | |
93 | struct cache_detail *cd) | |
94 | { | |
95 | cache_fresh_unlocked(ch, cd); | |
96 | cache_put(ch, cd); | |
97 | } | |
98 | ||
b92a8fab TM |
99 | static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail, |
100 | struct cache_head *key, | |
101 | int hash) | |
102 | { | |
103 | struct cache_head *new, *tmp, *freeme = NULL; | |
104 | struct hlist_head *head = &detail->hash_table[hash]; | |
15a5f6bd N |
105 | |
106 | new = detail->alloc(); | |
107 | if (!new) | |
108 | return NULL; | |
2f34931f NB |
109 | /* must fully initialise 'new', else |
110 | * we might get lose if we need to | |
111 | * cache_put it soon. | |
112 | */ | |
77862036 | 113 | cache_init(new, detail); |
2f34931f | 114 | detail->init(new, key); |
15a5f6bd | 115 | |
1863d77f | 116 | spin_lock(&detail->hash_lock); |
15a5f6bd N |
117 | |
118 | /* check if entry appeared while we slept */ | |
51cae673 AG |
119 | hlist_for_each_entry_rcu(tmp, head, cache_list, |
120 | lockdep_is_held(&detail->hash_lock)) { | |
277f27e2 TM |
121 | if (!detail->match(tmp, key)) |
122 | continue; | |
123 | if (test_bit(CACHE_VALID, &tmp->flags) && | |
124 | cache_is_expired(detail, tmp)) { | |
125 | sunrpc_begin_cache_remove_entry(tmp, detail); | |
78a947f5 | 126 | trace_cache_entry_expired(detail, tmp); |
277f27e2 TM |
127 | freeme = tmp; |
128 | break; | |
15a5f6bd | 129 | } |
277f27e2 TM |
130 | cache_get(tmp); |
131 | spin_unlock(&detail->hash_lock); | |
132 | cache_put(new, detail); | |
133 | return tmp; | |
15a5f6bd | 134 | } |
129e5824 | 135 | |
ae74136b | 136 | hlist_add_head_rcu(&new->cache_list, head); |
15a5f6bd N |
137 | detail->entries++; |
138 | cache_get(new); | |
1863d77f | 139 | spin_unlock(&detail->hash_lock); |
15a5f6bd | 140 | |
809fe3c5 TM |
141 | if (freeme) |
142 | sunrpc_end_cache_remove_entry(freeme, detail); | |
15a5f6bd N |
143 | return new; |
144 | } | |
15a5f6bd | 145 | |
ae74136b TM |
146 | struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail, |
147 | struct cache_head *key, int hash) | |
148 | { | |
149 | struct cache_head *ret; | |
150 | ||
151 | ret = sunrpc_cache_find_rcu(detail, key, hash); | |
152 | if (ret) | |
153 | return ret; | |
154 | /* Didn't find anything, insert an empty entry */ | |
155 | return sunrpc_cache_add_entry(detail, key, hash); | |
156 | } | |
157 | EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu); | |
158 | ||
f866a819 | 159 | static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch); |
ebd0cb1a | 160 | |
f559935e | 161 | static void cache_fresh_locked(struct cache_head *head, time64_t expiry, |
77862036 | 162 | struct cache_detail *detail) |
ebd0cb1a | 163 | { |
f559935e | 164 | time64_t now = seconds_since_boot(); |
77862036 NB |
165 | if (now <= detail->flush_time) |
166 | /* ensure it isn't immediately treated as expired */ | |
167 | now = detail->flush_time + 1; | |
ebd0cb1a | 168 | head->expiry_time = expiry; |
77862036 | 169 | head->last_refresh = now; |
fdef7aa5 | 170 | smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */ |
908329f2 | 171 | set_bit(CACHE_VALID, &head->flags); |
ebd0cb1a N |
172 | } |
173 | ||
174 | static void cache_fresh_unlocked(struct cache_head *head, | |
908329f2 | 175 | struct cache_detail *detail) |
ebd0cb1a | 176 | { |
ebd0cb1a N |
177 | if (test_and_clear_bit(CACHE_PENDING, &head->flags)) { |
178 | cache_revisit_request(head); | |
f866a819 | 179 | cache_dequeue(detail, head); |
ebd0cb1a N |
180 | } |
181 | } | |
182 | ||
78a947f5 TM |
183 | static void cache_make_negative(struct cache_detail *detail, |
184 | struct cache_head *h) | |
185 | { | |
186 | set_bit(CACHE_NEGATIVE, &h->flags); | |
187 | trace_cache_entry_make_negative(detail, h); | |
188 | } | |
189 | ||
190 | static void cache_entry_update(struct cache_detail *detail, | |
191 | struct cache_head *h, | |
192 | struct cache_head *new) | |
193 | { | |
194 | if (!test_bit(CACHE_NEGATIVE, &new->flags)) { | |
195 | detail->update(h, new); | |
196 | trace_cache_entry_update(detail, h); | |
197 | } else { | |
198 | cache_make_negative(detail, h); | |
199 | } | |
200 | } | |
201 | ||
15a5f6bd N |
202 | struct cache_head *sunrpc_cache_update(struct cache_detail *detail, |
203 | struct cache_head *new, struct cache_head *old, int hash) | |
204 | { | |
205 | /* The 'old' entry is to be replaced by 'new'. | |
206 | * If 'old' is not VALID, we update it directly, | |
207 | * otherwise we need to replace it | |
208 | */ | |
15a5f6bd N |
209 | struct cache_head *tmp; |
210 | ||
211 | if (!test_bit(CACHE_VALID, &old->flags)) { | |
1863d77f | 212 | spin_lock(&detail->hash_lock); |
15a5f6bd | 213 | if (!test_bit(CACHE_VALID, &old->flags)) { |
78a947f5 | 214 | cache_entry_update(detail, old, new); |
77862036 | 215 | cache_fresh_locked(old, new->expiry_time, detail); |
1863d77f | 216 | spin_unlock(&detail->hash_lock); |
908329f2 | 217 | cache_fresh_unlocked(old, detail); |
15a5f6bd N |
218 | return old; |
219 | } | |
1863d77f | 220 | spin_unlock(&detail->hash_lock); |
15a5f6bd N |
221 | } |
222 | /* We need to insert a new entry */ | |
223 | tmp = detail->alloc(); | |
224 | if (!tmp) { | |
baab935f | 225 | cache_put(old, detail); |
15a5f6bd N |
226 | return NULL; |
227 | } | |
77862036 | 228 | cache_init(tmp, detail); |
15a5f6bd | 229 | detail->init(tmp, old); |
15a5f6bd | 230 | |
1863d77f | 231 | spin_lock(&detail->hash_lock); |
78a947f5 | 232 | cache_entry_update(detail, tmp, new); |
129e5824 | 233 | hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]); |
f2d39586 | 234 | detail->entries++; |
15a5f6bd | 235 | cache_get(tmp); |
77862036 NB |
236 | cache_fresh_locked(tmp, new->expiry_time, detail); |
237 | cache_fresh_locked(old, 0, detail); | |
1863d77f | 238 | spin_unlock(&detail->hash_lock); |
908329f2 N |
239 | cache_fresh_unlocked(tmp, detail); |
240 | cache_fresh_unlocked(old, detail); | |
baab935f | 241 | cache_put(old, detail); |
15a5f6bd N |
242 | return tmp; |
243 | } | |
24c3767e | 244 | EXPORT_SYMBOL_GPL(sunrpc_cache_update); |
1da177e4 | 245 | |
b6040f97 | 246 | static inline int cache_is_valid(struct cache_head *h) |
989a19b9 | 247 | { |
d202cce8 | 248 | if (!test_bit(CACHE_VALID, &h->flags)) |
989a19b9 N |
249 | return -EAGAIN; |
250 | else { | |
251 | /* entry is valid */ | |
252 | if (test_bit(CACHE_NEGATIVE, &h->flags)) | |
253 | return -ENOENT; | |
fdef7aa5 BF |
254 | else { |
255 | /* | |
256 | * In combination with write barrier in | |
257 | * sunrpc_cache_update, ensures that anyone | |
258 | * using the cache entry after this sees the | |
259 | * updated contents: | |
260 | */ | |
261 | smp_rmb(); | |
989a19b9 | 262 | return 0; |
fdef7aa5 | 263 | } |
989a19b9 N |
264 | } |
265 | } | |
e9dc1221 | 266 | |
6bab93f8 BF |
267 | static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h) |
268 | { | |
269 | int rv; | |
270 | ||
1863d77f | 271 | spin_lock(&detail->hash_lock); |
b6040f97 | 272 | rv = cache_is_valid(h); |
2a1c7f53 | 273 | if (rv == -EAGAIN) { |
78a947f5 | 274 | cache_make_negative(detail, h); |
77862036 NB |
275 | cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY, |
276 | detail); | |
2a1c7f53 | 277 | rv = -ENOENT; |
6bab93f8 | 278 | } |
1863d77f | 279 | spin_unlock(&detail->hash_lock); |
6bab93f8 | 280 | cache_fresh_unlocked(h, detail); |
2a1c7f53 | 281 | return rv; |
6bab93f8 BF |
282 | } |
283 | ||
2f55dbe4 | 284 | int cache_check_rcu(struct cache_detail *detail, |
1da177e4 LT |
285 | struct cache_head *h, struct cache_req *rqstp) |
286 | { | |
287 | int rv; | |
f559935e | 288 | time64_t refresh_age, age; |
1da177e4 LT |
289 | |
290 | /* First decide return status as best we can */ | |
b6040f97 | 291 | rv = cache_is_valid(h); |
1da177e4 LT |
292 | |
293 | /* now see if we want to start an upcall */ | |
294 | refresh_age = (h->expiry_time - h->last_refresh); | |
c5b29f88 | 295 | age = seconds_since_boot() - h->last_refresh; |
1da177e4 LT |
296 | |
297 | if (rqstp == NULL) { | |
298 | if (rv == -EAGAIN) | |
299 | rv = -ENOENT; | |
0bebc633 N |
300 | } else if (rv == -EAGAIN || |
301 | (h->expiry_time != 0 && age > refresh_age/2)) { | |
f559935e | 302 | dprintk("RPC: Want update, refage=%lld, age=%lld\n", |
46121cf7 | 303 | refresh_age, age); |
65286b88 TM |
304 | switch (detail->cache_upcall(detail, h)) { |
305 | case -EINVAL: | |
9d69338c | 306 | rv = try_to_negate_entry(detail, h); |
65286b88 TM |
307 | break; |
308 | case -EAGAIN: | |
309 | cache_fresh_unlocked(h, detail); | |
310 | break; | |
311 | } | |
1da177e4 LT |
312 | } |
313 | ||
989a19b9 | 314 | if (rv == -EAGAIN) { |
d76d1815 BF |
315 | if (!cache_defer_req(rqstp, h)) { |
316 | /* | |
317 | * Request was not deferred; handle it as best | |
318 | * we can ourselves: | |
319 | */ | |
b6040f97 | 320 | rv = cache_is_valid(h); |
989a19b9 N |
321 | if (rv == -EAGAIN) |
322 | rv = -ETIMEDOUT; | |
323 | } | |
324 | } | |
2f55dbe4 YE |
325 | |
326 | return rv; | |
327 | } | |
328 | EXPORT_SYMBOL_GPL(cache_check_rcu); | |
329 | ||
330 | /* | |
331 | * This is the generic cache management routine for all | |
332 | * the authentication caches. | |
333 | * It checks the currency of a cache item and will (later) | |
334 | * initiate an upcall to fill it if needed. | |
335 | * | |
336 | * | |
337 | * Returns 0 if the cache_head can be used, or cache_puts it and returns | |
338 | * -EAGAIN if upcall is pending and request has been queued | |
339 | * -ETIMEDOUT if upcall failed or request could not be queue or | |
340 | * upcall completed but item is still invalid (implying that | |
341 | * the cache item has been replaced with a newer one). | |
342 | * -ENOENT if cache entry was negative | |
343 | */ | |
344 | int cache_check(struct cache_detail *detail, | |
345 | struct cache_head *h, struct cache_req *rqstp) | |
346 | { | |
347 | int rv; | |
348 | ||
349 | rv = cache_check_rcu(detail, h, rqstp); | |
4013edea | 350 | if (rv) |
baab935f | 351 | cache_put(h, detail); |
1da177e4 LT |
352 | return rv; |
353 | } | |
24c3767e | 354 | EXPORT_SYMBOL_GPL(cache_check); |
1da177e4 | 355 | |
1da177e4 LT |
356 | /* |
357 | * caches need to be periodically cleaned. | |
358 | * For this we maintain a list of cache_detail and | |
359 | * a current pointer into that list and into the table | |
360 | * for that entry. | |
361 | * | |
013920eb | 362 | * Each time cache_clean is called it finds the next non-empty entry |
1da177e4 LT |
363 | * in the current table and walks the list in that entry |
364 | * looking for entries that can be removed. | |
365 | * | |
366 | * An entry gets removed if: | |
367 | * - The expiry is before current time | |
368 | * - The last_refresh time is before the flush_time for that cache | |
369 | * | |
370 | * later we might drop old entries with non-NEVER expiry if that table | |
371 | * is getting 'full' for some definition of 'full' | |
372 | * | |
373 | * The question of "how often to scan a table" is an interesting one | |
374 | * and is answered in part by the use of the "nextcheck" field in the | |
375 | * cache_detail. | |
376 | * When a scan of a table begins, the nextcheck field is set to a time | |
377 | * that is well into the future. | |
378 | * While scanning, if an expiry time is found that is earlier than the | |
379 | * current nextcheck time, nextcheck is set to that expiry time. | |
380 | * If the flush_time is ever set to a time earlier than the nextcheck | |
381 | * time, the nextcheck time is then set to that flush_time. | |
382 | * | |
383 | * A table is then only scanned if the current time is at least | |
384 | * the nextcheck time. | |
cca5172a | 385 | * |
1da177e4 LT |
386 | */ |
387 | ||
388 | static LIST_HEAD(cache_list); | |
389 | static DEFINE_SPINLOCK(cache_list_lock); | |
390 | static struct cache_detail *current_detail; | |
391 | static int current_index; | |
392 | ||
65f27f38 | 393 | static void do_cache_clean(struct work_struct *work); |
8eab945c | 394 | static struct delayed_work cache_cleaner; |
1da177e4 | 395 | |
820f9442 | 396 | void sunrpc_init_cache_detail(struct cache_detail *cd) |
ffe9386b | 397 | { |
1863d77f | 398 | spin_lock_init(&cd->hash_lock); |
1da177e4 LT |
399 | INIT_LIST_HEAD(&cd->queue); |
400 | spin_lock(&cache_list_lock); | |
401 | cd->nextcheck = 0; | |
402 | cd->entries = 0; | |
64a38e84 | 403 | atomic_set(&cd->writers, 0); |
1da177e4 LT |
404 | cd->last_close = 0; |
405 | cd->last_warn = -1; | |
406 | list_add(&cd->others, &cache_list); | |
407 | spin_unlock(&cache_list_lock); | |
408 | ||
409 | /* start the cleaning process */ | |
77b00bc0 | 410 | queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0); |
1da177e4 | 411 | } |
820f9442 | 412 | EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail); |
1da177e4 | 413 | |
820f9442 | 414 | void sunrpc_destroy_cache_detail(struct cache_detail *cd) |
1da177e4 LT |
415 | { |
416 | cache_purge(cd); | |
417 | spin_lock(&cache_list_lock); | |
1863d77f | 418 | spin_lock(&cd->hash_lock); |
1da177e4 LT |
419 | if (current_detail == cd) |
420 | current_detail = NULL; | |
421 | list_del_init(&cd->others); | |
1863d77f | 422 | spin_unlock(&cd->hash_lock); |
1da177e4 | 423 | spin_unlock(&cache_list_lock); |
1da177e4 LT |
424 | if (list_empty(&cache_list)) { |
425 | /* module must be being unloaded so its safe to kill the worker */ | |
4011cd97 | 426 | cancel_delayed_work_sync(&cache_cleaner); |
1da177e4 | 427 | } |
1da177e4 | 428 | } |
820f9442 | 429 | EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail); |
1da177e4 LT |
430 | |
431 | /* clean cache tries to find something to clean | |
432 | * and cleans it. | |
433 | * It returns 1 if it cleaned something, | |
434 | * 0 if it didn't find anything this time | |
435 | * -1 if it fell off the end of the list. | |
436 | */ | |
437 | static int cache_clean(void) | |
438 | { | |
439 | int rv = 0; | |
440 | struct list_head *next; | |
441 | ||
442 | spin_lock(&cache_list_lock); | |
443 | ||
444 | /* find a suitable table if we don't already have one */ | |
445 | while (current_detail == NULL || | |
446 | current_index >= current_detail->hash_size) { | |
447 | if (current_detail) | |
448 | next = current_detail->others.next; | |
449 | else | |
450 | next = cache_list.next; | |
451 | if (next == &cache_list) { | |
452 | current_detail = NULL; | |
453 | spin_unlock(&cache_list_lock); | |
454 | return -1; | |
455 | } | |
456 | current_detail = list_entry(next, struct cache_detail, others); | |
c5b29f88 | 457 | if (current_detail->nextcheck > seconds_since_boot()) |
1da177e4 LT |
458 | current_index = current_detail->hash_size; |
459 | else { | |
460 | current_index = 0; | |
c5b29f88 | 461 | current_detail->nextcheck = seconds_since_boot()+30*60; |
1da177e4 LT |
462 | } |
463 | } | |
464 | ||
465 | /* find a non-empty bucket in the table */ | |
466 | while (current_detail && | |
467 | current_index < current_detail->hash_size && | |
129e5824 | 468 | hlist_empty(¤t_detail->hash_table[current_index])) |
1da177e4 LT |
469 | current_index++; |
470 | ||
471 | /* find a cleanable entry in the bucket and clean it, or set to next bucket */ | |
cca5172a | 472 | |
1da177e4 | 473 | if (current_detail && current_index < current_detail->hash_size) { |
129e5824 | 474 | struct cache_head *ch = NULL; |
1da177e4 | 475 | struct cache_detail *d; |
129e5824 KM |
476 | struct hlist_head *head; |
477 | struct hlist_node *tmp; | |
cca5172a | 478 | |
1863d77f | 479 | spin_lock(¤t_detail->hash_lock); |
1da177e4 LT |
480 | |
481 | /* Ok, now to clean this strand */ | |
cca5172a | 482 | |
129e5824 KM |
483 | head = ¤t_detail->hash_table[current_index]; |
484 | hlist_for_each_entry_safe(ch, tmp, head, cache_list) { | |
1da177e4 LT |
485 | if (current_detail->nextcheck > ch->expiry_time) |
486 | current_detail->nextcheck = ch->expiry_time+1; | |
2f50d8b6 | 487 | if (!cache_is_expired(current_detail, ch)) |
1da177e4 | 488 | continue; |
1da177e4 | 489 | |
809fe3c5 | 490 | sunrpc_begin_cache_remove_entry(ch, current_detail); |
78a947f5 | 491 | trace_cache_entry_expired(current_detail, ch); |
1da177e4 | 492 | rv = 1; |
3af4974e | 493 | break; |
1da177e4 | 494 | } |
3af4974e | 495 | |
1863d77f | 496 | spin_unlock(¤t_detail->hash_lock); |
1da177e4 LT |
497 | d = current_detail; |
498 | if (!ch) | |
499 | current_index ++; | |
500 | spin_unlock(&cache_list_lock); | |
809fe3c5 TM |
501 | if (ch) |
502 | sunrpc_end_cache_remove_entry(ch, d); | |
1da177e4 LT |
503 | } else |
504 | spin_unlock(&cache_list_lock); | |
505 | ||
506 | return rv; | |
507 | } | |
508 | ||
509 | /* | |
510 | * We want to regularly clean the cache, so we need to schedule some work ... | |
511 | */ | |
65f27f38 | 512 | static void do_cache_clean(struct work_struct *work) |
1da177e4 | 513 | { |
0aa99c4d | 514 | int delay; |
1da177e4 LT |
515 | |
516 | if (list_empty(&cache_list)) | |
0aa99c4d BF |
517 | return; |
518 | ||
519 | if (cache_clean() == -1) | |
520 | delay = round_jiffies_relative(30*HZ); | |
521 | else | |
522 | delay = 5; | |
1da177e4 | 523 | |
0aa99c4d | 524 | queue_delayed_work(system_power_efficient_wq, &cache_cleaner, delay); |
1da177e4 LT |
525 | } |
526 | ||
527 | ||
cca5172a | 528 | /* |
1da177e4 | 529 | * Clean all caches promptly. This just calls cache_clean |
cca5172a | 530 | * repeatedly until we are sure that every cache has had a chance to |
1da177e4 LT |
531 | * be fully cleaned |
532 | */ | |
533 | void cache_flush(void) | |
534 | { | |
535 | while (cache_clean() != -1) | |
536 | cond_resched(); | |
537 | while (cache_clean() != -1) | |
538 | cond_resched(); | |
539 | } | |
24c3767e | 540 | EXPORT_SYMBOL_GPL(cache_flush); |
1da177e4 LT |
541 | |
542 | void cache_purge(struct cache_detail *detail) | |
543 | { | |
471a930a KM |
544 | struct cache_head *ch = NULL; |
545 | struct hlist_head *head = NULL; | |
471a930a KM |
546 | int i = 0; |
547 | ||
1863d77f | 548 | spin_lock(&detail->hash_lock); |
471a930a | 549 | if (!detail->entries) { |
1863d77f | 550 | spin_unlock(&detail->hash_lock); |
471a930a KM |
551 | return; |
552 | } | |
553 | ||
554 | dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name); | |
555 | for (i = 0; i < detail->hash_size; i++) { | |
556 | head = &detail->hash_table[i]; | |
43e33924 YW |
557 | while (!hlist_empty(head)) { |
558 | ch = hlist_entry(head->first, struct cache_head, | |
559 | cache_list); | |
809fe3c5 | 560 | sunrpc_begin_cache_remove_entry(ch, detail); |
1863d77f | 561 | spin_unlock(&detail->hash_lock); |
809fe3c5 | 562 | sunrpc_end_cache_remove_entry(ch, detail); |
1863d77f | 563 | spin_lock(&detail->hash_lock); |
471a930a KM |
564 | } |
565 | } | |
1863d77f | 566 | spin_unlock(&detail->hash_lock); |
1da177e4 | 567 | } |
24c3767e | 568 | EXPORT_SYMBOL_GPL(cache_purge); |
1da177e4 LT |
569 | |
570 | ||
571 | /* | |
572 | * Deferral and Revisiting of Requests. | |
573 | * | |
574 | * If a cache lookup finds a pending entry, we | |
575 | * need to defer the request and revisit it later. | |
576 | * All deferred requests are stored in a hash table, | |
577 | * indexed by "struct cache_head *". | |
578 | * As it may be wasteful to store a whole request | |
cca5172a | 579 | * structure, we allow the request to provide a |
1da177e4 LT |
580 | * deferred form, which must contain a |
581 | * 'struct cache_deferred_req' | |
582 | * This cache_deferred_req contains a method to allow | |
583 | * it to be revisited when cache info is available | |
584 | */ | |
585 | ||
586 | #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head)) | |
587 | #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE) | |
588 | ||
589 | #define DFR_MAX 300 /* ??? */ | |
590 | ||
591 | static DEFINE_SPINLOCK(cache_defer_lock); | |
592 | static LIST_HEAD(cache_defer_list); | |
11174492 | 593 | static struct hlist_head cache_defer_hash[DFR_HASHSIZE]; |
1da177e4 LT |
594 | static int cache_defer_cnt; |
595 | ||
6610f720 BF |
596 | static void __unhash_deferred_req(struct cache_deferred_req *dreq) |
597 | { | |
11174492 | 598 | hlist_del_init(&dreq->hash); |
e33534d5 N |
599 | if (!list_empty(&dreq->recent)) { |
600 | list_del_init(&dreq->recent); | |
601 | cache_defer_cnt--; | |
602 | } | |
6610f720 BF |
603 | } |
604 | ||
605 | static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item) | |
1da177e4 | 606 | { |
1da177e4 LT |
607 | int hash = DFR_HASH(item); |
608 | ||
e33534d5 | 609 | INIT_LIST_HEAD(&dreq->recent); |
11174492 | 610 | hlist_add_head(&dreq->hash, &cache_defer_hash[hash]); |
6610f720 BF |
611 | } |
612 | ||
e33534d5 N |
613 | static void setup_deferral(struct cache_deferred_req *dreq, |
614 | struct cache_head *item, | |
615 | int count_me) | |
1da177e4 | 616 | { |
1da177e4 LT |
617 | |
618 | dreq->item = item; | |
1da177e4 LT |
619 | |
620 | spin_lock(&cache_defer_lock); | |
621 | ||
6610f720 | 622 | __hash_deferred_req(dreq, item); |
1da177e4 | 623 | |
e33534d5 N |
624 | if (count_me) { |
625 | cache_defer_cnt++; | |
626 | list_add(&dreq->recent, &cache_defer_list); | |
1da177e4 | 627 | } |
e33534d5 | 628 | |
1da177e4 LT |
629 | spin_unlock(&cache_defer_lock); |
630 | ||
3211af11 | 631 | } |
f16b6e8d | 632 | |
3211af11 BF |
633 | struct thread_deferred_req { |
634 | struct cache_deferred_req handle; | |
635 | struct completion completion; | |
636 | }; | |
637 | ||
638 | static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many) | |
639 | { | |
640 | struct thread_deferred_req *dr = | |
641 | container_of(dreq, struct thread_deferred_req, handle); | |
642 | complete(&dr->completion); | |
643 | } | |
644 | ||
d29068c4 | 645 | static void cache_wait_req(struct cache_req *req, struct cache_head *item) |
3211af11 BF |
646 | { |
647 | struct thread_deferred_req sleeper; | |
648 | struct cache_deferred_req *dreq = &sleeper.handle; | |
3211af11 BF |
649 | |
650 | sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion); | |
651 | dreq->revisit = cache_restart_thread; | |
652 | ||
e33534d5 | 653 | setup_deferral(dreq, item, 0); |
3211af11 | 654 | |
d29068c4 | 655 | if (!test_bit(CACHE_PENDING, &item->flags) || |
277f68db | 656 | wait_for_completion_interruptible_timeout( |
3211af11 BF |
657 | &sleeper.completion, req->thread_wait) <= 0) { |
658 | /* The completion wasn't completed, so we need | |
659 | * to clean up | |
660 | */ | |
661 | spin_lock(&cache_defer_lock); | |
11174492 | 662 | if (!hlist_unhashed(&sleeper.handle.hash)) { |
3211af11 BF |
663 | __unhash_deferred_req(&sleeper.handle); |
664 | spin_unlock(&cache_defer_lock); | |
665 | } else { | |
666 | /* cache_revisit_request already removed | |
667 | * this from the hash table, but hasn't | |
668 | * called ->revisit yet. It will very soon | |
669 | * and we need to wait for it. | |
f16b6e8d | 670 | */ |
3211af11 BF |
671 | spin_unlock(&cache_defer_lock); |
672 | wait_for_completion(&sleeper.completion); | |
f16b6e8d | 673 | } |
3211af11 | 674 | } |
3211af11 BF |
675 | } |
676 | ||
e33534d5 | 677 | static void cache_limit_defers(void) |
3211af11 | 678 | { |
e33534d5 N |
679 | /* Make sure we haven't exceed the limit of allowed deferred |
680 | * requests. | |
681 | */ | |
682 | struct cache_deferred_req *discard = NULL; | |
3211af11 | 683 | |
e33534d5 N |
684 | if (cache_defer_cnt <= DFR_MAX) |
685 | return; | |
d29068c4 | 686 | |
e33534d5 N |
687 | spin_lock(&cache_defer_lock); |
688 | ||
689 | /* Consider removing either the first or the last */ | |
690 | if (cache_defer_cnt > DFR_MAX) { | |
8032bf12 | 691 | if (get_random_u32_below(2)) |
e33534d5 N |
692 | discard = list_entry(cache_defer_list.next, |
693 | struct cache_deferred_req, recent); | |
694 | else | |
695 | discard = list_entry(cache_defer_list.prev, | |
696 | struct cache_deferred_req, recent); | |
697 | __unhash_deferred_req(discard); | |
698 | } | |
699 | spin_unlock(&cache_defer_lock); | |
cd68c374 | 700 | if (discard) |
cd68c374 | 701 | discard->revisit(discard, 1); |
e33534d5 | 702 | } |
cd68c374 | 703 | |
37324e6b CL |
704 | #if IS_ENABLED(CONFIG_FAIL_SUNRPC) |
705 | static inline bool cache_defer_immediately(void) | |
706 | { | |
707 | return !fail_sunrpc.ignore_cache_wait && | |
708 | should_fail(&fail_sunrpc.attr, 1); | |
709 | } | |
710 | #else | |
711 | static inline bool cache_defer_immediately(void) | |
712 | { | |
713 | return false; | |
714 | } | |
715 | #endif | |
716 | ||
d76d1815 BF |
717 | /* Return true if and only if a deferred request is queued. */ |
718 | static bool cache_defer_req(struct cache_req *req, struct cache_head *item) | |
e33534d5 N |
719 | { |
720 | struct cache_deferred_req *dreq; | |
d29068c4 | 721 | |
37324e6b | 722 | if (!cache_defer_immediately()) { |
d29068c4 N |
723 | cache_wait_req(req, item); |
724 | if (!test_bit(CACHE_PENDING, &item->flags)) | |
d76d1815 | 725 | return false; |
1da177e4 | 726 | } |
37324e6b | 727 | |
3211af11 BF |
728 | dreq = req->defer(req); |
729 | if (dreq == NULL) | |
d76d1815 | 730 | return false; |
e33534d5 | 731 | setup_deferral(dreq, item, 1); |
d29068c4 N |
732 | if (!test_bit(CACHE_PENDING, &item->flags)) |
733 | /* Bit could have been cleared before we managed to | |
734 | * set up the deferral, so need to revisit just in case | |
735 | */ | |
736 | cache_revisit_request(item); | |
e33534d5 N |
737 | |
738 | cache_limit_defers(); | |
d76d1815 | 739 | return true; |
1da177e4 LT |
740 | } |
741 | ||
742 | static void cache_revisit_request(struct cache_head *item) | |
743 | { | |
744 | struct cache_deferred_req *dreq; | |
b67bfe0d | 745 | struct hlist_node *tmp; |
1da177e4 | 746 | int hash = DFR_HASH(item); |
64a3ab99 | 747 | LIST_HEAD(pending); |
1da177e4 | 748 | |
1da177e4 | 749 | spin_lock(&cache_defer_lock); |
cca5172a | 750 | |
b67bfe0d | 751 | hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash) |
11174492 N |
752 | if (dreq->item == item) { |
753 | __unhash_deferred_req(dreq); | |
754 | list_add(&dreq->recent, &pending); | |
1da177e4 | 755 | } |
11174492 | 756 | |
1da177e4 LT |
757 | spin_unlock(&cache_defer_lock); |
758 | ||
759 | while (!list_empty(&pending)) { | |
760 | dreq = list_entry(pending.next, struct cache_deferred_req, recent); | |
761 | list_del_init(&dreq->recent); | |
762 | dreq->revisit(dreq, 0); | |
763 | } | |
764 | } | |
765 | ||
766 | void cache_clean_deferred(void *owner) | |
767 | { | |
768 | struct cache_deferred_req *dreq, *tmp; | |
64a3ab99 | 769 | LIST_HEAD(pending); |
1da177e4 | 770 | |
1da177e4 | 771 | spin_lock(&cache_defer_lock); |
cca5172a | 772 | |
1da177e4 LT |
773 | list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) { |
774 | if (dreq->owner == owner) { | |
6610f720 | 775 | __unhash_deferred_req(dreq); |
e95dffa4 | 776 | list_add(&dreq->recent, &pending); |
1da177e4 LT |
777 | } |
778 | } | |
779 | spin_unlock(&cache_defer_lock); | |
780 | ||
781 | while (!list_empty(&pending)) { | |
782 | dreq = list_entry(pending.next, struct cache_deferred_req, recent); | |
783 | list_del_init(&dreq->recent); | |
784 | dreq->revisit(dreq, 1); | |
785 | } | |
786 | } | |
787 | ||
788 | /* | |
789 | * communicate with user-space | |
790 | * | |
6489a8f4 | 791 | * We have a magic /proc file - /proc/net/rpc/<cachename>/channel. |
a490c681 BF |
792 | * On read, you get a full request, or block. |
793 | * On write, an update request is processed. | |
794 | * Poll works if anything to read, and always allows write. | |
1da177e4 | 795 | * |
cca5172a | 796 | * Implemented by linked list of requests. Each open file has |
a490c681 | 797 | * a ->private that also exists in this list. New requests are added |
1da177e4 LT |
798 | * to the end and may wakeup and preceding readers. |
799 | * New readers are added to the head. If, on read, an item is found with | |
800 | * CACHE_UPCALLING clear, we free it from the list. | |
801 | * | |
802 | */ | |
803 | ||
804 | static DEFINE_SPINLOCK(queue_lock); | |
1da177e4 LT |
805 | |
806 | struct cache_queue { | |
807 | struct list_head list; | |
808 | int reader; /* if 0, then request */ | |
809 | }; | |
810 | struct cache_request { | |
811 | struct cache_queue q; | |
812 | struct cache_head *item; | |
813 | char * buf; | |
814 | int len; | |
815 | int readers; | |
816 | }; | |
817 | struct cache_reader { | |
818 | struct cache_queue q; | |
819 | int offset; /* if non-0, we have a refcnt on next request */ | |
820 | }; | |
821 | ||
d94af6de SK |
822 | static int cache_request(struct cache_detail *detail, |
823 | struct cache_request *crq) | |
824 | { | |
825 | char *bp = crq->buf; | |
826 | int len = PAGE_SIZE; | |
827 | ||
828 | detail->cache_request(detail, crq->item, &bp, &len); | |
829 | if (len < 0) | |
0c217d50 | 830 | return -E2BIG; |
d94af6de SK |
831 | return PAGE_SIZE - len; |
832 | } | |
833 | ||
173912a6 TM |
834 | static ssize_t cache_read(struct file *filp, char __user *buf, size_t count, |
835 | loff_t *ppos, struct cache_detail *cd) | |
1da177e4 LT |
836 | { |
837 | struct cache_reader *rp = filp->private_data; | |
838 | struct cache_request *rq; | |
496ad9aa | 839 | struct inode *inode = file_inode(filp); |
1da177e4 LT |
840 | int err; |
841 | ||
842 | if (count == 0) | |
843 | return 0; | |
844 | ||
5955102c | 845 | inode_lock(inode); /* protect against multiple concurrent |
1da177e4 LT |
846 | * readers on this file */ |
847 | again: | |
848 | spin_lock(&queue_lock); | |
849 | /* need to find next request */ | |
850 | while (rp->q.list.next != &cd->queue && | |
851 | list_entry(rp->q.list.next, struct cache_queue, list) | |
852 | ->reader) { | |
853 | struct list_head *next = rp->q.list.next; | |
854 | list_move(&rp->q.list, next); | |
855 | } | |
856 | if (rp->q.list.next == &cd->queue) { | |
857 | spin_unlock(&queue_lock); | |
5955102c | 858 | inode_unlock(inode); |
0db74d9a | 859 | WARN_ON_ONCE(rp->offset); |
1da177e4 LT |
860 | return 0; |
861 | } | |
862 | rq = container_of(rp->q.list.next, struct cache_request, q.list); | |
0db74d9a | 863 | WARN_ON_ONCE(rq->q.reader); |
1da177e4 LT |
864 | if (rp->offset == 0) |
865 | rq->readers++; | |
866 | spin_unlock(&queue_lock); | |
867 | ||
d94af6de SK |
868 | if (rq->len == 0) { |
869 | err = cache_request(cd, rq); | |
870 | if (err < 0) | |
871 | goto out; | |
872 | rq->len = err; | |
873 | } | |
874 | ||
1da177e4 LT |
875 | if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) { |
876 | err = -EAGAIN; | |
877 | spin_lock(&queue_lock); | |
878 | list_move(&rp->q.list, &rq->q.list); | |
879 | spin_unlock(&queue_lock); | |
880 | } else { | |
881 | if (rp->offset + count > rq->len) | |
882 | count = rq->len - rp->offset; | |
883 | err = -EFAULT; | |
884 | if (copy_to_user(buf, rq->buf + rp->offset, count)) | |
885 | goto out; | |
886 | rp->offset += count; | |
887 | if (rp->offset >= rq->len) { | |
888 | rp->offset = 0; | |
889 | spin_lock(&queue_lock); | |
890 | list_move(&rp->q.list, &rq->q.list); | |
891 | spin_unlock(&queue_lock); | |
892 | } | |
893 | err = 0; | |
894 | } | |
895 | out: | |
896 | if (rp->offset == 0) { | |
897 | /* need to release rq */ | |
898 | spin_lock(&queue_lock); | |
899 | rq->readers--; | |
900 | if (rq->readers == 0 && | |
901 | !test_bit(CACHE_PENDING, &rq->item->flags)) { | |
902 | list_del(&rq->q.list); | |
903 | spin_unlock(&queue_lock); | |
baab935f | 904 | cache_put(rq->item, cd); |
1da177e4 LT |
905 | kfree(rq->buf); |
906 | kfree(rq); | |
907 | } else | |
908 | spin_unlock(&queue_lock); | |
909 | } | |
910 | if (err == -EAGAIN) | |
911 | goto again; | |
5955102c | 912 | inode_unlock(inode); |
1da177e4 LT |
913 | return err ? err : count; |
914 | } | |
915 | ||
da77005f TM |
916 | static ssize_t cache_do_downcall(char *kaddr, const char __user *buf, |
917 | size_t count, struct cache_detail *cd) | |
918 | { | |
919 | ssize_t ret; | |
1da177e4 | 920 | |
6d8d1749 DC |
921 | if (count == 0) |
922 | return -EINVAL; | |
da77005f TM |
923 | if (copy_from_user(kaddr, buf, count)) |
924 | return -EFAULT; | |
925 | kaddr[count] = '\0'; | |
926 | ret = cd->cache_parse(cd, kaddr, count); | |
927 | if (!ret) | |
928 | ret = count; | |
929 | return ret; | |
930 | } | |
931 | ||
da77005f TM |
932 | static ssize_t cache_downcall(struct address_space *mapping, |
933 | const char __user *buf, | |
934 | size_t count, struct cache_detail *cd) | |
935 | { | |
4b5cff7e | 936 | char *write_buf; |
da77005f TM |
937 | ssize_t ret = -ENOMEM; |
938 | ||
4b5cff7e RBC |
939 | if (count >= 32768) { /* 32k is max userland buffer, lets check anyway */ |
940 | ret = -EINVAL; | |
941 | goto out; | |
942 | } | |
da77005f | 943 | |
4b5cff7e RBC |
944 | write_buf = kvmalloc(count + 1, GFP_KERNEL); |
945 | if (!write_buf) | |
946 | goto out; | |
da77005f | 947 | |
4b5cff7e RBC |
948 | ret = cache_do_downcall(write_buf, buf, count, cd); |
949 | kvfree(write_buf); | |
950 | out: | |
da77005f | 951 | return ret; |
da77005f | 952 | } |
1da177e4 | 953 | |
173912a6 TM |
954 | static ssize_t cache_write(struct file *filp, const char __user *buf, |
955 | size_t count, loff_t *ppos, | |
956 | struct cache_detail *cd) | |
da77005f TM |
957 | { |
958 | struct address_space *mapping = filp->f_mapping; | |
496ad9aa | 959 | struct inode *inode = file_inode(filp); |
da77005f TM |
960 | ssize_t ret = -EINVAL; |
961 | ||
962 | if (!cd->cache_parse) | |
963 | goto out; | |
964 | ||
5955102c | 965 | inode_lock(inode); |
da77005f | 966 | ret = cache_downcall(mapping, buf, count, cd); |
5955102c | 967 | inode_unlock(inode); |
da77005f TM |
968 | out: |
969 | return ret; | |
1da177e4 LT |
970 | } |
971 | ||
972 | static DECLARE_WAIT_QUEUE_HEAD(queue_wait); | |
973 | ||
ade994f4 | 974 | static __poll_t cache_poll(struct file *filp, poll_table *wait, |
173912a6 | 975 | struct cache_detail *cd) |
1da177e4 | 976 | { |
ade994f4 | 977 | __poll_t mask; |
1da177e4 LT |
978 | struct cache_reader *rp = filp->private_data; |
979 | struct cache_queue *cq; | |
1da177e4 LT |
980 | |
981 | poll_wait(filp, &queue_wait, wait); | |
982 | ||
983 | /* alway allow write */ | |
a9a08845 | 984 | mask = EPOLLOUT | EPOLLWRNORM; |
1da177e4 LT |
985 | |
986 | if (!rp) | |
987 | return mask; | |
988 | ||
989 | spin_lock(&queue_lock); | |
990 | ||
991 | for (cq= &rp->q; &cq->list != &cd->queue; | |
992 | cq = list_entry(cq->list.next, struct cache_queue, list)) | |
993 | if (!cq->reader) { | |
a9a08845 | 994 | mask |= EPOLLIN | EPOLLRDNORM; |
1da177e4 LT |
995 | break; |
996 | } | |
997 | spin_unlock(&queue_lock); | |
998 | return mask; | |
999 | } | |
1000 | ||
173912a6 TM |
1001 | static int cache_ioctl(struct inode *ino, struct file *filp, |
1002 | unsigned int cmd, unsigned long arg, | |
1003 | struct cache_detail *cd) | |
1da177e4 LT |
1004 | { |
1005 | int len = 0; | |
1006 | struct cache_reader *rp = filp->private_data; | |
1007 | struct cache_queue *cq; | |
1da177e4 LT |
1008 | |
1009 | if (cmd != FIONREAD || !rp) | |
1010 | return -EINVAL; | |
1011 | ||
1012 | spin_lock(&queue_lock); | |
1013 | ||
1014 | /* only find the length remaining in current request, | |
1015 | * or the length of the next request | |
1016 | */ | |
1017 | for (cq= &rp->q; &cq->list != &cd->queue; | |
1018 | cq = list_entry(cq->list.next, struct cache_queue, list)) | |
1019 | if (!cq->reader) { | |
1020 | struct cache_request *cr = | |
1021 | container_of(cq, struct cache_request, q); | |
1022 | len = cr->len - rp->offset; | |
1023 | break; | |
1024 | } | |
1025 | spin_unlock(&queue_lock); | |
1026 | ||
1027 | return put_user(len, (int __user *)arg); | |
1028 | } | |
1029 | ||
173912a6 TM |
1030 | static int cache_open(struct inode *inode, struct file *filp, |
1031 | struct cache_detail *cd) | |
1da177e4 LT |
1032 | { |
1033 | struct cache_reader *rp = NULL; | |
1034 | ||
f7e86ab9 TM |
1035 | if (!cd || !try_module_get(cd->owner)) |
1036 | return -EACCES; | |
1da177e4 LT |
1037 | nonseekable_open(inode, filp); |
1038 | if (filp->f_mode & FMODE_READ) { | |
1da177e4 | 1039 | rp = kmalloc(sizeof(*rp), GFP_KERNEL); |
a7823c79 AK |
1040 | if (!rp) { |
1041 | module_put(cd->owner); | |
1da177e4 | 1042 | return -ENOMEM; |
a7823c79 | 1043 | } |
1da177e4 LT |
1044 | rp->offset = 0; |
1045 | rp->q.reader = 1; | |
64a38e84 | 1046 | |
1da177e4 LT |
1047 | spin_lock(&queue_lock); |
1048 | list_add(&rp->q.list, &cd->queue); | |
1049 | spin_unlock(&queue_lock); | |
1050 | } | |
64a38e84 DW |
1051 | if (filp->f_mode & FMODE_WRITE) |
1052 | atomic_inc(&cd->writers); | |
1da177e4 LT |
1053 | filp->private_data = rp; |
1054 | return 0; | |
1055 | } | |
1056 | ||
173912a6 TM |
1057 | static int cache_release(struct inode *inode, struct file *filp, |
1058 | struct cache_detail *cd) | |
1da177e4 LT |
1059 | { |
1060 | struct cache_reader *rp = filp->private_data; | |
1da177e4 LT |
1061 | |
1062 | if (rp) { | |
1063 | spin_lock(&queue_lock); | |
1064 | if (rp->offset) { | |
1065 | struct cache_queue *cq; | |
1066 | for (cq= &rp->q; &cq->list != &cd->queue; | |
1067 | cq = list_entry(cq->list.next, struct cache_queue, list)) | |
1068 | if (!cq->reader) { | |
1069 | container_of(cq, struct cache_request, q) | |
1070 | ->readers--; | |
1071 | break; | |
1072 | } | |
1073 | rp->offset = 0; | |
1074 | } | |
1075 | list_del(&rp->q.list); | |
1076 | spin_unlock(&queue_lock); | |
1077 | ||
1078 | filp->private_data = NULL; | |
1079 | kfree(rp); | |
1080 | ||
64a38e84 DW |
1081 | } |
1082 | if (filp->f_mode & FMODE_WRITE) { | |
1083 | atomic_dec(&cd->writers); | |
c5b29f88 | 1084 | cd->last_close = seconds_since_boot(); |
1da177e4 | 1085 | } |
f7e86ab9 | 1086 | module_put(cd->owner); |
1da177e4 LT |
1087 | return 0; |
1088 | } | |
1089 | ||
1090 | ||
1091 | ||
f866a819 | 1092 | static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch) |
1da177e4 | 1093 | { |
f9e1aedc N |
1094 | struct cache_queue *cq, *tmp; |
1095 | struct cache_request *cr; | |
64a3ab99 | 1096 | LIST_HEAD(dequeued); |
f9e1aedc | 1097 | |
1da177e4 | 1098 | spin_lock(&queue_lock); |
f9e1aedc | 1099 | list_for_each_entry_safe(cq, tmp, &detail->queue, list) |
1da177e4 | 1100 | if (!cq->reader) { |
f9e1aedc | 1101 | cr = container_of(cq, struct cache_request, q); |
1da177e4 LT |
1102 | if (cr->item != ch) |
1103 | continue; | |
f9e1aedc N |
1104 | if (test_bit(CACHE_PENDING, &ch->flags)) |
1105 | /* Lost a race and it is pending again */ | |
1106 | break; | |
1da177e4 | 1107 | if (cr->readers != 0) |
4013edea | 1108 | continue; |
f9e1aedc | 1109 | list_move(&cr->q.list, &dequeued); |
1da177e4 LT |
1110 | } |
1111 | spin_unlock(&queue_lock); | |
f9e1aedc N |
1112 | while (!list_empty(&dequeued)) { |
1113 | cr = list_entry(dequeued.next, struct cache_request, q.list); | |
1114 | list_del(&cr->q.list); | |
1115 | cache_put(cr->item, detail); | |
1116 | kfree(cr->buf); | |
1117 | kfree(cr); | |
1118 | } | |
1da177e4 LT |
1119 | } |
1120 | ||
1121 | /* | |
1122 | * Support routines for text-based upcalls. | |
1123 | * Fields are separated by spaces. | |
1124 | * Fields are either mangled to quote space tab newline slosh with slosh | |
1125 | * or a hexified with a leading \x | |
1126 | * Record is terminated with newline. | |
1127 | * | |
1128 | */ | |
1129 | ||
1130 | void qword_add(char **bpp, int *lp, char *str) | |
1131 | { | |
1132 | char *bp = *bpp; | |
1133 | int len = *lp; | |
1b2e122d | 1134 | int ret; |
1da177e4 LT |
1135 | |
1136 | if (len < 0) return; | |
1137 | ||
41416f23 RV |
1138 | ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t"); |
1139 | if (ret >= len) { | |
1140 | bp += len; | |
1b2e122d | 1141 | len = -1; |
41416f23 RV |
1142 | } else { |
1143 | bp += ret; | |
1b2e122d | 1144 | len -= ret; |
1da177e4 LT |
1145 | *bp++ = ' '; |
1146 | len--; | |
1147 | } | |
1148 | *bpp = bp; | |
1149 | *lp = len; | |
1150 | } | |
24c3767e | 1151 | EXPORT_SYMBOL_GPL(qword_add); |
1da177e4 LT |
1152 | |
1153 | void qword_addhex(char **bpp, int *lp, char *buf, int blen) | |
1154 | { | |
1155 | char *bp = *bpp; | |
1156 | int len = *lp; | |
1157 | ||
1158 | if (len < 0) return; | |
1159 | ||
1160 | if (len > 2) { | |
1161 | *bp++ = '\\'; | |
1162 | *bp++ = 'x'; | |
1163 | len -= 2; | |
1164 | while (blen && len >= 2) { | |
056785ea | 1165 | bp = hex_byte_pack(bp, *buf++); |
1da177e4 LT |
1166 | len -= 2; |
1167 | blen--; | |
1168 | } | |
1169 | } | |
1170 | if (blen || len<1) len = -1; | |
1171 | else { | |
1172 | *bp++ = ' '; | |
1173 | len--; | |
1174 | } | |
1175 | *bpp = bp; | |
1176 | *lp = len; | |
1177 | } | |
24c3767e | 1178 | EXPORT_SYMBOL_GPL(qword_addhex); |
1da177e4 LT |
1179 | |
1180 | static void warn_no_listener(struct cache_detail *detail) | |
1181 | { | |
1182 | if (detail->last_warn != detail->last_close) { | |
1183 | detail->last_warn = detail->last_close; | |
1184 | if (detail->warn_no_listener) | |
2da8ca26 | 1185 | detail->warn_no_listener(detail, detail->last_close != 0); |
1da177e4 LT |
1186 | } |
1187 | } | |
1188 | ||
06497524 BF |
1189 | static bool cache_listeners_exist(struct cache_detail *detail) |
1190 | { | |
64a38e84 | 1191 | if (atomic_read(&detail->writers)) |
06497524 BF |
1192 | return true; |
1193 | if (detail->last_close == 0) | |
1194 | /* This cache was never opened */ | |
1195 | return false; | |
1196 | if (detail->last_close < seconds_since_boot() - 30) | |
1197 | /* | |
1198 | * We allow for the possibility that someone might | |
1199 | * restart a userspace daemon without restarting the | |
1200 | * server; but after 30 seconds, we give up. | |
1201 | */ | |
1202 | return false; | |
1203 | return true; | |
1204 | } | |
1205 | ||
1da177e4 | 1206 | /* |
bc74b4f5 TM |
1207 | * register an upcall request to user-space and queue it up for read() by the |
1208 | * upcall daemon. | |
1209 | * | |
1da177e4 LT |
1210 | * Each request is at most one page long. |
1211 | */ | |
65286b88 | 1212 | static int cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h) |
1da177e4 | 1213 | { |
1da177e4 LT |
1214 | char *buf; |
1215 | struct cache_request *crq; | |
f9e1aedc | 1216 | int ret = 0; |
1da177e4 | 1217 | |
013920eb N |
1218 | if (test_bit(CACHE_CLEANED, &h->flags)) |
1219 | /* Too late to make an upcall */ | |
1220 | return -EAGAIN; | |
1da177e4 LT |
1221 | |
1222 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1223 | if (!buf) | |
1224 | return -EAGAIN; | |
1225 | ||
1226 | crq = kmalloc(sizeof (*crq), GFP_KERNEL); | |
1227 | if (!crq) { | |
1228 | kfree(buf); | |
1229 | return -EAGAIN; | |
1230 | } | |
1231 | ||
1da177e4 | 1232 | crq->q.reader = 0; |
1da177e4 | 1233 | crq->buf = buf; |
d94af6de | 1234 | crq->len = 0; |
1da177e4 LT |
1235 | crq->readers = 0; |
1236 | spin_lock(&queue_lock); | |
a6ab1e81 N |
1237 | if (test_bit(CACHE_PENDING, &h->flags)) { |
1238 | crq->item = cache_get(h); | |
f9e1aedc | 1239 | list_add_tail(&crq->q.list, &detail->queue); |
78a947f5 | 1240 | trace_cache_entry_upcall(detail, h); |
a6ab1e81 | 1241 | } else |
f9e1aedc N |
1242 | /* Lost a race, no longer PENDING, so don't enqueue */ |
1243 | ret = -EAGAIN; | |
1da177e4 LT |
1244 | spin_unlock(&queue_lock); |
1245 | wake_up(&queue_wait); | |
f9e1aedc N |
1246 | if (ret == -EAGAIN) { |
1247 | kfree(buf); | |
1248 | kfree(crq); | |
1249 | } | |
1250 | return ret; | |
1da177e4 | 1251 | } |
65286b88 TM |
1252 | |
1253 | int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h) | |
1254 | { | |
1255 | if (test_and_set_bit(CACHE_PENDING, &h->flags)) | |
1256 | return 0; | |
1257 | return cache_pipe_upcall(detail, h); | |
1258 | } | |
bc74b4f5 | 1259 | EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall); |
1da177e4 | 1260 | |
65286b88 TM |
1261 | int sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail, |
1262 | struct cache_head *h) | |
1263 | { | |
1264 | if (!cache_listeners_exist(detail)) { | |
1265 | warn_no_listener(detail); | |
78a947f5 | 1266 | trace_cache_entry_no_listener(detail, h); |
65286b88 TM |
1267 | return -EINVAL; |
1268 | } | |
1269 | return sunrpc_cache_pipe_upcall(detail, h); | |
1270 | } | |
1271 | EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall_timeout); | |
1272 | ||
1da177e4 LT |
1273 | /* |
1274 | * parse a message from user-space and pass it | |
1275 | * to an appropriate cache | |
1276 | * Messages are, like requests, separated into fields by | |
1277 | * spaces and dequotes as \xHEXSTRING or embedded \nnn octal | |
1278 | * | |
cca5172a | 1279 | * Message is |
1da177e4 LT |
1280 | * reply cachename expiry key ... content.... |
1281 | * | |
cca5172a | 1282 | * key and content are both parsed by cache |
1da177e4 LT |
1283 | */ |
1284 | ||
1da177e4 LT |
1285 | int qword_get(char **bpp, char *dest, int bufsize) |
1286 | { | |
1287 | /* return bytes copied, or -1 on error */ | |
1288 | char *bp = *bpp; | |
1289 | int len = 0; | |
1290 | ||
1291 | while (*bp == ' ') bp++; | |
1292 | ||
1293 | if (bp[0] == '\\' && bp[1] == 'x') { | |
1294 | /* HEX STRING */ | |
1295 | bp += 2; | |
b7052cd7 | 1296 | while (len < bufsize - 1) { |
e7f483ea AS |
1297 | int h, l; |
1298 | ||
1299 | h = hex_to_bin(bp[0]); | |
1300 | if (h < 0) | |
1301 | break; | |
1302 | ||
1303 | l = hex_to_bin(bp[1]); | |
1304 | if (l < 0) | |
1305 | break; | |
1306 | ||
1307 | *dest++ = (h << 4) | l; | |
1308 | bp += 2; | |
1da177e4 LT |
1309 | len++; |
1310 | } | |
1311 | } else { | |
1312 | /* text with \nnn octal quoting */ | |
1313 | while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) { | |
1314 | if (*bp == '\\' && | |
1315 | isodigit(bp[1]) && (bp[1] <= '3') && | |
1316 | isodigit(bp[2]) && | |
1317 | isodigit(bp[3])) { | |
1318 | int byte = (*++bp -'0'); | |
1319 | bp++; | |
1320 | byte = (byte << 3) | (*bp++ - '0'); | |
1321 | byte = (byte << 3) | (*bp++ - '0'); | |
1322 | *dest++ = byte; | |
1323 | len++; | |
1324 | } else { | |
1325 | *dest++ = *bp++; | |
1326 | len++; | |
1327 | } | |
1328 | } | |
1329 | } | |
1330 | ||
1331 | if (*bp != ' ' && *bp != '\n' && *bp != '\0') | |
1332 | return -1; | |
1333 | while (*bp == ' ') bp++; | |
1334 | *bpp = bp; | |
1335 | *dest = '\0'; | |
1336 | return len; | |
1337 | } | |
24c3767e | 1338 | EXPORT_SYMBOL_GPL(qword_get); |
1da177e4 LT |
1339 | |
1340 | ||
1341 | /* | |
6489a8f4 | 1342 | * support /proc/net/rpc/$CACHENAME/content |
1da177e4 LT |
1343 | * as a seqfile. |
1344 | * We call ->cache_show passing NULL for the item to | |
1345 | * get a header, then pass each real item in the cache | |
1346 | */ | |
1347 | ||
ae74136b | 1348 | static void *__cache_seq_start(struct seq_file *m, loff_t *pos) |
1da177e4 LT |
1349 | { |
1350 | loff_t n = *pos; | |
95c96174 | 1351 | unsigned int hash, entry; |
1da177e4 | 1352 | struct cache_head *ch; |
9936f2ae | 1353 | struct cache_detail *cd = m->private; |
1da177e4 | 1354 | |
1da177e4 LT |
1355 | if (!n--) |
1356 | return SEQ_START_TOKEN; | |
1357 | hash = n >> 32; | |
1358 | entry = n & ((1LL<<32) - 1); | |
1359 | ||
ae74136b | 1360 | hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list) |
1da177e4 LT |
1361 | if (!entry--) |
1362 | return ch; | |
1363 | n &= ~((1LL<<32) - 1); | |
1364 | do { | |
1365 | hash++; | |
1366 | n += 1LL<<32; | |
cca5172a | 1367 | } while(hash < cd->hash_size && |
129e5824 | 1368 | hlist_empty(&cd->hash_table[hash])); |
1da177e4 LT |
1369 | if (hash >= cd->hash_size) |
1370 | return NULL; | |
1371 | *pos = n+1; | |
ae74136b TM |
1372 | return hlist_entry_safe(rcu_dereference_raw( |
1373 | hlist_first_rcu(&cd->hash_table[hash])), | |
129e5824 | 1374 | struct cache_head, cache_list); |
1da177e4 | 1375 | } |
ae74136b | 1376 | |
d48cf356 | 1377 | static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos) |
1da177e4 LT |
1378 | { |
1379 | struct cache_head *ch = p; | |
1380 | int hash = (*pos >> 32); | |
9936f2ae | 1381 | struct cache_detail *cd = m->private; |
1da177e4 LT |
1382 | |
1383 | if (p == SEQ_START_TOKEN) | |
1384 | hash = 0; | |
129e5824 | 1385 | else if (ch->cache_list.next == NULL) { |
1da177e4 LT |
1386 | hash++; |
1387 | *pos += 1LL<<32; | |
1388 | } else { | |
1389 | ++*pos; | |
ae74136b TM |
1390 | return hlist_entry_safe(rcu_dereference_raw( |
1391 | hlist_next_rcu(&ch->cache_list)), | |
129e5824 | 1392 | struct cache_head, cache_list); |
1da177e4 LT |
1393 | } |
1394 | *pos &= ~((1LL<<32) - 1); | |
1395 | while (hash < cd->hash_size && | |
129e5824 | 1396 | hlist_empty(&cd->hash_table[hash])) { |
1da177e4 LT |
1397 | hash++; |
1398 | *pos += 1LL<<32; | |
1399 | } | |
1400 | if (hash >= cd->hash_size) | |
1401 | return NULL; | |
1402 | ++*pos; | |
ae74136b TM |
1403 | return hlist_entry_safe(rcu_dereference_raw( |
1404 | hlist_first_rcu(&cd->hash_table[hash])), | |
129e5824 | 1405 | struct cache_head, cache_list); |
1da177e4 LT |
1406 | } |
1407 | ||
ae74136b TM |
1408 | void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos) |
1409 | __acquires(RCU) | |
1410 | { | |
1411 | rcu_read_lock(); | |
1412 | return __cache_seq_start(m, pos); | |
1413 | } | |
1414 | EXPORT_SYMBOL_GPL(cache_seq_start_rcu); | |
1415 | ||
1416 | void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos) | |
1417 | { | |
1418 | return cache_seq_next(file, p, pos); | |
1419 | } | |
1420 | EXPORT_SYMBOL_GPL(cache_seq_next_rcu); | |
1421 | ||
1422 | void cache_seq_stop_rcu(struct seq_file *m, void *p) | |
1423 | __releases(RCU) | |
1424 | { | |
1425 | rcu_read_unlock(); | |
1426 | } | |
1427 | EXPORT_SYMBOL_GPL(cache_seq_stop_rcu); | |
1428 | ||
1da177e4 LT |
1429 | static int c_show(struct seq_file *m, void *p) |
1430 | { | |
1431 | struct cache_head *cp = p; | |
9936f2ae | 1432 | struct cache_detail *cd = m->private; |
1da177e4 LT |
1433 | |
1434 | if (p == SEQ_START_TOKEN) | |
1435 | return cd->cache_show(m, cd, NULL); | |
1436 | ||
1437 | ifdebug(CACHE) | |
f559935e | 1438 | seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n", |
c5b29f88 | 1439 | convert_to_wallclock(cp->expiry_time), |
2c935bc5 | 1440 | kref_read(&cp->ref), cp->flags); |
2862eee0 | 1441 | |
1b10f0b6 YE |
1442 | if (cache_check_rcu(cd, cp, NULL)) |
1443 | seq_puts(m, "# "); | |
1444 | else if (cache_is_expired(cd, cp)) | |
9dbc1f45 | 1445 | seq_puts(m, "# "); |
1da177e4 LT |
1446 | |
1447 | return cd->cache_show(m, cd, cp); | |
1448 | } | |
1449 | ||
56b3d975 | 1450 | static const struct seq_operations cache_content_op = { |
d48cf356 TM |
1451 | .start = cache_seq_start_rcu, |
1452 | .next = cache_seq_next_rcu, | |
1453 | .stop = cache_seq_stop_rcu, | |
1da177e4 LT |
1454 | .show = c_show, |
1455 | }; | |
1456 | ||
173912a6 TM |
1457 | static int content_open(struct inode *inode, struct file *file, |
1458 | struct cache_detail *cd) | |
1da177e4 | 1459 | { |
9936f2ae KM |
1460 | struct seq_file *seq; |
1461 | int err; | |
1da177e4 | 1462 | |
f7e86ab9 TM |
1463 | if (!cd || !try_module_get(cd->owner)) |
1464 | return -EACCES; | |
9936f2ae KM |
1465 | |
1466 | err = seq_open(file, &cache_content_op); | |
1467 | if (err) { | |
a5990ea1 | 1468 | module_put(cd->owner); |
9936f2ae | 1469 | return err; |
a5990ea1 | 1470 | } |
1da177e4 | 1471 | |
9936f2ae KM |
1472 | seq = file->private_data; |
1473 | seq->private = cd; | |
ec931035 | 1474 | return 0; |
1da177e4 | 1475 | } |
1da177e4 | 1476 | |
f7e86ab9 TM |
1477 | static int content_release(struct inode *inode, struct file *file, |
1478 | struct cache_detail *cd) | |
1479 | { | |
9936f2ae | 1480 | int ret = seq_release(inode, file); |
f7e86ab9 TM |
1481 | module_put(cd->owner); |
1482 | return ret; | |
1483 | } | |
1484 | ||
1485 | static int open_flush(struct inode *inode, struct file *file, | |
1486 | struct cache_detail *cd) | |
1487 | { | |
1488 | if (!cd || !try_module_get(cd->owner)) | |
1489 | return -EACCES; | |
1490 | return nonseekable_open(inode, file); | |
1491 | } | |
1492 | ||
1493 | static int release_flush(struct inode *inode, struct file *file, | |
1494 | struct cache_detail *cd) | |
1495 | { | |
1496 | module_put(cd->owner); | |
1497 | return 0; | |
1498 | } | |
1da177e4 LT |
1499 | |
1500 | static ssize_t read_flush(struct file *file, char __user *buf, | |
173912a6 TM |
1501 | size_t count, loff_t *ppos, |
1502 | struct cache_detail *cd) | |
1da177e4 | 1503 | { |
212ba906 | 1504 | char tbuf[22]; |
01b2969a | 1505 | size_t len; |
1da177e4 | 1506 | |
f559935e | 1507 | len = snprintf(tbuf, sizeof(tbuf), "%llu\n", |
8ccc8691 KM |
1508 | convert_to_wallclock(cd->flush_time)); |
1509 | return simple_read_from_buffer(buf, count, ppos, tbuf, len); | |
1da177e4 LT |
1510 | } |
1511 | ||
173912a6 TM |
1512 | static ssize_t write_flush(struct file *file, const char __user *buf, |
1513 | size_t count, loff_t *ppos, | |
1514 | struct cache_detail *cd) | |
1da177e4 | 1515 | { |
1da177e4 | 1516 | char tbuf[20]; |
3b68e6ee | 1517 | char *ep; |
f559935e | 1518 | time64_t now; |
c5b29f88 | 1519 | |
1da177e4 LT |
1520 | if (*ppos || count > sizeof(tbuf)-1) |
1521 | return -EINVAL; | |
1522 | if (copy_from_user(tbuf, buf, count)) | |
1523 | return -EFAULT; | |
1524 | tbuf[count] = 0; | |
c5b29f88 | 1525 | simple_strtoul(tbuf, &ep, 0); |
1da177e4 LT |
1526 | if (*ep && *ep != '\n') |
1527 | return -EINVAL; | |
3b68e6ee N |
1528 | /* Note that while we check that 'buf' holds a valid number, |
1529 | * we always ignore the value and just flush everything. | |
1530 | * Making use of the number leads to races. | |
1531 | */ | |
1da177e4 | 1532 | |
77862036 | 1533 | now = seconds_since_boot(); |
3b68e6ee N |
1534 | /* Always flush everything, so behave like cache_purge() |
1535 | * Do this by advancing flush_time to the current time, | |
1536 | * or by one second if it has already reached the current time. | |
1537 | * Newly added cache entries will always have ->last_refresh greater | |
1538 | * that ->flush_time, so they don't get flushed prematurely. | |
77862036 | 1539 | */ |
77862036 | 1540 | |
3b68e6ee N |
1541 | if (cd->flush_time >= now) |
1542 | now = cd->flush_time + 1; | |
1543 | ||
1544 | cd->flush_time = now; | |
1545 | cd->nextcheck = now; | |
1da177e4 LT |
1546 | cache_flush(); |
1547 | ||
f69d6d8e JL |
1548 | if (cd->flush) |
1549 | cd->flush(); | |
1550 | ||
1da177e4 LT |
1551 | *ppos += count; |
1552 | return count; | |
1553 | } | |
1554 | ||
173912a6 TM |
1555 | static ssize_t cache_read_procfs(struct file *filp, char __user *buf, |
1556 | size_t count, loff_t *ppos) | |
1557 | { | |
359745d7 | 1558 | struct cache_detail *cd = pde_data(file_inode(filp)); |
173912a6 TM |
1559 | |
1560 | return cache_read(filp, buf, count, ppos, cd); | |
1561 | } | |
1562 | ||
1563 | static ssize_t cache_write_procfs(struct file *filp, const char __user *buf, | |
1564 | size_t count, loff_t *ppos) | |
1565 | { | |
359745d7 | 1566 | struct cache_detail *cd = pde_data(file_inode(filp)); |
173912a6 TM |
1567 | |
1568 | return cache_write(filp, buf, count, ppos, cd); | |
1569 | } | |
1570 | ||
ade994f4 | 1571 | static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait) |
173912a6 | 1572 | { |
359745d7 | 1573 | struct cache_detail *cd = pde_data(file_inode(filp)); |
173912a6 TM |
1574 | |
1575 | return cache_poll(filp, wait, cd); | |
1576 | } | |
1577 | ||
d79b6f4d FW |
1578 | static long cache_ioctl_procfs(struct file *filp, |
1579 | unsigned int cmd, unsigned long arg) | |
173912a6 | 1580 | { |
496ad9aa | 1581 | struct inode *inode = file_inode(filp); |
359745d7 | 1582 | struct cache_detail *cd = pde_data(inode); |
173912a6 | 1583 | |
a6f8dbc6 | 1584 | return cache_ioctl(inode, filp, cmd, arg, cd); |
173912a6 TM |
1585 | } |
1586 | ||
1587 | static int cache_open_procfs(struct inode *inode, struct file *filp) | |
1588 | { | |
359745d7 | 1589 | struct cache_detail *cd = pde_data(inode); |
173912a6 TM |
1590 | |
1591 | return cache_open(inode, filp, cd); | |
1592 | } | |
1593 | ||
1594 | static int cache_release_procfs(struct inode *inode, struct file *filp) | |
1595 | { | |
359745d7 | 1596 | struct cache_detail *cd = pde_data(inode); |
173912a6 TM |
1597 | |
1598 | return cache_release(inode, filp, cd); | |
1599 | } | |
1600 | ||
97a32539 | 1601 | static const struct proc_ops cache_channel_proc_ops = { |
97a32539 AD |
1602 | .proc_read = cache_read_procfs, |
1603 | .proc_write = cache_write_procfs, | |
1604 | .proc_poll = cache_poll_procfs, | |
1605 | .proc_ioctl = cache_ioctl_procfs, /* for FIONREAD */ | |
1606 | .proc_open = cache_open_procfs, | |
1607 | .proc_release = cache_release_procfs, | |
1da177e4 | 1608 | }; |
173912a6 TM |
1609 | |
1610 | static int content_open_procfs(struct inode *inode, struct file *filp) | |
1611 | { | |
359745d7 | 1612 | struct cache_detail *cd = pde_data(inode); |
173912a6 TM |
1613 | |
1614 | return content_open(inode, filp, cd); | |
1615 | } | |
1616 | ||
f7e86ab9 TM |
1617 | static int content_release_procfs(struct inode *inode, struct file *filp) |
1618 | { | |
359745d7 | 1619 | struct cache_detail *cd = pde_data(inode); |
f7e86ab9 TM |
1620 | |
1621 | return content_release(inode, filp, cd); | |
1622 | } | |
1623 | ||
97a32539 AD |
1624 | static const struct proc_ops content_proc_ops = { |
1625 | .proc_open = content_open_procfs, | |
1626 | .proc_read = seq_read, | |
1627 | .proc_lseek = seq_lseek, | |
1628 | .proc_release = content_release_procfs, | |
173912a6 TM |
1629 | }; |
1630 | ||
f7e86ab9 TM |
1631 | static int open_flush_procfs(struct inode *inode, struct file *filp) |
1632 | { | |
359745d7 | 1633 | struct cache_detail *cd = pde_data(inode); |
f7e86ab9 TM |
1634 | |
1635 | return open_flush(inode, filp, cd); | |
1636 | } | |
1637 | ||
1638 | static int release_flush_procfs(struct inode *inode, struct file *filp) | |
1639 | { | |
359745d7 | 1640 | struct cache_detail *cd = pde_data(inode); |
f7e86ab9 TM |
1641 | |
1642 | return release_flush(inode, filp, cd); | |
1643 | } | |
1644 | ||
173912a6 TM |
1645 | static ssize_t read_flush_procfs(struct file *filp, char __user *buf, |
1646 | size_t count, loff_t *ppos) | |
1647 | { | |
359745d7 | 1648 | struct cache_detail *cd = pde_data(file_inode(filp)); |
173912a6 TM |
1649 | |
1650 | return read_flush(filp, buf, count, ppos, cd); | |
1651 | } | |
1652 | ||
1653 | static ssize_t write_flush_procfs(struct file *filp, | |
1654 | const char __user *buf, | |
1655 | size_t count, loff_t *ppos) | |
1656 | { | |
359745d7 | 1657 | struct cache_detail *cd = pde_data(file_inode(filp)); |
173912a6 TM |
1658 | |
1659 | return write_flush(filp, buf, count, ppos, cd); | |
1660 | } | |
1661 | ||
97a32539 AD |
1662 | static const struct proc_ops cache_flush_proc_ops = { |
1663 | .proc_open = open_flush_procfs, | |
1664 | .proc_read = read_flush_procfs, | |
1665 | .proc_write = write_flush_procfs, | |
1666 | .proc_release = release_flush_procfs, | |
1da177e4 | 1667 | }; |
173912a6 | 1668 | |
863d7d9c | 1669 | static void remove_cache_proc_entries(struct cache_detail *cd) |
173912a6 | 1670 | { |
863d7d9c KM |
1671 | if (cd->procfs) { |
1672 | proc_remove(cd->procfs); | |
1673 | cd->procfs = NULL; | |
1674 | } | |
173912a6 TM |
1675 | } |
1676 | ||
1677 | #ifdef CONFIG_PROC_FS | |
593ce16b | 1678 | static int create_cache_proc_entries(struct cache_detail *cd, struct net *net) |
173912a6 TM |
1679 | { |
1680 | struct proc_dir_entry *p; | |
4f42d0d5 | 1681 | struct sunrpc_net *sn; |
173912a6 | 1682 | |
4f42d0d5 | 1683 | sn = net_generic(net, sunrpc_net_id); |
863d7d9c KM |
1684 | cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc); |
1685 | if (cd->procfs == NULL) | |
173912a6 | 1686 | goto out_nomem; |
173912a6 | 1687 | |
d6444062 | 1688 | p = proc_create_data("flush", S_IFREG | 0600, |
97a32539 | 1689 | cd->procfs, &cache_flush_proc_ops, cd); |
173912a6 TM |
1690 | if (p == NULL) |
1691 | goto out_nomem; | |
1692 | ||
2d438338 | 1693 | if (cd->cache_request || cd->cache_parse) { |
d6444062 | 1694 | p = proc_create_data("channel", S_IFREG | 0600, cd->procfs, |
97a32539 | 1695 | &cache_channel_proc_ops, cd); |
173912a6 TM |
1696 | if (p == NULL) |
1697 | goto out_nomem; | |
1698 | } | |
1699 | if (cd->cache_show) { | |
d6444062 | 1700 | p = proc_create_data("content", S_IFREG | 0400, cd->procfs, |
97a32539 | 1701 | &content_proc_ops, cd); |
173912a6 TM |
1702 | if (p == NULL) |
1703 | goto out_nomem; | |
1704 | } | |
1705 | return 0; | |
1706 | out_nomem: | |
863d7d9c | 1707 | remove_cache_proc_entries(cd); |
173912a6 TM |
1708 | return -ENOMEM; |
1709 | } | |
1710 | #else /* CONFIG_PROC_FS */ | |
593ce16b | 1711 | static int create_cache_proc_entries(struct cache_detail *cd, struct net *net) |
173912a6 TM |
1712 | { |
1713 | return 0; | |
1714 | } | |
1715 | #endif | |
1716 | ||
8eab945c AB |
1717 | void __init cache_initialize(void) |
1718 | { | |
203b42f7 | 1719 | INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean); |
8eab945c AB |
1720 | } |
1721 | ||
593ce16b | 1722 | int cache_register_net(struct cache_detail *cd, struct net *net) |
173912a6 TM |
1723 | { |
1724 | int ret; | |
1725 | ||
1726 | sunrpc_init_cache_detail(cd); | |
593ce16b | 1727 | ret = create_cache_proc_entries(cd, net); |
173912a6 TM |
1728 | if (ret) |
1729 | sunrpc_destroy_cache_detail(cd); | |
1730 | return ret; | |
1731 | } | |
f5c8593b | 1732 | EXPORT_SYMBOL_GPL(cache_register_net); |
593ce16b | 1733 | |
593ce16b | 1734 | void cache_unregister_net(struct cache_detail *cd, struct net *net) |
173912a6 | 1735 | { |
863d7d9c | 1736 | remove_cache_proc_entries(cd); |
173912a6 TM |
1737 | sunrpc_destroy_cache_detail(cd); |
1738 | } | |
f5c8593b | 1739 | EXPORT_SYMBOL_GPL(cache_unregister_net); |
593ce16b | 1740 | |
d34971a6 | 1741 | struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net) |
0a402d5a SK |
1742 | { |
1743 | struct cache_detail *cd; | |
129e5824 | 1744 | int i; |
0a402d5a SK |
1745 | |
1746 | cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL); | |
1747 | if (cd == NULL) | |
1748 | return ERR_PTR(-ENOMEM); | |
1749 | ||
6396bb22 | 1750 | cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head), |
0a402d5a SK |
1751 | GFP_KERNEL); |
1752 | if (cd->hash_table == NULL) { | |
1753 | kfree(cd); | |
1754 | return ERR_PTR(-ENOMEM); | |
1755 | } | |
129e5824 KM |
1756 | |
1757 | for (i = 0; i < cd->hash_size; i++) | |
1758 | INIT_HLIST_HEAD(&cd->hash_table[i]); | |
0a402d5a SK |
1759 | cd->net = net; |
1760 | return cd; | |
1761 | } | |
1762 | EXPORT_SYMBOL_GPL(cache_create_net); | |
1763 | ||
1764 | void cache_destroy_net(struct cache_detail *cd, struct net *net) | |
593ce16b | 1765 | { |
0a402d5a SK |
1766 | kfree(cd->hash_table); |
1767 | kfree(cd); | |
593ce16b | 1768 | } |
0a402d5a | 1769 | EXPORT_SYMBOL_GPL(cache_destroy_net); |
8854e82d TM |
1770 | |
1771 | static ssize_t cache_read_pipefs(struct file *filp, char __user *buf, | |
1772 | size_t count, loff_t *ppos) | |
1773 | { | |
496ad9aa | 1774 | struct cache_detail *cd = RPC_I(file_inode(filp))->private; |
8854e82d TM |
1775 | |
1776 | return cache_read(filp, buf, count, ppos, cd); | |
1777 | } | |
1778 | ||
1779 | static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf, | |
1780 | size_t count, loff_t *ppos) | |
1781 | { | |
496ad9aa | 1782 | struct cache_detail *cd = RPC_I(file_inode(filp))->private; |
8854e82d TM |
1783 | |
1784 | return cache_write(filp, buf, count, ppos, cd); | |
1785 | } | |
1786 | ||
ade994f4 | 1787 | static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait) |
8854e82d | 1788 | { |
496ad9aa | 1789 | struct cache_detail *cd = RPC_I(file_inode(filp))->private; |
8854e82d TM |
1790 | |
1791 | return cache_poll(filp, wait, cd); | |
1792 | } | |
1793 | ||
9918ff26 | 1794 | static long cache_ioctl_pipefs(struct file *filp, |
8854e82d TM |
1795 | unsigned int cmd, unsigned long arg) |
1796 | { | |
496ad9aa | 1797 | struct inode *inode = file_inode(filp); |
8854e82d TM |
1798 | struct cache_detail *cd = RPC_I(inode)->private; |
1799 | ||
a6f8dbc6 | 1800 | return cache_ioctl(inode, filp, cmd, arg, cd); |
8854e82d TM |
1801 | } |
1802 | ||
1803 | static int cache_open_pipefs(struct inode *inode, struct file *filp) | |
1804 | { | |
1805 | struct cache_detail *cd = RPC_I(inode)->private; | |
1806 | ||
1807 | return cache_open(inode, filp, cd); | |
1808 | } | |
1809 | ||
1810 | static int cache_release_pipefs(struct inode *inode, struct file *filp) | |
1811 | { | |
1812 | struct cache_detail *cd = RPC_I(inode)->private; | |
1813 | ||
1814 | return cache_release(inode, filp, cd); | |
1815 | } | |
1816 | ||
1817 | const struct file_operations cache_file_operations_pipefs = { | |
1818 | .owner = THIS_MODULE, | |
8854e82d TM |
1819 | .read = cache_read_pipefs, |
1820 | .write = cache_write_pipefs, | |
1821 | .poll = cache_poll_pipefs, | |
9918ff26 | 1822 | .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */ |
8854e82d TM |
1823 | .open = cache_open_pipefs, |
1824 | .release = cache_release_pipefs, | |
1825 | }; | |
1826 | ||
1827 | static int content_open_pipefs(struct inode *inode, struct file *filp) | |
1828 | { | |
1829 | struct cache_detail *cd = RPC_I(inode)->private; | |
1830 | ||
1831 | return content_open(inode, filp, cd); | |
1832 | } | |
1833 | ||
f7e86ab9 TM |
1834 | static int content_release_pipefs(struct inode *inode, struct file *filp) |
1835 | { | |
1836 | struct cache_detail *cd = RPC_I(inode)->private; | |
1837 | ||
1838 | return content_release(inode, filp, cd); | |
1839 | } | |
1840 | ||
8854e82d TM |
1841 | const struct file_operations content_file_operations_pipefs = { |
1842 | .open = content_open_pipefs, | |
1843 | .read = seq_read, | |
1844 | .llseek = seq_lseek, | |
f7e86ab9 | 1845 | .release = content_release_pipefs, |
8854e82d TM |
1846 | }; |
1847 | ||
f7e86ab9 TM |
1848 | static int open_flush_pipefs(struct inode *inode, struct file *filp) |
1849 | { | |
1850 | struct cache_detail *cd = RPC_I(inode)->private; | |
1851 | ||
1852 | return open_flush(inode, filp, cd); | |
1853 | } | |
1854 | ||
1855 | static int release_flush_pipefs(struct inode *inode, struct file *filp) | |
1856 | { | |
1857 | struct cache_detail *cd = RPC_I(inode)->private; | |
1858 | ||
1859 | return release_flush(inode, filp, cd); | |
1860 | } | |
1861 | ||
8854e82d TM |
1862 | static ssize_t read_flush_pipefs(struct file *filp, char __user *buf, |
1863 | size_t count, loff_t *ppos) | |
1864 | { | |
496ad9aa | 1865 | struct cache_detail *cd = RPC_I(file_inode(filp))->private; |
8854e82d TM |
1866 | |
1867 | return read_flush(filp, buf, count, ppos, cd); | |
1868 | } | |
1869 | ||
1870 | static ssize_t write_flush_pipefs(struct file *filp, | |
1871 | const char __user *buf, | |
1872 | size_t count, loff_t *ppos) | |
1873 | { | |
496ad9aa | 1874 | struct cache_detail *cd = RPC_I(file_inode(filp))->private; |
8854e82d TM |
1875 | |
1876 | return write_flush(filp, buf, count, ppos, cd); | |
1877 | } | |
1878 | ||
1879 | const struct file_operations cache_flush_operations_pipefs = { | |
f7e86ab9 | 1880 | .open = open_flush_pipefs, |
8854e82d TM |
1881 | .read = read_flush_pipefs, |
1882 | .write = write_flush_pipefs, | |
f7e86ab9 | 1883 | .release = release_flush_pipefs, |
8854e82d TM |
1884 | }; |
1885 | ||
1886 | int sunrpc_cache_register_pipefs(struct dentry *parent, | |
64f1426f | 1887 | const char *name, umode_t umode, |
8854e82d TM |
1888 | struct cache_detail *cd) |
1889 | { | |
a95e691f AV |
1890 | struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd); |
1891 | if (IS_ERR(dir)) | |
1892 | return PTR_ERR(dir); | |
863d7d9c | 1893 | cd->pipefs = dir; |
a95e691f | 1894 | return 0; |
8854e82d TM |
1895 | } |
1896 | EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs); | |
1897 | ||
1898 | void sunrpc_cache_unregister_pipefs(struct cache_detail *cd) | |
1899 | { | |
863d7d9c KM |
1900 | if (cd->pipefs) { |
1901 | rpc_remove_cache_dir(cd->pipefs); | |
1902 | cd->pipefs = NULL; | |
1903 | } | |
8854e82d TM |
1904 | } |
1905 | EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs); | |
1906 | ||
2b477c00 NB |
1907 | void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h) |
1908 | { | |
1863d77f | 1909 | spin_lock(&cd->hash_lock); |
2b477c00 | 1910 | if (!hlist_unhashed(&h->cache_list)){ |
809fe3c5 | 1911 | sunrpc_begin_cache_remove_entry(h, cd); |
1863d77f | 1912 | spin_unlock(&cd->hash_lock); |
809fe3c5 | 1913 | sunrpc_end_cache_remove_entry(h, cd); |
2b477c00 | 1914 | } else |
1863d77f | 1915 | spin_unlock(&cd->hash_lock); |
2b477c00 NB |
1916 | } |
1917 | EXPORT_SYMBOL_GPL(sunrpc_cache_unhash); |