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