cache.h 8.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * include/linux/sunrpc/cache.h
 *
 * Generic code for various authentication-related caches
 * used by sunrpc clients and servers.
 *
 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
 *
 * Released under terms in GPL version 2.  See COPYING.
 *
 */

#ifndef _LINUX_SUNRPC_CACHE_H_
#define _LINUX_SUNRPC_CACHE_H_

A
Alexey Dobriyan 已提交
16
#include <linux/kref.h>
L
Linus Torvalds 已提交
17
#include <linux/slab.h>
A
Arun Sharma 已提交
18
#include <linux/atomic.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <linux/proc_fs.h>

/*
 * Each cache requires:
 *  - A 'struct cache_detail' which contains information specific to the cache
 *    for common code to use.
 *  - An item structure that must contain a "struct cache_head"
 *  - A lookup function defined using DefineCacheLookup
 *  - A 'put' function that can release a cache item. It will only
 *    be called after cache_put has succeed, so there are guarantee
 *    to be no references.
 *  - A function to calculate a hash of an item's key.
 *
 * as well as assorted code fragments (e.g. compare keys) and numbers
 * (e.g. hash size, goal_age, etc).
 *
 * Each cache must be registered so that it can be cleaned regularly.
 * When the cache is unregistered, it is flushed completely.
 *
L
Lucas De Marchi 已提交
38
 * Entries have a ref count and a 'hashed' flag which counts the existence
L
Linus Torvalds 已提交
39 40
 * in the hash table.
 * We only expire entries when refcount is zero.
L
Lucas De Marchi 已提交
41
 * Existence in the cache is counted  the refcount.
L
Linus Torvalds 已提交
42 43 44 45 46 47 48
 */

/* Every cache item has a common header that is used
 * for expiring and refreshing entries.
 * 
 */
struct cache_head {
49
	struct hlist_node	cache_list;
L
Linus Torvalds 已提交
50
	time_t		expiry_time;	/* After time time, don't use the data */
51 52 53 54
	time_t		last_refresh;   /* If CACHE_PENDING, this is when upcall was
					 * sent, else this is when update was
					 * received, though it is alway set to
					 * be *after* ->flush_time.
L
Linus Torvalds 已提交
55
					 */
56
	struct kref	ref;
L
Linus Torvalds 已提交
57 58 59 60 61
	unsigned long	flags;
};
#define	CACHE_VALID	0	/* Entry contains valid data */
#define	CACHE_NEGATIVE	1	/* Negative entry - there is no match for the key */
#define	CACHE_PENDING	2	/* An upcall has been sent but no reply received yet*/
62
#define	CACHE_CLEANED	3	/* Entry has been cleaned from cache */
L
Linus Torvalds 已提交
63 64 65 66

#define	CACHE_NEW_EXPIRY 120	/* keep new things pending confirmation for 120 seconds */

struct cache_detail {
67
	struct module *		owner;
L
Linus Torvalds 已提交
68
	int			hash_size;
69
	struct hlist_head *	hash_table;
L
Linus Torvalds 已提交
70 71 72
	rwlock_t		hash_lock;

	char			*name;
73
	void			(*cache_put)(struct kref *);
L
Linus Torvalds 已提交
74

75 76 77
	int			(*cache_upcall)(struct cache_detail *,
						struct cache_head *);

78 79 80 81
	void			(*cache_request)(struct cache_detail *cd,
						 struct cache_head *ch,
						 char **bpp, int *blen);

L
Linus Torvalds 已提交
82 83 84 85 86 87
	int			(*cache_parse)(struct cache_detail *,
					       char *buf, int len);

	int			(*cache_show)(struct seq_file *m,
					      struct cache_detail *cd,
					      struct cache_head *h);
88 89
	void			(*warn_no_listener)(struct cache_detail *cd,
					      int has_died);
L
Linus Torvalds 已提交
90

91 92 93 94 95
	struct cache_head *	(*alloc)(void);
	int			(*match)(struct cache_head *orig, struct cache_head *new);
	void			(*init)(struct cache_head *orig, struct cache_head *new);
	void			(*update)(struct cache_head *orig, struct cache_head *new);

L
Linus Torvalds 已提交
96 97 98
	/* fields below this comment are for internal use
	 * and should not be touched by cache owners
	 */
99 100 101 102 103 104
	time_t			flush_time;		/* flush all cache items with
							 * last_refresh at or earlier
							 * than this.  last_refresh
							 * is never set at or earlier
							 * than this.
							 */
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114
	struct list_head	others;
	time_t			nextcheck;
	int			entries;

	/* fields for communication over channel */
	struct list_head	queue;

	atomic_t		readers;		/* how many time is /chennel open */
	time_t			last_close;		/* if no readers, when did last close */
	time_t			last_warn;		/* when we last warned about no readers */
115 116

	union {
117 118 119
		struct proc_dir_entry	*procfs;
		struct dentry		*pipefs;
	};
120
	struct net		*net;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129
};


/* this must be embedded in any request structure that
 * identifies an object that will want a callback on
 * a cache fill
 */
struct cache_req {
	struct cache_deferred_req *(*defer)(struct cache_req *req);
130 131 132
	int thread_wait;  /* How long (jiffies) we can block the
			   * current thread to wait for updates.
			   */
L
Linus Torvalds 已提交
133 134 135 136 137
};
/* this must be embedded in a deferred_request that is being
 * delayed awaiting cache-fill
 */
struct cache_deferred_req {
138
	struct hlist_node	hash;	/* on hash chain */
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146
	struct list_head	recent; /* on fifo */
	struct cache_head	*item;  /* cache item we wait on */
	void			*owner; /* we might need to discard all defered requests
					 * owned by someone */
	void			(*revisit)(struct cache_deferred_req *req,
					   int too_many);
};

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/*
 * timestamps kept in the cache are expressed in seconds
 * since boot.  This is the best for measuring differences in
 * real time.
 */
static inline time_t seconds_since_boot(void)
{
	struct timespec boot;
	getboottime(&boot);
	return get_seconds() - boot.tv_sec;
}

static inline time_t convert_to_wallclock(time_t sinceboot)
{
	struct timespec boot;
	getboottime(&boot);
	return boot.tv_sec + sinceboot;
}
165

166 167 168 169
extern const struct file_operations cache_file_operations_pipefs;
extern const struct file_operations content_file_operations_pipefs;
extern const struct file_operations cache_flush_operations_pipefs;

170 171 172
extern struct cache_head *
sunrpc_cache_lookup_rcu(struct cache_detail *detail,
			struct cache_head *key, int hash);
173 174 175 176 177 178 179
extern struct cache_head *
sunrpc_cache_lookup(struct cache_detail *detail,
		    struct cache_head *key, int hash);
extern struct cache_head *
sunrpc_cache_update(struct cache_detail *detail,
		    struct cache_head *new, struct cache_head *old, int hash);

180
extern int
181
sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h);
182

L
Linus Torvalds 已提交
183 184 185 186 187

extern void cache_clean_deferred(void *owner);

static inline struct cache_head  *cache_get(struct cache_head *h)
{
188
	kref_get(&h->ref);
L
Linus Torvalds 已提交
189 190 191
	return h;
}

192 193 194 195 196 197
static inline struct cache_head  *cache_get_rcu(struct cache_head *h)
{
	if (kref_get_unless_zero(&h->ref))
		return h;
	return NULL;
}
L
Linus Torvalds 已提交
198

199
static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
L
Linus Torvalds 已提交
200
{
201
	if (kref_read(&h->ref) <= 2 &&
L
Linus Torvalds 已提交
202 203
	    h->expiry_time < cd->nextcheck)
		cd->nextcheck = h->expiry_time;
204
	kref_put(&h->ref, cd->cache_put);
L
Linus Torvalds 已提交
205 206
}

207
static inline bool cache_is_expired(struct cache_detail *detail, struct cache_head *h)
208
{
209 210 211
	if (!test_bit(CACHE_VALID, &h->flags))
		return false;

212
	return  (h->expiry_time < seconds_since_boot()) ||
213
		(detail->flush_time >= h->last_refresh);
214 215
}

L
Linus Torvalds 已提交
216 217 218 219 220
extern int cache_check(struct cache_detail *detail,
		       struct cache_head *h, struct cache_req *rqstp);
extern void cache_flush(void);
extern void cache_purge(struct cache_detail *detail);
#define NEVER (0x7FFFFFFF)
221
extern void __init cache_initialize(void);
222 223
extern int cache_register_net(struct cache_detail *cd, struct net *net);
extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
L
Linus Torvalds 已提交
224

225
extern struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net);
226 227
extern void cache_destroy_net(struct cache_detail *cd, struct net *net);

228 229
extern void sunrpc_init_cache_detail(struct cache_detail *cd);
extern void sunrpc_destroy_cache_detail(struct cache_detail *cd);
230
extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
A
Al Viro 已提交
231
					umode_t, struct cache_detail *);
232
extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
233
extern void sunrpc_cache_unhash(struct cache_detail *, struct cache_head *);
234

235 236 237 238
/* Must store cache_detail in seq_file->private if using next three functions */
extern void *cache_seq_start(struct seq_file *file, loff_t *pos);
extern void *cache_seq_next(struct seq_file *file, void *p, loff_t *pos);
extern void cache_seq_stop(struct seq_file *file, void *p);
239 240 241
extern void *cache_seq_start_rcu(struct seq_file *file, loff_t *pos);
extern void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos);
extern void cache_seq_stop_rcu(struct seq_file *file, void *p);
242

L
Linus Torvalds 已提交
243 244 245 246 247 248 249
extern void qword_add(char **bpp, int *lp, char *str);
extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
extern int qword_get(char **bpp, char *dest, int bufsize);

static inline int get_int(char **bpp, int *anint)
{
	char buf[50];
250 251
	char *ep;
	int rv;
E
Eldad Zack 已提交
252 253 254 255 256 257 258
	int len = qword_get(bpp, buf, sizeof(buf));

	if (len < 0)
		return -EINVAL;
	if (len == 0)
		return -ENOENT;

259 260
	rv = simple_strtol(buf, &ep, 0);
	if (*ep)
E
Eldad Zack 已提交
261 262
		return -EINVAL;

263
	*anint = rv;
L
Linus Torvalds 已提交
264 265 266
	return 0;
}

J
J. Bruce Fields 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static inline int get_uint(char **bpp, unsigned int *anint)
{
	char buf[50];
	int len = qword_get(bpp, buf, sizeof(buf));

	if (len < 0)
		return -EINVAL;
	if (len == 0)
		return -ENOENT;

	if (kstrtouint(buf, 0, anint))
		return -EINVAL;

	return 0;
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static inline int get_time(char **bpp, time_t *time)
{
	char buf[50];
	long long ll;
	int len = qword_get(bpp, buf, sizeof(buf));

	if (len < 0)
		return -EINVAL;
	if (len == 0)
		return -ENOENT;

	if (kstrtoll(buf, 0, &ll))
		return -EINVAL;

	*time = (time_t)ll;
	return 0;
}

L
Linus Torvalds 已提交
301 302
static inline time_t get_expiry(char **bpp)
{
303
	time_t rv;
304 305
	struct timespec boot;

306
	if (get_time(bpp, &rv))
L
Linus Torvalds 已提交
307 308 309
		return 0;
	if (rv < 0)
		return 0;
310 311
	getboottime(&boot);
	return rv - boot.tv_sec;
L
Linus Torvalds 已提交
312 313 314
}

#endif /*  _LINUX_SUNRPC_CACHE_H_ */