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