Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 :
3 : #include "fs.h"
4 : #include "messages.h"
5 : #include "discard.h"
6 : #include "transaction.h"
7 : #include "space-info.h"
8 : #include "super.h"
9 :
10 : #ifdef CONFIG_PRINTK
11 :
12 : #define STATE_STRING_PREFACE ": state "
13 : #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT)
14 :
15 : /*
16 : * Characters to print to indicate error conditions or uncommon filesystem state.
17 : * RO is not an error.
18 : */
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',
28 : };
29 :
30 0 : static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
31 : {
32 0 : unsigned int bit;
33 0 : bool states_printed = false;
34 0 : unsigned long fs_state = READ_ONCE(info->fs_state);
35 0 : char *curr = buf;
36 :
37 0 : memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
38 0 : curr += sizeof(STATE_STRING_PREFACE) - 1;
39 :
40 0 : for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
41 0 : WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
42 0 : if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
43 0 : *curr++ = fs_state_chars[bit];
44 0 : states_printed = true;
45 : }
46 : }
47 :
48 : /* If no states were printed, reset the buffer */
49 0 : if (!states_printed)
50 0 : curr = buf;
51 :
52 0 : *curr++ = 0;
53 0 : }
54 : #endif
55 :
56 : /*
57 : * Generally the error codes correspond to their respective errors, but there
58 : * are a few special cases.
59 : *
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.
64 : *
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.
70 : */
71 0 : const char * __attribute_const__ btrfs_decode_error(int errno)
72 : {
73 0 : char *errstr = "unknown";
74 :
75 0 : switch (errno) {
76 0 : case -ENOENT: /* -2 */
77 0 : errstr = "No such entry";
78 0 : break;
79 0 : case -EIO: /* -5 */
80 0 : errstr = "IO failure";
81 0 : break;
82 0 : case -ENOMEM: /* -12*/
83 0 : errstr = "Out of memory";
84 0 : break;
85 0 : case -EEXIST: /* -17 */
86 0 : errstr = "Object already exists";
87 0 : break;
88 0 : case -ENOSPC: /* -28 */
89 0 : errstr = "No space left";
90 0 : break;
91 0 : case -EROFS: /* -30 */
92 0 : errstr = "Readonly filesystem";
93 0 : break;
94 0 : case -EOPNOTSUPP: /* -95 */
95 0 : errstr = "Operation not supported";
96 0 : break;
97 0 : case -EUCLEAN: /* -117 */
98 0 : errstr = "Filesystem corrupted";
99 0 : break;
100 0 : case -EDQUOT: /* -122 */
101 0 : errstr = "Quota exceeded";
102 0 : break;
103 : }
104 :
105 0 : return errstr;
106 : }
107 :
108 : /*
109 : * __btrfs_handle_fs_error decodes expected errors from the caller and
110 : * invokes the appropriate error response.
111 : */
112 : __cold
113 0 : void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
114 : unsigned int line, int errno, const char *fmt, ...)
115 : {
116 0 : struct super_block *sb = fs_info->sb;
117 : #ifdef CONFIG_PRINTK
118 0 : char statestr[STATE_STRING_BUF_LEN];
119 0 : const char *errstr;
120 : #endif
121 :
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);
125 : #endif
126 :
127 : /*
128 : * Special case: if the error is EROFS, and we're already under
129 : * SB_RDONLY, then it is safe here.
130 : */
131 0 : if (errno == -EROFS && sb_rdonly(sb))
132 0 : return;
133 :
134 : #ifdef CONFIG_PRINTK
135 0 : errstr = btrfs_decode_error(errno);
136 0 : btrfs_state_to_string(fs_info, statestr);
137 0 : if (fmt) {
138 0 : struct va_format vaf;
139 0 : va_list args;
140 :
141 0 : va_start(args, fmt);
142 0 : vaf.fmt = fmt;
143 0 : vaf.va = &args;
144 :
145 0 : 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);
147 0 : va_end(args);
148 : } else {
149 0 : pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
150 : sb->s_id, statestr, function, line, errno, errstr);
151 : }
152 : #endif
153 :
154 : /*
155 : * Today we only save the error info to memory. Long term we'll also
156 : * send it down to the disk.
157 : */
158 0 : set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
159 :
160 : /* Don't go through full error handling during mount. */
161 0 : if (!(sb->s_flags & SB_BORN))
162 : return;
163 :
164 0 : if (sb_rdonly(sb))
165 : return;
166 :
167 0 : btrfs_discard_stop(fs_info);
168 :
169 : /* Handle error by forcing the filesystem readonly. */
170 0 : btrfs_set_sb_rdonly(sb);
171 0 : btrfs_info(fs_info, "forced readonly");
172 : /*
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.
179 : */
180 : }
181 :
182 : #ifdef CONFIG_PRINTK
183 : static const char * const logtypes[] = {
184 : "emergency",
185 : "alert",
186 : "critical",
187 : "error",
188 : "warning",
189 : "notice",
190 : "info",
191 : "debug",
192 : };
193 :
194 : /*
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.
197 : */
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),
207 : };
208 :
209 0 : void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
210 : {
211 0 : char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
212 0 : struct va_format vaf;
213 0 : va_list args;
214 0 : int kern_level;
215 0 : const char *type = logtypes[4];
216 0 : struct ratelimit_state *ratelimit = &printk_limits[4];
217 :
218 : #ifdef CONFIG_PRINTK_INDEX
219 : printk_index_subsys_emit("%sBTRFS %s (device %s): ", NULL, fmt);
220 : #endif
221 :
222 0 : va_start(args, fmt);
223 :
224 0 : while ((kern_level = printk_get_level(fmt)) != 0) {
225 0 : size_t size = printk_skip_level(fmt) - fmt;
226 :
227 0 : if (kern_level >= '0' && kern_level <= '7') {
228 0 : memcpy(lvl, fmt, size);
229 0 : lvl[size] = '\0';
230 0 : type = logtypes[kern_level - '0'];
231 0 : ratelimit = &printk_limits[kern_level - '0'];
232 : }
233 : fmt += size;
234 : }
235 :
236 0 : vaf.fmt = fmt;
237 0 : vaf.va = &args;
238 :
239 0 : if (__ratelimit(ratelimit)) {
240 0 : if (fs_info) {
241 0 : char statestr[STATE_STRING_BUF_LEN];
242 :
243 0 : btrfs_state_to_string(fs_info, statestr);
244 0 : _printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
245 0 : fs_info->sb->s_id, statestr, &vaf);
246 : } else {
247 0 : _printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
248 : }
249 : }
250 :
251 0 : va_end(args);
252 0 : }
253 : #endif
254 :
255 0 : void __cold btrfs_print_v0_err(struct btrfs_fs_info *fs_info)
256 : {
257 0 : btrfs_err(fs_info,
258 : "Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
259 0 : }
260 :
261 : #if BITS_PER_LONG == 32
262 : void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
263 : {
264 : if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
265 : btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
266 : btrfs_warn(fs_info,
267 : "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
268 : BTRFS_32BIT_MAX_FILE_SIZE >> 40);
269 : btrfs_warn(fs_info,
270 : "please consider upgrading to 64bit kernel/hardware");
271 : }
272 : }
273 :
274 : void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
275 : {
276 : if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
277 : btrfs_err(fs_info, "reached 32bit limit for logical addresses");
278 : btrfs_err(fs_info,
279 : "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
280 : BTRFS_32BIT_MAX_FILE_SIZE >> 40);
281 : btrfs_err(fs_info,
282 : "please consider upgrading to 64bit kernel/hardware");
283 : }
284 : }
285 : #endif
286 :
287 : /*
288 : * __btrfs_panic decodes unexpected, fatal errors from the caller, issues an
289 : * alert, and either panics or BUGs, depending on mount options.
290 : */
291 : __cold
292 0 : void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
293 : unsigned int line, int errno, const char *fmt, ...)
294 : {
295 0 : char *s_id = "<unknown>";
296 0 : const char *errstr;
297 0 : struct va_format vaf = { .fmt = fmt };
298 0 : va_list args;
299 :
300 0 : if (fs_info)
301 0 : s_id = fs_info->sb->s_id;
302 :
303 0 : va_start(args, fmt);
304 0 : vaf.va = &args;
305 :
306 0 : errstr = btrfs_decode_error(errno);
307 0 : if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
308 0 : panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
309 : s_id, function, line, &vaf, errno, errstr);
310 :
311 0 : btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
312 : function, line, &vaf, errno, errstr);
313 0 : va_end(args);
314 : /* Caller calls BUG() */
315 0 : }
|