messages.h 6.5 KB

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