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