internal.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Internal definitions for FS-Cache
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #ifdef pr_fmt
  8. #undef pr_fmt
  9. #endif
  10. #define pr_fmt(fmt) "FS-Cache: " fmt
  11. #include <linux/slab.h>
  12. #include <linux/fscache-cache.h>
  13. #include <trace/events/fscache.h>
  14. #include <linux/sched.h>
  15. #include <linux/seq_file.h>
  16. /*
  17. * cache.c
  18. */
  19. #ifdef CONFIG_PROC_FS
  20. extern const struct seq_operations fscache_caches_seq_ops;
  21. #endif
  22. bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
  23. void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
  24. struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache);
  25. void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where);
  26. static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
  27. {
  28. return smp_load_acquire(&cache->state);
  29. }
  30. static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
  31. {
  32. return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
  33. }
  34. static inline void fscache_set_cache_state(struct fscache_cache *cache,
  35. enum fscache_cache_state new_state)
  36. {
  37. smp_store_release(&cache->state, new_state);
  38. }
  39. static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache,
  40. enum fscache_cache_state old_state,
  41. enum fscache_cache_state new_state)
  42. {
  43. return try_cmpxchg_release(&cache->state, &old_state, new_state);
  44. }
  45. /*
  46. * cookie.c
  47. */
  48. extern struct kmem_cache *fscache_cookie_jar;
  49. #ifdef CONFIG_PROC_FS
  50. extern const struct seq_operations fscache_cookies_seq_ops;
  51. #endif
  52. extern struct timer_list fscache_cookie_lru_timer;
  53. extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
  54. extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
  55. enum fscache_access_trace why);
  56. static inline void fscache_see_cookie(struct fscache_cookie *cookie,
  57. enum fscache_cookie_trace where)
  58. {
  59. trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
  60. where);
  61. }
  62. /*
  63. * main.c
  64. */
  65. extern unsigned fscache_debug;
  66. extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
  67. /*
  68. * proc.c
  69. */
  70. #ifdef CONFIG_PROC_FS
  71. extern int __init fscache_proc_init(void);
  72. extern void fscache_proc_cleanup(void);
  73. #else
  74. #define fscache_proc_init() (0)
  75. #define fscache_proc_cleanup() do {} while (0)
  76. #endif
  77. /*
  78. * stats.c
  79. */
  80. #ifdef CONFIG_FSCACHE_STATS
  81. extern atomic_t fscache_n_volumes;
  82. extern atomic_t fscache_n_volumes_collision;
  83. extern atomic_t fscache_n_volumes_nomem;
  84. extern atomic_t fscache_n_cookies;
  85. extern atomic_t fscache_n_cookies_lru;
  86. extern atomic_t fscache_n_cookies_lru_expired;
  87. extern atomic_t fscache_n_cookies_lru_removed;
  88. extern atomic_t fscache_n_cookies_lru_dropped;
  89. extern atomic_t fscache_n_acquires;
  90. extern atomic_t fscache_n_acquires_ok;
  91. extern atomic_t fscache_n_acquires_oom;
  92. extern atomic_t fscache_n_invalidates;
  93. extern atomic_t fscache_n_relinquishes;
  94. extern atomic_t fscache_n_relinquishes_retire;
  95. extern atomic_t fscache_n_relinquishes_dropped;
  96. extern atomic_t fscache_n_resizes;
  97. extern atomic_t fscache_n_resizes_null;
  98. static inline void fscache_stat(atomic_t *stat)
  99. {
  100. atomic_inc(stat);
  101. }
  102. static inline void fscache_stat_d(atomic_t *stat)
  103. {
  104. atomic_dec(stat);
  105. }
  106. #define __fscache_stat(stat) (stat)
  107. int fscache_stats_show(struct seq_file *m, void *v);
  108. #else
  109. #define __fscache_stat(stat) (NULL)
  110. #define fscache_stat(stat) do {} while (0)
  111. #define fscache_stat_d(stat) do {} while (0)
  112. #endif
  113. /*
  114. * volume.c
  115. */
  116. #ifdef CONFIG_PROC_FS
  117. extern const struct seq_operations fscache_volumes_seq_ops;
  118. #endif
  119. struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
  120. enum fscache_volume_trace where);
  121. void fscache_put_volume(struct fscache_volume *volume,
  122. enum fscache_volume_trace where);
  123. bool fscache_begin_volume_access(struct fscache_volume *volume,
  124. struct fscache_cookie *cookie,
  125. enum fscache_access_trace why);
  126. void fscache_create_volume(struct fscache_volume *volume, bool wait);
  127. /*****************************************************************************/
  128. /*
  129. * debug tracing
  130. */
  131. #define dbgprintk(FMT, ...) \
  132. printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
  133. #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  134. #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  135. #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
  136. #define kjournal(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
  137. #ifdef __KDEBUG
  138. #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
  139. #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
  140. #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
  141. #elif defined(CONFIG_FSCACHE_DEBUG)
  142. #define _enter(FMT, ...) \
  143. do { \
  144. if (__do_kdebug(ENTER)) \
  145. kenter(FMT, ##__VA_ARGS__); \
  146. } while (0)
  147. #define _leave(FMT, ...) \
  148. do { \
  149. if (__do_kdebug(LEAVE)) \
  150. kleave(FMT, ##__VA_ARGS__); \
  151. } while (0)
  152. #define _debug(FMT, ...) \
  153. do { \
  154. if (__do_kdebug(DEBUG)) \
  155. kdebug(FMT, ##__VA_ARGS__); \
  156. } while (0)
  157. #else
  158. #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  159. #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  160. #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
  161. #endif
  162. /*
  163. * determine whether a particular optional debugging point should be logged
  164. * - we need to go through three steps to persuade cpp to correctly join the
  165. * shorthand in FSCACHE_DEBUG_LEVEL with its prefix
  166. */
  167. #define ____do_kdebug(LEVEL, POINT) \
  168. unlikely((fscache_debug & \
  169. (FSCACHE_POINT_##POINT << (FSCACHE_DEBUG_ ## LEVEL * 3))))
  170. #define ___do_kdebug(LEVEL, POINT) \
  171. ____do_kdebug(LEVEL, POINT)
  172. #define __do_kdebug(POINT) \
  173. ___do_kdebug(FSCACHE_DEBUG_LEVEL, POINT)
  174. #define FSCACHE_DEBUG_CACHE 0
  175. #define FSCACHE_DEBUG_COOKIE 1
  176. #define FSCACHE_DEBUG_OBJECT 2
  177. #define FSCACHE_DEBUG_OPERATION 3
  178. #define FSCACHE_POINT_ENTER 1
  179. #define FSCACHE_POINT_LEAVE 2
  180. #define FSCACHE_POINT_DEBUG 4
  181. #ifndef FSCACHE_DEBUG_LEVEL
  182. #define FSCACHE_DEBUG_LEVEL CACHE
  183. #endif
  184. /*
  185. * assertions
  186. */
  187. #if 1 /* defined(__KDEBUGALL) */
  188. #define ASSERT(X) \
  189. do { \
  190. if (unlikely(!(X))) { \
  191. pr_err("\n"); \
  192. pr_err("Assertion failed\n"); \
  193. BUG(); \
  194. } \
  195. } while (0)
  196. #define ASSERTCMP(X, OP, Y) \
  197. do { \
  198. if (unlikely(!((X) OP (Y)))) { \
  199. pr_err("\n"); \
  200. pr_err("Assertion failed\n"); \
  201. pr_err("%lx " #OP " %lx is false\n", \
  202. (unsigned long)(X), (unsigned long)(Y)); \
  203. BUG(); \
  204. } \
  205. } while (0)
  206. #define ASSERTIF(C, X) \
  207. do { \
  208. if (unlikely((C) && !(X))) { \
  209. pr_err("\n"); \
  210. pr_err("Assertion failed\n"); \
  211. BUG(); \
  212. } \
  213. } while (0)
  214. #define ASSERTIFCMP(C, X, OP, Y) \
  215. do { \
  216. if (unlikely((C) && !((X) OP (Y)))) { \
  217. pr_err("\n"); \
  218. pr_err("Assertion failed\n"); \
  219. pr_err("%lx " #OP " %lx is false\n", \
  220. (unsigned long)(X), (unsigned long)(Y)); \
  221. BUG(); \
  222. } \
  223. } while (0)
  224. #else
  225. #define ASSERT(X) do {} while (0)
  226. #define ASSERTCMP(X, OP, Y) do {} while (0)
  227. #define ASSERTIF(C, X) do {} while (0)
  228. #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
  229. #endif /* assert or not */