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