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