xref: /openbmc/linux/fs/btrfs/messages.h (revision 43ee1e3f)
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 
8 struct btrfs_fs_info;
9 struct btrfs_trans_handle;
10 
11 static inline __printf(2, 3) __cold
12 void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
13 {
14 }
15 
16 #ifdef CONFIG_PRINTK
17 
18 #define btrfs_printk(fs_info, fmt, args...)				\
19 	_btrfs_printk(fs_info, fmt, ##args)
20 
21 __printf(2, 3)
22 __cold
23 void _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
24 
25 #else
26 
27 #define btrfs_printk(fs_info, fmt, args...) \
28 	btrfs_no_printk(fs_info, fmt, ##args)
29 #endif
30 
31 #define btrfs_emerg(fs_info, fmt, args...) \
32 	btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
33 #define btrfs_alert(fs_info, fmt, args...) \
34 	btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
35 #define btrfs_crit(fs_info, fmt, args...) \
36 	btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
37 #define btrfs_err(fs_info, fmt, args...) \
38 	btrfs_printk(fs_info, KERN_ERR fmt, ##args)
39 #define btrfs_warn(fs_info, fmt, args...) \
40 	btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
41 #define btrfs_notice(fs_info, fmt, args...) \
42 	btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
43 #define btrfs_info(fs_info, fmt, args...) \
44 	btrfs_printk(fs_info, KERN_INFO fmt, ##args)
45 
46 /*
47  * Wrappers that use printk_in_rcu
48  */
49 #define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
50 	btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
51 #define btrfs_alert_in_rcu(fs_info, fmt, args...) \
52 	btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
53 #define btrfs_crit_in_rcu(fs_info, fmt, args...) \
54 	btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
55 #define btrfs_err_in_rcu(fs_info, fmt, args...) \
56 	btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
57 #define btrfs_warn_in_rcu(fs_info, fmt, args...) \
58 	btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
59 #define btrfs_notice_in_rcu(fs_info, fmt, args...) \
60 	btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
61 #define btrfs_info_in_rcu(fs_info, fmt, args...) \
62 	btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
63 
64 /*
65  * Wrappers that use a ratelimited printk_in_rcu
66  */
67 #define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
68 	btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
69 #define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
70 	btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
71 #define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
72 	btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
73 #define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
74 	btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
75 #define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
76 	btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
77 #define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
78 	btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
79 #define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
80 	btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
81 
82 /*
83  * Wrappers that use a ratelimited printk
84  */
85 #define btrfs_emerg_rl(fs_info, fmt, args...) \
86 	btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
87 #define btrfs_alert_rl(fs_info, fmt, args...) \
88 	btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
89 #define btrfs_crit_rl(fs_info, fmt, args...) \
90 	btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
91 #define btrfs_err_rl(fs_info, fmt, args...) \
92 	btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
93 #define btrfs_warn_rl(fs_info, fmt, args...) \
94 	btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
95 #define btrfs_notice_rl(fs_info, fmt, args...) \
96 	btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
97 #define btrfs_info_rl(fs_info, fmt, args...) \
98 	btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
99 
100 #if defined(CONFIG_DYNAMIC_DEBUG)
101 #define btrfs_debug(fs_info, fmt, args...)				\
102 	_dynamic_func_call_no_desc(fmt, btrfs_printk,			\
103 				   fs_info, KERN_DEBUG fmt, ##args)
104 #define btrfs_debug_in_rcu(fs_info, fmt, args...)			\
105 	_dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu,		\
106 				   fs_info, KERN_DEBUG fmt, ##args)
107 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...)			\
108 	_dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu,		\
109 				   fs_info, KERN_DEBUG fmt, ##args)
110 #define btrfs_debug_rl(fs_info, fmt, args...)				\
111 	_dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited,	\
112 				   fs_info, KERN_DEBUG fmt, ##args)
113 #elif defined(DEBUG)
114 #define btrfs_debug(fs_info, fmt, args...) \
115 	btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
116 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
117 	btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
118 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
119 	btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
120 #define btrfs_debug_rl(fs_info, fmt, args...) \
121 	btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
122 #else
123 #define btrfs_debug(fs_info, fmt, args...) \
124 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
125 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
126 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
127 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
128 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
129 #define btrfs_debug_rl(fs_info, fmt, args...) \
130 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
131 #endif
132 
133 #define btrfs_printk_in_rcu(fs_info, fmt, args...)	\
134 do {							\
135 	rcu_read_lock();				\
136 	btrfs_printk(fs_info, fmt, ##args);		\
137 	rcu_read_unlock();				\
138 } while (0)
139 
140 #define btrfs_no_printk_in_rcu(fs_info, fmt, args...)	\
141 do {							\
142 	rcu_read_lock();				\
143 	btrfs_no_printk(fs_info, fmt, ##args);		\
144 	rcu_read_unlock();				\
145 } while (0)
146 
147 #define btrfs_printk_ratelimited(fs_info, fmt, args...)		\
148 do {								\
149 	static DEFINE_RATELIMIT_STATE(_rs,			\
150 		DEFAULT_RATELIMIT_INTERVAL,			\
151 		DEFAULT_RATELIMIT_BURST);			\
152 	if (__ratelimit(&_rs))					\
153 		btrfs_printk(fs_info, fmt, ##args);		\
154 } while (0)
155 
156 #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...)		\
157 do {								\
158 	rcu_read_lock();					\
159 	btrfs_printk_ratelimited(fs_info, fmt, ##args);		\
160 	rcu_read_unlock();					\
161 } while (0)
162 
163 #ifdef CONFIG_BTRFS_ASSERT
164 void __cold btrfs_assertfail(const char *expr, const char *file, int line);
165 
166 #define ASSERT(expr)						\
167 	(likely(expr) ? (void)0 : btrfs_assertfail(#expr, __FILE__, __LINE__))
168 #else
169 #define ASSERT(expr)	(void)(expr)
170 #endif
171 
172 void __cold btrfs_print_v0_err(struct btrfs_fs_info *fs_info);
173 
174 __printf(5, 6)
175 __cold
176 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
177 		     unsigned int line, int errno, const char *fmt, ...);
178 
179 const char * __attribute_const__ btrfs_decode_error(int errno);
180 
181 __cold
182 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
183 			       const char *function,
184 			       unsigned int line, int errno, bool first_hit);
185 
186 bool __cold abort_should_print_stack(int errno);
187 
188 /*
189  * Call btrfs_abort_transaction as early as possible when an error condition is
190  * detected, that way the exact stack trace is reported for some errors.
191  */
192 #define btrfs_abort_transaction(trans, errno)			\
193 do {								\
194 	bool first = false;					\
195 	/* Report first abort since mount */			\
196 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,	\
197 			      &((trans)->fs_info->fs_state))) {	\
198 		first = true;					\
199 		if (WARN(abort_should_print_stack(errno),       \
200 			KERN_ERR				\
201 			"BTRFS: Transaction aborted (error %d)\n",	\
202 			(errno))) {					\
203 			/* Stack trace printed. */			\
204 		} else {						\
205 			btrfs_err((trans)->fs_info,			\
206 				  "Transaction aborted (error %d)",     \
207 				  (errno));			\
208 		}						\
209 	}							\
210 	__btrfs_abort_transaction((trans), __func__,		\
211 				  __LINE__, (errno), first);	\
212 } while (0)
213 
214 #define btrfs_handle_fs_error(fs_info, errno, fmt, args...)		\
215 	__btrfs_handle_fs_error((fs_info), __func__, __LINE__,		\
216 				(errno), fmt, ##args)
217 
218 __printf(5, 6)
219 __cold
220 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
221 		   unsigned int line, int errno, const char *fmt, ...);
222 /*
223  * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
224  * will panic().  Otherwise we BUG() here.
225  */
226 #define btrfs_panic(fs_info, errno, fmt, args...)			\
227 do {									\
228 	__btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args);	\
229 	BUG();								\
230 } while (0)
231 
232 #if BITS_PER_LONG == 32
233 #define BTRFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT)
234 /*
235  * The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical
236  * addresses of extents.
237  *
238  * For 4K page size it's about 10T, for 64K it's 160T.
239  */
240 #define BTRFS_32BIT_EARLY_WARN_THRESHOLD (BTRFS_32BIT_MAX_FILE_SIZE * 5 / 8)
241 void btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info);
242 void btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info);
243 #endif
244 
245 #endif
246