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