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