cache.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <linux/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. void (*cache_request)(struct cache_detail *cd,
  75. struct cache_head *ch,
  76. char **bpp, int *blen);
  77. int (*cache_parse)(struct cache_detail *,
  78. char *buf, int len);
  79. int (*cache_show)(struct seq_file *m,
  80. struct cache_detail *cd,
  81. struct cache_head *h);
  82. void (*warn_no_listener)(struct cache_detail *cd,
  83. int has_died);
  84. struct cache_head * (*alloc)(void);
  85. int (*match)(struct cache_head *orig, struct cache_head *new);
  86. void (*init)(struct cache_head *orig, struct cache_head *new);
  87. void (*update)(struct cache_head *orig, struct cache_head *new);
  88. /* fields below this comment are for internal use
  89. * and should not be touched by cache owners
  90. */
  91. time_t flush_time; /* flush all cache items with last_refresh
  92. * earlier than this */
  93. struct list_head others;
  94. time_t nextcheck;
  95. int entries;
  96. /* fields for communication over channel */
  97. struct list_head queue;
  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. union {
  102. struct cache_detail_procfs procfs;
  103. struct cache_detail_pipefs pipefs;
  104. } u;
  105. struct net *net;
  106. };
  107. /* this must be embedded in any request structure that
  108. * identifies an object that will want a callback on
  109. * a cache fill
  110. */
  111. struct cache_req {
  112. struct cache_deferred_req *(*defer)(struct cache_req *req);
  113. int thread_wait; /* How long (jiffies) we can block the
  114. * current thread to wait for updates.
  115. */
  116. };
  117. /* this must be embedded in a deferred_request that is being
  118. * delayed awaiting cache-fill
  119. */
  120. struct cache_deferred_req {
  121. struct hlist_node hash; /* on hash chain */
  122. struct list_head recent; /* on fifo */
  123. struct cache_head *item; /* cache item we wait on */
  124. void *owner; /* we might need to discard all defered requests
  125. * owned by someone */
  126. void (*revisit)(struct cache_deferred_req *req,
  127. int too_many);
  128. };
  129. extern const struct file_operations cache_file_operations_pipefs;
  130. extern const struct file_operations content_file_operations_pipefs;
  131. extern const struct file_operations cache_flush_operations_pipefs;
  132. extern struct cache_head *
  133. sunrpc_cache_lookup(struct cache_detail *detail,
  134. struct cache_head *key, int hash);
  135. extern struct cache_head *
  136. sunrpc_cache_update(struct cache_detail *detail,
  137. struct cache_head *new, struct cache_head *old, int hash);
  138. extern int
  139. sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
  140. void (*cache_request)(struct cache_detail *,
  141. struct cache_head *,
  142. char **,
  143. int *));
  144. extern void cache_clean_deferred(void *owner);
  145. static inline struct cache_head *cache_get(struct cache_head *h)
  146. {
  147. kref_get(&h->ref);
  148. return h;
  149. }
  150. static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
  151. {
  152. if (atomic_read(&h->ref.refcount) <= 2 &&
  153. h->expiry_time < cd->nextcheck)
  154. cd->nextcheck = h->expiry_time;
  155. kref_put(&h->ref, cd->cache_put);
  156. }
  157. static inline int cache_valid(struct cache_head *h)
  158. {
  159. /* If an item has been unhashed pending removal when
  160. * the refcount drops to 0, the expiry_time will be
  161. * set to 0. We don't want to consider such items
  162. * valid in this context even though CACHE_VALID is
  163. * set.
  164. */
  165. return (h->expiry_time != 0 && test_bit(CACHE_VALID, &h->flags));
  166. }
  167. extern int cache_check(struct cache_detail *detail,
  168. struct cache_head *h, struct cache_req *rqstp);
  169. extern void cache_flush(void);
  170. extern void cache_purge(struct cache_detail *detail);
  171. #define NEVER (0x7FFFFFFF)
  172. extern void __init cache_initialize(void);
  173. extern int cache_register_net(struct cache_detail *cd, struct net *net);
  174. extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
  175. extern struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net);
  176. extern void cache_destroy_net(struct cache_detail *cd, struct net *net);
  177. extern void sunrpc_init_cache_detail(struct cache_detail *cd);
  178. extern void sunrpc_destroy_cache_detail(struct cache_detail *cd);
  179. extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
  180. umode_t, struct cache_detail *);
  181. extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
  182. extern void qword_add(char **bpp, int *lp, char *str);
  183. extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
  184. extern int qword_get(char **bpp, char *dest, int bufsize);
  185. static inline int get_int(char **bpp, int *anint)
  186. {
  187. char buf[50];
  188. char *ep;
  189. int rv;
  190. int len = qword_get(bpp, buf, sizeof(buf));
  191. if (len < 0)
  192. return -EINVAL;
  193. if (len == 0)
  194. return -ENOENT;
  195. rv = simple_strtol(buf, &ep, 0);
  196. if (*ep)
  197. return -EINVAL;
  198. *anint = rv;
  199. return 0;
  200. }
  201. static inline int get_uint(char **bpp, unsigned int *anint)
  202. {
  203. char buf[50];
  204. int len = qword_get(bpp, buf, sizeof(buf));
  205. if (len < 0)
  206. return -EINVAL;
  207. if (len == 0)
  208. return -ENOENT;
  209. if (kstrtouint(buf, 0, anint))
  210. return -EINVAL;
  211. return 0;
  212. }
  213. /*
  214. * timestamps kept in the cache are expressed in seconds
  215. * since boot. This is the best for measuring differences in
  216. * real time.
  217. */
  218. static inline time_t seconds_since_boot(void)
  219. {
  220. struct timespec boot;
  221. getboottime(&boot);
  222. return get_seconds() - boot.tv_sec;
  223. }
  224. static inline time_t convert_to_wallclock(time_t sinceboot)
  225. {
  226. struct timespec boot;
  227. getboottime(&boot);
  228. return boot.tv_sec + sinceboot;
  229. }
  230. static inline time_t get_expiry(char **bpp)
  231. {
  232. int rv;
  233. struct timespec boot;
  234. if (get_int(bpp, &rv))
  235. return 0;
  236. if (rv < 0)
  237. return 0;
  238. getboottime(&boot);
  239. return rv - boot.tv_sec;
  240. }
  241. #endif /* _LINUX_SUNRPC_CACHE_H_ */