cache.h 10 KB

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