Commit | Line | Data |
---|---|---|
ddc64d0a | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
1da177e4 LT |
2 | /* |
3 | * include/linux/sunrpc/cache.h | |
4 | * | |
5 | * Generic code for various authentication-related caches | |
6 | * used by sunrpc clients and servers. | |
7 | * | |
8 | * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au> | |
1da177e4 LT |
9 | */ |
10 | ||
11 | #ifndef _LINUX_SUNRPC_CACHE_H_ | |
12 | #define _LINUX_SUNRPC_CACHE_H_ | |
13 | ||
57cc7215 | 14 | #include <linux/kref.h> |
1da177e4 | 15 | #include <linux/slab.h> |
60063497 | 16 | #include <linux/atomic.h> |
1da177e4 LT |
17 | #include <linux/proc_fs.h> |
18 | ||
19 | /* | |
20 | * Each cache requires: | |
21 | * - A 'struct cache_detail' which contains information specific to the cache | |
22 | * for common code to use. | |
23 | * - An item structure that must contain a "struct cache_head" | |
24 | * - A lookup function defined using DefineCacheLookup | |
25 | * - A 'put' function that can release a cache item. It will only | |
26 | * be called after cache_put has succeed, so there are guarantee | |
27 | * to be no references. | |
28 | * - A function to calculate a hash of an item's key. | |
29 | * | |
30 | * as well as assorted code fragments (e.g. compare keys) and numbers | |
31 | * (e.g. hash size, goal_age, etc). | |
32 | * | |
33 | * Each cache must be registered so that it can be cleaned regularly. | |
34 | * When the cache is unregistered, it is flushed completely. | |
35 | * | |
25985edc | 36 | * Entries have a ref count and a 'hashed' flag which counts the existence |
1da177e4 LT |
37 | * in the hash table. |
38 | * We only expire entries when refcount is zero. | |
25985edc | 39 | * Existence in the cache is counted the refcount. |
1da177e4 LT |
40 | */ |
41 | ||
42 | /* Every cache item has a common header that is used | |
43 | * for expiring and refreshing entries. | |
44 | * | |
45 | */ | |
46 | struct cache_head { | |
129e5824 | 47 | struct hlist_node cache_list; |
1da177e4 | 48 | time_t expiry_time; /* After time time, don't use the data */ |
77862036 NB |
49 | time_t last_refresh; /* If CACHE_PENDING, this is when upcall was |
50 | * sent, else this is when update was | |
51 | * received, though it is alway set to | |
52 | * be *after* ->flush_time. | |
1da177e4 | 53 | */ |
baab935f | 54 | struct kref ref; |
1da177e4 LT |
55 | unsigned long flags; |
56 | }; | |
57 | #define CACHE_VALID 0 /* Entry contains valid data */ | |
58 | #define CACHE_NEGATIVE 1 /* Negative entry - there is no match for the key */ | |
59 | #define CACHE_PENDING 2 /* An upcall has been sent but no reply received yet*/ | |
013920eb | 60 | #define CACHE_CLEANED 3 /* Entry has been cleaned from cache */ |
1da177e4 LT |
61 | |
62 | #define CACHE_NEW_EXPIRY 120 /* keep new things pending confirmation for 120 seconds */ | |
63 | ||
64 | struct cache_detail { | |
f35279d3 | 65 | struct module * owner; |
1da177e4 | 66 | int hash_size; |
129e5824 | 67 | struct hlist_head * hash_table; |
1863d77f | 68 | spinlock_t hash_lock; |
1da177e4 | 69 | |
1da177e4 | 70 | char *name; |
baab935f | 71 | void (*cache_put)(struct kref *); |
1da177e4 | 72 | |
bc74b4f5 TM |
73 | int (*cache_upcall)(struct cache_detail *, |
74 | struct cache_head *); | |
75 | ||
73fb847a SK |
76 | void (*cache_request)(struct cache_detail *cd, |
77 | struct cache_head *ch, | |
78 | char **bpp, int *blen); | |
79 | ||
1da177e4 LT |
80 | int (*cache_parse)(struct cache_detail *, |
81 | char *buf, int len); | |
82 | ||
83 | int (*cache_show)(struct seq_file *m, | |
84 | struct cache_detail *cd, | |
85 | struct cache_head *h); | |
2da8ca26 TM |
86 | void (*warn_no_listener)(struct cache_detail *cd, |
87 | int has_died); | |
1da177e4 | 88 | |
15a5f6bd | 89 | struct cache_head * (*alloc)(void); |
f69d6d8e | 90 | void (*flush)(void); |
15a5f6bd N |
91 | int (*match)(struct cache_head *orig, struct cache_head *new); |
92 | void (*init)(struct cache_head *orig, struct cache_head *new); | |
93 | void (*update)(struct cache_head *orig, struct cache_head *new); | |
94 | ||
1da177e4 LT |
95 | /* fields below this comment are for internal use |
96 | * and should not be touched by cache owners | |
97 | */ | |
77862036 NB |
98 | time_t flush_time; /* flush all cache items with |
99 | * last_refresh at or earlier | |
100 | * than this. last_refresh | |
101 | * is never set at or earlier | |
102 | * than this. | |
103 | */ | |
1da177e4 LT |
104 | struct list_head others; |
105 | time_t nextcheck; | |
106 | int entries; | |
107 | ||
108 | /* fields for communication over channel */ | |
109 | struct list_head queue; | |
1da177e4 | 110 | |
64a38e84 DW |
111 | atomic_t writers; /* how many time is /channel open */ |
112 | time_t last_close; /* if no writers, when did last close */ | |
113 | time_t last_warn; /* when we last warned about no writers */ | |
173912a6 TM |
114 | |
115 | union { | |
863d7d9c KM |
116 | struct proc_dir_entry *procfs; |
117 | struct dentry *pipefs; | |
118 | }; | |
0a402d5a | 119 | struct net *net; |
1da177e4 LT |
120 | }; |
121 | ||
122 | ||
123 | /* this must be embedded in any request structure that | |
124 | * identifies an object that will want a callback on | |
125 | * a cache fill | |
126 | */ | |
127 | struct cache_req { | |
128 | struct cache_deferred_req *(*defer)(struct cache_req *req); | |
f16b6e8d N |
129 | int thread_wait; /* How long (jiffies) we can block the |
130 | * current thread to wait for updates. | |
131 | */ | |
1da177e4 LT |
132 | }; |
133 | /* this must be embedded in a deferred_request that is being | |
134 | * delayed awaiting cache-fill | |
135 | */ | |
136 | struct cache_deferred_req { | |
11174492 | 137 | struct hlist_node hash; /* on hash chain */ |
1da177e4 LT |
138 | struct list_head recent; /* on fifo */ |
139 | struct cache_head *item; /* cache item we wait on */ | |
1da177e4 LT |
140 | void *owner; /* we might need to discard all defered requests |
141 | * owned by someone */ | |
142 | void (*revisit)(struct cache_deferred_req *req, | |
143 | int too_many); | |
144 | }; | |
145 | ||
7715cde8 N |
146 | /* |
147 | * timestamps kept in the cache are expressed in seconds | |
148 | * since boot. This is the best for measuring differences in | |
149 | * real time. | |
150 | */ | |
151 | static inline time_t seconds_since_boot(void) | |
152 | { | |
153 | struct timespec boot; | |
154 | getboottime(&boot); | |
155 | return get_seconds() - boot.tv_sec; | |
156 | } | |
157 | ||
158 | static inline time_t convert_to_wallclock(time_t sinceboot) | |
159 | { | |
160 | struct timespec boot; | |
161 | getboottime(&boot); | |
162 | return boot.tv_sec + sinceboot; | |
163 | } | |
7d317f2c | 164 | |
8854e82d TM |
165 | extern const struct file_operations cache_file_operations_pipefs; |
166 | extern const struct file_operations content_file_operations_pipefs; | |
167 | extern const struct file_operations cache_flush_operations_pipefs; | |
168 | ||
ae74136b TM |
169 | extern struct cache_head * |
170 | sunrpc_cache_lookup_rcu(struct cache_detail *detail, | |
171 | struct cache_head *key, int hash); | |
15a5f6bd | 172 | extern struct cache_head * |
15a5f6bd N |
173 | sunrpc_cache_update(struct cache_detail *detail, |
174 | struct cache_head *new, struct cache_head *old, int hash); | |
175 | ||
bc74b4f5 | 176 | extern int |
21cd1254 | 177 | sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h); |
bc74b4f5 | 178 | |
1da177e4 | 179 | |
1da177e4 LT |
180 | extern void cache_clean_deferred(void *owner); |
181 | ||
182 | static inline struct cache_head *cache_get(struct cache_head *h) | |
183 | { | |
baab935f | 184 | kref_get(&h->ref); |
1da177e4 LT |
185 | return h; |
186 | } | |
187 | ||
ae74136b TM |
188 | static inline struct cache_head *cache_get_rcu(struct cache_head *h) |
189 | { | |
190 | if (kref_get_unless_zero(&h->ref)) | |
191 | return h; | |
192 | return NULL; | |
193 | } | |
1da177e4 | 194 | |
baab935f | 195 | static inline void cache_put(struct cache_head *h, struct cache_detail *cd) |
1da177e4 | 196 | { |
2c935bc5 | 197 | if (kref_read(&h->ref) <= 2 && |
1da177e4 LT |
198 | h->expiry_time < cd->nextcheck) |
199 | cd->nextcheck = h->expiry_time; | |
baab935f | 200 | kref_put(&h->ref, cd->cache_put); |
1da177e4 LT |
201 | } |
202 | ||
d6fc8821 | 203 | static inline bool cache_is_expired(struct cache_detail *detail, struct cache_head *h) |
7b2b1fee | 204 | { |
d6fc8821 KM |
205 | if (!test_bit(CACHE_VALID, &h->flags)) |
206 | return false; | |
207 | ||
7715cde8 | 208 | return (h->expiry_time < seconds_since_boot()) || |
77862036 | 209 | (detail->flush_time >= h->last_refresh); |
7b2b1fee GB |
210 | } |
211 | ||
1da177e4 LT |
212 | extern int cache_check(struct cache_detail *detail, |
213 | struct cache_head *h, struct cache_req *rqstp); | |
214 | extern void cache_flush(void); | |
215 | extern void cache_purge(struct cache_detail *detail); | |
216 | #define NEVER (0x7FFFFFFF) | |
8eab945c | 217 | extern void __init cache_initialize(void); |
593ce16b | 218 | extern int cache_register_net(struct cache_detail *cd, struct net *net); |
593ce16b | 219 | extern void cache_unregister_net(struct cache_detail *cd, struct net *net); |
1da177e4 | 220 | |
d34971a6 | 221 | extern struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net); |
0a402d5a SK |
222 | extern void cache_destroy_net(struct cache_detail *cd, struct net *net); |
223 | ||
820f9442 SK |
224 | extern void sunrpc_init_cache_detail(struct cache_detail *cd); |
225 | extern void sunrpc_destroy_cache_detail(struct cache_detail *cd); | |
8854e82d | 226 | extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *, |
64f1426f | 227 | umode_t, struct cache_detail *); |
8854e82d | 228 | extern void sunrpc_cache_unregister_pipefs(struct cache_detail *); |
2b477c00 | 229 | extern void sunrpc_cache_unhash(struct cache_detail *, struct cache_head *); |
8854e82d | 230 | |
c8c081b7 | 231 | /* Must store cache_detail in seq_file->private if using next three functions */ |
ae74136b TM |
232 | extern void *cache_seq_start_rcu(struct seq_file *file, loff_t *pos); |
233 | extern void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos); | |
234 | extern void cache_seq_stop_rcu(struct seq_file *file, void *p); | |
c8c081b7 | 235 | |
1da177e4 LT |
236 | extern void qword_add(char **bpp, int *lp, char *str); |
237 | extern void qword_addhex(char **bpp, int *lp, char *buf, int blen); | |
238 | extern int qword_get(char **bpp, char *dest, int bufsize); | |
239 | ||
240 | static inline int get_int(char **bpp, int *anint) | |
241 | { | |
242 | char buf[50]; | |
621eb19c BF |
243 | char *ep; |
244 | int rv; | |
d9c2ede6 EZ |
245 | int len = qword_get(bpp, buf, sizeof(buf)); |
246 | ||
247 | if (len < 0) | |
248 | return -EINVAL; | |
249 | if (len == 0) | |
250 | return -ENOENT; | |
251 | ||
621eb19c BF |
252 | rv = simple_strtol(buf, &ep, 0); |
253 | if (*ep) | |
d9c2ede6 EZ |
254 | return -EINVAL; |
255 | ||
621eb19c | 256 | *anint = rv; |
1da177e4 LT |
257 | return 0; |
258 | } | |
259 | ||
a007c4c3 BF |
260 | static inline int get_uint(char **bpp, unsigned int *anint) |
261 | { | |
262 | char buf[50]; | |
263 | int len = qword_get(bpp, buf, sizeof(buf)); | |
264 | ||
265 | if (len < 0) | |
266 | return -EINVAL; | |
267 | if (len == 0) | |
268 | return -ENOENT; | |
269 | ||
270 | if (kstrtouint(buf, 0, anint)) | |
271 | return -EINVAL; | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
2f74f972 HJ |
276 | static inline int get_time(char **bpp, time_t *time) |
277 | { | |
278 | char buf[50]; | |
279 | long long ll; | |
280 | int len = qword_get(bpp, buf, sizeof(buf)); | |
281 | ||
282 | if (len < 0) | |
283 | return -EINVAL; | |
284 | if (len == 0) | |
285 | return -ENOENT; | |
286 | ||
287 | if (kstrtoll(buf, 0, &ll)) | |
288 | return -EINVAL; | |
289 | ||
290 | *time = (time_t)ll; | |
291 | return 0; | |
292 | } | |
293 | ||
1da177e4 LT |
294 | static inline time_t get_expiry(char **bpp) |
295 | { | |
2f74f972 | 296 | time_t rv; |
c5b29f88 N |
297 | struct timespec boot; |
298 | ||
2f74f972 | 299 | if (get_time(bpp, &rv)) |
1da177e4 LT |
300 | return 0; |
301 | if (rv < 0) | |
302 | return 0; | |
c5b29f88 N |
303 | getboottime(&boot); |
304 | return rv - boot.tv_sec; | |
1da177e4 LT |
305 | } |
306 | ||
307 | #endif /* _LINUX_SUNRPC_CACHE_H_ */ |