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