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