messages.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "fs.h"
  3. #include "messages.h"
  4. #include "discard.h"
  5. #include "super.h"
  6. #ifdef CONFIG_PRINTK
  7. #define STATE_STRING_PREFACE " state "
  8. #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT + 1)
  9. /*
  10. * Characters to print to indicate error conditions or uncommon filesystem state.
  11. * RO is not an error.
  12. */
  13. static const char fs_state_chars[] = {
  14. [BTRFS_FS_STATE_REMOUNTING] = 'M',
  15. [BTRFS_FS_STATE_RO] = 0,
  16. [BTRFS_FS_STATE_TRANS_ABORTED] = 'A',
  17. [BTRFS_FS_STATE_LOG_REPLAY_ABORTED] = 'O',
  18. [BTRFS_FS_STATE_DEV_REPLACING] = 'R',
  19. [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0,
  20. [BTRFS_FS_STATE_NO_DATA_CSUMS] = 'C',
  21. [BTRFS_FS_STATE_SKIP_META_CSUMS] = 'S',
  22. [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L',
  23. [BTRFS_FS_STATE_EMERGENCY_SHUTDOWN] = 'E',
  24. };
  25. static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
  26. {
  27. unsigned int bit;
  28. bool states_printed = false;
  29. unsigned long fs_state = READ_ONCE(info->fs_state);
  30. char *curr = buf;
  31. memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
  32. curr += sizeof(STATE_STRING_PREFACE) - 1;
  33. if (BTRFS_FS_ERROR(info)) {
  34. *curr++ = 'E';
  35. states_printed = true;
  36. }
  37. for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
  38. WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
  39. if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
  40. *curr++ = fs_state_chars[bit];
  41. states_printed = true;
  42. }
  43. }
  44. /* If no states were printed, reset the buffer */
  45. if (!states_printed)
  46. curr = buf;
  47. *curr++ = 0;
  48. }
  49. #endif
  50. /*
  51. * Generally the error codes correspond to their respective errors, but there
  52. * are a few special cases.
  53. *
  54. * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for
  55. * instance will return EUCLEAN if any of the blocks are corrupted in
  56. * a way that is problematic. We want to reserve EUCLEAN for these
  57. * sort of corruptions.
  58. *
  59. * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
  60. * need to use EROFS for this case. We will have no idea of the
  61. * original failure, that will have been reported at the time we tripped
  62. * over the error. Each subsequent error that doesn't have any context
  63. * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
  64. */
  65. const char * __attribute_const__ btrfs_decode_error(int error)
  66. {
  67. char *errstr = "unknown";
  68. switch (error) {
  69. case -ENOENT: /* -2 */
  70. errstr = "No such entry";
  71. break;
  72. case -EIO: /* -5 */
  73. errstr = "IO failure";
  74. break;
  75. case -ENOMEM: /* -12*/
  76. errstr = "Out of memory";
  77. break;
  78. case -EEXIST: /* -17 */
  79. errstr = "Object already exists";
  80. break;
  81. case -ENOSPC: /* -28 */
  82. errstr = "No space left";
  83. break;
  84. case -EROFS: /* -30 */
  85. errstr = "Readonly filesystem";
  86. break;
  87. case -EOPNOTSUPP: /* -95 */
  88. errstr = "Operation not supported";
  89. break;
  90. case -EUCLEAN: /* -117 */
  91. errstr = "Filesystem corrupted";
  92. break;
  93. case -EDQUOT: /* -122 */
  94. errstr = "Quota exceeded";
  95. break;
  96. }
  97. return errstr;
  98. }
  99. /*
  100. * Decodes expected errors from the caller and invokes the appropriate error
  101. * response.
  102. */
  103. __cold
  104. void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
  105. unsigned int line, int error, const char *fmt, ...)
  106. {
  107. struct super_block *sb = fs_info->sb;
  108. #ifdef CONFIG_PRINTK
  109. char statestr[STATE_STRING_BUF_LEN];
  110. const char *errstr;
  111. #endif
  112. #ifdef CONFIG_PRINTK_INDEX
  113. printk_index_subsys_emit(
  114. "BTRFS: error (device %s%s) in %s:%d: errno=%d %s", KERN_CRIT, fmt);
  115. #endif
  116. /*
  117. * Special case: if the error is EROFS, and we're already under
  118. * SB_RDONLY, then it is safe here.
  119. */
  120. if (error == -EROFS && sb_rdonly(sb))
  121. return;
  122. #ifdef CONFIG_PRINTK
  123. errstr = btrfs_decode_error(error);
  124. btrfs_state_to_string(fs_info, statestr);
  125. if (fmt) {
  126. struct va_format vaf;
  127. va_list args;
  128. va_start(args, fmt);
  129. vaf.fmt = fmt;
  130. vaf.va = &args;
  131. pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
  132. sb->s_id, statestr, function, line, error, errstr, &vaf);
  133. va_end(args);
  134. } else {
  135. pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
  136. sb->s_id, statestr, function, line, error, errstr);
  137. }
  138. #endif
  139. /*
  140. * Today we only save the error info to memory. Long term we'll also
  141. * send it down to the disk.
  142. */
  143. WRITE_ONCE(fs_info->fs_error, error);
  144. /* Don't go through full error handling during mount. */
  145. if (!(sb->s_flags & SB_BORN))
  146. return;
  147. if (sb_rdonly(sb))
  148. return;
  149. btrfs_discard_stop(fs_info);
  150. /* Handle error by forcing the filesystem readonly. */
  151. btrfs_set_sb_rdonly(sb);
  152. btrfs_info(fs_info, "forced readonly");
  153. /*
  154. * Note that a running device replace operation is not canceled here
  155. * although there is no way to update the progress. It would add the
  156. * risk of a deadlock, therefore the canceling is omitted. The only
  157. * penalty is that some I/O remains active until the procedure
  158. * completes. The next time when the filesystem is mounted writable
  159. * again, the device replace operation continues.
  160. */
  161. }
  162. #ifdef CONFIG_PRINTK
  163. static const char * const logtypes[] = {
  164. "emergency",
  165. "alert",
  166. "critical",
  167. "error",
  168. "warning",
  169. "notice",
  170. "info",
  171. "debug",
  172. };
  173. /*
  174. * Use one ratelimit state per log level so that a flood of less important
  175. * messages doesn't cause more important ones to be dropped.
  176. */
  177. static struct ratelimit_state printk_limits[] = {
  178. RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
  179. RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
  180. RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
  181. RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
  182. RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
  183. RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
  184. RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
  185. RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
  186. };
  187. __printf(3, 4) __cold
  188. void _btrfs_printk(const struct btrfs_fs_info *fs_info, unsigned int level, const char *fmt, ...)
  189. {
  190. struct va_format vaf;
  191. va_list args;
  192. const char *type = logtypes[level];
  193. struct ratelimit_state *ratelimit = &printk_limits[level];
  194. #ifdef CONFIG_PRINTK_INDEX
  195. printk_index_subsys_emit("%sBTRFS %s (device %s): ", NULL, fmt);
  196. #endif
  197. va_start(args, fmt);
  198. vaf.fmt = fmt;
  199. vaf.va = &args;
  200. /* Do not ratelimit if CONFIG_BTRFS_DEBUG is enabled. */
  201. if (IS_ENABLED(CONFIG_BTRFS_DEBUG) || __ratelimit(ratelimit)) {
  202. if (fs_info) {
  203. char statestr[STATE_STRING_BUF_LEN];
  204. btrfs_state_to_string(fs_info, statestr);
  205. _printk(KERN_SOH "%dBTRFS %s (device %s%s): %pV\n", level, type,
  206. fs_info->sb->s_id, statestr, &vaf);
  207. } else {
  208. _printk(KERN_SOH "%dBTRFS %s: %pV\n", level, type, &vaf);
  209. }
  210. }
  211. va_end(args);
  212. }
  213. #endif
  214. #if BITS_PER_LONG == 32
  215. void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
  216. {
  217. if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
  218. btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
  219. btrfs_warn(fs_info,
  220. "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
  221. BTRFS_32BIT_MAX_FILE_SIZE >> 40);
  222. btrfs_warn(fs_info,
  223. "please consider upgrading to 64bit kernel/hardware");
  224. }
  225. }
  226. void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
  227. {
  228. if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
  229. btrfs_err(fs_info, "reached 32bit limit for logical addresses");
  230. btrfs_err(fs_info,
  231. "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
  232. BTRFS_32BIT_MAX_FILE_SIZE >> 40);
  233. btrfs_err(fs_info,
  234. "please consider upgrading to 64bit kernel/hardware");
  235. }
  236. }
  237. #endif
  238. /*
  239. * Decode unexpected, fatal errors from the caller, issue an alert, and either
  240. * panic or BUGs, depending on mount options.
  241. */
  242. __cold
  243. void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function,
  244. unsigned int line, int error, const char *fmt, ...)
  245. {
  246. char *s_id = "<unknown>";
  247. const char *errstr;
  248. struct va_format vaf = { .fmt = fmt };
  249. va_list args;
  250. if (fs_info)
  251. s_id = fs_info->sb->s_id;
  252. va_start(args, fmt);
  253. vaf.va = &args;
  254. errstr = btrfs_decode_error(error);
  255. if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
  256. panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
  257. s_id, function, line, &vaf, error, errstr);
  258. btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
  259. function, line, &vaf, error, errstr);
  260. va_end(args);
  261. /* Caller calls BUG() */
  262. }