blob: f121c21590dcf2950f404d2d76ef32f998fa0425 [file] [log] [blame]
David Howells1e1236b2021-10-20 14:34:41 +01001/* 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/*
David Howells95493322021-10-20 15:00:26 +010021 * cache.c
22 */
23#ifdef CONFIG_PROC_FS
24extern const struct seq_operations fscache_caches_seq_ops;
25#endif
David Howells23e12e22021-10-20 15:00:26 +010026bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
27void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
David Howells95493322021-10-20 15:00:26 +010028struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache);
29void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where);
30
31static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
32{
33 return smp_load_acquire(&cache->state);
34}
35
36static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
37{
38 return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
39}
40
41static 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
48static 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/*
David Howells7f3283a2021-10-20 15:53:34 +010056 * cookie.c
57 */
58extern struct kmem_cache *fscache_cookie_jar;
59extern const struct seq_operations fscache_cookies_seq_ops;
David Howells12bb21a2021-10-20 15:53:34 +010060extern struct timer_list fscache_cookie_lru_timer;
David Howells7f3283a2021-10-20 15:53:34 +010061
62extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
David Howellsa7733fb2021-10-20 15:53:34 +010063extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
64 enum fscache_access_trace why);
65
David Howells7f3283a2021-10-20 15:53:34 +010066static 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/*
David Howellsd64f4552021-10-20 14:06:34 +010074 * io.c
75 */
76static inline void fscache_end_operation(struct netfs_cache_resources *cres)
77{
78 const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
79
80 if (ops)
81 ops->end_operation(cres);
82}
83
84/*
David Howells1e1236b2021-10-20 14:34:41 +010085 * main.c
86 */
87extern unsigned fscache_debug;
88
David Howellse8a07c92021-10-20 15:45:28 +010089extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
90
David Howells1e1236b2021-10-20 14:34:41 +010091/*
92 * proc.c
93 */
94#ifdef CONFIG_PROC_FS
95extern int __init fscache_proc_init(void);
96extern void fscache_proc_cleanup(void);
97#else
98#define fscache_proc_init() (0)
99#define fscache_proc_cleanup() do {} while (0)
100#endif
101
102/*
103 * stats.c
104 */
105#ifdef CONFIG_FSCACHE_STATS
David Howells62ab6332021-10-20 15:26:17 +0100106extern atomic_t fscache_n_volumes;
107extern atomic_t fscache_n_volumes_collision;
108extern atomic_t fscache_n_volumes_nomem;
David Howells7f3283a2021-10-20 15:53:34 +0100109extern atomic_t fscache_n_cookies;
David Howells12bb21a2021-10-20 15:53:34 +0100110extern atomic_t fscache_n_cookies_lru;
111extern atomic_t fscache_n_cookies_lru_expired;
112extern atomic_t fscache_n_cookies_lru_removed;
113extern atomic_t fscache_n_cookies_lru_dropped;
David Howells7f3283a2021-10-20 15:53:34 +0100114
115extern atomic_t fscache_n_acquires;
116extern atomic_t fscache_n_acquires_ok;
117extern atomic_t fscache_n_acquires_oom;
118
David Howellsd24af132021-10-20 15:53:34 +0100119extern atomic_t fscache_n_invalidates;
120
David Howells7f3283a2021-10-20 15:53:34 +0100121extern atomic_t fscache_n_relinquishes;
122extern atomic_t fscache_n_relinquishes_retire;
123extern atomic_t fscache_n_relinquishes_dropped;
David Howells1e1236b2021-10-20 14:34:41 +0100124
David Howells16a96bd2021-10-20 14:06:34 +0100125extern atomic_t fscache_n_resizes;
126extern atomic_t fscache_n_resizes_null;
127
David Howells1e1236b2021-10-20 14:34:41 +0100128static inline void fscache_stat(atomic_t *stat)
129{
130 atomic_inc(stat);
131}
132
133static inline void fscache_stat_d(atomic_t *stat)
134{
135 atomic_dec(stat);
136}
137
138#define __fscache_stat(stat) (stat)
139
140int fscache_stats_show(struct seq_file *m, void *v);
141#else
142
143#define __fscache_stat(stat) (NULL)
144#define fscache_stat(stat) do {} while (0)
145#define fscache_stat_d(stat) do {} while (0)
146#endif
147
David Howells62ab6332021-10-20 15:26:17 +0100148/*
149 * volume.c
150 */
151extern const struct seq_operations fscache_volumes_seq_ops;
152
153struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
154 enum fscache_volume_trace where);
155void fscache_put_volume(struct fscache_volume *volume,
156 enum fscache_volume_trace where);
David Howellse6acd322021-10-20 15:26:17 +0100157bool fscache_begin_volume_access(struct fscache_volume *volume,
158 struct fscache_cookie *cookie,
159 enum fscache_access_trace why);
David Howells62ab6332021-10-20 15:26:17 +0100160void fscache_create_volume(struct fscache_volume *volume, bool wait);
161
David Howells1e1236b2021-10-20 14:34:41 +0100162
163/*****************************************************************************/
164/*
165 * debug tracing
166 */
167#define dbgprintk(FMT, ...) \
168 printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
169
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__)
173
174#define kjournal(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
175
176#ifdef __KDEBUG
177#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
178#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
179#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
180
181#elif defined(CONFIG_FSCACHE_DEBUG)
182#define _enter(FMT, ...) \
183do { \
184 if (__do_kdebug(ENTER)) \
185 kenter(FMT, ##__VA_ARGS__); \
186} while (0)
187
188#define _leave(FMT, ...) \
189do { \
190 if (__do_kdebug(LEAVE)) \
191 kleave(FMT, ##__VA_ARGS__); \
192} while (0)
193
194#define _debug(FMT, ...) \
195do { \
196 if (__do_kdebug(DEBUG)) \
197 kdebug(FMT, ##__VA_ARGS__); \
198} while (0)
199
200#else
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__)
204#endif
205
206/*
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
210 */
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)
218
219#define FSCACHE_DEBUG_CACHE 0
220#define FSCACHE_DEBUG_COOKIE 1
221#define FSCACHE_DEBUG_OBJECT 2
222#define FSCACHE_DEBUG_OPERATION 3
223
224#define FSCACHE_POINT_ENTER 1
225#define FSCACHE_POINT_LEAVE 2
226#define FSCACHE_POINT_DEBUG 4
227
228#ifndef FSCACHE_DEBUG_LEVEL
229#define FSCACHE_DEBUG_LEVEL CACHE
230#endif
231
232/*
233 * assertions
234 */
235#if 1 /* defined(__KDEBUGALL) */
236
237#define ASSERT(X) \
238do { \
239 if (unlikely(!(X))) { \
240 pr_err("\n"); \
241 pr_err("Assertion failed\n"); \
242 BUG(); \
243 } \
244} while (0)
245
246#define ASSERTCMP(X, OP, Y) \
247do { \
248 if (unlikely(!((X) OP (Y)))) { \
249 pr_err("\n"); \
250 pr_err("Assertion failed\n"); \
251 pr_err("%lx " #OP " %lx is false\n", \
252 (unsigned long)(X), (unsigned long)(Y)); \
253 BUG(); \
254 } \
255} while (0)
256
257#define ASSERTIF(C, X) \
258do { \
259 if (unlikely((C) && !(X))) { \
260 pr_err("\n"); \
261 pr_err("Assertion failed\n"); \
262 BUG(); \
263 } \
264} while (0)
265
266#define ASSERTIFCMP(C, X, OP, Y) \
267do { \
268 if (unlikely((C) && !((X) OP (Y)))) { \
269 pr_err("\n"); \
270 pr_err("Assertion failed\n"); \
271 pr_err("%lx " #OP " %lx is false\n", \
272 (unsigned long)(X), (unsigned long)(Y)); \
273 BUG(); \
274 } \
275} while (0)
276
277#else
278
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)
283
284#endif /* assert or not */