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