cache.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. struct kref ref;
  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_procfs {
  57. struct proc_dir_entry *proc_ent;
  58. struct proc_dir_entry *flush_ent, *channel_ent, *content_ent;
  59. };
  60. struct cache_detail_pipefs {
  61. struct dentry *dir;
  62. };
  63. struct cache_detail {
  64. struct module * owner;
  65. int hash_size;
  66. struct cache_head ** hash_table;
  67. rwlock_t hash_lock;
  68. atomic_t inuse; /* active user-space update or lookup */
  69. char *name;
  70. void (*cache_put)(struct kref *);
  71. int (*cache_upcall)(struct cache_detail *,
  72. struct cache_head *);
  73. int (*cache_parse)(struct cache_detail *,
  74. char *buf, int len);
  75. int (*cache_show)(struct seq_file *m,
  76. struct cache_detail *cd,
  77. struct cache_head *h);
  78. void (*warn_no_listener)(struct cache_detail *cd,
  79. int has_died);
  80. struct cache_head * (*alloc)(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. time_t flush_time; /* flush all cache items with last_refresh
  88. * earlier than this */
  89. struct list_head others;
  90. time_t nextcheck;
  91. int entries;
  92. /* fields for communication over channel */
  93. struct list_head queue;
  94. atomic_t readers; /* how many time is /chennel open */
  95. time_t last_close; /* if no readers, when did last close */
  96. time_t last_warn; /* when we last warned about no readers */
  97. union {
  98. struct cache_detail_procfs procfs;
  99. struct cache_detail_pipefs pipefs;
  100. } u;
  101. };
  102. /* this must be embedded in any request structure that
  103. * identifies an object that will want a callback on
  104. * a cache fill
  105. */
  106. struct cache_req {
  107. struct cache_deferred_req *(*defer)(struct cache_req *req);
  108. int thread_wait; /* How long (jiffies) we can block the
  109. * current thread to wait for updates.
  110. */
  111. };
  112. /* this must be embedded in a deferred_request that is being
  113. * delayed awaiting cache-fill
  114. */
  115. struct cache_deferred_req {
  116. struct hlist_node hash; /* on hash chain */
  117. struct list_head recent; /* on fifo */
  118. struct cache_head *item; /* cache item we wait on */
  119. void *owner; /* we might need to discard all defered requests
  120. * owned by someone */
  121. void (*revisit)(struct cache_deferred_req *req,
  122. int too_many);
  123. };
  124. extern const struct file_operations cache_file_operations_pipefs;
  125. extern const struct file_operations content_file_operations_pipefs;
  126. extern const struct file_operations cache_flush_operations_pipefs;
  127. extern struct cache_head *
  128. sunrpc_cache_lookup(struct cache_detail *detail,
  129. struct cache_head *key, int hash);
  130. extern struct cache_head *
  131. sunrpc_cache_update(struct cache_detail *detail,
  132. struct cache_head *new, struct cache_head *old, int hash);
  133. extern int
  134. sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
  135. void (*cache_request)(struct cache_detail *,
  136. struct cache_head *,
  137. char **,
  138. int *));
  139. extern void cache_clean_deferred(void *owner);
  140. static inline struct cache_head *cache_get(struct cache_head *h)
  141. {
  142. kref_get(&h->ref);
  143. return h;
  144. }
  145. static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
  146. {
  147. if (atomic_read(&h->ref.refcount) <= 2 &&
  148. h->expiry_time < cd->nextcheck)
  149. cd->nextcheck = h->expiry_time;
  150. kref_put(&h->ref, cd->cache_put);
  151. }
  152. static inline int cache_valid(struct cache_head *h)
  153. {
  154. /* If an item has been unhashed pending removal when
  155. * the refcount drops to 0, the expiry_time will be
  156. * set to 0. We don't want to consider such items
  157. * valid in this context even though CACHE_VALID is
  158. * set.
  159. */
  160. return (h->expiry_time != 0 && test_bit(CACHE_VALID, &h->flags));
  161. }
  162. extern int cache_check(struct cache_detail *detail,
  163. struct cache_head *h, struct cache_req *rqstp);
  164. extern void cache_flush(void);
  165. extern void cache_purge(struct cache_detail *detail);
  166. #define NEVER (0x7FFFFFFF)
  167. extern void __init cache_initialize(void);
  168. extern int cache_register(struct cache_detail *cd);
  169. extern int cache_register_net(struct cache_detail *cd, struct net *net);
  170. extern void cache_unregister(struct cache_detail *cd);
  171. extern void cache_unregister_net(struct cache_detail *cd, struct net *net);
  172. extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *,
  173. mode_t, struct cache_detail *);
  174. extern void sunrpc_cache_unregister_pipefs(struct cache_detail *);
  175. extern void qword_add(char **bpp, int *lp, char *str);
  176. extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
  177. extern int qword_get(char **bpp, char *dest, int bufsize);
  178. static inline int get_int(char **bpp, int *anint)
  179. {
  180. char buf[50];
  181. char *ep;
  182. int rv;
  183. int len = qword_get(bpp, buf, 50);
  184. if (len < 0) return -EINVAL;
  185. if (len ==0) return -ENOENT;
  186. rv = simple_strtol(buf, &ep, 0);
  187. if (*ep) return -EINVAL;
  188. *anint = rv;
  189. return 0;
  190. }
  191. /*
  192. * timestamps kept in the cache are expressed in seconds
  193. * since boot. This is the best for measuring differences in
  194. * real time.
  195. */
  196. static inline time_t seconds_since_boot(void)
  197. {
  198. struct timespec boot;
  199. getboottime(&boot);
  200. return get_seconds() - boot.tv_sec;
  201. }
  202. static inline time_t convert_to_wallclock(time_t sinceboot)
  203. {
  204. struct timespec boot;
  205. getboottime(&boot);
  206. return boot.tv_sec + sinceboot;
  207. }
  208. static inline time_t get_expiry(char **bpp)
  209. {
  210. int rv;
  211. struct timespec boot;
  212. if (get_int(bpp, &rv))
  213. return 0;
  214. if (rv < 0)
  215. return 0;
  216. getboottime(&boot);
  217. return rv - boot.tv_sec;
  218. }
  219. #ifdef CONFIG_NFSD_DEPRECATED
  220. static inline void sunrpc_invalidate(struct cache_head *h,
  221. struct cache_detail *detail)
  222. {
  223. h->expiry_time = seconds_since_boot() - 1;
  224. detail->nextcheck = seconds_since_boot();
  225. }
  226. #endif /* CONFIG_NFSD_DEPRECATED */
  227. #endif /* _LINUX_SUNRPC_CACHE_H_ */