Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/slab.h> |
| 17 | #include <asm/atomic.h> |
| 18 | #include <linux/proc_fs.h> |
| 19 | |
| 20 | /* |
| 21 | * Each cache requires: |
| 22 | * - A 'struct cache_detail' which contains information specific to the cache |
| 23 | * for common code to use. |
| 24 | * - An item structure that must contain a "struct cache_head" |
| 25 | * - A lookup function defined using DefineCacheLookup |
| 26 | * - A 'put' function that can release a cache item. It will only |
| 27 | * be called after cache_put has succeed, so there are guarantee |
| 28 | * to be no references. |
| 29 | * - A function to calculate a hash of an item's key. |
| 30 | * |
| 31 | * as well as assorted code fragments (e.g. compare keys) and numbers |
| 32 | * (e.g. hash size, goal_age, etc). |
| 33 | * |
| 34 | * Each cache must be registered so that it can be cleaned regularly. |
| 35 | * When the cache is unregistered, it is flushed completely. |
| 36 | * |
| 37 | * Entries have a ref count and a 'hashed' flag which counts the existance |
| 38 | * in the hash table. |
| 39 | * We only expire entries when refcount is zero. |
| 40 | * Existance in the cache is counted the refcount. |
| 41 | */ |
| 42 | |
| 43 | /* Every cache item has a common header that is used |
| 44 | * for expiring and refreshing entries. |
| 45 | * |
| 46 | */ |
| 47 | struct cache_head { |
| 48 | struct cache_head * next; |
| 49 | time_t expiry_time; /* After time time, don't use the data */ |
| 50 | time_t last_refresh; /* If CACHE_PENDING, this is when upcall |
| 51 | * was sent, else this is when update was received |
| 52 | */ |
| 53 | atomic_t refcnt; |
| 54 | unsigned long flags; |
| 55 | }; |
| 56 | #define CACHE_VALID 0 /* Entry contains valid data */ |
| 57 | #define CACHE_NEGATIVE 1 /* Negative entry - there is no match for the key */ |
| 58 | #define CACHE_PENDING 2 /* An upcall has been sent but no reply received yet*/ |
| 59 | |
| 60 | #define CACHE_NEW_EXPIRY 120 /* keep new things pending confirmation for 120 seconds */ |
| 61 | |
| 62 | struct cache_detail { |
Bruce Allan | f35279d | 2005-09-06 15:17:08 -0700 | [diff] [blame] | 63 | struct module * owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | int hash_size; |
| 65 | struct cache_head ** hash_table; |
| 66 | rwlock_t hash_lock; |
| 67 | |
| 68 | atomic_t inuse; /* active user-space update or lookup */ |
| 69 | |
| 70 | char *name; |
| 71 | void (*cache_put)(struct cache_head *, |
| 72 | struct cache_detail*); |
| 73 | |
| 74 | void (*cache_request)(struct cache_detail *cd, |
| 75 | struct cache_head *h, |
| 76 | char **bpp, int *blen); |
| 77 | int (*cache_parse)(struct cache_detail *, |
| 78 | char *buf, int len); |
| 79 | |
| 80 | int (*cache_show)(struct seq_file *m, |
| 81 | struct cache_detail *cd, |
| 82 | struct cache_head *h); |
| 83 | |
| 84 | /* fields below this comment are for internal use |
| 85 | * and should not be touched by cache owners |
| 86 | */ |
| 87 | time_t flush_time; /* flush all cache items with last_refresh |
| 88 | * earlier than this */ |
| 89 | struct list_head others; |
| 90 | time_t nextcheck; |
| 91 | int entries; |
| 92 | |
| 93 | /* fields for communication over channel */ |
| 94 | struct list_head queue; |
| 95 | struct proc_dir_entry *proc_ent; |
| 96 | struct proc_dir_entry *flush_ent, *channel_ent, *content_ent; |
| 97 | |
| 98 | atomic_t readers; /* how many time is /chennel open */ |
| 99 | time_t last_close; /* if no readers, when did last close */ |
| 100 | time_t last_warn; /* when we last warned about no readers */ |
| 101 | void (*warn_no_listener)(struct cache_detail *cd); |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | /* this must be embedded in any request structure that |
| 106 | * identifies an object that will want a callback on |
| 107 | * a cache fill |
| 108 | */ |
| 109 | struct cache_req { |
| 110 | struct cache_deferred_req *(*defer)(struct cache_req *req); |
| 111 | }; |
| 112 | /* this must be embedded in a deferred_request that is being |
| 113 | * delayed awaiting cache-fill |
| 114 | */ |
| 115 | struct cache_deferred_req { |
| 116 | struct list_head hash; /* on hash chain */ |
| 117 | struct list_head recent; /* on fifo */ |
| 118 | struct cache_head *item; /* cache item we wait on */ |
| 119 | time_t recv_time; |
| 120 | void *owner; /* we might need to discard all defered requests |
| 121 | * owned by someone */ |
| 122 | void (*revisit)(struct cache_deferred_req *req, |
| 123 | int too_many); |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * just like a template in C++, this macro does cache lookup |
| 128 | * for us. |
| 129 | * The function is passed some sort of HANDLE from which a cache_detail |
| 130 | * structure can be determined (via SETUP, DETAIL), a template |
| 131 | * cache entry (type RTN*), and a "set" flag. Using the HASHFN and the |
| 132 | * TEST, the function will try to find a matching cache entry in the cache. |
| 133 | * If "set" == 0 : |
| 134 | * If an entry is found, it is returned |
| 135 | * If no entry is found, a new non-VALID entry is created. |
NeilBrown | 7d317f2 | 2006-03-27 01:15:01 -0800 | [diff] [blame^] | 136 | * If "set" == 1 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * If no entry is found a new one is inserted with data from "template" |
| 138 | * If a non-CACHE_VALID entry is found, it is updated from template using UPDATE |
| 139 | * If a CACHE_VALID entry is found, a new entry is swapped in with data |
| 140 | * from "template" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * |
| 142 | * If the passed handle has the CACHE_NEGATIVE flag set, then UPDATE is not |
| 143 | * run but insteead CACHE_NEGATIVE is set in any new item. |
| 144 | |
| 145 | * In any case, the new entry is returned with a reference count. |
| 146 | * |
| 147 | * |
| 148 | * RTN is a struct type for a cache entry |
| 149 | * MEMBER is the member of the cache which is cache_head, which must be first |
| 150 | * FNAME is the name for the function |
| 151 | * ARGS are arguments to function and must contain RTN *item, int set. May |
| 152 | * also contain something to be usedby SETUP or DETAIL to find cache_detail. |
| 153 | * SETUP locates the cache detail and makes it available as... |
| 154 | * DETAIL identifies the cache detail, possibly set up by SETUP |
| 155 | * HASHFN returns a hash value of the cache entry "item" |
| 156 | * TEST tests if "tmp" matches "item" |
| 157 | * INIT copies key information from "item" to "new" |
| 158 | * UPDATE copies content information from "item" to "tmp" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | */ |
NeilBrown | 7d317f2 | 2006-03-27 01:15:01 -0800 | [diff] [blame^] | 160 | #define DefineCacheLookup(RTN,MEMBER,FNAME,ARGS,SETUP,DETAIL,HASHFN,TEST,INIT,UPDATE) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | RTN *FNAME ARGS \ |
| 162 | { \ |
| 163 | RTN *tmp, *new=NULL; \ |
| 164 | struct cache_head **hp, **head; \ |
| 165 | SETUP; \ |
| 166 | head = &(DETAIL)->hash_table[HASHFN]; \ |
| 167 | retry: \ |
| 168 | if (set||new) write_lock(&(DETAIL)->hash_lock); \ |
| 169 | else read_lock(&(DETAIL)->hash_lock); \ |
| 170 | for(hp=head; *hp != NULL; hp = &tmp->MEMBER.next) { \ |
| 171 | tmp = container_of(*hp, RTN, MEMBER); \ |
| 172 | if (TEST) { /* found a match */ \ |
| 173 | \ |
NeilBrown | 7d317f2 | 2006-03-27 01:15:01 -0800 | [diff] [blame^] | 174 | if (set && test_bit(CACHE_VALID, &tmp->MEMBER.flags) && !new) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | break; \ |
| 176 | \ |
| 177 | if (new) \ |
| 178 | {INIT;} \ |
| 179 | if (set) { \ |
NeilBrown | 7d317f2 | 2006-03-27 01:15:01 -0800 | [diff] [blame^] | 180 | if (test_bit(CACHE_VALID, &tmp->MEMBER.flags))\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { /* need to swap in new */ \ |
| 182 | RTN *t2; \ |
| 183 | \ |
| 184 | new->MEMBER.next = tmp->MEMBER.next; \ |
| 185 | *hp = &new->MEMBER; \ |
| 186 | tmp->MEMBER.next = NULL; \ |
| 187 | t2 = tmp; tmp = new; new = t2; \ |
| 188 | } \ |
| 189 | if (test_bit(CACHE_NEGATIVE, &item->MEMBER.flags)) \ |
| 190 | set_bit(CACHE_NEGATIVE, &tmp->MEMBER.flags); \ |
| 191 | else { \ |
| 192 | UPDATE; \ |
| 193 | clear_bit(CACHE_NEGATIVE, &tmp->MEMBER.flags); \ |
| 194 | } \ |
| 195 | } \ |
| 196 | cache_get(&tmp->MEMBER); \ |
| 197 | if (set||new) write_unlock(&(DETAIL)->hash_lock); \ |
| 198 | else read_unlock(&(DETAIL)->hash_lock); \ |
| 199 | if (set) \ |
| 200 | cache_fresh(DETAIL, &tmp->MEMBER, item->MEMBER.expiry_time); \ |
NeilBrown | 7d317f2 | 2006-03-27 01:15:01 -0800 | [diff] [blame^] | 201 | if (set && new) cache_fresh(DETAIL, &new->MEMBER, 0); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (new) (DETAIL)->cache_put(&new->MEMBER, DETAIL); \ |
| 203 | return tmp; \ |
| 204 | } \ |
| 205 | } \ |
| 206 | /* Didn't find anything */ \ |
| 207 | if (new) { \ |
| 208 | INIT; \ |
| 209 | new->MEMBER.next = *head; \ |
| 210 | *head = &new->MEMBER; \ |
| 211 | (DETAIL)->entries ++; \ |
| 212 | cache_get(&new->MEMBER); \ |
| 213 | if (set) { \ |
| 214 | tmp = new; \ |
| 215 | if (test_bit(CACHE_NEGATIVE, &item->MEMBER.flags)) \ |
| 216 | set_bit(CACHE_NEGATIVE, &tmp->MEMBER.flags); \ |
| 217 | else {UPDATE;} \ |
| 218 | } \ |
| 219 | } \ |
| 220 | if (set||new) write_unlock(&(DETAIL)->hash_lock); \ |
| 221 | else read_unlock(&(DETAIL)->hash_lock); \ |
| 222 | if (new && set) \ |
| 223 | cache_fresh(DETAIL, &new->MEMBER, item->MEMBER.expiry_time); \ |
| 224 | if (new) \ |
| 225 | return new; \ |
| 226 | new = kmalloc(sizeof(*new), GFP_KERNEL); \ |
| 227 | if (new) { \ |
| 228 | cache_init(&new->MEMBER); \ |
| 229 | goto retry; \ |
| 230 | } \ |
| 231 | return NULL; \ |
| 232 | } |
| 233 | |
NeilBrown | 7d317f2 | 2006-03-27 01:15:01 -0800 | [diff] [blame^] | 234 | #define DefineSimpleCacheLookup(STRUCT, FUNC) \ |
| 235 | DefineCacheLookup(struct STRUCT, h, FUNC##_lookup, \ |
| 236 | (struct STRUCT *item, int set), /*no setup */, \ |
| 237 | & FUNC##_cache, FUNC##_hash(item), FUNC##_match(item, tmp), \ |
| 238 | STRUCT##_init(new, item), STRUCT##_update(tmp, item)) |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | #define cache_for_each(pos, detail, index, member) \ |
| 242 | for (({read_lock(&(detail)->hash_lock); index = (detail)->hash_size;}) ; \ |
| 243 | ({if (index==0)read_unlock(&(detail)->hash_lock); index--;}); \ |
| 244 | ) \ |
| 245 | for (pos = container_of((detail)->hash_table[index], typeof(*pos), member); \ |
| 246 | &pos->member; \ |
| 247 | pos = container_of(pos->member.next, typeof(*pos), member)) |
| 248 | |
| 249 | |
| 250 | |
| 251 | extern void cache_clean_deferred(void *owner); |
| 252 | |
| 253 | static inline struct cache_head *cache_get(struct cache_head *h) |
| 254 | { |
| 255 | atomic_inc(&h->refcnt); |
| 256 | return h; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static inline int cache_put(struct cache_head *h, struct cache_detail *cd) |
| 261 | { |
| 262 | if (atomic_read(&h->refcnt) <= 2 && |
| 263 | h->expiry_time < cd->nextcheck) |
| 264 | cd->nextcheck = h->expiry_time; |
| 265 | return atomic_dec_and_test(&h->refcnt); |
| 266 | } |
| 267 | |
| 268 | extern void cache_init(struct cache_head *h); |
| 269 | extern void cache_fresh(struct cache_detail *detail, |
| 270 | struct cache_head *head, time_t expiry); |
| 271 | extern int cache_check(struct cache_detail *detail, |
| 272 | struct cache_head *h, struct cache_req *rqstp); |
| 273 | extern void cache_flush(void); |
| 274 | extern void cache_purge(struct cache_detail *detail); |
| 275 | #define NEVER (0x7FFFFFFF) |
| 276 | extern void cache_register(struct cache_detail *cd); |
| 277 | extern int cache_unregister(struct cache_detail *cd); |
| 278 | |
| 279 | extern void qword_add(char **bpp, int *lp, char *str); |
| 280 | extern void qword_addhex(char **bpp, int *lp, char *buf, int blen); |
| 281 | extern int qword_get(char **bpp, char *dest, int bufsize); |
| 282 | |
| 283 | static inline int get_int(char **bpp, int *anint) |
| 284 | { |
| 285 | char buf[50]; |
| 286 | char *ep; |
| 287 | int rv; |
| 288 | int len = qword_get(bpp, buf, 50); |
| 289 | if (len < 0) return -EINVAL; |
| 290 | if (len ==0) return -ENOENT; |
| 291 | rv = simple_strtol(buf, &ep, 0); |
| 292 | if (*ep) return -EINVAL; |
| 293 | *anint = rv; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static inline time_t get_expiry(char **bpp) |
| 298 | { |
| 299 | int rv; |
| 300 | if (get_int(bpp, &rv)) |
| 301 | return 0; |
| 302 | if (rv < 0) |
| 303 | return 0; |
| 304 | return rv; |
| 305 | } |
| 306 | |
| 307 | #endif /* _LINUX_SUNRPC_CACHE_H_ */ |