messages.c 8.6 KB

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