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