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
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 #define btrfs_emerg(fs_info, fmt, args...) \
41 	btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
42 #define btrfs_alert(fs_info, fmt, args...) \
43 	btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
44 #define btrfs_crit(fs_info, fmt, args...) \
45 	btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
46 #define btrfs_err(fs_info, fmt, args...) \
47 	btrfs_printk(fs_info, KERN_ERR fmt, ##args)
48 #define btrfs_warn(fs_info, fmt, args...) \
49 	btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
50 #define btrfs_notice(fs_info, fmt, args...) \
51 	btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
52 #define btrfs_info(fs_info, fmt, args...) \
53 	btrfs_printk(fs_info, KERN_INFO fmt, ##args)
54 
55 /*
56  * Wrappers that use printk_in_rcu
57  */
58 #define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
59 	btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
60 #define btrfs_alert_in_rcu(fs_info, fmt, args...) \
61 	btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
62 #define btrfs_crit_in_rcu(fs_info, fmt, args...) \
63 	btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
64 #define btrfs_err_in_rcu(fs_info, fmt, args...) \
65 	btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
66 #define btrfs_warn_in_rcu(fs_info, fmt, args...) \
67 	btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
68 #define btrfs_notice_in_rcu(fs_info, fmt, args...) \
69 	btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
70 #define btrfs_info_in_rcu(fs_info, fmt, args...) \
71 	btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
72 
73 /*
74  * Wrappers that use a ratelimited printk_in_rcu
75  */
76 #define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
77 	btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
78 #define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
79 	btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
80 #define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
81 	btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
82 #define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
83 	btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
84 #define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
85 	btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
86 #define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
87 	btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
88 #define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
89 	btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
90 
91 /*
92  * Wrappers that use a ratelimited printk
93  */
94 #define btrfs_emerg_rl(fs_info, fmt, args...) \
95 	btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
96 #define btrfs_alert_rl(fs_info, fmt, args...) \
97 	btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
98 #define btrfs_crit_rl(fs_info, fmt, args...) \
99 	btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
100 #define btrfs_err_rl(fs_info, fmt, args...) \
101 	btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
102 #define btrfs_warn_rl(fs_info, fmt, args...) \
103 	btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
104 #define btrfs_notice_rl(fs_info, fmt, args...) \
105 	btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
106 #define btrfs_info_rl(fs_info, fmt, args...) \
107 	btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
108 
109 #if defined(CONFIG_DYNAMIC_DEBUG)
110 #define btrfs_debug(fs_info, fmt, args...)				\
111 	_dynamic_func_call_no_desc(fmt, btrfs_printk,			\
112 				   fs_info, KERN_DEBUG fmt, ##args)
113 #define btrfs_debug_in_rcu(fs_info, fmt, args...)			\
114 	_dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu,		\
115 				   fs_info, KERN_DEBUG fmt, ##args)
116 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...)			\
117 	_dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu,		\
118 				   fs_info, KERN_DEBUG fmt, ##args)
119 #define btrfs_debug_rl(fs_info, fmt, args...)				\
120 	_dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited,	\
121 				   fs_info, KERN_DEBUG fmt, ##args)
122 #elif defined(DEBUG)
123 #define btrfs_debug(fs_info, fmt, args...) \
124 	btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
125 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
126 	btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
127 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
128 	btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
129 #define btrfs_debug_rl(fs_info, fmt, args...) \
130 	btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
131 #else
132 #define btrfs_debug(fs_info, fmt, args...) \
133 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
134 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
135 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
136 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
137 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
138 #define btrfs_debug_rl(fs_info, fmt, args...) \
139 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
140 #endif
141 
142 #define btrfs_printk_in_rcu(fs_info, fmt, args...)	\
143 do {							\
144 	rcu_read_lock();				\
145 	btrfs_printk(fs_info, fmt, ##args);		\
146 	rcu_read_unlock();				\
147 } while (0)
148 
149 #define btrfs_no_printk_in_rcu(fs_info, fmt, args...)	\
150 do {							\
151 	rcu_read_lock();				\
152 	btrfs_no_printk(fs_info, fmt, ##args);		\
153 	rcu_read_unlock();				\
154 } while (0)
155 
156 #define btrfs_printk_ratelimited(fs_info, fmt, args...)		\
157 do {								\
158 	static DEFINE_RATELIMIT_STATE(_rs,			\
159 		DEFAULT_RATELIMIT_INTERVAL,			\
160 		DEFAULT_RATELIMIT_BURST);			\
161 	if (__ratelimit(&_rs))					\
162 		btrfs_printk(fs_info, fmt, ##args);		\
163 } while (0)
164 
165 #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...)		\
166 do {								\
167 	rcu_read_lock();					\
168 	btrfs_printk_ratelimited(fs_info, fmt, ##args);		\
169 	rcu_read_unlock();					\
170 } while (0)
171 
172 #ifdef CONFIG_BTRFS_ASSERT
173 
174 __printf(1, 2)
175 static inline void verify_assert_printk_format(const char *fmt, ...) {
176 	/* Stub to verify the assertion format string. */
177 }
178 
179 /* Take the first token if any. */
180 #define __FIRST_ARG(_, ...) _
181 /*
182  * Skip the first token and return the rest, if it's empty the comma is dropped.
183  * As ##__VA_ARGS__ cannot be at the beginning of the macro the __VA_OPT__ is needed
184  * and supported since GCC 8 and Clang 12.
185  */
186 #define __REST_ARGS(_, ... ) __VA_OPT__(,) __VA_ARGS__
187 
188 #if defined(CONFIG_CC_IS_CLANG) || GCC_VERSION >= 80000
189 /*
190  * Assertion with optional printk() format.
191  *
192  * Accepted syntax:
193  * ASSERT(condition);
194  * ASSERT(condition, "string");
195  * ASSERT(condition, "variable=%d", variable);
196  *
197  * How it works:
198  * - if there's no format string, ""[0] evaluates at compile time to 0 and the
199  *   true branch is executed
200  * - any non-empty format string with the "" prefix evaluates to != 0 at
201  *   compile time and the false branch is executed
202  * - stringified condition is printed as %s so we don't accidentally mix format
203  *   strings (the % operator)
204  * - there can be only one printk() call, so the format strings and arguments are
205  *   spliced together:
206  *   DEFAULT_FMT [USER_FMT], DEFAULT_ARGS [, USER_ARGS]
207  * - comma between DEFAULT_ARGS and USER_ARGS is handled by preprocessor
208  *   (requires __VA_OPT__ support)
209  * - otherwise we could use __VA_OPT(,) __VA_ARGS__ for the 2nd+ argument of args,
210  */
211 #define ASSERT(cond, args...)							\
212 do {										\
213 	verify_assert_printk_format("check the format string" args);		\
214 	if (!likely(cond)) {							\
215 		if (("" __FIRST_ARG(args) [0]) == 0) {				\
216 			pr_err("assertion failed: %s :: %ld, in %s:%d\n",	\
217 				#cond, (long)(cond), __FILE__, __LINE__);	\
218 		} else {							\
219 			pr_err("assertion failed: %s :: %ld, in %s:%d (" __FIRST_ARG(args) ")\n", \
220 				#cond, (long)(cond), __FILE__, __LINE__ __REST_ARGS(args)); \
221 		}								\
222 		BUG();								\
223 	}									\
224 } while(0)
225 
226 #else
227 
228 /* For GCC < 8.x only the simple output. */
229 
230 #define ASSERT(cond, args...)							\
231 do {										\
232 	verify_assert_printk_format("check the format string" args);		\
233 	if (!likely(cond)) {							\
234 		pr_err("assertion failed: %s :: %ld, in %s:%d\n",		\
235 			#cond, (long)(cond), __FILE__, __LINE__);		\
236 		BUG();								\
237 	}									\
238 } while(0)
239 
240 #endif
241 
242 #else
243 #define ASSERT(cond, args...)			(void)(cond)
244 #endif
245 
246 #ifdef CONFIG_BTRFS_DEBUG
247 /* Verbose warning only under debug build. */
248 #define DEBUG_WARN(args...)			WARN(1, KERN_ERR args)
249 #else
250 #define DEBUG_WARN(...)				do {} while(0)
251 #endif
252 
253 __printf(5, 6)
254 __cold
255 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
256 		     unsigned int line, int error, const char *fmt, ...);
257 
258 const char * __attribute_const__ btrfs_decode_error(int error);
259 
260 #define btrfs_handle_fs_error(fs_info, error, fmt, args...)		\
261 	__btrfs_handle_fs_error((fs_info), __func__, __LINE__,		\
262 				(error), fmt, ##args)
263 
264 __printf(5, 6)
265 __cold
266 void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function,
267 		   unsigned int line, int error, const char *fmt, ...);
268 /*
269  * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
270  * will panic().  Otherwise we BUG() here.
271  */
272 #define btrfs_panic(fs_info, error, fmt, args...)			\
273 do {									\
274 	__btrfs_panic(fs_info, __func__, __LINE__, error, fmt, ##args);	\
275 	BUG();								\
276 } while (0)
277 
278 #if BITS_PER_LONG == 32
279 #define BTRFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT)
280 /*
281  * The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical
282  * addresses of extents.
283  *
284  * For 4K page size it's about 10T, for 64K it's 160T.
285  */
286 #define BTRFS_32BIT_EARLY_WARN_THRESHOLD (BTRFS_32BIT_MAX_FILE_SIZE * 5 / 8)
287 void btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info);
288 void btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info);
289 #endif
290 
291 #endif
292