1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef BTRFS_MESSAGES_H
4 #define BTRFS_MESSAGES_H
5
6 #include <linux/types.h>
7 #include <linux/types.h>
8 #include <linux/printk.h>
9 #include <linux/bug.h>
10
11 struct btrfs_fs_info;
12
13 /*
14 * We want to be able to override this in btrfs-progs.
15 */
16 #ifdef __KERNEL__
17
18 static inline __printf(2, 3) __cold
btrfs_no_printk(const struct btrfs_fs_info * fs_info,const char * fmt,...)19 void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
20 {
21 }
22
23 #endif
24
25 #ifdef CONFIG_PRINTK
26
27 #define btrfs_printk(fs_info, fmt, args...) \
28 _btrfs_printk(fs_info, fmt, ##args)
29
30 __printf(2, 3)
31 __cold
32 void _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
33
34 #else
35
36 #define btrfs_printk(fs_info, fmt, args...) \
37 btrfs_no_printk(fs_info, fmt, ##args)
38 #endif
39
40 /*
41 * Print a message with filesystem info, enclosed in RCU protection.
42 */
43 #define btrfs_crit(fs_info, fmt, args...) \
44 btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
45 #define btrfs_err(fs_info, fmt, args...) \
46 btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
47 #define btrfs_warn(fs_info, fmt, args...) \
48 btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
49 #define btrfs_info(fs_info, fmt, args...) \
50 btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
51
52 /*
53 * Wrappers that use a ratelimited printk
54 */
55 #define btrfs_crit_rl(fs_info, fmt, args...) \
56 btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
57 #define btrfs_err_rl(fs_info, fmt, args...) \
58 btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
59 #define btrfs_warn_rl(fs_info, fmt, args...) \
60 btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
61 #define btrfs_info_rl(fs_info, fmt, args...) \
62 btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
63
64 #if defined(CONFIG_DYNAMIC_DEBUG)
65 #define btrfs_debug(fs_info, fmt, args...) \
66 _dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu, \
67 fs_info, KERN_DEBUG fmt, ##args)
68 #define btrfs_debug_rl(fs_info, fmt, args...) \
69 _dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu, \
70 fs_info, KERN_DEBUG fmt, ##args)
71 #elif defined(DEBUG)
72 #define btrfs_debug(fs_info, fmt, args...) \
73 btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
74 #define btrfs_debug_rl(fs_info, fmt, args...) \
75 btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
76 #else
77 /* When printk() is no_printk(), expand to no-op. */
78 #define btrfs_debug(fs_info, fmt, args...) do { (void)(fs_info); } while(0)
79 #define btrfs_debug_rl(fs_info, fmt, args...) do { (void)(fs_info); } while(0)
80 #endif
81
82 #define btrfs_printk_in_rcu(fs_info, fmt, args...) \
83 do { \
84 rcu_read_lock(); \
85 btrfs_printk(fs_info, fmt, ##args); \
86 rcu_read_unlock(); \
87 } while (0)
88
89 #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...) \
90 do { \
91 static DEFINE_RATELIMIT_STATE(_rs, \
92 DEFAULT_RATELIMIT_INTERVAL, \
93 DEFAULT_RATELIMIT_BURST); \
94 \
95 rcu_read_lock(); \
96 if (__ratelimit(&_rs)) \
97 btrfs_printk(fs_info, fmt, ##args); \
98 rcu_read_unlock(); \
99 } while (0)
100
101 #ifdef CONFIG_BTRFS_ASSERT
102
103 __printf(1, 2)
verify_assert_printk_format(const char * fmt,...)104 static inline void verify_assert_printk_format(const char *fmt, ...) {
105 /* Stub to verify the assertion format string. */
106 }
107
108 /* Take the first token if any. */
109 #define __FIRST_ARG(_, ...) _
110 /*
111 * Skip the first token and return the rest, if it's empty the comma is dropped.
112 * As ##__VA_ARGS__ cannot be at the beginning of the macro the __VA_OPT__ is needed
113 * and supported since GCC 8 and Clang 12.
114 */
115 #define __REST_ARGS(_, ... ) __VA_OPT__(,) __VA_ARGS__
116
117 #if defined(CONFIG_CC_IS_CLANG) || GCC_VERSION >= 80000
118 /*
119 * Assertion with optional printk() format.
120 *
121 * Accepted syntax:
122 * ASSERT(condition);
123 * ASSERT(condition, "string");
124 * ASSERT(condition, "variable=%d", variable);
125 *
126 * How it works:
127 * - if there's no format string, ""[0] evaluates at compile time to 0 and the
128 * true branch is executed
129 * - any non-empty format string with the "" prefix evaluates to != 0 at
130 * compile time and the false branch is executed
131 * - stringified condition is printed as %s so we don't accidentally mix format
132 * strings (the % operator)
133 * - there can be only one printk() call, so the format strings and arguments are
134 * spliced together:
135 * DEFAULT_FMT [USER_FMT], DEFAULT_ARGS [, USER_ARGS]
136 * - comma between DEFAULT_ARGS and USER_ARGS is handled by preprocessor
137 * (requires __VA_OPT__ support)
138 * - otherwise we could use __VA_OPT(,) __VA_ARGS__ for the 2nd+ argument of args,
139 */
140 #define ASSERT(cond, args...) \
141 do { \
142 verify_assert_printk_format("check the format string" args); \
143 if (!likely(cond)) { \
144 if (("" __FIRST_ARG(args) [0]) == 0) { \
145 pr_err("assertion failed: %s :: %ld, in %s:%d\n", \
146 #cond, (long)(cond), __FILE__, __LINE__); \
147 } else { \
148 pr_err("assertion failed: %s :: %ld, in %s:%d (" __FIRST_ARG(args) ")\n", \
149 #cond, (long)(cond), __FILE__, __LINE__ __REST_ARGS(args)); \
150 } \
151 BUG(); \
152 } \
153 } while(0)
154
155 #else
156
157 /* For GCC < 8.x only the simple output. */
158
159 #define ASSERT(cond, args...) \
160 do { \
161 verify_assert_printk_format("check the format string" args); \
162 if (!likely(cond)) { \
163 pr_err("assertion failed: %s :: %ld, in %s:%d\n", \
164 #cond, (long)(cond), __FILE__, __LINE__); \
165 BUG(); \
166 } \
167 } while(0)
168
169 #endif
170
171 #else
172 #define ASSERT(cond, args...) (void)(cond)
173 #endif
174
175 #ifdef CONFIG_BTRFS_DEBUG
176 /* Verbose warning only under debug build. */
177 #define DEBUG_WARN(args...) WARN(1, KERN_ERR args)
178 #else
179 #define DEBUG_WARN(...) do {} while(0)
180 #endif
181
182 __printf(5, 6)
183 __cold
184 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
185 unsigned int line, int error, const char *fmt, ...);
186
187 const char * __attribute_const__ btrfs_decode_error(int error);
188
189 #define btrfs_handle_fs_error(fs_info, error, fmt, args...) \
190 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
191 (error), fmt, ##args)
192
193 __printf(5, 6)
194 __cold
195 void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function,
196 unsigned int line, int error, const char *fmt, ...);
197 /*
198 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
199 * will panic(). Otherwise we BUG() here.
200 */
201 #define btrfs_panic(fs_info, error, fmt, args...) \
202 do { \
203 __btrfs_panic(fs_info, __func__, __LINE__, error, fmt, ##args); \
204 BUG(); \
205 } while (0)
206
207 #if BITS_PER_LONG == 32
208 #define BTRFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT)
209 /*
210 * The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical
211 * addresses of extents.
212 *
213 * For 4K page size it's about 10T, for 64K it's 160T.
214 */
215 #define BTRFS_32BIT_EARLY_WARN_THRESHOLD (BTRFS_32BIT_MAX_FILE_SIZE * 5 / 8)
216 void btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info);
217 void btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info);
218 #endif
219
220 #endif
221