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