1 // SPDX-License-Identifier: GPL-2.0
6 #include "transaction.h"
7 #include "space-info.h"
12 #define STATE_STRING_PREFACE ": state "
13 #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT)
16 * Characters to print to indicate error conditions or uncommon filesystem state.
19 static const char fs_state_chars[] = {
20 [BTRFS_FS_STATE_ERROR] = 'E',
21 [BTRFS_FS_STATE_REMOUNTING] = 'M',
22 [BTRFS_FS_STATE_RO] = 0,
23 [BTRFS_FS_STATE_TRANS_ABORTED] = 'A',
24 [BTRFS_FS_STATE_DEV_REPLACING] = 'R',
25 [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0,
26 [BTRFS_FS_STATE_NO_CSUMS] = 'C',
27 [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L',
30 static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
33 bool states_printed = false;
34 unsigned long fs_state = READ_ONCE(info->fs_state);
37 memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
38 curr += sizeof(STATE_STRING_PREFACE) - 1;
40 for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
41 WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
42 if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
43 *curr++ = fs_state_chars[bit];
44 states_printed = true;
48 /* If no states were printed, reset the buffer */
57 * Generally the error codes correspond to their respective errors, but there
58 * are a few special cases.
60 * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for
61 * instance will return EUCLEAN if any of the blocks are corrupted in
62 * a way that is problematic. We want to reserve EUCLEAN for these
63 * sort of corruptions.
65 * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
66 * need to use EROFS for this case. We will have no idea of the
67 * original failure, that will have been reported at the time we tripped
68 * over the error. Each subsequent error that doesn't have any context
69 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
71 const char * __attribute_const__ btrfs_decode_error(int errno)
73 char *errstr = "unknown";
76 case -ENOENT: /* -2 */
77 errstr = "No such entry";
80 errstr = "IO failure";
82 case -ENOMEM: /* -12*/
83 errstr = "Out of memory";
85 case -EEXIST: /* -17 */
86 errstr = "Object already exists";
88 case -ENOSPC: /* -28 */
89 errstr = "No space left";
91 case -EROFS: /* -30 */
92 errstr = "Readonly filesystem";
94 case -EOPNOTSUPP: /* -95 */
95 errstr = "Operation not supported";
97 case -EUCLEAN: /* -117 */
98 errstr = "Filesystem corrupted";
100 case -EDQUOT: /* -122 */
101 errstr = "Quota exceeded";
109 * __btrfs_handle_fs_error decodes expected errors from the caller and
110 * invokes the appropriate error response.
113 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
114 unsigned int line, int errno, const char *fmt, ...)
116 struct super_block *sb = fs_info->sb;
118 char statestr[STATE_STRING_BUF_LEN];
122 #ifdef CONFIG_PRINTK_INDEX
123 printk_index_subsys_emit(
124 "BTRFS: error (device %s%s) in %s:%d: errno=%d %s", KERN_CRIT, fmt);
128 * Special case: if the error is EROFS, and we're already under
129 * SB_RDONLY, then it is safe here.
131 if (errno == -EROFS && sb_rdonly(sb))
135 errstr = btrfs_decode_error(errno);
136 btrfs_state_to_string(fs_info, statestr);
138 struct va_format vaf;
145 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
146 sb->s_id, statestr, function, line, errno, errstr, &vaf);
149 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
150 sb->s_id, statestr, function, line, errno, errstr);
155 * Today we only save the error info to memory. Long term we'll also
156 * send it down to the disk.
158 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
160 /* Don't go through full error handling during mount. */
161 if (!(sb->s_flags & SB_BORN))
167 btrfs_discard_stop(fs_info);
169 /* Handle error by forcing the filesystem readonly. */
170 btrfs_set_sb_rdonly(sb);
171 btrfs_info(fs_info, "forced readonly");
173 * Note that a running device replace operation is not canceled here
174 * although there is no way to update the progress. It would add the
175 * risk of a deadlock, therefore the canceling is omitted. The only
176 * penalty is that some I/O remains active until the procedure
177 * completes. The next time when the filesystem is mounted writable
178 * again, the device replace operation continues.
183 static const char * const logtypes[] = {
195 * Use one ratelimit state per log level so that a flood of less important
196 * messages doesn't cause more important ones to be dropped.
198 static struct ratelimit_state printk_limits[] = {
199 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
200 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
201 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
202 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
203 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
204 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
205 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
206 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
209 void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
211 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
212 struct va_format vaf;
215 const char *type = logtypes[4];
216 struct ratelimit_state *ratelimit = &printk_limits[4];
218 #ifdef CONFIG_PRINTK_INDEX
219 printk_index_subsys_emit("%sBTRFS %s (device %s): ", NULL, fmt);
224 while ((kern_level = printk_get_level(fmt)) != 0) {
225 size_t size = printk_skip_level(fmt) - fmt;
227 if (kern_level >= '0' && kern_level <= '7') {
228 memcpy(lvl, fmt, size);
230 type = logtypes[kern_level - '0'];
231 ratelimit = &printk_limits[kern_level - '0'];
239 if (__ratelimit(ratelimit)) {
241 char statestr[STATE_STRING_BUF_LEN];
243 btrfs_state_to_string(fs_info, statestr);
244 _printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
245 fs_info->sb->s_id, statestr, &vaf);
247 _printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
255 #ifdef CONFIG_BTRFS_ASSERT
256 void __cold btrfs_assertfail(const char *expr, const char *file, int line)
258 pr_err("assertion failed: %s, in %s:%d\n", expr, file, line);
263 void __cold btrfs_print_v0_err(struct btrfs_fs_info *fs_info)
266 "Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
269 #if BITS_PER_LONG == 32
270 void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
272 if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
273 btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
275 "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
276 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
278 "please consider upgrading to 64bit kernel/hardware");
282 void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
284 if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
285 btrfs_err(fs_info, "reached 32bit limit for logical addresses");
287 "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
288 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
290 "please consider upgrading to 64bit kernel/hardware");
296 * We only mark the transaction aborted and then set the file system read-only.
297 * This will prevent new transactions from starting or trying to join this
300 * This means that error recovery at the call site is limited to freeing
301 * any local memory allocations and passing the error code up without
302 * further cleanup. The transaction should complete as it normally would
303 * in the call path but will return -EIO.
305 * We'll complete the cleanup in btrfs_end_transaction and
306 * btrfs_commit_transaction.
309 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
310 const char *function,
311 unsigned int line, int errno, bool first_hit)
313 struct btrfs_fs_info *fs_info = trans->fs_info;
315 WRITE_ONCE(trans->aborted, errno);
316 WRITE_ONCE(trans->transaction->aborted, errno);
317 if (first_hit && errno == -ENOSPC)
318 btrfs_dump_space_info_for_trans_abort(fs_info);
319 /* Wake up anybody who may be waiting on this transaction */
320 wake_up(&fs_info->transaction_wait);
321 wake_up(&fs_info->transaction_blocked_wait);
322 __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
326 * __btrfs_panic decodes unexpected, fatal errors from the caller, issues an
327 * alert, and either panics or BUGs, depending on mount options.
330 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
331 unsigned int line, int errno, const char *fmt, ...)
333 char *s_id = "<unknown>";
335 struct va_format vaf = { .fmt = fmt };
339 s_id = fs_info->sb->s_id;
344 errstr = btrfs_decode_error(errno);
345 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
346 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
347 s_id, function, line, &vaf, errno, errstr);
349 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
350 function, line, &vaf, errno, errstr);
352 /* Caller calls BUG() */