1
2
3
4
5
6
7
8
9
10
11#ifndef _LINUX_SUNRPC_CACHE_H_
12#define _LINUX_SUNRPC_CACHE_H_
13
14#include <linux/kref.h>
15#include <linux/slab.h>
16#include <linux/atomic.h>
17#include <linux/proc_fs.h>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46struct cache_head {
47 struct hlist_node cache_list;
48 time_t expiry_time;
49 time_t last_refresh;
50
51
52
53
54 struct kref ref;
55 unsigned long flags;
56};
57#define CACHE_VALID 0
58#define CACHE_NEGATIVE 1
59#define CACHE_PENDING 2
60#define CACHE_CLEANED 3
61
62#define CACHE_NEW_EXPIRY 120
63
64struct cache_detail {
65 struct module * owner;
66 int hash_size;
67 struct hlist_head * hash_table;
68 spinlock_t hash_lock;
69
70 char *name;
71 void (*cache_put)(struct kref *);
72
73 int (*cache_upcall)(struct cache_detail *,
74 struct cache_head *);
75
76 void (*cache_request)(struct cache_detail *cd,
77 struct cache_head *ch,
78 char **bpp, int *blen);
79
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);
86 void (*warn_no_listener)(struct cache_detail *cd,
87 int has_died);
88
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);
93
94
95
96
97 time_t flush_time;
98
99
100
101
102
103 struct list_head others;
104 time_t nextcheck;
105 int entries;
106
107
108 struct list_head queue;
109
110 atomic_t readers;
111 time_t last_close;
112 time_t last_warn;
113
114 union {
115 struct proc_dir_entry *procfs;
116 struct dentry *pipefs;
117 };
118 struct net *net;
119};
120
121
122
123
124
125
126struct cache_req {
127 struct cache_deferred_req *(*defer)(struct cache_req *req);
128 int thread_wait;
129
130
131};
132
133
134
135struct cache_deferred_req {
136 struct hlist_node hash;
137 struct list_head recent;
138 struct cache_head *item;
139 void *owner;
140
141 void (*revisit)(struct cache_deferred_req *req,
142 int too_many);
143};
144
145
146
147
148
149
150static inline time_t seconds_since_boot(void)
151{
152 struct timespec boot;
153 getboottime(&boot);
154 return get_seconds() - boot.tv_sec;
155}
156
157static inline time_t convert_to_wallclock(time_t sinceboot)
158{
159 struct timespec boot;
160 getboottime(&boot);
161 return boot.tv_sec + sinceboot;
162}
163
164extern const struct file_operations cache_file_operations_pipefs;
165extern const struct file_operations content_file_operations_pipefs;
166extern const struct file_operations cache_flush_operations_pipefs;
167
168extern struct cache_head *
169sunrpc_cache_lookup_rcu(struct cache_detail *detail,
170 struct cache_head *key, int hash);
171extern struct cache_head *
172sunrpc_cache_update(struct cache_detail *detail,
173 struct cache_head *new, struct cache_head *old, int hash);
174
175extern int
176sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h);
177
178
179extern void cache_clean_deferred(void *owner);
180
181static inline struct cache_head *cache_get(struct cache_head *h)
182{
183 kref_get(&h->ref);
184 return h;
185}
186
187static inline struct cache_head *cache_get_rcu(struct cache_head *h)
188{
189 if (kref_get_unless_zero(&h->ref))
190 return h;
191 return NULL;
192}
193
194static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
195{
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);
200}
201
202static inline bool cache_is_expired(struct cache_detail *detail, struct cache_head *h)
203{
204 if (!test_bit(CACHE_VALID, &h->flags))
205 return false;
206
207 return (h->expiry_time < seconds_since_boot()) ||
208 (detail->flush_time >= h->last_refresh);
209}
210
211extern int cache_check(struct cache_detail *detail,
212 struct cache_head *h, struct cache_req *rqstp);
213extern void cache_flush(void);
214extern void cache_purge(struct cache_detail *detail);
215#define NEVER (0x7FFFFFFF)
216extern void __init cache_initialize(void);
217extern int cache_register_net(struct cache_detail *cd, struct net *net);
218extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
219
220extern struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net);
221extern void cache_destroy_net(struct cache_detail *cd, struct net *net);
222
223extern void sunrpc_init_cache_detail(struct cache_detail *cd);
224extern void sunrpc_destroy_cache_detail(struct cache_detail *cd);
225extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
226 umode_t, struct cache_detail *);
227extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
228extern void sunrpc_cache_unhash(struct cache_detail *, struct cache_head *);
229
230
231extern void *cache_seq_start_rcu(struct seq_file *file, loff_t *pos);
232extern void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos);
233extern void cache_seq_stop_rcu(struct seq_file *file, void *p);
234
235extern void qword_add(char **bpp, int *lp, char *str);
236extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
237extern int qword_get(char **bpp, char *dest, int bufsize);
238
239static inline int get_int(char **bpp, int *anint)
240{
241 char buf[50];
242 char *ep;
243 int rv;
244 int len = qword_get(bpp, buf, sizeof(buf));
245
246 if (len < 0)
247 return -EINVAL;
248 if (len == 0)
249 return -ENOENT;
250
251 rv = simple_strtol(buf, &ep, 0);
252 if (*ep)
253 return -EINVAL;
254
255 *anint = rv;
256 return 0;
257}
258
259static inline int get_uint(char **bpp, unsigned int *anint)
260{
261 char buf[50];
262 int len = qword_get(bpp, buf, sizeof(buf));
263
264 if (len < 0)
265 return -EINVAL;
266 if (len == 0)
267 return -ENOENT;
268
269 if (kstrtouint(buf, 0, anint))
270 return -EINVAL;
271
272 return 0;
273}
274
275static inline int get_time(char **bpp, time_t *time)
276{
277 char buf[50];
278 long long ll;
279 int len = qword_get(bpp, buf, sizeof(buf));
280
281 if (len < 0)
282 return -EINVAL;
283 if (len == 0)
284 return -ENOENT;
285
286 if (kstrtoll(buf, 0, &ll))
287 return -EINVAL;
288
289 *time = (time_t)ll;
290 return 0;
291}
292
293static inline time_t get_expiry(char **bpp)
294{
295 time_t rv;
296 struct timespec boot;
297
298 if (get_time(bpp, &rv))
299 return 0;
300 if (rv < 0)
301 return 0;
302 getboottime(&boot);
303 return rv - boot.tv_sec;
304}
305
306#endif
307