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