ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / linux / sunrpc / cache.h
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 not measured in refcount but rather in
41  * CACHE_HASHED flag.
42  */
43
44 /* Every cache item has a common header that is used
45  * for expiring and refreshing entries.
46  * 
47  */
48 struct 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         atomic_t        refcnt;
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_HASHED    3       /* Entry is in a hash table */
61
62 #define CACHE_NEW_EXPIRY 120    /* keep new things pending confirmation for 120 seconds */
63
64 struct cache_detail {
65         int                     hash_size;
66         struct cache_head **    hash_table;
67         rwlock_t                hash_lock;
68
69         atomic_t                inuse; /* active user-space update or lookup */
70
71         char                    *name;
72         void                    (*cache_put)(struct cache_head *,
73                                              struct cache_detail*);
74
75         void                    (*cache_request)(struct cache_detail *cd,
76                                                  struct cache_head *h,
77                                                  char **bpp, int *blen);
78         int                     (*cache_parse)(struct cache_detail *,
79                                                char *buf, int len);
80
81         int                     (*cache_show)(struct seq_file *m,
82                                               struct cache_detail *cd,
83                                               struct cache_head *h);
84
85         /* fields below this comment are for internal use
86          * and should not be touched by cache owners
87          */
88         time_t                  flush_time;             /* flush all cache items with last_refresh
89                                                          * earlier than this */
90         struct list_head        others;
91         time_t                  nextcheck;
92         int                     entries;
93
94         /* fields for communication over channel */
95         struct list_head        queue;
96         struct proc_dir_entry   *proc_ent;
97         struct proc_dir_entry   *flush_ent, *channel_ent, *content_ent;
98
99         atomic_t                readers;                /* how many time is /chennel open */
100         time_t                  last_close;             /* it no readers, when did last close */
101 };
102
103
104 /* this must be embedded in any request structure that
105  * identifies an object that will want a callback on
106  * a cache fill
107  */
108 struct cache_req {
109         struct cache_deferred_req *(*defer)(struct cache_req *req);
110 };
111 /* this must be embedded in a deferred_request that is being
112  * delayed awaiting cache-fill
113  */
114 struct cache_deferred_req {
115         struct list_head        hash;   /* on hash chain */
116         struct list_head        recent; /* on fifo */
117         struct cache_head       *item;  /* cache item we wait on */
118         time_t                  recv_time;
119         void                    *owner; /* we might need to discard all defered requests
120                                          * owned by someone */
121         void                    (*revisit)(struct cache_deferred_req *req,
122                                            int too_many);
123 };
124
125 /*
126  * just like a template in C++, this macro does cache lookup
127  * for us.
128  * The function is passed some sort of HANDLE from which a cache_detail
129  * structure can be determined (via SETUP, DETAIL), a template
130  * cache entry (type RTN*), and a "set" flag.  Using the HASHFN and the 
131  * TEST, the function will try to find a matching cache entry in the cache.
132  * If "set" == 0 :
133  *    If an entry is found, it is returned
134  *    If no entry is found, a new non-VALID entry is created.
135  * If "set" == 1 and INPLACE == 0 :
136  *    If no entry is found a new one is inserted with data from "template"
137  *    If a non-CACHE_VALID entry is found, it is updated from template using UPDATE
138  *    If a CACHE_VALID entry is found, a new entry is swapped in with data
139  *       from "template"
140  * If set == 1, and INPLACE == 1 :
141  *    As above, except that if a CACHE_VALID entry is found, we UPDATE in place
142  *       instead of swapping in a new entry.
143  *
144  * If the passed handle has the CACHE_NEGATIVE flag set, then UPDATE is not
145  * run but insteead CACHE_NEGATIVE is set in any new item.
146
147  *  In any case, the new entry is returned with a reference count.
148  *
149  *    
150  * RTN is a struct type for a cache entry
151  * MEMBER is the member of the cache which is cache_head, which must be first
152  * FNAME is the name for the function   
153  * ARGS are arguments to function and must contain RTN *item, int set.  May
154  *   also contain something to be usedby SETUP or DETAIL to find cache_detail.
155  * SETUP  locates the cache detail and makes it available as...
156  * DETAIL identifies the cache detail, possibly set up by SETUP
157  * HASHFN returns a hash value of the cache entry "item"
158  * TEST  tests if "tmp" matches "item"
159  * INIT copies key information from "item" to "new"
160  * UPDATE copies content information from "item" to "tmp"
161  * INPLACE is true if updates can happen inplace rather than allocating a new structure
162  */
163 #define DefineCacheLookup(RTN,MEMBER,FNAME,ARGS,SETUP,DETAIL,HASHFN,TEST,INIT,UPDATE,INPLACE)   \
164 RTN *FNAME ARGS                                                                         \
165 {                                                                                       \
166         RTN *tmp, *new=NULL;                                                            \
167         struct cache_head **hp, **head;                                                 \
168         SETUP;                                                                          \
169         head = &(DETAIL)->hash_table[HASHFN];                                           \
170  retry:                                                                                 \
171         if (set||new) write_lock(&(DETAIL)->hash_lock);                                 \
172         else read_lock(&(DETAIL)->hash_lock);                                           \
173         for(hp=head; *hp != NULL; hp = &tmp->MEMBER.next) {                             \
174                 tmp = container_of(*hp, RTN, MEMBER);                                   \
175                 if (TEST) { /* found a match */                                         \
176                                                                                         \
177                         if (set && !INPLACE && test_bit(CACHE_VALID, &tmp->MEMBER.flags) && !new) \
178                                 break;                                                  \
179                                                                                         \
180                         if (new)                                                        \
181                                 {INIT;}                                                 \
182                         cache_get(&tmp->MEMBER);                                        \
183                         if (set) {                                                      \
184                                 if (!INPLACE && test_bit(CACHE_VALID, &tmp->MEMBER.flags))\
185                                 { /* need to swap in new */                             \
186                                         RTN *t2;                                        \
187                                                                                         \
188                                         new->MEMBER.next = tmp->MEMBER.next;            \
189                                         *hp = &new->MEMBER;                             \
190                                         tmp->MEMBER.next = NULL;                        \
191                                         set_bit(CACHE_HASHED, &new->MEMBER.flags);      \
192                                         clear_bit(CACHE_HASHED, &tmp->MEMBER.flags);    \
193                                         t2 = tmp; tmp = new; new = t2;                  \
194                                 }                                                       \
195                                 if (test_bit(CACHE_NEGATIVE,  &item->MEMBER.flags))     \
196                                          set_bit(CACHE_NEGATIVE, &tmp->MEMBER.flags);   \
197                                 else {UPDATE;}                                          \
198                         }                                                               \
199                         if (set||new) write_unlock(&(DETAIL)->hash_lock);               \
200                         else read_unlock(&(DETAIL)->hash_lock);                         \
201                         if (set)                                                        \
202                                 cache_fresh(DETAIL, &tmp->MEMBER, item->MEMBER.expiry_time); \
203                         if (set && !INPLACE && new) cache_fresh(DETAIL, &new->MEMBER, 0);       \
204                         if (new) (DETAIL)->cache_put(&new->MEMBER, DETAIL);             \
205                         return tmp;                                                     \
206                 }                                                                       \
207         }                                                                               \
208         /* Didn't find anything */                                                      \
209         if (new) {                                                                      \
210                 INIT;                                                                   \
211                 new->MEMBER.next = *head;                                               \
212                 *head = &new->MEMBER;                                                   \
213                 (DETAIL)->entries ++;                                                   \
214                 set_bit(CACHE_HASHED, &new->MEMBER.flags);                              \
215                 if (set) {                                                              \
216                         tmp = new;                                                      \
217                         if (test_bit(CACHE_NEGATIVE, &item->MEMBER.flags))              \
218                                 set_bit(CACHE_NEGATIVE, &tmp->MEMBER.flags);            \
219                         else {UPDATE;}                                                  \
220                 }                                                                       \
221         }                                                                               \
222         if (set||new) write_unlock(&(DETAIL)->hash_lock);                               \
223         else read_unlock(&(DETAIL)->hash_lock);                                         \
224         if (new && set)                                                                 \
225                 cache_fresh(DETAIL, &new->MEMBER, item->MEMBER.expiry_time);            \
226         if (new)                                                                        \
227                 return new;                                                             \
228         new = kmalloc(sizeof(*new), GFP_KERNEL);                                        \
229         if (new) {                                                                      \
230                 cache_init(&new->MEMBER);                                               \
231                 cache_get(&new->MEMBER);                                                \
232                 goto retry;                                                             \
233         }                                                                               \
234         return NULL;                                                                    \
235 }
236
237 #define DefineSimpleCacheLookup(STRUCT,INPLACE) \
238         DefineCacheLookup(struct STRUCT, h, STRUCT##_lookup, (struct STRUCT *item, int set), /*no setup */,     \
239                           & STRUCT##_cache, STRUCT##_hash(item), STRUCT##_match(item, tmp),\
240                           STRUCT##_init(new, item), STRUCT##_update(tmp, item),INPLACE)
241
242 #define cache_for_each(pos, detail, index, member)                                              \
243         for (({read_lock(&(detail)->hash_lock); index = (detail)->hash_size;}) ;                \
244              ({if (index==0)read_unlock(&(detail)->hash_lock); index--;});                      \
245                 )                                                                               \
246                 for (pos = container_of((detail)->hash_table[index], typeof(*pos), member);     \
247                      &pos->member;                                                              \
248                      pos = container_of(pos->member.next, typeof(*pos), member))
249
250              
251
252 extern void cache_defer_req(struct cache_req *req, struct cache_head *item);
253 extern void cache_revisit_request(struct cache_head *item);
254 extern void cache_clean_deferred(void *owner);
255
256 static inline struct cache_head  *cache_get(struct cache_head *h)
257 {
258         atomic_inc(&h->refcnt);
259         return h;
260 }
261
262
263 static inline int cache_put(struct cache_head *h, struct cache_detail *cd)
264 {
265         atomic_dec(&h->refcnt);
266         if (!atomic_read(&h->refcnt) &&
267             h->expiry_time < cd->nextcheck)
268                 cd->nextcheck = h->expiry_time;
269         if (!test_bit(CACHE_HASHED, &h->flags) &&
270             !atomic_read(&h->refcnt))
271                 return 1;
272
273         return 0;
274 }
275
276 extern void cache_init(struct cache_head *h);
277 extern void cache_fresh(struct cache_detail *detail,
278                         struct cache_head *head, time_t expiry);
279 extern int cache_check(struct cache_detail *detail,
280                        struct cache_head *h, struct cache_req *rqstp);
281 extern int cache_clean(void);
282 extern void cache_flush(void);
283 extern void cache_purge(struct cache_detail *detail);
284 #define NEVER (0x7FFFFFFF)
285 extern void cache_register(struct cache_detail *cd);
286 extern int cache_unregister(struct cache_detail *cd);
287 extern struct cache_detail *cache_find(char *name);
288 extern void cache_drop(struct cache_detail *detail);
289
290 extern void qword_add(char **bpp, int *lp, char *str);
291 extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
292 extern int qword_get(char **bpp, char *dest, int bufsize);
293
294 static inline int get_int(char **bpp, int *anint)
295 {
296         char buf[50];
297         char *ep;
298         int rv;
299         int len = qword_get(bpp, buf, 50);
300         if (len < 0) return -EINVAL;
301         if (len ==0) return -ENOENT;
302         rv = simple_strtol(buf, &ep, 0);
303         if (*ep) return -EINVAL;
304         *anint = rv;
305         return 0;
306 }
307
308 static inline time_t get_expiry(char **bpp)
309 {
310         int rv;
311         if (get_int(bpp, &rv))
312                 return 0;
313         if (rv < 0)
314                 return 0;
315         return rv;
316 }
317
318 #endif /*  _LINUX_SUNRPC_CACHE_H_ */