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