xfs_error.c 12.8 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
3 4
 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
5 6
 */
#include "xfs.h"
7
#include "xfs_shared.h"
8
#include "xfs_format.h"
9
#include "xfs_fs.h"
10 11
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
L
Linus Torvalds 已提交
12
#include "xfs_mount.h"
13
#include "xfs_errortag.h"
L
Linus Torvalds 已提交
14
#include "xfs_error.h"
15
#include "xfs_sysfs.h"
16
#include "xfs_inode.h"
L
Linus Torvalds 已提交
17 18 19

#ifdef DEBUG

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
static unsigned int xfs_errortag_random_default[] = {
	XFS_RANDOM_DEFAULT,
	XFS_RANDOM_IFLUSH_1,
	XFS_RANDOM_IFLUSH_2,
	XFS_RANDOM_IFLUSH_3,
	XFS_RANDOM_IFLUSH_4,
	XFS_RANDOM_IFLUSH_5,
	XFS_RANDOM_IFLUSH_6,
	XFS_RANDOM_DA_READ_BUF,
	XFS_RANDOM_BTREE_CHECK_LBLOCK,
	XFS_RANDOM_BTREE_CHECK_SBLOCK,
	XFS_RANDOM_ALLOC_READ_AGF,
	XFS_RANDOM_IALLOC_READ_AGI,
	XFS_RANDOM_ITOBP_INOTOBP,
	XFS_RANDOM_IUNLINK,
	XFS_RANDOM_IUNLINK_REMOVE,
	XFS_RANDOM_DIR_INO_VALIDATE,
	XFS_RANDOM_BULKSTAT_READ_CHUNK,
	XFS_RANDOM_IODONE_IOERR,
	XFS_RANDOM_STRATREAD_IOERR,
	XFS_RANDOM_STRATCMPL_IOERR,
	XFS_RANDOM_DIOWRITE_IOERR,
	XFS_RANDOM_BMAPIFORMAT,
	XFS_RANDOM_FREE_EXTENT,
	XFS_RANDOM_RMAP_FINISH_ONE,
	XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE,
	XFS_RANDOM_REFCOUNT_FINISH_ONE,
	XFS_RANDOM_BMAP_FINISH_ONE,
	XFS_RANDOM_AG_RESV_CRITICAL,
49
	XFS_RANDOM_DROP_WRITES,
50
	XFS_RANDOM_LOG_BAD_CRC,
51
	XFS_RANDOM_LOG_ITEM_PIN,
52
	XFS_RANDOM_BUF_LRU_REF,
53
	XFS_RANDOM_FORCE_SCRUB_REPAIR,
54
	XFS_RANDOM_FORCE_SUMMARY_RECALC,
55
	XFS_RANDOM_IUNLINK_FALLBACK,
56
	XFS_RANDOM_BUF_IOERROR,
57
	XFS_RANDOM_REDUCE_MAX_IEXTENTS,
58
	XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT,
59
	XFS_RANDOM_AG_RESV_FAIL,
60
	XFS_RANDOM_LARP,
61
	XFS_RANDOM_DA_LEAF_SPLIT,
62
};
L
Linus Torvalds 已提交
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
struct xfs_errortag_attr {
	struct attribute	attr;
	unsigned int		tag;
};

static inline struct xfs_errortag_attr *
to_attr(struct attribute *attr)
{
	return container_of(attr, struct xfs_errortag_attr, attr);
}

static inline struct xfs_mount *
to_mp(struct kobject *kobject)
{
	struct xfs_kobj *kobj = to_kobj(kobject);

	return container_of(kobj, struct xfs_mount, m_errortag_kobj);
}

STATIC ssize_t
xfs_errortag_attr_store(
	struct kobject		*kobject,
	struct attribute	*attr,
	const char		*buf,
	size_t			count)
{
	struct xfs_mount	*mp = to_mp(kobject);
	struct xfs_errortag_attr *xfs_attr = to_attr(attr);
	int			ret;
	unsigned int		val;

	if (strcmp(buf, "default") == 0) {
		val = xfs_errortag_random_default[xfs_attr->tag];
	} else {
		ret = kstrtouint(buf, 0, &val);
		if (ret)
			return ret;
	}

	ret = xfs_errortag_set(mp, xfs_attr->tag, val);
	if (ret)
		return ret;
	return count;
}

STATIC ssize_t
xfs_errortag_attr_show(
	struct kobject		*kobject,
	struct attribute	*attr,
	char			*buf)
{
	struct xfs_mount	*mp = to_mp(kobject);
	struct xfs_errortag_attr *xfs_attr = to_attr(attr);

	return snprintf(buf, PAGE_SIZE, "%u\n",
			xfs_errortag_get(mp, xfs_attr->tag));
}

static const struct sysfs_ops xfs_errortag_sysfs_ops = {
	.show = xfs_errortag_attr_show,
	.store = xfs_errortag_attr_store,
};

#define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
static struct xfs_errortag_attr xfs_errortag_attr_##_name = {		\
	.attr = {.name = __stringify(_name),				\
		 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) },	\
	.tag	= (_tag),						\
}

#define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr

XFS_ERRORTAG_ATTR_RW(noerror,		XFS_ERRTAG_NOERROR);
XFS_ERRORTAG_ATTR_RW(iflush1,		XFS_ERRTAG_IFLUSH_1);
XFS_ERRORTAG_ATTR_RW(iflush2,		XFS_ERRTAG_IFLUSH_2);
XFS_ERRORTAG_ATTR_RW(iflush3,		XFS_ERRTAG_IFLUSH_3);
XFS_ERRORTAG_ATTR_RW(iflush4,		XFS_ERRTAG_IFLUSH_4);
XFS_ERRORTAG_ATTR_RW(iflush5,		XFS_ERRTAG_IFLUSH_5);
XFS_ERRORTAG_ATTR_RW(iflush6,		XFS_ERRTAG_IFLUSH_6);
XFS_ERRORTAG_ATTR_RW(dareadbuf,		XFS_ERRTAG_DA_READ_BUF);
XFS_ERRORTAG_ATTR_RW(btree_chk_lblk,	XFS_ERRTAG_BTREE_CHECK_LBLOCK);
XFS_ERRORTAG_ATTR_RW(btree_chk_sblk,	XFS_ERRTAG_BTREE_CHECK_SBLOCK);
XFS_ERRORTAG_ATTR_RW(readagf,		XFS_ERRTAG_ALLOC_READ_AGF);
XFS_ERRORTAG_ATTR_RW(readagi,		XFS_ERRTAG_IALLOC_READ_AGI);
XFS_ERRORTAG_ATTR_RW(itobp,		XFS_ERRTAG_ITOBP_INOTOBP);
XFS_ERRORTAG_ATTR_RW(iunlink,		XFS_ERRTAG_IUNLINK);
XFS_ERRORTAG_ATTR_RW(iunlinkrm,		XFS_ERRTAG_IUNLINK_REMOVE);
XFS_ERRORTAG_ATTR_RW(dirinovalid,	XFS_ERRTAG_DIR_INO_VALIDATE);
XFS_ERRORTAG_ATTR_RW(bulkstat,		XFS_ERRTAG_BULKSTAT_READ_CHUNK);
XFS_ERRORTAG_ATTR_RW(logiodone,		XFS_ERRTAG_IODONE_IOERR);
XFS_ERRORTAG_ATTR_RW(stratread,		XFS_ERRTAG_STRATREAD_IOERR);
XFS_ERRORTAG_ATTR_RW(stratcmpl,		XFS_ERRTAG_STRATCMPL_IOERR);
XFS_ERRORTAG_ATTR_RW(diowrite,		XFS_ERRTAG_DIOWRITE_IOERR);
XFS_ERRORTAG_ATTR_RW(bmapifmt,		XFS_ERRTAG_BMAPIFORMAT);
XFS_ERRORTAG_ATTR_RW(free_extent,	XFS_ERRTAG_FREE_EXTENT);
XFS_ERRORTAG_ATTR_RW(rmap_finish_one,	XFS_ERRTAG_RMAP_FINISH_ONE);
XFS_ERRORTAG_ATTR_RW(refcount_continue_update,	XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE);
XFS_ERRORTAG_ATTR_RW(refcount_finish_one,	XFS_ERRTAG_REFCOUNT_FINISH_ONE);
XFS_ERRORTAG_ATTR_RW(bmap_finish_one,	XFS_ERRTAG_BMAP_FINISH_ONE);
XFS_ERRORTAG_ATTR_RW(ag_resv_critical,	XFS_ERRTAG_AG_RESV_CRITICAL);
164
XFS_ERRORTAG_ATTR_RW(drop_writes,	XFS_ERRTAG_DROP_WRITES);
165
XFS_ERRORTAG_ATTR_RW(log_bad_crc,	XFS_ERRTAG_LOG_BAD_CRC);
166
XFS_ERRORTAG_ATTR_RW(log_item_pin,	XFS_ERRTAG_LOG_ITEM_PIN);
167
XFS_ERRORTAG_ATTR_RW(buf_lru_ref,	XFS_ERRTAG_BUF_LRU_REF);
168
XFS_ERRORTAG_ATTR_RW(force_repair,	XFS_ERRTAG_FORCE_SCRUB_REPAIR);
169
XFS_ERRORTAG_ATTR_RW(bad_summary,	XFS_ERRTAG_FORCE_SUMMARY_RECALC);
170
XFS_ERRORTAG_ATTR_RW(iunlink_fallback,	XFS_ERRTAG_IUNLINK_FALLBACK);
171
XFS_ERRORTAG_ATTR_RW(buf_ioerror,	XFS_ERRTAG_BUF_IOERROR);
172
XFS_ERRORTAG_ATTR_RW(reduce_max_iextents,	XFS_ERRTAG_REDUCE_MAX_IEXTENTS);
173
XFS_ERRORTAG_ATTR_RW(bmap_alloc_minlen_extent,	XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT);
174
XFS_ERRORTAG_ATTR_RW(ag_resv_fail, XFS_ERRTAG_AG_RESV_FAIL);
175
XFS_ERRORTAG_ATTR_RW(larp,		XFS_ERRTAG_LARP);
176
XFS_ERRORTAG_ATTR_RW(da_leaf_split,	XFS_ERRTAG_DA_LEAF_SPLIT);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

static struct attribute *xfs_errortag_attrs[] = {
	XFS_ERRORTAG_ATTR_LIST(noerror),
	XFS_ERRORTAG_ATTR_LIST(iflush1),
	XFS_ERRORTAG_ATTR_LIST(iflush2),
	XFS_ERRORTAG_ATTR_LIST(iflush3),
	XFS_ERRORTAG_ATTR_LIST(iflush4),
	XFS_ERRORTAG_ATTR_LIST(iflush5),
	XFS_ERRORTAG_ATTR_LIST(iflush6),
	XFS_ERRORTAG_ATTR_LIST(dareadbuf),
	XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk),
	XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk),
	XFS_ERRORTAG_ATTR_LIST(readagf),
	XFS_ERRORTAG_ATTR_LIST(readagi),
	XFS_ERRORTAG_ATTR_LIST(itobp),
	XFS_ERRORTAG_ATTR_LIST(iunlink),
	XFS_ERRORTAG_ATTR_LIST(iunlinkrm),
	XFS_ERRORTAG_ATTR_LIST(dirinovalid),
	XFS_ERRORTAG_ATTR_LIST(bulkstat),
	XFS_ERRORTAG_ATTR_LIST(logiodone),
	XFS_ERRORTAG_ATTR_LIST(stratread),
	XFS_ERRORTAG_ATTR_LIST(stratcmpl),
	XFS_ERRORTAG_ATTR_LIST(diowrite),
	XFS_ERRORTAG_ATTR_LIST(bmapifmt),
	XFS_ERRORTAG_ATTR_LIST(free_extent),
	XFS_ERRORTAG_ATTR_LIST(rmap_finish_one),
	XFS_ERRORTAG_ATTR_LIST(refcount_continue_update),
	XFS_ERRORTAG_ATTR_LIST(refcount_finish_one),
	XFS_ERRORTAG_ATTR_LIST(bmap_finish_one),
	XFS_ERRORTAG_ATTR_LIST(ag_resv_critical),
207
	XFS_ERRORTAG_ATTR_LIST(drop_writes),
208
	XFS_ERRORTAG_ATTR_LIST(log_bad_crc),
209
	XFS_ERRORTAG_ATTR_LIST(log_item_pin),
210
	XFS_ERRORTAG_ATTR_LIST(buf_lru_ref),
211
	XFS_ERRORTAG_ATTR_LIST(force_repair),
212
	XFS_ERRORTAG_ATTR_LIST(bad_summary),
213
	XFS_ERRORTAG_ATTR_LIST(iunlink_fallback),
214
	XFS_ERRORTAG_ATTR_LIST(buf_ioerror),
215
	XFS_ERRORTAG_ATTR_LIST(reduce_max_iextents),
216
	XFS_ERRORTAG_ATTR_LIST(bmap_alloc_minlen_extent),
217
	XFS_ERRORTAG_ATTR_LIST(ag_resv_fail),
218
	XFS_ERRORTAG_ATTR_LIST(larp),
219
	XFS_ERRORTAG_ATTR_LIST(da_leaf_split),
220 221
	NULL,
};
222
ATTRIBUTE_GROUPS(xfs_errortag);
223

224
static struct kobj_type xfs_errortag_ktype = {
225 226
	.release = xfs_sysfs_release,
	.sysfs_ops = &xfs_errortag_sysfs_ops,
227
	.default_groups = xfs_errortag_groups,
228 229
};

L
Linus Torvalds 已提交
230
int
231 232
xfs_errortag_init(
	struct xfs_mount	*mp)
L
Linus Torvalds 已提交
233
{
234
	mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
235
			KM_MAYFAIL);
236 237
	if (!mp->m_errortag)
		return -ENOMEM;
238 239 240

	return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
			       &mp->m_kobj, "errortag");
241
}
L
Linus Torvalds 已提交
242

243 244 245 246
void
xfs_errortag_del(
	struct xfs_mount	*mp)
{
247
	xfs_sysfs_del(&mp->m_errortag_kobj);
248 249
	kmem_free(mp->m_errortag);
}
L
Linus Torvalds 已提交
250

251 252 253 254 255 256 257 258 259
bool
xfs_errortag_test(
	struct xfs_mount	*mp,
	const char		*expression,
	const char		*file,
	int			line,
	unsigned int		error_tag)
{
	unsigned int		randfactor;
L
Linus Torvalds 已提交
260

261 262 263 264 265 266 267 268 269 270 271
	/*
	 * To be able to use error injection anywhere, we need to ensure error
	 * injection mechanism is already initialized.
	 *
	 * Code paths like I/O completion can be called before the
	 * initialization is complete, but be able to inject errors in such
	 * places is still useful.
	 */
	if (!mp->m_errortag)
		return false;

272 273 274 275
	ASSERT(error_tag < XFS_ERRTAG_MAX);
	randfactor = mp->m_errortag[error_tag];
	if (!randfactor || prandom_u32() % randfactor)
		return false;
L
Linus Torvalds 已提交
276

277 278
	xfs_warn_ratelimited(mp,
"Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
279
			expression, file, line, mp->m_super->s_id);
280
	return true;
L
Linus Torvalds 已提交
281 282
}

283 284 285 286 287 288 289 290 291 292 293
int
xfs_errortag_get(
	struct xfs_mount	*mp,
	unsigned int		error_tag)
{
	if (error_tag >= XFS_ERRTAG_MAX)
		return -EINVAL;

	return mp->m_errortag[error_tag];
}

L
Linus Torvalds 已提交
294
int
295 296 297 298
xfs_errortag_set(
	struct xfs_mount	*mp,
	unsigned int		error_tag,
	unsigned int		tag_value)
L
Linus Torvalds 已提交
299
{
300 301 302
	if (error_tag >= XFS_ERRTAG_MAX)
		return -EINVAL;

303 304
	mp->m_errortag[error_tag] = tag_value;
	return 0;
L
Linus Torvalds 已提交
305 306 307
}

int
308 309 310
xfs_errortag_add(
	struct xfs_mount	*mp,
	unsigned int		error_tag)
L
Linus Torvalds 已提交
311
{
312 313
	BUILD_BUG_ON(ARRAY_SIZE(xfs_errortag_random_default) != XFS_ERRTAG_MAX);

314 315
	if (error_tag >= XFS_ERRTAG_MAX)
		return -EINVAL;
L
Linus Torvalds 已提交
316

317 318 319
	return xfs_errortag_set(mp, error_tag,
			xfs_errortag_random_default[error_tag]);
}
L
Linus Torvalds 已提交
320

321 322 323 324 325
int
xfs_errortag_clearall(
	struct xfs_mount	*mp)
{
	memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
L
Linus Torvalds 已提交
326 327
	return 0;
}
C
Christoph Hellwig 已提交
328
#endif /* DEBUG */
L
Linus Torvalds 已提交
329 330 331

void
xfs_error_report(
332 333 334 335 336
	const char		*tag,
	int			level,
	struct xfs_mount	*mp,
	const char		*filename,
	int			linenum,
337
	xfs_failaddr_t		failaddr)
L
Linus Torvalds 已提交
338 339
{
	if (level <= xfs_error_level) {
340
		xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
341
		"Internal error %s at line %d of file %s.  Caller %pS",
342
			    tag, linenum, filename, failaddr);
L
Linus Torvalds 已提交
343 344 345 346 347 348 349

		xfs_stack_trace();
	}
}

void
xfs_corruption_error(
350 351 352
	const char		*tag,
	int			level,
	struct xfs_mount	*mp,
353
	const void		*buf,
354
	size_t			bufsize,
355 356
	const char		*filename,
	int			linenum,
357
	xfs_failaddr_t		failaddr)
L
Linus Torvalds 已提交
358
{
359
	if (buf && level <= xfs_error_level)
360
		xfs_hex_dump(buf, bufsize);
361
	xfs_error_report(tag, level, mp, filename, linenum, failaddr);
362
	xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
L
Linus Torvalds 已提交
363
}
E
Eric Sandeen 已提交
364

365 366 367 368
/*
 * Complain about the kinds of metadata corruption that we can't detect from a
 * verifier, such as incorrect inter-block relationship data.  Does not set
 * bp->b_error.
369 370
 *
 * Call xfs_buf_mark_corrupt, not this function.
371 372 373
 */
void
xfs_buf_corruption_error(
374 375
	struct xfs_buf		*bp,
	xfs_failaddr_t		fa)
376 377 378 379 380
{
	struct xfs_mount	*mp = bp->b_mount;

	xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
		  "Metadata corruption detected at %pS, %s block 0x%llx",
381
		  fa, bp->b_ops->name, xfs_buf_daddr(bp));
382 383 384 385 386 387 388

	xfs_alert(mp, "Unmount and run xfs_repair");

	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
		xfs_stack_trace();
}

E
Eric Sandeen 已提交
389 390 391 392 393
/*
 * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
 * values, and omit the stack trace unless the error level is tuned high.
 */
void
394
xfs_buf_verifier_error(
395
	struct xfs_buf		*bp,
396
	int			error,
397
	const char		*name,
398
	const void		*buf,
399
	size_t			bufsz,
400
	xfs_failaddr_t		failaddr)
E
Eric Sandeen 已提交
401
{
402
	struct xfs_mount	*mp = bp->b_mount;
403
	xfs_failaddr_t		fa;
404
	int			sz;
E
Eric Sandeen 已提交
405

406 407
	fa = failaddr ? failaddr : __return_address;
	__xfs_buf_ioerror(bp, error, fa);
408

409 410
	xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
		  "Metadata %s detected at %pS, %s block 0x%llx %s",
D
Dave Chinner 已提交
411
		  bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
412
		  fa, bp->b_ops->name, xfs_buf_daddr(bp), name);
E
Eric Sandeen 已提交
413 414 415 416

	xfs_alert(mp, "Unmount and run xfs_repair");

	if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
417
		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
418
		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
419 420
				sz);
		xfs_hex_dump(buf, sz);
E
Eric Sandeen 已提交
421 422 423 424 425
	}

	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
		xfs_stack_trace();
}
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440
/*
 * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
 * values, and omit the stack trace unless the error level is tuned high.
 */
void
xfs_verifier_error(
	struct xfs_buf		*bp,
	int			error,
	xfs_failaddr_t		failaddr)
{
	return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0),
			XFS_CORRUPTION_DUMP_LEN, failaddr);
}

441 442 443 444 445 446 447 448 449
/*
 * Warnings for inode corruption problems.  Don't bother with the stack
 * trace unless the error level is turned up high.
 */
void
xfs_inode_verifier_error(
	struct xfs_inode	*ip,
	int			error,
	const char		*name,
450
	const void		*buf,
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	size_t			bufsz,
	xfs_failaddr_t		failaddr)
{
	struct xfs_mount	*mp = ip->i_mount;
	xfs_failaddr_t		fa;
	int			sz;

	fa = failaddr ? failaddr : __return_address;

	xfs_alert(mp, "Metadata %s detected at %pS, inode 0x%llx %s",
		  error == -EFSBADCRC ? "CRC error" : "corruption",
		  fa, ip->i_ino, name);

	xfs_alert(mp, "Unmount and run xfs_repair");

	if (buf && xfs_error_level >= XFS_ERRLEVEL_LOW) {
		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
				sz);
		xfs_hex_dump(buf, sz);
	}

	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
		xfs_stack_trace();
}