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 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 __printf(3, 4) __cold 27 void _btrfs_printk(const struct btrfs_fs_info *fs_info, unsigned int level, const char *fmt, ...); 28 29 #else 30 31 #define btrfs_printk_in_rcu(fs_info, level, fmt, args...) \ 32 btrfs_no_printk(fs_info, fmt, ##args) 33 34 #define btrfs_printk_rl_in_rcu(fs_info, level, fmt, args...) \ 35 btrfs_no_printk(fs_info, fmt, ##args) 36 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, LOGLEVEL_CRIT, fmt, ##args) 44 #define btrfs_err(fs_info, fmt, args...) \ 45 btrfs_printk_in_rcu(fs_info, LOGLEVEL_ERR, fmt, ##args) 46 #define btrfs_warn(fs_info, fmt, args...) \ 47 btrfs_printk_in_rcu(fs_info, LOGLEVEL_WARNING, fmt, ##args) 48 #define btrfs_info(fs_info, fmt, args...) \ 49 btrfs_printk_in_rcu(fs_info, LOGLEVEL_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, LOGLEVEL_CRIT, fmt, ##args) 56 #define btrfs_err_rl(fs_info, fmt, args...) \ 57 btrfs_printk_rl_in_rcu(fs_info, LOGLEVEL_ERR, fmt, ##args) 58 #define btrfs_warn_rl(fs_info, fmt, args...) \ 59 btrfs_printk_rl_in_rcu(fs_info, LOGLEVEL_WARNING, fmt, ##args) 60 #define btrfs_info_rl(fs_info, fmt, args...) \ 61 btrfs_printk_rl_in_rcu(fs_info, LOGLEVEL_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, LOGLEVEL_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, LOGLEVEL_DEBUG, fmt, ##args) 70 #elif defined(DEBUG) 71 #define btrfs_debug(fs_info, fmt, args...) \ 72 btrfs_printk_in_rcu(fs_info, LOGLEVEL_DEBUG, fmt, ##args) 73 #define btrfs_debug_rl(fs_info, fmt, args...) \ 74 btrfs_printk_rl_in_rcu(fs_info, LOGLEVEl_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 #ifdef CONFIG_PRINTK 82 83 #define btrfs_printk_in_rcu(fs_info, level, fmt, args...) \ 84 do { \ 85 rcu_read_lock(); \ 86 _btrfs_printk(fs_info, level, fmt, ##args); \ 87 rcu_read_unlock(); \ 88 } while (0) 89 90 #define btrfs_printk_rl_in_rcu(fs_info, level, fmt, args...) \ 91 do { \ 92 static DEFINE_RATELIMIT_STATE(_rs, \ 93 DEFAULT_RATELIMIT_INTERVAL, \ 94 DEFAULT_RATELIMIT_BURST); \ 95 \ 96 rcu_read_lock(); \ 97 if (__ratelimit(&_rs)) \ 98 _btrfs_printk(fs_info, level, fmt, ##args); \ 99 rcu_read_unlock(); \ 100 } while (0) 101 102 #endif 103 104 #ifdef CONFIG_BTRFS_ASSERT 105 106 __printf(1, 2) 107 static inline void verify_assert_printk_format(const char *fmt, ...) { 108 /* Stub to verify the assertion format string. */ 109 } 110 111 /* Take the first token if any. */ 112 #define __FIRST_ARG(_, ...) _ 113 /* 114 * Skip the first token and return the rest, if it's empty the comma is dropped. 115 * As ##__VA_ARGS__ cannot be at the beginning of the macro the __VA_OPT__ is needed 116 * and supported since GCC 8 and Clang 12. 117 */ 118 #define __REST_ARGS(_, ... ) __VA_OPT__(,) __VA_ARGS__ 119 120 /* 121 * Assertion with optional printk() format. 122 * 123 * Accepted syntax: 124 * ASSERT(condition); 125 * ASSERT(condition, "string"); 126 * ASSERT(condition, "variable=%d", variable); 127 * 128 * How it works: 129 * - if there's no format string, ""[0] evaluates at compile time to 0 and the 130 * true branch is executed 131 * - any non-empty format string with the "" prefix evaluates to != 0 at 132 * compile time and the false branch is executed 133 * - stringified condition is printed as %s so we don't accidentally mix format 134 * strings (the % operator) 135 * - there can be only one printk() call, so the format strings and arguments are 136 * spliced together: 137 * DEFAULT_FMT [USER_FMT], DEFAULT_ARGS [, USER_ARGS] 138 * - comma between DEFAULT_ARGS and USER_ARGS is handled by preprocessor 139 * (requires __VA_OPT__ support) 140 * - otherwise we could use __VA_OPT(,) __VA_ARGS__ for the 2nd+ argument of args, 141 */ 142 #define ASSERT(cond, args...) \ 143 do { \ 144 verify_assert_printk_format("check the format string" args); \ 145 if (!likely(cond)) { \ 146 if (("" __FIRST_ARG(args) [0]) == 0) { \ 147 pr_err("assertion failed: %s, in %s:%d\n", \ 148 #cond, __FILE__, __LINE__); \ 149 } else { \ 150 pr_err("assertion failed: %s, in %s:%d (" __FIRST_ARG(args) ")\n", \ 151 #cond, __FILE__, __LINE__ __REST_ARGS(args)); \ 152 } \ 153 BUG(); \ 154 } \ 155 } while(0) 156 157 #else 158 /* Compile check the @cond expression but don't generate any code. */ 159 #define ASSERT(cond, args...) BUILD_BUG_ON_INVALID(cond) 160 #endif 161 162 #ifdef CONFIG_BTRFS_DEBUG 163 /* Verbose warning only under debug build. */ 164 #define DEBUG_WARN(args...) WARN(1, KERN_ERR args) 165 #else 166 #define DEBUG_WARN(...) do {} while(0) 167 #endif 168 169 __printf(5, 6) 170 __cold 171 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 172 unsigned int line, int error, const char *fmt, ...); 173 174 const char * __attribute_const__ btrfs_decode_error(int error); 175 176 #define btrfs_handle_fs_error(fs_info, error, fmt, args...) \ 177 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \ 178 (error), fmt, ##args) 179 180 __printf(5, 6) 181 __cold 182 void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function, 183 unsigned int line, int error, const char *fmt, ...); 184 /* 185 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic 186 * will panic(). Otherwise we BUG() here. 187 */ 188 #define btrfs_panic(fs_info, error, fmt, args...) \ 189 do { \ 190 __btrfs_panic(fs_info, __func__, __LINE__, error, fmt, ##args); \ 191 BUG(); \ 192 } while (0) 193 194 #if BITS_PER_LONG == 32 195 #define BTRFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT) 196 /* 197 * The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical 198 * addresses of extents. 199 * 200 * For 4K page size it's about 10T, for 64K it's 160T. 201 */ 202 #define BTRFS_32BIT_EARLY_WARN_THRESHOLD (BTRFS_32BIT_MAX_FILE_SIZE * 5 / 8) 203 void btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info); 204 void btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info); 205 #endif 206 207 #endif 208