1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * include/linux/sunrpc/cache.h
5 * Generic code for various authentication-related caches
6 * used by sunrpc clients and servers.
11 #ifndef _LINUX_SUNRPC_CACHE_H_
12 #define _LINUX_SUNRPC_CACHE_H_
14 #include <linux/kref.h>
15 #include <linux/slab.h>
16 #include <linux/atomic.h>
17 #include <linux/proc_fs.h>
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.
30 * as well as assorted code fragments (e.g. compare keys) and numbers
31 * (e.g. hash size, goal_age, etc).
33 * Each cache must be registered so that it can be cleaned regularly.
34 * When the cache is unregistered, it is flushed completely.
36 * Entries have a ref count and a 'hashed' flag which counts the existence
38 * We only expire entries when refcount is zero.
39 * Existence in the cache is counted the refcount.
42 /* Every cache item has a common header that is used
43 * for expiring and refreshing entries.
47 struct hlist_node cache_list;
48 time_t expiry_time; /* After time time, don't use the data */
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.
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*/
60 #define CACHE_CLEANED 3 /* Entry has been cleaned from cache */
62 #define CACHE_NEW_EXPIRY 120 /* keep new things pending confirmation for 120 seconds */
65 struct module * owner;
67 struct hlist_head * hash_table;
71 void (*cache_put)(struct kref *);
73 int (*cache_upcall)(struct cache_detail *,
76 void (*cache_request)(struct cache_detail *cd,
77 struct cache_head *ch,
78 char **bpp, int *blen);
80 int (*cache_parse)(struct cache_detail *,
83 int (*cache_show)(struct seq_file *m,
84 struct cache_detail *cd,
85 struct cache_head *h);
86 void (*warn_no_listener)(struct cache_detail *cd,
89 struct cache_head * (*alloc)(void);
90 int (*match)(struct cache_head *orig, struct cache_head *new);
91 void (*init)(struct cache_head *orig, struct cache_head *new);
92 void (*update)(struct cache_head *orig, struct cache_head *new);
94 /* fields below this comment are for internal use
95 * and should not be touched by cache owners
97 time_t flush_time; /* flush all cache items with
98 * last_refresh at or earlier
99 * than this. last_refresh
100 * is never set at or earlier
103 struct list_head others;
107 /* fields for communication over channel */
108 struct list_head queue;
110 atomic_t readers; /* how many time is /chennel open */
111 time_t last_close; /* if no readers, when did last close */
112 time_t last_warn; /* when we last warned about no readers */
115 struct proc_dir_entry *procfs;
116 struct dentry *pipefs;
122 /* this must be embedded in any request structure that
123 * identifies an object that will want a callback on
127 struct cache_deferred_req *(*defer)(struct cache_req *req);
128 int thread_wait; /* How long (jiffies) we can block the
129 * current thread to wait for updates.
132 /* this must be embedded in a deferred_request that is being
133 * delayed awaiting cache-fill
135 struct cache_deferred_req {
136 struct hlist_node hash; /* on hash chain */
137 struct list_head recent; /* on fifo */
138 struct cache_head *item; /* cache item we wait on */
139 void *owner; /* we might need to discard all defered requests
140 * owned by someone */
141 void (*revisit)(struct cache_deferred_req *req,
146 * timestamps kept in the cache are expressed in seconds
147 * since boot. This is the best for measuring differences in
150 static inline time_t seconds_since_boot(void)
152 struct timespec boot;
154 return get_seconds() - boot.tv_sec;
157 static inline time_t convert_to_wallclock(time_t sinceboot)
159 struct timespec boot;
161 return boot.tv_sec + sinceboot;
164 extern const struct file_operations cache_file_operations_pipefs;
165 extern const struct file_operations content_file_operations_pipefs;
166 extern const struct file_operations cache_flush_operations_pipefs;
168 extern struct cache_head *
169 sunrpc_cache_lookup_rcu(struct cache_detail *detail,
170 struct cache_head *key, int hash);
171 extern struct cache_head *
172 sunrpc_cache_update(struct cache_detail *detail,
173 struct cache_head *new, struct cache_head *old, int hash);
176 sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h);
179 extern void cache_clean_deferred(void *owner);
181 static inline struct cache_head *cache_get(struct cache_head *h)
187 static inline struct cache_head *cache_get_rcu(struct cache_head *h)
189 if (kref_get_unless_zero(&h->ref))
194 static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
196 if (kref_read(&h->ref) <= 2 &&
197 h->expiry_time < cd->nextcheck)
198 cd->nextcheck = h->expiry_time;
199 kref_put(&h->ref, cd->cache_put);
202 static inline bool cache_is_expired(struct cache_detail *detail, struct cache_head *h)
204 if (!test_bit(CACHE_VALID, &h->flags))
207 return (h->expiry_time < seconds_since_boot()) ||
208 (detail->flush_time >= h->last_refresh);
211 extern int cache_check(struct cache_detail *detail,
212 struct cache_head *h, struct cache_req *rqstp);
213 extern void cache_flush(void);
214 extern void cache_purge(struct cache_detail *detail);
215 #define NEVER (0x7FFFFFFF)
216 extern void __init cache_initialize(void);
217 extern int cache_register_net(struct cache_detail *cd, struct net *net);
218 extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
220 extern struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net);
221 extern void cache_destroy_net(struct cache_detail *cd, struct net *net);
223 extern void sunrpc_init_cache_detail(struct cache_detail *cd);
224 extern void sunrpc_destroy_cache_detail(struct cache_detail *cd);
225 extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
226 umode_t, struct cache_detail *);
227 extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
228 extern void sunrpc_cache_unhash(struct cache_detail *, struct cache_head *);
230 /* Must store cache_detail in seq_file->private if using next three functions */
231 extern void *cache_seq_start_rcu(struct seq_file *file, loff_t *pos);
232 extern void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos);
233 extern void cache_seq_stop_rcu(struct seq_file *file, void *p);
235 extern void qword_add(char **bpp, int *lp, char *str);
236 extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
237 extern int qword_get(char **bpp, char *dest, int bufsize);
239 static inline int get_int(char **bpp, int *anint)
244 int len = qword_get(bpp, buf, sizeof(buf));
251 rv = simple_strtol(buf, &ep, 0);
259 static inline int get_uint(char **bpp, unsigned int *anint)
262 int len = qword_get(bpp, buf, sizeof(buf));
269 if (kstrtouint(buf, 0, anint))
275 static inline int get_time(char **bpp, time_t *time)
279 int len = qword_get(bpp, buf, sizeof(buf));
286 if (kstrtoll(buf, 0, &ll))
293 static inline time_t get_expiry(char **bpp)
296 struct timespec boot;
298 if (get_time(bpp, &rv))
303 return rv - boot.tv_sec;
306 #endif /* _LINUX_SUNRPC_CACHE_H_ */