cache.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 hlist_node cache_list;
  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 was
  48. * sent, else this is when update was
  49. * received, though it is alway set to
  50. * be *after* ->flush_time.
  51. */
  52. struct kref ref;
  53. unsigned long flags;
  54. };
  55. #define CACHE_VALID 0 /* Entry contains valid data */
  56. #define CACHE_NEGATIVE 1 /* Negative entry - there is no match for the key */
  57. #define CACHE_PENDING 2 /* An upcall has been sent but no reply received yet*/
  58. #define CACHE_CLEANED 3 /* Entry has been cleaned from cache */
  59. #define CACHE_NEW_EXPIRY 120 /* keep new things pending confirmation for 120 seconds */
  60. struct cache_detail {
  61. struct module * owner;
  62. int hash_size;
  63. struct hlist_head * hash_table;
  64. rwlock_t hash_lock;
  65. char *name;
  66. void (*cache_put)(struct kref *);
  67. int (*cache_upcall)(struct cache_detail *,
  68. struct cache_head *);
  69. void (*cache_request)(struct cache_detail *cd,
  70. struct cache_head *ch,
  71. char **bpp, int *blen);
  72. int (*cache_parse)(struct cache_detail *,
  73. char *buf, int len);
  74. int (*cache_show)(struct seq_file *m,
  75. struct cache_detail *cd,
  76. struct cache_head *h);
  77. void (*warn_no_listener)(struct cache_detail *cd,
  78. int has_died);
  79. struct cache_head * (*alloc)(void);
  80. int (*match)(struct cache_head *orig, struct cache_head *new);
  81. void (*init)(struct cache_head *orig, struct cache_head *new);
  82. void (*update)(struct cache_head *orig, struct cache_head *new);
  83. /* fields below this comment are for internal use
  84. * and should not be touched by cache owners
  85. */
  86. time_t flush_time; /* flush all cache items with
  87. * last_refresh at or earlier
  88. * than this. last_refresh
  89. * is never set at or earlier
  90. * than this.
  91. */
  92. struct list_head others;
  93. time_t nextcheck;
  94. int entries;
  95. /* fields for communication over channel */
  96. struct list_head queue;
  97. atomic_t readers; /* how many time is /chennel open */
  98. time_t last_close; /* if no readers, when did last close */
  99. time_t last_warn; /* when we last warned about no readers */
  100. union {
  101. struct proc_dir_entry *procfs;
  102. struct dentry *pipefs;
  103. };
  104. struct net *net;
  105. };
  106. /* this must be embedded in any request structure that
  107. * identifies an object that will want a callback on
  108. * a cache fill
  109. */
  110. struct cache_req {
  111. struct cache_deferred_req *(*defer)(struct cache_req *req);
  112. int thread_wait; /* How long (jiffies) we can block the
  113. * current thread to wait for updates.
  114. */
  115. };
  116. /* this must be embedded in a deferred_request that is being
  117. * delayed awaiting cache-fill
  118. */
  119. struct cache_deferred_req {
  120. struct hlist_node hash; /* on hash chain */
  121. struct list_head recent; /* on fifo */
  122. struct cache_head *item; /* cache item we wait on */
  123. void *owner; /* we might need to discard all defered requests
  124. * owned by someone */
  125. void (*revisit)(struct cache_deferred_req *req,
  126. int too_many);
  127. };
  128. /*
  129. * timestamps kept in the cache are expressed in seconds
  130. * since boot. This is the best for measuring differences in
  131. * real time.
  132. */
  133. static inline time_t seconds_since_boot(void)
  134. {
  135. struct timespec boot;
  136. getboottime(&boot);
  137. return get_seconds() - boot.tv_sec;
  138. }
  139. static inline time_t convert_to_wallclock(time_t sinceboot)
  140. {
  141. struct timespec boot;
  142. getboottime(&boot);
  143. return boot.tv_sec + sinceboot;
  144. }
  145. extern const struct file_operations cache_file_operations_pipefs;
  146. extern const struct file_operations content_file_operations_pipefs;
  147. extern const struct file_operations cache_flush_operations_pipefs;
  148. extern struct cache_head *
  149. sunrpc_cache_lookup(struct cache_detail *detail,
  150. struct cache_head *key, int hash);
  151. extern struct cache_head *
  152. sunrpc_cache_update(struct cache_detail *detail,
  153. struct cache_head *new, struct cache_head *old, int hash);
  154. extern int
  155. sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h);
  156. extern void cache_clean_deferred(void *owner);
  157. static inline struct cache_head *cache_get(struct cache_head *h)
  158. {
  159. kref_get(&h->ref);
  160. return h;
  161. }
  162. static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
  163. {
  164. if (kref_read(&h->ref) <= 2 &&
  165. h->expiry_time < cd->nextcheck)
  166. cd->nextcheck = h->expiry_time;
  167. kref_put(&h->ref, cd->cache_put);
  168. }
  169. static inline bool cache_is_expired(struct cache_detail *detail, struct cache_head *h)
  170. {
  171. if (!test_bit(CACHE_VALID, &h->flags))
  172. return false;
  173. return (h->expiry_time < seconds_since_boot()) ||
  174. (detail->flush_time >= h->last_refresh);
  175. }
  176. extern int cache_check(struct cache_detail *detail,
  177. struct cache_head *h, struct cache_req *rqstp);
  178. extern void cache_flush(void);
  179. extern void cache_purge(struct cache_detail *detail);
  180. #define NEVER (0x7FFFFFFF)
  181. extern void __init cache_initialize(void);
  182. extern int cache_register_net(struct cache_detail *cd, struct net *net);
  183. extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
  184. extern struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net);
  185. extern void cache_destroy_net(struct cache_detail *cd, struct net *net);
  186. extern void sunrpc_init_cache_detail(struct cache_detail *cd);
  187. extern void sunrpc_destroy_cache_detail(struct cache_detail *cd);
  188. extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
  189. umode_t, struct cache_detail *);
  190. extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
  191. extern void sunrpc_cache_unhash(struct cache_detail *, struct cache_head *);
  192. /* Must store cache_detail in seq_file->private if using next three functions */
  193. extern void *cache_seq_start(struct seq_file *file, loff_t *pos);
  194. extern void *cache_seq_next(struct seq_file *file, void *p, loff_t *pos);
  195. extern void cache_seq_stop(struct seq_file *file, void *p);
  196. extern void qword_add(char **bpp, int *lp, char *str);
  197. extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
  198. extern int qword_get(char **bpp, char *dest, int bufsize);
  199. static inline int get_int(char **bpp, int *anint)
  200. {
  201. char buf[50];
  202. char *ep;
  203. int rv;
  204. int len = qword_get(bpp, buf, sizeof(buf));
  205. if (len < 0)
  206. return -EINVAL;
  207. if (len == 0)
  208. return -ENOENT;
  209. rv = simple_strtol(buf, &ep, 0);
  210. if (*ep)
  211. return -EINVAL;
  212. *anint = rv;
  213. return 0;
  214. }
  215. static inline int get_uint(char **bpp, unsigned int *anint)
  216. {
  217. char buf[50];
  218. int len = qword_get(bpp, buf, sizeof(buf));
  219. if (len < 0)
  220. return -EINVAL;
  221. if (len == 0)
  222. return -ENOENT;
  223. if (kstrtouint(buf, 0, anint))
  224. return -EINVAL;
  225. return 0;
  226. }
  227. static inline int get_time(char **bpp, time_t *time)
  228. {
  229. char buf[50];
  230. long long ll;
  231. int len = qword_get(bpp, buf, sizeof(buf));
  232. if (len < 0)
  233. return -EINVAL;
  234. if (len == 0)
  235. return -ENOENT;
  236. if (kstrtoll(buf, 0, &ll))
  237. return -EINVAL;
  238. *time = (time_t)ll;
  239. return 0;
  240. }
  241. static inline time_t get_expiry(char **bpp)
  242. {
  243. time_t rv;
  244. struct timespec boot;
  245. if (get_time(bpp, &rv))
  246. return 0;
  247. if (rv < 0)
  248. return 0;
  249. getboottime(&boot);
  250. return rv - boot.tv_sec;
  251. }
  252. #endif /* _LINUX_SUNRPC_CACHE_H_ */