cache.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/kref.h>
  15. #include <linux/slab.h>
  16. #include <asm/atomic.h>
  17. #include <linux/proc_fs.h>
  18. /*
  19. * Each cache requires:
  20. * - A 'struct cache_detail' which contains information specific to the cache
  21. * for common code to use.
  22. * - An item structure that must contain a "struct cache_head"
  23. * - A lookup function defined using DefineCacheLookup
  24. * - A 'put' function that can release a cache item. It will only
  25. * be called after cache_put has succeed, so there are guarantee
  26. * to be no references.
  27. * - A function to calculate a hash of an item's key.
  28. *
  29. * as well as assorted code fragments (e.g. compare keys) and numbers
  30. * (e.g. hash size, goal_age, etc).
  31. *
  32. * Each cache must be registered so that it can be cleaned regularly.
  33. * When the cache is unregistered, it is flushed completely.
  34. *
  35. * Entries have a ref count and a 'hashed' flag which counts the existence
  36. * in the hash table.
  37. * We only expire entries when refcount is zero.
  38. * Existence in the cache is counted the refcount.
  39. */
  40. /* Every cache item has a common header that is used
  41. * for expiring and refreshing entries.
  42. *
  43. */
  44. struct cache_head {
  45. struct cache_head * next;
  46. time_t expiry_time; /* After time time, don't use the data */
  47. time_t last_refresh; /* If CACHE_PENDING, this is when upcall
  48. * was sent, else this is when update was received
  49. */
  50. struct kref ref;
  51. unsigned long flags;
  52. };
  53. #define CACHE_VALID 0 /* Entry contains valid data */
  54. #define CACHE_NEGATIVE 1 /* Negative entry - there is no match for the key */
  55. #define CACHE_PENDING 2 /* An upcall has been sent but no reply received yet*/
  56. #define CACHE_NEW_EXPIRY 120 /* keep new things pending confirmation for 120 seconds */
  57. struct cache_detail_procfs {
  58. struct proc_dir_entry *proc_ent;
  59. struct proc_dir_entry *flush_ent, *channel_ent, *content_ent;
  60. };
  61. struct cache_detail_pipefs {
  62. struct dentry *dir;
  63. };
  64. struct cache_detail {
  65. struct module * owner;
  66. int hash_size;
  67. struct cache_head ** hash_table;
  68. rwlock_t hash_lock;
  69. atomic_t inuse; /* active user-space update or lookup */
  70. char *name;
  71. void (*cache_put)(struct kref *);
  72. int (*cache_upcall)(struct cache_detail *,
  73. struct cache_head *);
  74. int (*cache_parse)(struct cache_detail *,
  75. char *buf, int len);
  76. int (*cache_show)(struct seq_file *m,
  77. struct cache_detail *cd,
  78. struct cache_head *h);
  79. void (*warn_no_listener)(struct cache_detail *cd,
  80. int has_died);
  81. struct cache_head * (*alloc)(void);
  82. int (*match)(struct cache_head *orig, struct cache_head *new);
  83. void (*init)(struct cache_head *orig, struct cache_head *new);
  84. void (*update)(struct cache_head *orig, struct cache_head *new);
  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. /* fields for communication over channel */
  94. struct list_head queue;
  95. atomic_t readers; /* how many time is /chennel open */
  96. time_t last_close; /* if no readers, when did last close */
  97. time_t last_warn; /* when we last warned about no readers */
  98. union {
  99. struct cache_detail_procfs procfs;
  100. struct cache_detail_pipefs pipefs;
  101. } u;
  102. };
  103. /* this must be embedded in any request structure that
  104. * identifies an object that will want a callback on
  105. * a cache fill
  106. */
  107. struct cache_req {
  108. struct cache_deferred_req *(*defer)(struct cache_req *req);
  109. int thread_wait; /* How long (jiffies) we can block the
  110. * current thread to wait for updates.
  111. */
  112. };
  113. /* this must be embedded in a deferred_request that is being
  114. * delayed awaiting cache-fill
  115. */
  116. struct cache_deferred_req {
  117. struct hlist_node hash; /* on hash chain */
  118. struct list_head recent; /* on fifo */
  119. struct cache_head *item; /* cache item we wait on */
  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. extern const struct file_operations cache_file_operations_pipefs;
  126. extern const struct file_operations content_file_operations_pipefs;
  127. extern const struct file_operations cache_flush_operations_pipefs;
  128. extern struct cache_head *
  129. sunrpc_cache_lookup(struct cache_detail *detail,
  130. struct cache_head *key, int hash);
  131. extern struct cache_head *
  132. sunrpc_cache_update(struct cache_detail *detail,
  133. struct cache_head *new, struct cache_head *old, int hash);
  134. extern int
  135. sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
  136. void (*cache_request)(struct cache_detail *,
  137. struct cache_head *,
  138. char **,
  139. int *));
  140. extern void cache_clean_deferred(void *owner);
  141. static inline struct cache_head *cache_get(struct cache_head *h)
  142. {
  143. kref_get(&h->ref);
  144. return h;
  145. }
  146. static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
  147. {
  148. if (atomic_read(&h->ref.refcount) <= 2 &&
  149. h->expiry_time < cd->nextcheck)
  150. cd->nextcheck = h->expiry_time;
  151. kref_put(&h->ref, cd->cache_put);
  152. }
  153. static inline int cache_valid(struct cache_head *h)
  154. {
  155. /* If an item has been unhashed pending removal when
  156. * the refcount drops to 0, the expiry_time will be
  157. * set to 0. We don't want to consider such items
  158. * valid in this context even though CACHE_VALID is
  159. * set.
  160. */
  161. return (h->expiry_time != 0 && test_bit(CACHE_VALID, &h->flags));
  162. }
  163. extern int cache_check(struct cache_detail *detail,
  164. struct cache_head *h, struct cache_req *rqstp);
  165. extern void cache_flush(void);
  166. extern void cache_purge(struct cache_detail *detail);
  167. #define NEVER (0x7FFFFFFF)
  168. extern void __init cache_initialize(void);
  169. extern int cache_register(struct cache_detail *cd);
  170. extern int cache_register_net(struct cache_detail *cd, struct net *net);
  171. extern void cache_unregister(struct cache_detail *cd);
  172. extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
  173. extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
  174. mode_t, struct cache_detail *);
  175. extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
  176. extern void qword_add(char **bpp, int *lp, char *str);
  177. extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
  178. extern int qword_get(char **bpp, char *dest, int bufsize);
  179. static inline int get_int(char **bpp, int *anint)
  180. {
  181. char buf[50];
  182. char *ep;
  183. int rv;
  184. int len = qword_get(bpp, buf, 50);
  185. if (len < 0) return -EINVAL;
  186. if (len ==0) return -ENOENT;
  187. rv = simple_strtol(buf, &ep, 0);
  188. if (*ep) return -EINVAL;
  189. *anint = rv;
  190. return 0;
  191. }
  192. /*
  193. * timestamps kept in the cache are expressed in seconds
  194. * since boot. This is the best for measuring differences in
  195. * real time.
  196. */
  197. static inline time_t seconds_since_boot(void)
  198. {
  199. struct timespec boot;
  200. getboottime(&boot);
  201. return get_seconds() - boot.tv_sec;
  202. }
  203. static inline time_t convert_to_wallclock(time_t sinceboot)
  204. {
  205. struct timespec boot;
  206. getboottime(&boot);
  207. return boot.tv_sec + sinceboot;
  208. }
  209. static inline time_t get_expiry(char **bpp)
  210. {
  211. int rv;
  212. struct timespec boot;
  213. if (get_int(bpp, &rv))
  214. return 0;
  215. if (rv < 0)
  216. return 0;
  217. getboottime(&boot);
  218. return rv - boot.tv_sec;
  219. }
  220. #endif /* _LINUX_SUNRPC_CACHE_H_ */