cache.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * include/linux/sunrpc/cache.h
  4. *
  5. * Generic code for various authentication-related caches
  6. * used by sunrpc clients and servers.
  7. *
  8. * Copyright (C) 2002 Neil Brown <[email protected]>
  9. */
  10. #ifndef _LINUX_SUNRPC_CACHE_H_
  11. #define _LINUX_SUNRPC_CACHE_H_
  12. #include <linux/kref.h>
  13. #include <linux/slab.h>
  14. #include <linux/atomic.h>
  15. #include <linux/kstrtox.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 existence
  35. * in the hash table.
  36. * We only expire entries when refcount is zero.
  37. * Existence 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 hlist_node cache_list;
  45. time64_t expiry_time; /* After time expiry_time, don't use
  46. * the data */
  47. time64_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. spinlock_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. void (*flush)(void);
  81. int (*match)(struct cache_head *orig, struct cache_head *new);
  82. void (*init)(struct cache_head *orig, struct cache_head *new);
  83. void (*update)(struct cache_head *orig, struct cache_head *new);
  84. /* fields below this comment are for internal use
  85. * and should not be touched by cache owners
  86. */
  87. time64_t flush_time; /* flush all cache items with
  88. * last_refresh at or earlier
  89. * than this. last_refresh
  90. * is never set at or earlier
  91. * than this.
  92. */
  93. struct list_head others;
  94. time64_t nextcheck;
  95. int entries;
  96. /* fields for communication over channel */
  97. struct list_head queue;
  98. atomic_t writers; /* how many time is /channel open */
  99. time64_t last_close; /* if no writers, when did last close */
  100. time64_t last_warn; /* when we last warned about no writers */
  101. union {
  102. struct proc_dir_entry *procfs;
  103. struct dentry *pipefs;
  104. };
  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. unsigned long 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. /*
  130. * timestamps kept in the cache are expressed in seconds
  131. * since boot. This is the best for measuring differences in
  132. * real time.
  133. * This reimplemnts ktime_get_boottime_seconds() in a slightly
  134. * faster but less accurate way. When we end up converting
  135. * back to wallclock (CLOCK_REALTIME), that error often
  136. * cancels out during the reverse operation.
  137. */
  138. static inline time64_t seconds_since_boot(void)
  139. {
  140. struct timespec64 boot;
  141. getboottime64(&boot);
  142. return ktime_get_real_seconds() - boot.tv_sec;
  143. }
  144. static inline time64_t convert_to_wallclock(time64_t sinceboot)
  145. {
  146. struct timespec64 boot;
  147. getboottime64(&boot);
  148. return boot.tv_sec + sinceboot;
  149. }
  150. extern const struct file_operations cache_file_operations_pipefs;
  151. extern const struct file_operations content_file_operations_pipefs;
  152. extern const struct file_operations cache_flush_operations_pipefs;
  153. extern struct cache_head *
  154. sunrpc_cache_lookup_rcu(struct cache_detail *detail,
  155. struct cache_head *key, int hash);
  156. extern struct cache_head *
  157. sunrpc_cache_update(struct cache_detail *detail,
  158. struct cache_head *new, struct cache_head *old, int hash);
  159. extern int
  160. sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h);
  161. extern int
  162. sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail,
  163. struct cache_head *h);
  164. extern void cache_clean_deferred(void *owner);
  165. static inline struct cache_head *cache_get(struct cache_head *h)
  166. {
  167. kref_get(&h->ref);
  168. return h;
  169. }
  170. static inline struct cache_head *cache_get_rcu(struct cache_head *h)
  171. {
  172. if (kref_get_unless_zero(&h->ref))
  173. return h;
  174. return NULL;
  175. }
  176. static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
  177. {
  178. if (kref_read(&h->ref) <= 2 &&
  179. h->expiry_time < cd->nextcheck)
  180. cd->nextcheck = h->expiry_time;
  181. kref_put(&h->ref, cd->cache_put);
  182. }
  183. static inline bool cache_is_expired(struct cache_detail *detail, struct cache_head *h)
  184. {
  185. if (h->expiry_time < seconds_since_boot())
  186. return true;
  187. if (!test_bit(CACHE_VALID, &h->flags))
  188. return false;
  189. return detail->flush_time >= h->last_refresh;
  190. }
  191. extern int cache_check(struct cache_detail *detail,
  192. struct cache_head *h, struct cache_req *rqstp);
  193. extern void cache_flush(void);
  194. extern void cache_purge(struct cache_detail *detail);
  195. #define NEVER (0x7FFFFFFF)
  196. extern void __init cache_initialize(void);
  197. extern int cache_register_net(struct cache_detail *cd, struct net *net);
  198. extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
  199. extern struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net);
  200. extern void cache_destroy_net(struct cache_detail *cd, struct net *net);
  201. extern void sunrpc_init_cache_detail(struct cache_detail *cd);
  202. extern void sunrpc_destroy_cache_detail(struct cache_detail *cd);
  203. extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
  204. umode_t, struct cache_detail *);
  205. extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
  206. extern void sunrpc_cache_unhash(struct cache_detail *, struct cache_head *);
  207. /* Must store cache_detail in seq_file->private if using next three functions */
  208. extern void *cache_seq_start_rcu(struct seq_file *file, loff_t *pos);
  209. extern void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos);
  210. extern void cache_seq_stop_rcu(struct seq_file *file, void *p);
  211. extern void qword_add(char **bpp, int *lp, char *str);
  212. extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
  213. extern int qword_get(char **bpp, char *dest, int bufsize);
  214. static inline int get_int(char **bpp, int *anint)
  215. {
  216. char buf[50];
  217. char *ep;
  218. int rv;
  219. int len = qword_get(bpp, buf, sizeof(buf));
  220. if (len < 0)
  221. return -EINVAL;
  222. if (len == 0)
  223. return -ENOENT;
  224. rv = simple_strtol(buf, &ep, 0);
  225. if (*ep)
  226. return -EINVAL;
  227. *anint = rv;
  228. return 0;
  229. }
  230. static inline int get_uint(char **bpp, unsigned int *anint)
  231. {
  232. char buf[50];
  233. int len = qword_get(bpp, buf, sizeof(buf));
  234. if (len < 0)
  235. return -EINVAL;
  236. if (len == 0)
  237. return -ENOENT;
  238. if (kstrtouint(buf, 0, anint))
  239. return -EINVAL;
  240. return 0;
  241. }
  242. static inline int get_time(char **bpp, time64_t *time)
  243. {
  244. char buf[50];
  245. long long ll;
  246. int len = qword_get(bpp, buf, sizeof(buf));
  247. if (len < 0)
  248. return -EINVAL;
  249. if (len == 0)
  250. return -ENOENT;
  251. if (kstrtoll(buf, 0, &ll))
  252. return -EINVAL;
  253. *time = ll;
  254. return 0;
  255. }
  256. static inline time64_t get_expiry(char **bpp)
  257. {
  258. time64_t rv;
  259. struct timespec64 boot;
  260. if (get_time(bpp, &rv))
  261. return 0;
  262. if (rv < 0)
  263. return 0;
  264. getboottime64(&boot);
  265. return rv - boot.tv_sec;
  266. }
  267. #endif /* _LINUX_SUNRPC_CACHE_H_ */