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