Linux Audio

Check our new training course

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