xref: /openbmc/linux/fs/btrfs/messages.h (revision 182741d2)
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 #define btrfs_emerg(fs_info, fmt, args...) \
40 	btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
41 #define btrfs_alert(fs_info, fmt, args...) \
42 	btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
43 #define btrfs_crit(fs_info, fmt, args...) \
44 	btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
45 #define btrfs_err(fs_info, fmt, args...) \
46 	btrfs_printk(fs_info, KERN_ERR fmt, ##args)
47 #define btrfs_warn(fs_info, fmt, args...) \
48 	btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
49 #define btrfs_notice(fs_info, fmt, args...) \
50 	btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
51 #define btrfs_info(fs_info, fmt, args...) \
52 	btrfs_printk(fs_info, KERN_INFO fmt, ##args)
53 
54 /*
55  * Wrappers that use printk_in_rcu
56  */
57 #define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
58 	btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
59 #define btrfs_alert_in_rcu(fs_info, fmt, args...) \
60 	btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
61 #define btrfs_crit_in_rcu(fs_info, fmt, args...) \
62 	btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
63 #define btrfs_err_in_rcu(fs_info, fmt, args...) \
64 	btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
65 #define btrfs_warn_in_rcu(fs_info, fmt, args...) \
66 	btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
67 #define btrfs_notice_in_rcu(fs_info, fmt, args...) \
68 	btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
69 #define btrfs_info_in_rcu(fs_info, fmt, args...) \
70 	btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
71 
72 /*
73  * Wrappers that use a ratelimited printk_in_rcu
74  */
75 #define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
76 	btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
77 #define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
78 	btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
79 #define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
80 	btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
81 #define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
82 	btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
83 #define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
84 	btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
85 #define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
86 	btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
87 #define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
88 	btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
89 
90 /*
91  * Wrappers that use a ratelimited printk
92  */
93 #define btrfs_emerg_rl(fs_info, fmt, args...) \
94 	btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
95 #define btrfs_alert_rl(fs_info, fmt, args...) \
96 	btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
97 #define btrfs_crit_rl(fs_info, fmt, args...) \
98 	btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
99 #define btrfs_err_rl(fs_info, fmt, args...) \
100 	btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
101 #define btrfs_warn_rl(fs_info, fmt, args...) \
102 	btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
103 #define btrfs_notice_rl(fs_info, fmt, args...) \
104 	btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
105 #define btrfs_info_rl(fs_info, fmt, args...) \
106 	btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
107 
108 #if defined(CONFIG_DYNAMIC_DEBUG)
109 #define btrfs_debug(fs_info, fmt, args...)				\
110 	_dynamic_func_call_no_desc(fmt, btrfs_printk,			\
111 				   fs_info, KERN_DEBUG fmt, ##args)
112 #define btrfs_debug_in_rcu(fs_info, fmt, args...)			\
113 	_dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu,		\
114 				   fs_info, KERN_DEBUG fmt, ##args)
115 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...)			\
116 	_dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu,		\
117 				   fs_info, KERN_DEBUG fmt, ##args)
118 #define btrfs_debug_rl(fs_info, fmt, args...)				\
119 	_dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited,	\
120 				   fs_info, KERN_DEBUG fmt, ##args)
121 #elif defined(DEBUG)
122 #define btrfs_debug(fs_info, fmt, args...) \
123 	btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
124 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
125 	btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
126 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
127 	btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
128 #define btrfs_debug_rl(fs_info, fmt, args...) \
129 	btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
130 #else
131 #define btrfs_debug(fs_info, fmt, args...) \
132 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
133 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
134 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
135 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
136 	btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
137 #define btrfs_debug_rl(fs_info, fmt, args...) \
138 	btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
139 #endif
140 
141 #define btrfs_printk_in_rcu(fs_info, fmt, args...)	\
142 do {							\
143 	rcu_read_lock();				\
144 	btrfs_printk(fs_info, fmt, ##args);		\
145 	rcu_read_unlock();				\
146 } while (0)
147 
148 #define btrfs_no_printk_in_rcu(fs_info, fmt, args...)	\
149 do {							\
150 	rcu_read_lock();				\
151 	btrfs_no_printk(fs_info, fmt, ##args);		\
152 	rcu_read_unlock();				\
153 } while (0)
154 
155 #define btrfs_printk_ratelimited(fs_info, fmt, args...)		\
156 do {								\
157 	static DEFINE_RATELIMIT_STATE(_rs,			\
158 		DEFAULT_RATELIMIT_INTERVAL,			\
159 		DEFAULT_RATELIMIT_BURST);			\
160 	if (__ratelimit(&_rs))					\
161 		btrfs_printk(fs_info, fmt, ##args);		\
162 } while (0)
163 
164 #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...)		\
165 do {								\
166 	rcu_read_lock();					\
167 	btrfs_printk_ratelimited(fs_info, fmt, ##args);		\
168 	rcu_read_unlock();					\
169 } while (0)
170 
171 #ifdef CONFIG_BTRFS_ASSERT
172 
173 #define btrfs_assertfail(expr, file, line)	({				\
174 	pr_err("assertion failed: %s, in %s:%d\n", (expr), (file), (line));	\
175 	BUG();								\
176 })
177 
178 #define ASSERT(expr)						\
179 	(likely(expr) ? (void)0 : btrfs_assertfail(#expr, __FILE__, __LINE__))
180 #else
181 #define ASSERT(expr)	(void)(expr)
182 #endif
183 
184 __printf(5, 6)
185 __cold
186 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
187 		     unsigned int line, int errno, const char *fmt, ...);
188 
189 const char * __attribute_const__ btrfs_decode_error(int errno);
190 
191 #define btrfs_handle_fs_error(fs_info, errno, fmt, args...)		\
192 	__btrfs_handle_fs_error((fs_info), __func__, __LINE__,		\
193 				(errno), fmt, ##args)
194 
195 __printf(5, 6)
196 __cold
197 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
198 		   unsigned int line, int errno, const char *fmt, ...);
199 /*
200  * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
201  * will panic().  Otherwise we BUG() here.
202  */
203 #define btrfs_panic(fs_info, errno, fmt, args...)			\
204 do {									\
205 	__btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args);	\
206 	BUG();								\
207 } while (0)
208 
209 #if BITS_PER_LONG == 32
210 #define BTRFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT)
211 /*
212  * The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical
213  * addresses of extents.
214  *
215  * For 4K page size it's about 10T, for 64K it's 160T.
216  */
217 #define BTRFS_32BIT_EARLY_WARN_THRESHOLD (BTRFS_32BIT_MAX_FILE_SIZE * 5 / 8)
218 void btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info);
219 void btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info);
220 #endif
221 
222 #endif
223