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