1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Internal definitions for FS-Cache
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
12 #define pr_fmt(fmt) "FS-Cache: " fmt
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>
24 extern const struct seq_operations fscache_caches_seq_ops;
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);
31 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
33 return smp_load_acquire(&cache->state);
36 static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
38 return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
41 static inline void fscache_set_cache_state(struct fscache_cache *cache,
42 enum fscache_cache_state new_state)
44 smp_store_release(&cache->state, new_state);
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)
52 return try_cmpxchg_release(&cache->state, &old_state, new_state);
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;
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);
66 static inline void fscache_see_cookie(struct fscache_cookie *cookie,
67 enum fscache_cookie_trace where)
69 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
76 static inline void fscache_end_operation(struct netfs_cache_resources *cres)
78 const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
81 ops->end_operation(cres);
87 extern unsigned fscache_debug;
89 extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
95 extern int __init fscache_proc_init(void);
96 extern void fscache_proc_cleanup(void);
98 #define fscache_proc_init() (0)
99 #define fscache_proc_cleanup() do {} while (0)
105 #ifdef CONFIG_FSCACHE_STATS
106 extern atomic_t fscache_n_volumes;
107 extern atomic_t fscache_n_volumes_collision;
108 extern atomic_t fscache_n_volumes_nomem;
109 extern atomic_t fscache_n_cookies;
110 extern atomic_t fscache_n_cookies_lru;
111 extern atomic_t fscache_n_cookies_lru_expired;
112 extern atomic_t fscache_n_cookies_lru_removed;
113 extern atomic_t fscache_n_cookies_lru_dropped;
115 extern atomic_t fscache_n_acquires;
116 extern atomic_t fscache_n_acquires_ok;
117 extern atomic_t fscache_n_acquires_oom;
119 extern atomic_t fscache_n_invalidates;
121 extern atomic_t fscache_n_relinquishes;
122 extern atomic_t fscache_n_relinquishes_retire;
123 extern atomic_t fscache_n_relinquishes_dropped;
125 extern atomic_t fscache_n_resizes;
126 extern atomic_t fscache_n_resizes_null;
128 static inline void fscache_stat(atomic_t *stat)
133 static inline void fscache_stat_d(atomic_t *stat)
138 #define __fscache_stat(stat) (stat)
140 int fscache_stats_show(struct seq_file *m, void *v);
143 #define __fscache_stat(stat) (NULL)
144 #define fscache_stat(stat) do {} while (0)
145 #define fscache_stat_d(stat) do {} while (0)
151 extern const struct seq_operations fscache_volumes_seq_ops;
153 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
154 enum fscache_volume_trace where);
155 void fscache_put_volume(struct fscache_volume *volume,
156 enum fscache_volume_trace where);
157 bool fscache_begin_volume_access(struct fscache_volume *volume,
158 struct fscache_cookie *cookie,
159 enum fscache_access_trace why);
160 void fscache_create_volume(struct fscache_volume *volume, bool wait);
163 /*****************************************************************************/
167 #define dbgprintk(FMT, ...) \
168 printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
170 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
171 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
172 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
174 #define kjournal(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
177 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
178 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
179 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
181 #elif defined(CONFIG_FSCACHE_DEBUG)
182 #define _enter(FMT, ...) \
184 if (__do_kdebug(ENTER)) \
185 kenter(FMT, ##__VA_ARGS__); \
188 #define _leave(FMT, ...) \
190 if (__do_kdebug(LEAVE)) \
191 kleave(FMT, ##__VA_ARGS__); \
194 #define _debug(FMT, ...) \
196 if (__do_kdebug(DEBUG)) \
197 kdebug(FMT, ##__VA_ARGS__); \
201 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
202 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
203 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
207 * determine whether a particular optional debugging point should be logged
208 * - we need to go through three steps to persuade cpp to correctly join the
209 * shorthand in FSCACHE_DEBUG_LEVEL with its prefix
211 #define ____do_kdebug(LEVEL, POINT) \
212 unlikely((fscache_debug & \
213 (FSCACHE_POINT_##POINT << (FSCACHE_DEBUG_ ## LEVEL * 3))))
214 #define ___do_kdebug(LEVEL, POINT) \
215 ____do_kdebug(LEVEL, POINT)
216 #define __do_kdebug(POINT) \
217 ___do_kdebug(FSCACHE_DEBUG_LEVEL, POINT)
219 #define FSCACHE_DEBUG_CACHE 0
220 #define FSCACHE_DEBUG_COOKIE 1
221 #define FSCACHE_DEBUG_OBJECT 2
222 #define FSCACHE_DEBUG_OPERATION 3
224 #define FSCACHE_POINT_ENTER 1
225 #define FSCACHE_POINT_LEAVE 2
226 #define FSCACHE_POINT_DEBUG 4
228 #ifndef FSCACHE_DEBUG_LEVEL
229 #define FSCACHE_DEBUG_LEVEL CACHE
235 #if 1 /* defined(__KDEBUGALL) */
239 if (unlikely(!(X))) { \
241 pr_err("Assertion failed\n"); \
246 #define ASSERTCMP(X, OP, Y) \
248 if (unlikely(!((X) OP (Y)))) { \
250 pr_err("Assertion failed\n"); \
251 pr_err("%lx " #OP " %lx is false\n", \
252 (unsigned long)(X), (unsigned long)(Y)); \
257 #define ASSERTIF(C, X) \
259 if (unlikely((C) && !(X))) { \
261 pr_err("Assertion failed\n"); \
266 #define ASSERTIFCMP(C, X, OP, Y) \
268 if (unlikely((C) && !((X) OP (Y)))) { \
270 pr_err("Assertion failed\n"); \
271 pr_err("%lx " #OP " %lx is false\n", \
272 (unsigned long)(X), (unsigned long)(Y)); \
279 #define ASSERT(X) do {} while (0)
280 #define ASSERTCMP(X, OP, Y) do {} while (0)
281 #define ASSERTIF(C, X) do {} while (0)
282 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
284 #endif /* assert or not */