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 | { |
| 45 | time_t now = get_seconds(); |
| 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 | { |
| 55 | return (h->expiry_time < get_seconds()) || |
| 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; |
| 130 | head->last_refresh = get_seconds(); |
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); |
| 241 | age = get_seconds() - h->last_refresh; |
| 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 | 908329f | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 256 | cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY); |
| 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); |
| 391 | if (current_detail->nextcheck > get_seconds()) |
| 392 | current_index = current_detail->hash_size; |
| 393 | else { |
| 394 | current_index = 0; |
| 395 | current_detail->nextcheck = get_seconds()+30*60; |
| 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; |
| 480 | detail->nextcheck = get_seconds(); |
| 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 | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 512 | 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] | 513 | { |
NeilBrown | cd68c37 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 514 | struct cache_deferred_req *dreq, *discard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | int hash = DFR_HASH(item); |
| 516 | |
J.Bruce Fields | 01f3bd1 | 2006-12-13 00:35:26 -0800 | [diff] [blame] | 517 | if (cache_defer_cnt >= DFR_MAX) { |
| 518 | /* too much in the cache, randomly drop this one, |
| 519 | * or continue and drop the oldest below |
| 520 | */ |
| 521 | if (net_random()&1) |
NeilBrown | 9e4c637 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 522 | return -ENOMEM; |
J.Bruce Fields | 01f3bd1 | 2006-12-13 00:35:26 -0800 | [diff] [blame] | 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | dreq = req->defer(req); |
| 525 | if (dreq == NULL) |
NeilBrown | 9e4c637 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 526 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
| 528 | dreq->item = item; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | spin_lock(&cache_defer_lock); |
| 531 | |
| 532 | list_add(&dreq->recent, &cache_defer_list); |
| 533 | |
| 534 | if (cache_defer_hash[hash].next == NULL) |
| 535 | INIT_LIST_HEAD(&cache_defer_hash[hash]); |
| 536 | list_add(&dreq->hash, &cache_defer_hash[hash]); |
| 537 | |
| 538 | /* it is in, now maybe clean up */ |
NeilBrown | cd68c37 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 539 | discard = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | if (++cache_defer_cnt > DFR_MAX) { |
NeilBrown | cd68c37 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 541 | discard = list_entry(cache_defer_list.prev, |
| 542 | struct cache_deferred_req, recent); |
| 543 | list_del_init(&discard->recent); |
| 544 | list_del_init(&discard->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | cache_defer_cnt--; |
| 546 | } |
| 547 | spin_unlock(&cache_defer_lock); |
| 548 | |
NeilBrown | cd68c37 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 549 | if (discard) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | /* there was one too many */ |
NeilBrown | cd68c37 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 551 | discard->revisit(discard, 1); |
| 552 | |
NeilBrown | 4013ede | 2006-03-27 01:15:07 -0800 | [diff] [blame] | 553 | if (!test_bit(CACHE_PENDING, &item->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | /* must have just been validated... */ |
| 555 | cache_revisit_request(item); |
NeilBrown | 9e4c637 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 556 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
NeilBrown | 9e4c637 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 558 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void cache_revisit_request(struct cache_head *item) |
| 562 | { |
| 563 | struct cache_deferred_req *dreq; |
| 564 | struct list_head pending; |
| 565 | |
| 566 | struct list_head *lp; |
| 567 | int hash = DFR_HASH(item); |
| 568 | |
| 569 | INIT_LIST_HEAD(&pending); |
| 570 | spin_lock(&cache_defer_lock); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | lp = cache_defer_hash[hash].next; |
| 573 | if (lp) { |
| 574 | while (lp != &cache_defer_hash[hash]) { |
| 575 | dreq = list_entry(lp, struct cache_deferred_req, hash); |
| 576 | lp = lp->next; |
| 577 | if (dreq->item == item) { |
NeilBrown | 67e7328 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 578 | list_del_init(&dreq->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | list_move(&dreq->recent, &pending); |
| 580 | cache_defer_cnt--; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | spin_unlock(&cache_defer_lock); |
| 585 | |
| 586 | while (!list_empty(&pending)) { |
| 587 | dreq = list_entry(pending.next, struct cache_deferred_req, recent); |
| 588 | list_del_init(&dreq->recent); |
| 589 | dreq->revisit(dreq, 0); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void cache_clean_deferred(void *owner) |
| 594 | { |
| 595 | struct cache_deferred_req *dreq, *tmp; |
| 596 | struct list_head pending; |
| 597 | |
| 598 | |
| 599 | INIT_LIST_HEAD(&pending); |
| 600 | spin_lock(&cache_defer_lock); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) { |
| 603 | if (dreq->owner == owner) { |
NeilBrown | 67e7328 | 2009-09-09 16:32:54 +1000 | [diff] [blame] | 604 | list_del_init(&dreq->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | list_move(&dreq->recent, &pending); |
| 606 | cache_defer_cnt--; |
| 607 | } |
| 608 | } |
| 609 | spin_unlock(&cache_defer_lock); |
| 610 | |
| 611 | while (!list_empty(&pending)) { |
| 612 | dreq = list_entry(pending.next, struct cache_deferred_req, recent); |
| 613 | list_del_init(&dreq->recent); |
| 614 | dreq->revisit(dreq, 1); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * communicate with user-space |
| 620 | * |
J. Bruce Fields | a490c68 | 2007-11-06 14:15:19 -0500 | [diff] [blame] | 621 | * We have a magic /proc file - /proc/sunrpc/<cachename>/channel. |
| 622 | * On read, you get a full request, or block. |
| 623 | * On write, an update request is processed. |
| 624 | * Poll works if anything to read, and always allows write. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | * |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 626 | * Implemented by linked list of requests. Each open file has |
J. Bruce Fields | a490c68 | 2007-11-06 14:15:19 -0500 | [diff] [blame] | 627 | * a ->private that also exists in this list. New requests are added |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | * to the end and may wakeup and preceding readers. |
| 629 | * New readers are added to the head. If, on read, an item is found with |
| 630 | * CACHE_UPCALLING clear, we free it from the list. |
| 631 | * |
| 632 | */ |
| 633 | |
| 634 | static DEFINE_SPINLOCK(queue_lock); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 635 | static DEFINE_MUTEX(queue_io_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
| 637 | struct cache_queue { |
| 638 | struct list_head list; |
| 639 | int reader; /* if 0, then request */ |
| 640 | }; |
| 641 | struct cache_request { |
| 642 | struct cache_queue q; |
| 643 | struct cache_head *item; |
| 644 | char * buf; |
| 645 | int len; |
| 646 | int readers; |
| 647 | }; |
| 648 | struct cache_reader { |
| 649 | struct cache_queue q; |
| 650 | int offset; /* if non-0, we have a refcnt on next request */ |
| 651 | }; |
| 652 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 653 | static ssize_t cache_read(struct file *filp, char __user *buf, size_t count, |
| 654 | loff_t *ppos, struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | { |
| 656 | struct cache_reader *rp = filp->private_data; |
| 657 | struct cache_request *rq; |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 658 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | int err; |
| 660 | |
| 661 | if (count == 0) |
| 662 | return 0; |
| 663 | |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 664 | mutex_lock(&inode->i_mutex); /* protect against multiple concurrent |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | * readers on this file */ |
| 666 | again: |
| 667 | spin_lock(&queue_lock); |
| 668 | /* need to find next request */ |
| 669 | while (rp->q.list.next != &cd->queue && |
| 670 | list_entry(rp->q.list.next, struct cache_queue, list) |
| 671 | ->reader) { |
| 672 | struct list_head *next = rp->q.list.next; |
| 673 | list_move(&rp->q.list, next); |
| 674 | } |
| 675 | if (rp->q.list.next == &cd->queue) { |
| 676 | spin_unlock(&queue_lock); |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 677 | mutex_unlock(&inode->i_mutex); |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 678 | BUG_ON(rp->offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | return 0; |
| 680 | } |
| 681 | rq = container_of(rp->q.list.next, struct cache_request, q.list); |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 682 | BUG_ON(rq->q.reader); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if (rp->offset == 0) |
| 684 | rq->readers++; |
| 685 | spin_unlock(&queue_lock); |
| 686 | |
| 687 | if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) { |
| 688 | err = -EAGAIN; |
| 689 | spin_lock(&queue_lock); |
| 690 | list_move(&rp->q.list, &rq->q.list); |
| 691 | spin_unlock(&queue_lock); |
| 692 | } else { |
| 693 | if (rp->offset + count > rq->len) |
| 694 | count = rq->len - rp->offset; |
| 695 | err = -EFAULT; |
| 696 | if (copy_to_user(buf, rq->buf + rp->offset, count)) |
| 697 | goto out; |
| 698 | rp->offset += count; |
| 699 | if (rp->offset >= rq->len) { |
| 700 | rp->offset = 0; |
| 701 | spin_lock(&queue_lock); |
| 702 | list_move(&rp->q.list, &rq->q.list); |
| 703 | spin_unlock(&queue_lock); |
| 704 | } |
| 705 | err = 0; |
| 706 | } |
| 707 | out: |
| 708 | if (rp->offset == 0) { |
| 709 | /* need to release rq */ |
| 710 | spin_lock(&queue_lock); |
| 711 | rq->readers--; |
| 712 | if (rq->readers == 0 && |
| 713 | !test_bit(CACHE_PENDING, &rq->item->flags)) { |
| 714 | list_del(&rq->q.list); |
| 715 | spin_unlock(&queue_lock); |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 716 | cache_put(rq->item, cd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | kfree(rq->buf); |
| 718 | kfree(rq); |
| 719 | } else |
| 720 | spin_unlock(&queue_lock); |
| 721 | } |
| 722 | if (err == -EAGAIN) |
| 723 | goto again; |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 724 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return err ? err : count; |
| 726 | } |
| 727 | |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 728 | static ssize_t cache_do_downcall(char *kaddr, const char __user *buf, |
| 729 | size_t count, struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 731 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 733 | if (copy_from_user(kaddr, buf, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | return -EFAULT; |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 735 | kaddr[count] = '\0'; |
| 736 | ret = cd->cache_parse(cd, kaddr, count); |
| 737 | if (!ret) |
| 738 | ret = count; |
| 739 | return ret; |
| 740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 742 | static ssize_t cache_slow_downcall(const char __user *buf, |
| 743 | size_t count, struct cache_detail *cd) |
| 744 | { |
| 745 | static char write_buf[8192]; /* protected by queue_io_mutex */ |
| 746 | ssize_t ret = -EINVAL; |
| 747 | |
| 748 | if (count >= sizeof(write_buf)) |
| 749 | goto out; |
| 750 | mutex_lock(&queue_io_mutex); |
| 751 | ret = cache_do_downcall(write_buf, buf, count, cd); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 752 | mutex_unlock(&queue_io_mutex); |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 753 | out: |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | static ssize_t cache_downcall(struct address_space *mapping, |
| 758 | const char __user *buf, |
| 759 | size_t count, struct cache_detail *cd) |
| 760 | { |
| 761 | struct page *page; |
| 762 | char *kaddr; |
| 763 | ssize_t ret = -ENOMEM; |
| 764 | |
| 765 | if (count >= PAGE_CACHE_SIZE) |
| 766 | goto out_slow; |
| 767 | |
| 768 | page = find_or_create_page(mapping, 0, GFP_KERNEL); |
| 769 | if (!page) |
| 770 | goto out_slow; |
| 771 | |
| 772 | kaddr = kmap(page); |
| 773 | ret = cache_do_downcall(kaddr, buf, count, cd); |
| 774 | kunmap(page); |
| 775 | unlock_page(page); |
| 776 | page_cache_release(page); |
| 777 | return ret; |
| 778 | out_slow: |
| 779 | return cache_slow_downcall(buf, count, cd); |
| 780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 782 | static ssize_t cache_write(struct file *filp, const char __user *buf, |
| 783 | size_t count, loff_t *ppos, |
| 784 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | { |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 786 | struct address_space *mapping = filp->f_mapping; |
| 787 | struct inode *inode = filp->f_path.dentry->d_inode; |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 788 | ssize_t ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 790 | if (!cd->cache_parse) |
| 791 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Trond Myklebust | da77005 | 2009-08-09 15:14:28 -0400 | [diff] [blame] | 793 | mutex_lock(&inode->i_mutex); |
| 794 | ret = cache_downcall(mapping, buf, count, cd); |
| 795 | mutex_unlock(&inode->i_mutex); |
| 796 | out: |
| 797 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static DECLARE_WAIT_QUEUE_HEAD(queue_wait); |
| 801 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 802 | static unsigned int cache_poll(struct file *filp, poll_table *wait, |
| 803 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | { |
| 805 | unsigned int mask; |
| 806 | struct cache_reader *rp = filp->private_data; |
| 807 | struct cache_queue *cq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | poll_wait(filp, &queue_wait, wait); |
| 810 | |
| 811 | /* alway allow write */ |
| 812 | mask = POLL_OUT | POLLWRNORM; |
| 813 | |
| 814 | if (!rp) |
| 815 | return mask; |
| 816 | |
| 817 | spin_lock(&queue_lock); |
| 818 | |
| 819 | for (cq= &rp->q; &cq->list != &cd->queue; |
| 820 | cq = list_entry(cq->list.next, struct cache_queue, list)) |
| 821 | if (!cq->reader) { |
| 822 | mask |= POLLIN | POLLRDNORM; |
| 823 | break; |
| 824 | } |
| 825 | spin_unlock(&queue_lock); |
| 826 | return mask; |
| 827 | } |
| 828 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 829 | static int cache_ioctl(struct inode *ino, struct file *filp, |
| 830 | unsigned int cmd, unsigned long arg, |
| 831 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
| 833 | int len = 0; |
| 834 | struct cache_reader *rp = filp->private_data; |
| 835 | struct cache_queue *cq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
| 837 | if (cmd != FIONREAD || !rp) |
| 838 | return -EINVAL; |
| 839 | |
| 840 | spin_lock(&queue_lock); |
| 841 | |
| 842 | /* only find the length remaining in current request, |
| 843 | * or the length of the next request |
| 844 | */ |
| 845 | for (cq= &rp->q; &cq->list != &cd->queue; |
| 846 | cq = list_entry(cq->list.next, struct cache_queue, list)) |
| 847 | if (!cq->reader) { |
| 848 | struct cache_request *cr = |
| 849 | container_of(cq, struct cache_request, q); |
| 850 | len = cr->len - rp->offset; |
| 851 | break; |
| 852 | } |
| 853 | spin_unlock(&queue_lock); |
| 854 | |
| 855 | return put_user(len, (int __user *)arg); |
| 856 | } |
| 857 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 858 | static int cache_open(struct inode *inode, struct file *filp, |
| 859 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
| 861 | struct cache_reader *rp = NULL; |
| 862 | |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 863 | if (!cd || !try_module_get(cd->owner)) |
| 864 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | nonseekable_open(inode, filp); |
| 866 | if (filp->f_mode & FMODE_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | rp = kmalloc(sizeof(*rp), GFP_KERNEL); |
| 868 | if (!rp) |
| 869 | return -ENOMEM; |
| 870 | rp->offset = 0; |
| 871 | rp->q.reader = 1; |
| 872 | atomic_inc(&cd->readers); |
| 873 | spin_lock(&queue_lock); |
| 874 | list_add(&rp->q.list, &cd->queue); |
| 875 | spin_unlock(&queue_lock); |
| 876 | } |
| 877 | filp->private_data = rp; |
| 878 | return 0; |
| 879 | } |
| 880 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 881 | static int cache_release(struct inode *inode, struct file *filp, |
| 882 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
| 884 | struct cache_reader *rp = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
| 886 | if (rp) { |
| 887 | spin_lock(&queue_lock); |
| 888 | if (rp->offset) { |
| 889 | struct cache_queue *cq; |
| 890 | for (cq= &rp->q; &cq->list != &cd->queue; |
| 891 | cq = list_entry(cq->list.next, struct cache_queue, list)) |
| 892 | if (!cq->reader) { |
| 893 | container_of(cq, struct cache_request, q) |
| 894 | ->readers--; |
| 895 | break; |
| 896 | } |
| 897 | rp->offset = 0; |
| 898 | } |
| 899 | list_del(&rp->q.list); |
| 900 | spin_unlock(&queue_lock); |
| 901 | |
| 902 | filp->private_data = NULL; |
| 903 | kfree(rp); |
| 904 | |
| 905 | cd->last_close = get_seconds(); |
| 906 | atomic_dec(&cd->readers); |
| 907 | } |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 908 | module_put(cd->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | |
| 913 | |
NeilBrown | f866a81 | 2009-08-04 15:22:38 +1000 | [diff] [blame] | 914 | static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | { |
| 916 | struct cache_queue *cq; |
| 917 | spin_lock(&queue_lock); |
| 918 | list_for_each_entry(cq, &detail->queue, list) |
| 919 | if (!cq->reader) { |
| 920 | struct cache_request *cr = container_of(cq, struct cache_request, q); |
| 921 | if (cr->item != ch) |
| 922 | continue; |
| 923 | if (cr->readers != 0) |
NeilBrown | 4013ede | 2006-03-27 01:15:07 -0800 | [diff] [blame] | 924 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | list_del(&cr->q.list); |
| 926 | spin_unlock(&queue_lock); |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 927 | cache_put(cr->item, detail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | kfree(cr->buf); |
| 929 | kfree(cr); |
| 930 | return; |
| 931 | } |
| 932 | spin_unlock(&queue_lock); |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Support routines for text-based upcalls. |
| 937 | * Fields are separated by spaces. |
| 938 | * Fields are either mangled to quote space tab newline slosh with slosh |
| 939 | * or a hexified with a leading \x |
| 940 | * Record is terminated with newline. |
| 941 | * |
| 942 | */ |
| 943 | |
| 944 | void qword_add(char **bpp, int *lp, char *str) |
| 945 | { |
| 946 | char *bp = *bpp; |
| 947 | int len = *lp; |
| 948 | char c; |
| 949 | |
| 950 | if (len < 0) return; |
| 951 | |
| 952 | while ((c=*str++) && len) |
| 953 | switch(c) { |
| 954 | case ' ': |
| 955 | case '\t': |
| 956 | case '\n': |
| 957 | case '\\': |
| 958 | if (len >= 4) { |
| 959 | *bp++ = '\\'; |
| 960 | *bp++ = '0' + ((c & 0300)>>6); |
| 961 | *bp++ = '0' + ((c & 0070)>>3); |
| 962 | *bp++ = '0' + ((c & 0007)>>0); |
| 963 | } |
| 964 | len -= 4; |
| 965 | break; |
| 966 | default: |
| 967 | *bp++ = c; |
| 968 | len--; |
| 969 | } |
| 970 | if (c || len <1) len = -1; |
| 971 | else { |
| 972 | *bp++ = ' '; |
| 973 | len--; |
| 974 | } |
| 975 | *bpp = bp; |
| 976 | *lp = len; |
| 977 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 978 | EXPORT_SYMBOL_GPL(qword_add); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
| 980 | void qword_addhex(char **bpp, int *lp, char *buf, int blen) |
| 981 | { |
| 982 | char *bp = *bpp; |
| 983 | int len = *lp; |
| 984 | |
| 985 | if (len < 0) return; |
| 986 | |
| 987 | if (len > 2) { |
| 988 | *bp++ = '\\'; |
| 989 | *bp++ = 'x'; |
| 990 | len -= 2; |
| 991 | while (blen && len >= 2) { |
| 992 | unsigned char c = *buf++; |
| 993 | *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); |
| 994 | *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); |
| 995 | len -= 2; |
| 996 | blen--; |
| 997 | } |
| 998 | } |
| 999 | if (blen || len<1) len = -1; |
| 1000 | else { |
| 1001 | *bp++ = ' '; |
| 1002 | len--; |
| 1003 | } |
| 1004 | *bpp = bp; |
| 1005 | *lp = len; |
| 1006 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 1007 | EXPORT_SYMBOL_GPL(qword_addhex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | static void warn_no_listener(struct cache_detail *detail) |
| 1010 | { |
| 1011 | if (detail->last_warn != detail->last_close) { |
| 1012 | detail->last_warn = detail->last_close; |
| 1013 | if (detail->warn_no_listener) |
Trond Myklebust | 2da8ca2 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 1014 | detail->warn_no_listener(detail, detail->last_close != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /* |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1019 | * register an upcall request to user-space and queue it up for read() by the |
| 1020 | * upcall daemon. |
| 1021 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | * Each request is at most one page long. |
| 1023 | */ |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1024 | int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h, |
| 1025 | void (*cache_request)(struct cache_detail *, |
| 1026 | struct cache_head *, |
| 1027 | char **, |
| 1028 | int *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | { |
| 1030 | |
| 1031 | char *buf; |
| 1032 | struct cache_request *crq; |
| 1033 | char *bp; |
| 1034 | int len; |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (atomic_read(&detail->readers) == 0 && |
| 1037 | detail->last_close < get_seconds() - 30) { |
| 1038 | warn_no_listener(detail); |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | |
| 1042 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 1043 | if (!buf) |
| 1044 | return -EAGAIN; |
| 1045 | |
| 1046 | crq = kmalloc(sizeof (*crq), GFP_KERNEL); |
| 1047 | if (!crq) { |
| 1048 | kfree(buf); |
| 1049 | return -EAGAIN; |
| 1050 | } |
| 1051 | |
| 1052 | bp = buf; len = PAGE_SIZE; |
| 1053 | |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1054 | cache_request(detail, h, &bp, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | if (len < 0) { |
| 1057 | kfree(buf); |
| 1058 | kfree(crq); |
| 1059 | return -EAGAIN; |
| 1060 | } |
| 1061 | crq->q.reader = 0; |
| 1062 | crq->item = cache_get(h); |
| 1063 | crq->buf = buf; |
| 1064 | crq->len = PAGE_SIZE - len; |
| 1065 | crq->readers = 0; |
| 1066 | spin_lock(&queue_lock); |
| 1067 | list_add_tail(&crq->q.list, &detail->queue); |
| 1068 | spin_unlock(&queue_lock); |
| 1069 | wake_up(&queue_wait); |
| 1070 | return 0; |
| 1071 | } |
Trond Myklebust | bc74b4f | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1072 | EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * parse a message from user-space and pass it |
| 1076 | * to an appropriate cache |
| 1077 | * Messages are, like requests, separated into fields by |
| 1078 | * spaces and dequotes as \xHEXSTRING or embedded \nnn octal |
| 1079 | * |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1080 | * Message is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | * reply cachename expiry key ... content.... |
| 1082 | * |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1083 | * key and content are both parsed by cache |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | */ |
| 1085 | |
| 1086 | #define isodigit(c) (isdigit(c) && c <= '7') |
| 1087 | int qword_get(char **bpp, char *dest, int bufsize) |
| 1088 | { |
| 1089 | /* return bytes copied, or -1 on error */ |
| 1090 | char *bp = *bpp; |
| 1091 | int len = 0; |
| 1092 | |
| 1093 | while (*bp == ' ') bp++; |
| 1094 | |
| 1095 | if (bp[0] == '\\' && bp[1] == 'x') { |
| 1096 | /* HEX STRING */ |
| 1097 | bp += 2; |
| 1098 | while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) { |
| 1099 | int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10; |
| 1100 | bp++; |
| 1101 | byte <<= 4; |
| 1102 | byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10; |
| 1103 | *dest++ = byte; |
| 1104 | bp++; |
| 1105 | len++; |
| 1106 | } |
| 1107 | } else { |
| 1108 | /* text with \nnn octal quoting */ |
| 1109 | while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) { |
| 1110 | if (*bp == '\\' && |
| 1111 | isodigit(bp[1]) && (bp[1] <= '3') && |
| 1112 | isodigit(bp[2]) && |
| 1113 | isodigit(bp[3])) { |
| 1114 | int byte = (*++bp -'0'); |
| 1115 | bp++; |
| 1116 | byte = (byte << 3) | (*bp++ - '0'); |
| 1117 | byte = (byte << 3) | (*bp++ - '0'); |
| 1118 | *dest++ = byte; |
| 1119 | len++; |
| 1120 | } else { |
| 1121 | *dest++ = *bp++; |
| 1122 | len++; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if (*bp != ' ' && *bp != '\n' && *bp != '\0') |
| 1128 | return -1; |
| 1129 | while (*bp == ' ') bp++; |
| 1130 | *bpp = bp; |
| 1131 | *dest = '\0'; |
| 1132 | return len; |
| 1133 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 1134 | EXPORT_SYMBOL_GPL(qword_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
| 1136 | |
| 1137 | /* |
| 1138 | * support /proc/sunrpc/cache/$CACHENAME/content |
| 1139 | * as a seqfile. |
| 1140 | * We call ->cache_show passing NULL for the item to |
| 1141 | * get a header, then pass each real item in the cache |
| 1142 | */ |
| 1143 | |
| 1144 | struct handle { |
| 1145 | struct cache_detail *cd; |
| 1146 | }; |
| 1147 | |
| 1148 | static void *c_start(struct seq_file *m, loff_t *pos) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 1149 | __acquires(cd->hash_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | { |
| 1151 | loff_t n = *pos; |
| 1152 | unsigned hash, entry; |
| 1153 | struct cache_head *ch; |
| 1154 | struct cache_detail *cd = ((struct handle*)m->private)->cd; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
| 1157 | read_lock(&cd->hash_lock); |
| 1158 | if (!n--) |
| 1159 | return SEQ_START_TOKEN; |
| 1160 | hash = n >> 32; |
| 1161 | entry = n & ((1LL<<32) - 1); |
| 1162 | |
| 1163 | for (ch=cd->hash_table[hash]; ch; ch=ch->next) |
| 1164 | if (!entry--) |
| 1165 | return ch; |
| 1166 | n &= ~((1LL<<32) - 1); |
| 1167 | do { |
| 1168 | hash++; |
| 1169 | n += 1LL<<32; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1170 | } while(hash < cd->hash_size && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | cd->hash_table[hash]==NULL); |
| 1172 | if (hash >= cd->hash_size) |
| 1173 | return NULL; |
| 1174 | *pos = n+1; |
| 1175 | return cd->hash_table[hash]; |
| 1176 | } |
| 1177 | |
| 1178 | static void *c_next(struct seq_file *m, void *p, loff_t *pos) |
| 1179 | { |
| 1180 | struct cache_head *ch = p; |
| 1181 | int hash = (*pos >> 32); |
| 1182 | struct cache_detail *cd = ((struct handle*)m->private)->cd; |
| 1183 | |
| 1184 | if (p == SEQ_START_TOKEN) |
| 1185 | hash = 0; |
| 1186 | else if (ch->next == NULL) { |
| 1187 | hash++; |
| 1188 | *pos += 1LL<<32; |
| 1189 | } else { |
| 1190 | ++*pos; |
| 1191 | return ch->next; |
| 1192 | } |
| 1193 | *pos &= ~((1LL<<32) - 1); |
| 1194 | while (hash < cd->hash_size && |
| 1195 | cd->hash_table[hash] == NULL) { |
| 1196 | hash++; |
| 1197 | *pos += 1LL<<32; |
| 1198 | } |
| 1199 | if (hash >= cd->hash_size) |
| 1200 | return NULL; |
| 1201 | ++*pos; |
| 1202 | return cd->hash_table[hash]; |
| 1203 | } |
| 1204 | |
| 1205 | static void c_stop(struct seq_file *m, void *p) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 1206 | __releases(cd->hash_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | { |
| 1208 | struct cache_detail *cd = ((struct handle*)m->private)->cd; |
| 1209 | read_unlock(&cd->hash_lock); |
| 1210 | } |
| 1211 | |
| 1212 | static int c_show(struct seq_file *m, void *p) |
| 1213 | { |
| 1214 | struct cache_head *cp = p; |
| 1215 | struct cache_detail *cd = ((struct handle*)m->private)->cd; |
| 1216 | |
| 1217 | if (p == SEQ_START_TOKEN) |
| 1218 | return cd->cache_show(m, cd, NULL); |
| 1219 | |
| 1220 | ifdebug(CACHE) |
NeilBrown | 4013ede | 2006-03-27 01:15:07 -0800 | [diff] [blame] | 1221 | seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n", |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 1222 | cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | cache_get(cp); |
| 1224 | if (cache_check(cd, cp, NULL)) |
| 1225 | /* cache_check does a cache_put on failure */ |
| 1226 | seq_printf(m, "# "); |
| 1227 | else |
| 1228 | cache_put(cp, cd); |
| 1229 | |
| 1230 | return cd->cache_show(m, cd, cp); |
| 1231 | } |
| 1232 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1233 | static const struct seq_operations cache_content_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | .start = c_start, |
| 1235 | .next = c_next, |
| 1236 | .stop = c_stop, |
| 1237 | .show = c_show, |
| 1238 | }; |
| 1239 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1240 | static int content_open(struct inode *inode, struct file *file, |
| 1241 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | struct handle *han; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1245 | if (!cd || !try_module_get(cd->owner)) |
| 1246 | return -EACCES; |
Pavel Emelyanov | ec93103 | 2007-10-10 02:31:07 -0700 | [diff] [blame] | 1247 | han = __seq_open_private(file, &cache_content_op, sizeof(*han)); |
Li Zefan | a5990ea | 2010-03-11 14:08:10 -0800 | [diff] [blame] | 1248 | if (han == NULL) { |
| 1249 | module_put(cd->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | return -ENOMEM; |
Li Zefan | a5990ea | 2010-03-11 14:08:10 -0800 | [diff] [blame] | 1251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
| 1253 | han->cd = cd; |
Pavel Emelyanov | ec93103 | 2007-10-10 02:31:07 -0700 | [diff] [blame] | 1254 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1257 | static int content_release(struct inode *inode, struct file *file, |
| 1258 | struct cache_detail *cd) |
| 1259 | { |
| 1260 | int ret = seq_release_private(inode, file); |
| 1261 | module_put(cd->owner); |
| 1262 | return ret; |
| 1263 | } |
| 1264 | |
| 1265 | static int open_flush(struct inode *inode, struct file *file, |
| 1266 | struct cache_detail *cd) |
| 1267 | { |
| 1268 | if (!cd || !try_module_get(cd->owner)) |
| 1269 | return -EACCES; |
| 1270 | return nonseekable_open(inode, file); |
| 1271 | } |
| 1272 | |
| 1273 | static int release_flush(struct inode *inode, struct file *file, |
| 1274 | struct cache_detail *cd) |
| 1275 | { |
| 1276 | module_put(cd->owner); |
| 1277 | return 0; |
| 1278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
| 1280 | static ssize_t read_flush(struct file *file, char __user *buf, |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1281 | size_t count, loff_t *ppos, |
| 1282 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | char tbuf[20]; |
| 1285 | unsigned long p = *ppos; |
Chuck Lever | 01b2969 | 2007-10-26 13:31:20 -0400 | [diff] [blame] | 1286 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
| 1288 | sprintf(tbuf, "%lu\n", cd->flush_time); |
| 1289 | len = strlen(tbuf); |
| 1290 | if (p >= len) |
| 1291 | return 0; |
| 1292 | len -= p; |
Chuck Lever | 01b2969 | 2007-10-26 13:31:20 -0400 | [diff] [blame] | 1293 | if (len > count) |
| 1294 | len = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | if (copy_to_user(buf, (void*)(tbuf+p), len)) |
Chuck Lever | 01b2969 | 2007-10-26 13:31:20 -0400 | [diff] [blame] | 1296 | return -EFAULT; |
| 1297 | *ppos += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | return len; |
| 1299 | } |
| 1300 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1301 | static ssize_t write_flush(struct file *file, const char __user *buf, |
| 1302 | size_t count, loff_t *ppos, |
| 1303 | struct cache_detail *cd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | char tbuf[20]; |
| 1306 | char *ep; |
| 1307 | long flushtime; |
| 1308 | if (*ppos || count > sizeof(tbuf)-1) |
| 1309 | return -EINVAL; |
| 1310 | if (copy_from_user(tbuf, buf, count)) |
| 1311 | return -EFAULT; |
| 1312 | tbuf[count] = 0; |
| 1313 | flushtime = simple_strtoul(tbuf, &ep, 0); |
| 1314 | if (*ep && *ep != '\n') |
| 1315 | return -EINVAL; |
| 1316 | |
| 1317 | cd->flush_time = flushtime; |
| 1318 | cd->nextcheck = get_seconds(); |
| 1319 | cache_flush(); |
| 1320 | |
| 1321 | *ppos += count; |
| 1322 | return count; |
| 1323 | } |
| 1324 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1325 | static ssize_t cache_read_procfs(struct file *filp, char __user *buf, |
| 1326 | size_t count, loff_t *ppos) |
| 1327 | { |
| 1328 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; |
| 1329 | |
| 1330 | return cache_read(filp, buf, count, ppos, cd); |
| 1331 | } |
| 1332 | |
| 1333 | static ssize_t cache_write_procfs(struct file *filp, const char __user *buf, |
| 1334 | size_t count, loff_t *ppos) |
| 1335 | { |
| 1336 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; |
| 1337 | |
| 1338 | return cache_write(filp, buf, count, ppos, cd); |
| 1339 | } |
| 1340 | |
| 1341 | static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait) |
| 1342 | { |
| 1343 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; |
| 1344 | |
| 1345 | return cache_poll(filp, wait, cd); |
| 1346 | } |
| 1347 | |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 1348 | static long cache_ioctl_procfs(struct file *filp, |
| 1349 | unsigned int cmd, unsigned long arg) |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1350 | { |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 1351 | long ret; |
| 1352 | struct inode *inode = filp->f_path.dentry->d_inode; |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1353 | struct cache_detail *cd = PDE(inode)->data; |
| 1354 | |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 1355 | lock_kernel(); |
| 1356 | ret = cache_ioctl(inode, filp, cmd, arg, cd); |
| 1357 | unlock_kernel(); |
| 1358 | |
| 1359 | return ret; |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static int cache_open_procfs(struct inode *inode, struct file *filp) |
| 1363 | { |
| 1364 | struct cache_detail *cd = PDE(inode)->data; |
| 1365 | |
| 1366 | return cache_open(inode, filp, cd); |
| 1367 | } |
| 1368 | |
| 1369 | static int cache_release_procfs(struct inode *inode, struct file *filp) |
| 1370 | { |
| 1371 | struct cache_detail *cd = PDE(inode)->data; |
| 1372 | |
| 1373 | return cache_release(inode, filp, cd); |
| 1374 | } |
| 1375 | |
| 1376 | static const struct file_operations cache_file_operations_procfs = { |
| 1377 | .owner = THIS_MODULE, |
| 1378 | .llseek = no_llseek, |
| 1379 | .read = cache_read_procfs, |
| 1380 | .write = cache_write_procfs, |
| 1381 | .poll = cache_poll_procfs, |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 1382 | .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */ |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1383 | .open = cache_open_procfs, |
| 1384 | .release = cache_release_procfs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | }; |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1386 | |
| 1387 | static int content_open_procfs(struct inode *inode, struct file *filp) |
| 1388 | { |
| 1389 | struct cache_detail *cd = PDE(inode)->data; |
| 1390 | |
| 1391 | return content_open(inode, filp, cd); |
| 1392 | } |
| 1393 | |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1394 | static int content_release_procfs(struct inode *inode, struct file *filp) |
| 1395 | { |
| 1396 | struct cache_detail *cd = PDE(inode)->data; |
| 1397 | |
| 1398 | return content_release(inode, filp, cd); |
| 1399 | } |
| 1400 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1401 | static const struct file_operations content_file_operations_procfs = { |
| 1402 | .open = content_open_procfs, |
| 1403 | .read = seq_read, |
| 1404 | .llseek = seq_lseek, |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1405 | .release = content_release_procfs, |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1406 | }; |
| 1407 | |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1408 | static int open_flush_procfs(struct inode *inode, struct file *filp) |
| 1409 | { |
| 1410 | struct cache_detail *cd = PDE(inode)->data; |
| 1411 | |
| 1412 | return open_flush(inode, filp, cd); |
| 1413 | } |
| 1414 | |
| 1415 | static int release_flush_procfs(struct inode *inode, struct file *filp) |
| 1416 | { |
| 1417 | struct cache_detail *cd = PDE(inode)->data; |
| 1418 | |
| 1419 | return release_flush(inode, filp, cd); |
| 1420 | } |
| 1421 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1422 | static ssize_t read_flush_procfs(struct file *filp, char __user *buf, |
| 1423 | size_t count, loff_t *ppos) |
| 1424 | { |
| 1425 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; |
| 1426 | |
| 1427 | return read_flush(filp, buf, count, ppos, cd); |
| 1428 | } |
| 1429 | |
| 1430 | static ssize_t write_flush_procfs(struct file *filp, |
| 1431 | const char __user *buf, |
| 1432 | size_t count, loff_t *ppos) |
| 1433 | { |
| 1434 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; |
| 1435 | |
| 1436 | return write_flush(filp, buf, count, ppos, cd); |
| 1437 | } |
| 1438 | |
| 1439 | static const struct file_operations cache_flush_operations_procfs = { |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1440 | .open = open_flush_procfs, |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1441 | .read = read_flush_procfs, |
| 1442 | .write = write_flush_procfs, |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1443 | .release = release_flush_procfs, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame^] | 1444 | .llseek = no_llseek, |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1445 | }; |
| 1446 | |
| 1447 | static void remove_cache_proc_entries(struct cache_detail *cd) |
| 1448 | { |
| 1449 | if (cd->u.procfs.proc_ent == NULL) |
| 1450 | return; |
| 1451 | if (cd->u.procfs.flush_ent) |
| 1452 | remove_proc_entry("flush", cd->u.procfs.proc_ent); |
| 1453 | if (cd->u.procfs.channel_ent) |
| 1454 | remove_proc_entry("channel", cd->u.procfs.proc_ent); |
| 1455 | if (cd->u.procfs.content_ent) |
| 1456 | remove_proc_entry("content", cd->u.procfs.proc_ent); |
| 1457 | cd->u.procfs.proc_ent = NULL; |
| 1458 | remove_proc_entry(cd->name, proc_net_rpc); |
| 1459 | } |
| 1460 | |
| 1461 | #ifdef CONFIG_PROC_FS |
| 1462 | static int create_cache_proc_entries(struct cache_detail *cd) |
| 1463 | { |
| 1464 | struct proc_dir_entry *p; |
| 1465 | |
| 1466 | cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc); |
| 1467 | if (cd->u.procfs.proc_ent == NULL) |
| 1468 | goto out_nomem; |
| 1469 | cd->u.procfs.channel_ent = NULL; |
| 1470 | cd->u.procfs.content_ent = NULL; |
| 1471 | |
| 1472 | p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR, |
| 1473 | cd->u.procfs.proc_ent, |
| 1474 | &cache_flush_operations_procfs, cd); |
| 1475 | cd->u.procfs.flush_ent = p; |
| 1476 | if (p == NULL) |
| 1477 | goto out_nomem; |
| 1478 | |
| 1479 | if (cd->cache_upcall || cd->cache_parse) { |
| 1480 | p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR, |
| 1481 | cd->u.procfs.proc_ent, |
| 1482 | &cache_file_operations_procfs, cd); |
| 1483 | cd->u.procfs.channel_ent = p; |
| 1484 | if (p == NULL) |
| 1485 | goto out_nomem; |
| 1486 | } |
| 1487 | if (cd->cache_show) { |
| 1488 | p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR, |
| 1489 | cd->u.procfs.proc_ent, |
| 1490 | &content_file_operations_procfs, cd); |
| 1491 | cd->u.procfs.content_ent = p; |
| 1492 | if (p == NULL) |
| 1493 | goto out_nomem; |
| 1494 | } |
| 1495 | return 0; |
| 1496 | out_nomem: |
| 1497 | remove_cache_proc_entries(cd); |
| 1498 | return -ENOMEM; |
| 1499 | } |
| 1500 | #else /* CONFIG_PROC_FS */ |
| 1501 | static int create_cache_proc_entries(struct cache_detail *cd) |
| 1502 | { |
| 1503 | return 0; |
| 1504 | } |
| 1505 | #endif |
| 1506 | |
Artem Bityutskiy | 8eab945 | 2010-07-01 18:05:56 +0300 | [diff] [blame] | 1507 | void __init cache_initialize(void) |
| 1508 | { |
| 1509 | INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean); |
| 1510 | } |
| 1511 | |
Trond Myklebust | 173912a | 2009-08-09 15:14:29 -0400 | [diff] [blame] | 1512 | int cache_register(struct cache_detail *cd) |
| 1513 | { |
| 1514 | int ret; |
| 1515 | |
| 1516 | sunrpc_init_cache_detail(cd); |
| 1517 | ret = create_cache_proc_entries(cd); |
| 1518 | if (ret) |
| 1519 | sunrpc_destroy_cache_detail(cd); |
| 1520 | return ret; |
| 1521 | } |
| 1522 | EXPORT_SYMBOL_GPL(cache_register); |
| 1523 | |
| 1524 | void cache_unregister(struct cache_detail *cd) |
| 1525 | { |
| 1526 | remove_cache_proc_entries(cd); |
| 1527 | sunrpc_destroy_cache_detail(cd); |
| 1528 | } |
| 1529 | EXPORT_SYMBOL_GPL(cache_unregister); |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1530 | |
| 1531 | static ssize_t cache_read_pipefs(struct file *filp, char __user *buf, |
| 1532 | size_t count, loff_t *ppos) |
| 1533 | { |
| 1534 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; |
| 1535 | |
| 1536 | return cache_read(filp, buf, count, ppos, cd); |
| 1537 | } |
| 1538 | |
| 1539 | static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf, |
| 1540 | size_t count, loff_t *ppos) |
| 1541 | { |
| 1542 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; |
| 1543 | |
| 1544 | return cache_write(filp, buf, count, ppos, cd); |
| 1545 | } |
| 1546 | |
| 1547 | static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait) |
| 1548 | { |
| 1549 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; |
| 1550 | |
| 1551 | return cache_poll(filp, wait, cd); |
| 1552 | } |
| 1553 | |
Frederic Weisbecker | 9918ff2 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 1554 | static long cache_ioctl_pipefs(struct file *filp, |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1555 | unsigned int cmd, unsigned long arg) |
| 1556 | { |
Frederic Weisbecker | 9918ff2 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 1557 | struct inode *inode = filp->f_dentry->d_inode; |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1558 | struct cache_detail *cd = RPC_I(inode)->private; |
Frederic Weisbecker | 9918ff2 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 1559 | long ret; |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1560 | |
Frederic Weisbecker | 9918ff2 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 1561 | lock_kernel(); |
| 1562 | ret = cache_ioctl(inode, filp, cmd, arg, cd); |
| 1563 | unlock_kernel(); |
| 1564 | |
| 1565 | return ret; |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | static int cache_open_pipefs(struct inode *inode, struct file *filp) |
| 1569 | { |
| 1570 | struct cache_detail *cd = RPC_I(inode)->private; |
| 1571 | |
| 1572 | return cache_open(inode, filp, cd); |
| 1573 | } |
| 1574 | |
| 1575 | static int cache_release_pipefs(struct inode *inode, struct file *filp) |
| 1576 | { |
| 1577 | struct cache_detail *cd = RPC_I(inode)->private; |
| 1578 | |
| 1579 | return cache_release(inode, filp, cd); |
| 1580 | } |
| 1581 | |
| 1582 | const struct file_operations cache_file_operations_pipefs = { |
| 1583 | .owner = THIS_MODULE, |
| 1584 | .llseek = no_llseek, |
| 1585 | .read = cache_read_pipefs, |
| 1586 | .write = cache_write_pipefs, |
| 1587 | .poll = cache_poll_pipefs, |
Frederic Weisbecker | 9918ff2 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 1588 | .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */ |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1589 | .open = cache_open_pipefs, |
| 1590 | .release = cache_release_pipefs, |
| 1591 | }; |
| 1592 | |
| 1593 | static int content_open_pipefs(struct inode *inode, struct file *filp) |
| 1594 | { |
| 1595 | struct cache_detail *cd = RPC_I(inode)->private; |
| 1596 | |
| 1597 | return content_open(inode, filp, cd); |
| 1598 | } |
| 1599 | |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1600 | static int content_release_pipefs(struct inode *inode, struct file *filp) |
| 1601 | { |
| 1602 | struct cache_detail *cd = RPC_I(inode)->private; |
| 1603 | |
| 1604 | return content_release(inode, filp, cd); |
| 1605 | } |
| 1606 | |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1607 | const struct file_operations content_file_operations_pipefs = { |
| 1608 | .open = content_open_pipefs, |
| 1609 | .read = seq_read, |
| 1610 | .llseek = seq_lseek, |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1611 | .release = content_release_pipefs, |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1612 | }; |
| 1613 | |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1614 | static int open_flush_pipefs(struct inode *inode, struct file *filp) |
| 1615 | { |
| 1616 | struct cache_detail *cd = RPC_I(inode)->private; |
| 1617 | |
| 1618 | return open_flush(inode, filp, cd); |
| 1619 | } |
| 1620 | |
| 1621 | static int release_flush_pipefs(struct inode *inode, struct file *filp) |
| 1622 | { |
| 1623 | struct cache_detail *cd = RPC_I(inode)->private; |
| 1624 | |
| 1625 | return release_flush(inode, filp, cd); |
| 1626 | } |
| 1627 | |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1628 | static ssize_t read_flush_pipefs(struct file *filp, char __user *buf, |
| 1629 | size_t count, loff_t *ppos) |
| 1630 | { |
| 1631 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; |
| 1632 | |
| 1633 | return read_flush(filp, buf, count, ppos, cd); |
| 1634 | } |
| 1635 | |
| 1636 | static ssize_t write_flush_pipefs(struct file *filp, |
| 1637 | const char __user *buf, |
| 1638 | size_t count, loff_t *ppos) |
| 1639 | { |
| 1640 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; |
| 1641 | |
| 1642 | return write_flush(filp, buf, count, ppos, cd); |
| 1643 | } |
| 1644 | |
| 1645 | const struct file_operations cache_flush_operations_pipefs = { |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1646 | .open = open_flush_pipefs, |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1647 | .read = read_flush_pipefs, |
| 1648 | .write = write_flush_pipefs, |
Trond Myklebust | f7e86ab | 2009-08-19 18:13:00 -0400 | [diff] [blame] | 1649 | .release = release_flush_pipefs, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame^] | 1650 | .llseek = no_llseek, |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 1651 | }; |
| 1652 | |
| 1653 | int sunrpc_cache_register_pipefs(struct dentry *parent, |
| 1654 | const char *name, mode_t umode, |
| 1655 | struct cache_detail *cd) |
| 1656 | { |
| 1657 | struct qstr q; |
| 1658 | struct dentry *dir; |
| 1659 | int ret = 0; |
| 1660 | |
| 1661 | sunrpc_init_cache_detail(cd); |
| 1662 | q.name = name; |
| 1663 | q.len = strlen(name); |
| 1664 | q.hash = full_name_hash(q.name, q.len); |
| 1665 | dir = rpc_create_cache_dir(parent, &q, umode, cd); |
| 1666 | if (!IS_ERR(dir)) |
| 1667 | cd->u.pipefs.dir = dir; |
| 1668 | else { |
| 1669 | sunrpc_destroy_cache_detail(cd); |
| 1670 | ret = PTR_ERR(dir); |
| 1671 | } |
| 1672 | return ret; |
| 1673 | } |
| 1674 | EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs); |
| 1675 | |
| 1676 | void sunrpc_cache_unregister_pipefs(struct cache_detail *cd) |
| 1677 | { |
| 1678 | rpc_remove_cache_dir(cd->u.pipefs.dir); |
| 1679 | cd->u.pipefs.dir = NULL; |
| 1680 | sunrpc_destroy_cache_detail(cd); |
| 1681 | } |
| 1682 | EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs); |
| 1683 | |