common.c 21.6 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0+
D
Darrick J. Wong 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2017 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <darrick.wong@oracle.com>
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_trans_resv.h"
#include "xfs_mount.h"
#include "xfs_btree.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_inode.h"
D
Darrick J. Wong 已提交
17
#include "xfs_icache.h"
D
Darrick J. Wong 已提交
18 19 20 21 22 23 24
#include "xfs_alloc.h"
#include "xfs_alloc_btree.h"
#include "xfs_ialloc.h"
#include "xfs_ialloc_btree.h"
#include "xfs_refcount_btree.h"
#include "xfs_rmap.h"
#include "xfs_rmap_btree.h"
D
Darrick J. Wong 已提交
25 26
#include "xfs_log.h"
#include "xfs_trans_priv.h"
27 28
#include "xfs_attr.h"
#include "xfs_reflink.h"
D
Darrick J. Wong 已提交
29 30 31
#include "scrub/scrub.h"
#include "scrub/common.h"
#include "scrub/trace.h"
32
#include "scrub/repair.h"
33
#include "scrub/health.h"
D
Darrick J. Wong 已提交
34 35 36

/* Common code for the metadata scrubbers. */

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
 * Handling operational errors.
 *
 * The *_process_error() family of functions are used to process error return
 * codes from functions called as part of a scrub operation.
 *
 * If there's no error, we return true to tell the caller that it's ok
 * to move on to the next check in its list.
 *
 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
 * caller that something bad happened, and we preserve *error so that
 * the caller can return the *error up the stack to userspace.
 *
 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
 * OFLAG_CORRUPT in sm_flags and the *error is cleared.  In other words,
 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
 * not via return codes.  We return false to tell the caller that
 * something bad happened.  Since the error has been cleared, the caller
 * will (presumably) return that zero and scrubbing will move on to
 * whatever's next.
 *
 * ftrace can be used to record the precise metadata location and the
 * approximate code location of the failed operation.
 */

/* Check for operational errors. */
63
static bool
D
Darrick J. Wong 已提交
64
__xchk_process_error(
65
	struct xfs_scrub	*sc,
66 67 68 69 70
	xfs_agnumber_t		agno,
	xfs_agblock_t		bno,
	int			*error,
	__u32			errflag,
	void			*ret_ip)
71 72 73 74 75 76
{
	switch (*error) {
	case 0:
		return true;
	case -EDEADLOCK:
		/* Used to restart an op with deadlock avoidance. */
D
Darrick J. Wong 已提交
77
		trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
78 79 80 81
		break;
	case -EFSBADCRC:
	case -EFSCORRUPTED:
		/* Note the badness but don't abort. */
82
		sc->sm->sm_flags |= errflag;
83 84 85
		*error = 0;
		/* fall through */
	default:
D
Darrick J. Wong 已提交
86
		trace_xchk_op_error(sc, agno, bno, *error,
87
				ret_ip);
88 89 90 91 92 93
		break;
	}
	return false;
}

bool
D
Darrick J. Wong 已提交
94
xchk_process_error(
95
	struct xfs_scrub	*sc,
96 97 98
	xfs_agnumber_t		agno,
	xfs_agblock_t		bno,
	int			*error)
99
{
D
Darrick J. Wong 已提交
100
	return __xchk_process_error(sc, agno, bno, error,
101 102 103 104
			XFS_SCRUB_OFLAG_CORRUPT, __return_address);
}

bool
D
Darrick J. Wong 已提交
105
xchk_xref_process_error(
106
	struct xfs_scrub	*sc,
107 108 109
	xfs_agnumber_t		agno,
	xfs_agblock_t		bno,
	int			*error)
110
{
D
Darrick J. Wong 已提交
111
	return __xchk_process_error(sc, agno, bno, error,
112 113 114 115 116
			XFS_SCRUB_OFLAG_XFAIL, __return_address);
}

/* Check for operational errors for a file offset. */
static bool
D
Darrick J. Wong 已提交
117
__xchk_fblock_process_error(
118
	struct xfs_scrub	*sc,
119 120 121 122 123
	int			whichfork,
	xfs_fileoff_t		offset,
	int			*error,
	__u32			errflag,
	void			*ret_ip)
124 125 126 127 128 129
{
	switch (*error) {
	case 0:
		return true;
	case -EDEADLOCK:
		/* Used to restart an op with deadlock avoidance. */
D
Darrick J. Wong 已提交
130
		trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
131 132 133 134
		break;
	case -EFSBADCRC:
	case -EFSCORRUPTED:
		/* Note the badness but don't abort. */
135
		sc->sm->sm_flags |= errflag;
136 137 138
		*error = 0;
		/* fall through */
	default:
D
Darrick J. Wong 已提交
139
		trace_xchk_file_op_error(sc, whichfork, offset, *error,
140
				ret_ip);
141 142 143 144 145
		break;
	}
	return false;
}

146
bool
D
Darrick J. Wong 已提交
147
xchk_fblock_process_error(
148
	struct xfs_scrub	*sc,
149 150 151
	int			whichfork,
	xfs_fileoff_t		offset,
	int			*error)
152
{
D
Darrick J. Wong 已提交
153
	return __xchk_fblock_process_error(sc, whichfork, offset, error,
154 155 156 157
			XFS_SCRUB_OFLAG_CORRUPT, __return_address);
}

bool
D
Darrick J. Wong 已提交
158
xchk_fblock_xref_process_error(
159
	struct xfs_scrub	*sc,
160 161 162
	int			whichfork,
	xfs_fileoff_t		offset,
	int			*error)
163
{
D
Darrick J. Wong 已提交
164
	return __xchk_fblock_process_error(sc, whichfork, offset, error,
165 166 167
			XFS_SCRUB_OFLAG_XFAIL, __return_address);
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181
/*
 * Handling scrub corruption/optimization/warning checks.
 *
 * The *_set_{corrupt,preen,warning}() family of functions are used to
 * record the presence of metadata that is incorrect (corrupt), could be
 * optimized somehow (preen), or should be flagged for administrative
 * review but is not incorrect (warn).
 *
 * ftrace can be used to record the precise metadata location and
 * approximate code location of the failed check.
 */

/* Record a block which could be optimized. */
void
D
Darrick J. Wong 已提交
182
xchk_block_set_preen(
183
	struct xfs_scrub	*sc,
184
	struct xfs_buf		*bp)
185 186
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
D
Darrick J. Wong 已提交
187
	trace_xchk_block_preen(sc, bp->b_bn, __return_address);
188 189 190 191 192 193 194 195
}

/*
 * Record an inode which could be optimized.  The trace data will
 * include the block given by bp if bp is given; otherwise it will use
 * the block location of the inode record itself.
 */
void
D
Darrick J. Wong 已提交
196
xchk_ino_set_preen(
197
	struct xfs_scrub	*sc,
198
	xfs_ino_t		ino)
199 200
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
D
Darrick J. Wong 已提交
201
	trace_xchk_ino_preen(sc, ino, __return_address);
202 203
}

204 205 206 207 208 209 210 211 212
/* Record something being wrong with the filesystem primary superblock. */
void
xchk_set_corrupt(
	struct xfs_scrub	*sc)
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
	trace_xchk_fs_error(sc, 0, __return_address);
}

213 214
/* Record a corrupt block. */
void
D
Darrick J. Wong 已提交
215
xchk_block_set_corrupt(
216
	struct xfs_scrub	*sc,
217
	struct xfs_buf		*bp)
218 219
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
D
Darrick J. Wong 已提交
220
	trace_xchk_block_error(sc, bp->b_bn, __return_address);
221 222
}

223 224
/* Record a corruption while cross-referencing. */
void
D
Darrick J. Wong 已提交
225
xchk_block_xref_set_corrupt(
226
	struct xfs_scrub	*sc,
227
	struct xfs_buf		*bp)
228 229
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
D
Darrick J. Wong 已提交
230
	trace_xchk_block_error(sc, bp->b_bn, __return_address);
231 232
}

233 234 235 236 237 238
/*
 * Record a corrupt inode.  The trace data will include the block given
 * by bp if bp is given; otherwise it will use the block location of the
 * inode record itself.
 */
void
D
Darrick J. Wong 已提交
239
xchk_ino_set_corrupt(
240
	struct xfs_scrub	*sc,
241
	xfs_ino_t		ino)
242 243
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
D
Darrick J. Wong 已提交
244
	trace_xchk_ino_error(sc, ino, __return_address);
245 246
}

247 248
/* Record a corruption while cross-referencing with an inode. */
void
D
Darrick J. Wong 已提交
249
xchk_ino_xref_set_corrupt(
250
	struct xfs_scrub	*sc,
251
	xfs_ino_t		ino)
252 253
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
D
Darrick J. Wong 已提交
254
	trace_xchk_ino_error(sc, ino, __return_address);
255 256
}

257 258
/* Record corruption in a block indexed by a file fork. */
void
D
Darrick J. Wong 已提交
259
xchk_fblock_set_corrupt(
260
	struct xfs_scrub	*sc,
261 262
	int			whichfork,
	xfs_fileoff_t		offset)
263 264
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
D
Darrick J. Wong 已提交
265
	trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
266 267
}

268 269
/* Record a corruption while cross-referencing a fork block. */
void
D
Darrick J. Wong 已提交
270
xchk_fblock_xref_set_corrupt(
271
	struct xfs_scrub	*sc,
272 273
	int			whichfork,
	xfs_fileoff_t		offset)
274 275
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
D
Darrick J. Wong 已提交
276
	trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
277 278
}

279 280 281 282 283
/*
 * Warn about inodes that need administrative review but is not
 * incorrect.
 */
void
D
Darrick J. Wong 已提交
284
xchk_ino_set_warning(
285
	struct xfs_scrub	*sc,
286
	xfs_ino_t		ino)
287 288
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
D
Darrick J. Wong 已提交
289
	trace_xchk_ino_warning(sc, ino, __return_address);
290 291 292 293
}

/* Warn about a block indexed by a file fork that needs review. */
void
D
Darrick J. Wong 已提交
294
xchk_fblock_set_warning(
295
	struct xfs_scrub	*sc,
296 297
	int			whichfork,
	xfs_fileoff_t		offset)
298 299
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
D
Darrick J. Wong 已提交
300
	trace_xchk_fblock_warning(sc, whichfork, offset, __return_address);
301 302 303 304
}

/* Signal an incomplete scrub. */
void
D
Darrick J. Wong 已提交
305
xchk_set_incomplete(
306
	struct xfs_scrub	*sc)
307 308
{
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
D
Darrick J. Wong 已提交
309
	trace_xchk_incomplete(sc, __return_address);
310 311
}

312 313 314 315 316
/*
 * rmap scrubbing -- compute the number of blocks with a given owner,
 * at least according to the reverse mapping data.
 */

D
Darrick J. Wong 已提交
317
struct xchk_rmap_ownedby_info {
318 319
	const struct xfs_owner_info	*oinfo;
	xfs_filblks_t			*blocks;
320 321 322
};

STATIC int
D
Darrick J. Wong 已提交
323
xchk_count_rmap_ownedby_irec(
324 325 326
	struct xfs_btree_cur		*cur,
	struct xfs_rmap_irec		*rec,
	void				*priv)
327
{
328 329 330
	struct xchk_rmap_ownedby_info	*sroi = priv;
	bool				irec_attr;
	bool				oinfo_attr;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

	irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
	oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;

	if (rec->rm_owner != sroi->oinfo->oi_owner)
		return 0;

	if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
		(*sroi->blocks) += rec->rm_blockcount;

	return 0;
}

/*
 * Calculate the number of blocks the rmap thinks are owned by something.
 * The caller should pass us an rmapbt cursor.
 */
int
D
Darrick J. Wong 已提交
349
xchk_count_rmap_ownedby_ag(
350
	struct xfs_scrub		*sc,
351
	struct xfs_btree_cur		*cur,
352
	const struct xfs_owner_info	*oinfo,
353
	xfs_filblks_t			*blocks)
354
{
355 356 357 358
	struct xchk_rmap_ownedby_info	sroi = {
		.oinfo			= oinfo,
		.blocks			= blocks,
	};
359 360

	*blocks = 0;
D
Darrick J. Wong 已提交
361
	return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec,
362 363 364
			&sroi);
}

365 366 367 368 369 370 371 372
/*
 * AG scrubbing
 *
 * These helpers facilitate locking an allocation group's header
 * buffers, setting up cursors for all btrees that are present, and
 * cleaning everything up once we're through.
 */

D
Darrick J. Wong 已提交
373 374 375
/* Decide if we want to return an AG header read failure. */
static inline bool
want_ag_read_header_failure(
376
	struct xfs_scrub	*sc,
377
	unsigned int		type)
D
Darrick J. Wong 已提交
378 379 380
{
	/* Return all AG header read failures when scanning btrees. */
	if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
D
Darrick J. Wong 已提交
381 382
	    sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
	    sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
D
Darrick J. Wong 已提交
383 384 385 386 387 388 389 390 391 392 393
		return true;
	/*
	 * If we're scanning a given type of AG header, we only want to
	 * see read failures from that specific header.  We'd like the
	 * other headers to cross-check them, but this isn't required.
	 */
	if (sc->sm->sm_type == type)
		return true;
	return false;
}

394 395 396
/*
 * Grab all the headers for an AG.
 *
D
Darrick J. Wong 已提交
397
 * The headers should be released by xchk_ag_free, but as a fail
398 399 400 401
 * safe we attach all the buffers we grab to the scrub transaction so
 * they'll all be freed when we cancel it.
 */
int
D
Darrick J. Wong 已提交
402
xchk_ag_read_headers(
403
	struct xfs_scrub	*sc,
404 405 406 407
	xfs_agnumber_t		agno,
	struct xfs_buf		**agi,
	struct xfs_buf		**agf,
	struct xfs_buf		**agfl)
408
{
409 410
	struct xfs_mount	*mp = sc->mp;
	int			error;
411 412

	error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
D
Darrick J. Wong 已提交
413
	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
414 415 416
		goto out;

	error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
D
Darrick J. Wong 已提交
417
	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
418 419 420
		goto out;

	error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
D
Darrick J. Wong 已提交
421
	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
422
		goto out;
423
	error = 0;
424 425 426 427 428 429
out:
	return error;
}

/* Release all the AG btree cursors. */
void
D
Darrick J. Wong 已提交
430 431
xchk_ag_btcur_free(
	struct xchk_ag		*sa)
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
{
	if (sa->refc_cur)
		xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
	if (sa->rmap_cur)
		xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
	if (sa->fino_cur)
		xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
	if (sa->ino_cur)
		xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
	if (sa->cnt_cur)
		xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
	if (sa->bno_cur)
		xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);

	sa->refc_cur = NULL;
	sa->rmap_cur = NULL;
	sa->fino_cur = NULL;
	sa->ino_cur = NULL;
	sa->bno_cur = NULL;
	sa->cnt_cur = NULL;
}

/* Initialize all the btree cursors for an AG. */
int
D
Darrick J. Wong 已提交
456
xchk_ag_btcur_init(
457
	struct xfs_scrub	*sc,
D
Darrick J. Wong 已提交
458
	struct xchk_ag		*sa)
459
{
460 461
	struct xfs_mount	*mp = sc->mp;
	xfs_agnumber_t		agno = sa->agno;
462

463 464 465
	xchk_perag_get(sc->mp, sa);
	if (sa->agf_bp &&
	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_BNO)) {
466 467 468
		/* Set up a bnobt cursor for cross-referencing. */
		sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
				agno, XFS_BTNUM_BNO);
469
	}
470

471 472
	if (sa->agf_bp &&
	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_CNT)) {
473 474 475 476 477 478
		/* Set up a cntbt cursor for cross-referencing. */
		sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
				agno, XFS_BTNUM_CNT);
	}

	/* Set up a inobt cursor for cross-referencing. */
479 480
	if (sa->agi_bp &&
	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_INO)) {
481 482 483 484 485
		sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
					agno, XFS_BTNUM_INO);
	}

	/* Set up a finobt cursor for cross-referencing. */
486 487
	if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb) &&
	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_FINO)) {
488 489 490 491 492
		sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
				agno, XFS_BTNUM_FINO);
	}

	/* Set up a rmapbt cursor for cross-referencing. */
493 494
	if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb) &&
	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_RMAP)) {
495 496 497 498 499
		sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
				agno);
	}

	/* Set up a refcountbt cursor for cross-referencing. */
500 501
	if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb) &&
	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_REFC)) {
502
		sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
503
				sa->agf_bp, agno);
504 505 506 507 508 509 510
	}

	return 0;
}

/* Release the AG header context and btree cursors. */
void
D
Darrick J. Wong 已提交
511
xchk_ag_free(
512
	struct xfs_scrub	*sc,
D
Darrick J. Wong 已提交
513
	struct xchk_ag		*sa)
514
{
D
Darrick J. Wong 已提交
515
	xchk_ag_btcur_free(sa);
516 517 518 519 520 521 522 523 524 525 526 527
	if (sa->agfl_bp) {
		xfs_trans_brelse(sc->tp, sa->agfl_bp);
		sa->agfl_bp = NULL;
	}
	if (sa->agf_bp) {
		xfs_trans_brelse(sc->tp, sa->agf_bp);
		sa->agf_bp = NULL;
	}
	if (sa->agi_bp) {
		xfs_trans_brelse(sc->tp, sa->agi_bp);
		sa->agi_bp = NULL;
	}
528 529 530 531
	if (sa->pag) {
		xfs_perag_put(sa->pag);
		sa->pag = NULL;
	}
532 533 534 535 536 537 538 539 540 541 542
	sa->agno = NULLAGNUMBER;
}

/*
 * For scrub, grab the AGI and the AGF headers, in that order.  Locking
 * order requires us to get the AGI before the AGF.  We use the
 * transaction to avoid deadlocking on crosslinked metadata buffers;
 * either the caller passes one in (bmap scrub) or we have to create a
 * transaction ourselves.
 */
int
D
Darrick J. Wong 已提交
543
xchk_ag_init(
544
	struct xfs_scrub	*sc,
545
	xfs_agnumber_t		agno,
D
Darrick J. Wong 已提交
546
	struct xchk_ag		*sa)
547
{
548
	int			error;
549 550

	sa->agno = agno;
D
Darrick J. Wong 已提交
551
	error = xchk_ag_read_headers(sc, agno, &sa->agi_bp,
552 553 554 555
			&sa->agf_bp, &sa->agfl_bp);
	if (error)
		return error;

D
Darrick J. Wong 已提交
556
	return xchk_ag_btcur_init(sc, sa);
557 558
}

559 560
/*
 * Grab the per-ag structure if we haven't already gotten it.  Teardown of the
D
Darrick J. Wong 已提交
561
 * xchk_ag will release it for us.
562 563
 */
void
D
Darrick J. Wong 已提交
564
xchk_perag_get(
565
	struct xfs_mount	*mp,
566
	struct xchk_ag		*sa)
567 568 569 570 571
{
	if (!sa->pag)
		sa->pag = xfs_perag_get(mp, sa->agno);
}

D
Darrick J. Wong 已提交
572 573
/* Per-scrubber setup functions */

574 575 576
/*
 * Grab an empty transaction so that we can re-grab locked buffers if
 * one of our btrees turns out to be cyclic.
577 578 579 580 581 582
 *
 * If we're going to repair something, we need to ask for the largest possible
 * log reservation so that we can handle the worst case scenario for metadata
 * updates while rebuilding a metadata item.  We also need to reserve as many
 * blocks in the head transaction as we think we're going to need to rebuild
 * the metadata object.
583 584
 */
int
D
Darrick J. Wong 已提交
585
xchk_trans_alloc(
586
	struct xfs_scrub	*sc,
587
	uint			resblks)
588
{
589 590 591 592
	if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
		return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
				resblks, 0, 0, &sc->tp);

593 594 595
	return xfs_trans_alloc_empty(sc->mp, &sc->tp);
}

D
Darrick J. Wong 已提交
596 597
/* Set us up with a transaction and an empty context. */
int
D
Darrick J. Wong 已提交
598
xchk_setup_fs(
599
	struct xfs_scrub	*sc,
600
	struct xfs_inode	*ip)
D
Darrick J. Wong 已提交
601
{
602
	uint			resblks;
603

604
	resblks = xrep_calc_ag_resblks(sc);
D
Darrick J. Wong 已提交
605
	return xchk_trans_alloc(sc, resblks);
D
Darrick J. Wong 已提交
606
}
D
Darrick J. Wong 已提交
607 608 609

/* Set us up with AG headers and btree cursors. */
int
D
Darrick J. Wong 已提交
610
xchk_setup_ag_btree(
611
	struct xfs_scrub	*sc,
612 613
	struct xfs_inode	*ip,
	bool			force_log)
D
Darrick J. Wong 已提交
614
{
615 616
	struct xfs_mount	*mp = sc->mp;
	int			error;
D
Darrick J. Wong 已提交
617

D
Darrick J. Wong 已提交
618 619 620 621 622 623 624
	/*
	 * If the caller asks us to checkpont the log, do so.  This
	 * expensive operation should be performed infrequently and only
	 * as a last resort.  Any caller that sets force_log should
	 * document why they need to do so.
	 */
	if (force_log) {
D
Darrick J. Wong 已提交
625
		error = xchk_checkpoint_log(mp);
D
Darrick J. Wong 已提交
626 627 628 629
		if (error)
			return error;
	}

D
Darrick J. Wong 已提交
630
	error = xchk_setup_fs(sc, ip);
D
Darrick J. Wong 已提交
631 632 633
	if (error)
		return error;

D
Darrick J. Wong 已提交
634
	return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa);
D
Darrick J. Wong 已提交
635
}
D
Darrick J. Wong 已提交
636 637 638

/* Push everything out of the log onto disk. */
int
D
Darrick J. Wong 已提交
639
xchk_checkpoint_log(
D
Darrick J. Wong 已提交
640 641 642 643
	struct xfs_mount	*mp)
{
	int			error;

644
	error = xfs_log_force(mp, XFS_LOG_SYNC);
D
Darrick J. Wong 已提交
645 646 647 648 649
	if (error)
		return error;
	xfs_ail_push_all_sync(mp->m_ail);
	return 0;
}
D
Darrick J. Wong 已提交
650 651 652 653 654 655 656

/*
 * Given an inode and the scrub control structure, grab either the
 * inode referenced in the control structure or the inode passed in.
 * The inode is not locked.
 */
int
D
Darrick J. Wong 已提交
657
xchk_get_inode(
658
	struct xfs_scrub	*sc,
659
	struct xfs_inode	*ip_in)
D
Darrick J. Wong 已提交
660
{
661 662 663 664
	struct xfs_imap		imap;
	struct xfs_mount	*mp = sc->mp;
	struct xfs_inode	*ip = NULL;
	int			error;
D
Darrick J. Wong 已提交
665 666 667 668 669 670 671 672 673 674 675 676

	/* We want to scan the inode we already had opened. */
	if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
		sc->ip = ip_in;
		return 0;
	}

	/* Look up the inode, see if the generation number matches. */
	if (xfs_internal_inum(mp, sc->sm->sm_ino))
		return -ENOENT;
	error = xfs_iget(mp, NULL, sc->sm->sm_ino,
			XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	switch (error) {
	case -ENOENT:
		/* Inode doesn't exist, just bail out. */
		return error;
	case 0:
		/* Got an inode, continue. */
		break;
	case -EINVAL:
		/*
		 * -EINVAL with IGET_UNTRUSTED could mean one of several
		 * things: userspace gave us an inode number that doesn't
		 * correspond to fs space, or doesn't have an inobt entry;
		 * or it could simply mean that the inode buffer failed the
		 * read verifiers.
		 *
		 * Try just the inode mapping lookup -- if it succeeds, then
		 * the inode buffer verifier failed and something needs fixing.
		 * Otherwise, we really couldn't find it so tell userspace
		 * that it no longer exists.
		 */
		error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap,
				XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
		if (error)
			return -ENOENT;
		error = -EFSCORRUPTED;
		/* fall through */
	default:
D
Darrick J. Wong 已提交
704
		trace_xchk_op_error(sc,
D
Darrick J. Wong 已提交
705 706 707 708 709 710
				XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
				XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
				error, __return_address);
		return error;
	}
	if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
711
		xfs_irele(ip);
D
Darrick J. Wong 已提交
712 713 714 715 716 717
		return -ENOENT;
	}

	sc->ip = ip;
	return 0;
}
D
Darrick J. Wong 已提交
718 719 720

/* Set us up to scrub a file's contents. */
int
D
Darrick J. Wong 已提交
721
xchk_setup_inode_contents(
722
	struct xfs_scrub	*sc,
723 724
	struct xfs_inode	*ip,
	unsigned int		resblks)
D
Darrick J. Wong 已提交
725
{
726
	int			error;
D
Darrick J. Wong 已提交
727

D
Darrick J. Wong 已提交
728
	error = xchk_get_inode(sc, ip);
D
Darrick J. Wong 已提交
729 730 731 732 733 734
	if (error)
		return error;

	/* Got the inode, lock it and we're ready to go. */
	sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
	xfs_ilock(sc->ip, sc->ilock_flags);
D
Darrick J. Wong 已提交
735
	error = xchk_trans_alloc(sc, resblks);
D
Darrick J. Wong 已提交
736 737 738 739 740 741 742 743 744
	if (error)
		goto out;
	sc->ilock_flags |= XFS_ILOCK_EXCL;
	xfs_ilock(sc->ip, XFS_ILOCK_EXCL);

out:
	/* scrub teardown will unlock and release the inode for us */
	return error;
}
745 746 747 748 749 750 751

/*
 * Predicate that decides if we need to evaluate the cross-reference check.
 * If there was an error accessing the cross-reference btree, just delete
 * the cursor and skip the check.
 */
bool
D
Darrick J. Wong 已提交
752
xchk_should_check_xref(
753
	struct xfs_scrub	*sc,
754 755
	int			*error,
	struct xfs_btree_cur	**curpp)
756
{
757
	/* No point in xref if we already know we're corrupt. */
D
Darrick J. Wong 已提交
758
	if (xchk_skip_xref(sc->sm))
759 760
		return false;

761 762 763 764 765 766 767 768 769 770 771 772 773 774
	if (*error == 0)
		return true;

	if (curpp) {
		/* If we've already given up on xref, just bail out. */
		if (!*curpp)
			return false;

		/* xref error, delete cursor and bail out. */
		xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
		*curpp = NULL;
	}

	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
D
Darrick J. Wong 已提交
775
	trace_xchk_xref_error(sc, *error, __return_address);
776 777 778 779 780 781 782 783

	/*
	 * Errors encountered during cross-referencing with another
	 * data structure should not cause this scrubber to abort.
	 */
	*error = 0;
	return false;
}
D
Darrick J. Wong 已提交
784 785 786

/* Run the structure verifiers on in-memory buffers to detect bad memory. */
void
D
Darrick J. Wong 已提交
787
xchk_buffer_recheck(
788
	struct xfs_scrub	*sc,
789
	struct xfs_buf		*bp)
D
Darrick J. Wong 已提交
790
{
791
	xfs_failaddr_t		fa;
D
Darrick J. Wong 已提交
792 793

	if (bp->b_ops == NULL) {
D
Darrick J. Wong 已提交
794
		xchk_block_set_corrupt(sc, bp);
D
Darrick J. Wong 已提交
795 796 797
		return;
	}
	if (bp->b_ops->verify_struct == NULL) {
D
Darrick J. Wong 已提交
798
		xchk_set_incomplete(sc);
D
Darrick J. Wong 已提交
799 800 801 802 803 804
		return;
	}
	fa = bp->b_ops->verify_struct(bp);
	if (!fa)
		return;
	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
D
Darrick J. Wong 已提交
805
	trace_xchk_block_error(sc, bp->b_bn, fa);
D
Darrick J. Wong 已提交
806
}
807 808 809 810 811 812

/*
 * Scrub the attr/data forks of a metadata inode.  The metadata inode must be
 * pointed to by sc->ip and the ILOCK must be held.
 */
int
D
Darrick J. Wong 已提交
813
xchk_metadata_inode_forks(
814
	struct xfs_scrub	*sc)
815
{
816 817 818
	__u32			smtype;
	bool			shared;
	int			error;
819 820 821 822 823 824

	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
		return 0;

	/* Metadata inodes don't live on the rt device. */
	if (sc->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
D
Darrick J. Wong 已提交
825
		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
826 827 828 829 830
		return 0;
	}

	/* They should never participate in reflink. */
	if (xfs_is_reflink_inode(sc->ip)) {
D
Darrick J. Wong 已提交
831
		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
832 833 834 835 836
		return 0;
	}

	/* They also should never have extended attributes. */
	if (xfs_inode_hasattr(sc->ip)) {
D
Darrick J. Wong 已提交
837
		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
838 839 840 841 842 843
		return 0;
	}

	/* Invoke the data fork scrubber. */
	smtype = sc->sm->sm_type;
	sc->sm->sm_type = XFS_SCRUB_TYPE_BMBTD;
D
Darrick J. Wong 已提交
844
	error = xchk_bmap_data(sc);
845 846 847 848 849 850 851 852
	sc->sm->sm_type = smtype;
	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
		return error;

	/* Look for incorrect shared blocks. */
	if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
		error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
				&shared);
D
Darrick J. Wong 已提交
853
		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
854 855 856
				&error))
			return error;
		if (shared)
D
Darrick J. Wong 已提交
857
			xchk_ino_set_corrupt(sc, sc->ip->i_ino);
858 859 860 861
	}

	return error;
}
862 863 864 865 866 867 868 869 870

/*
 * Try to lock an inode in violation of the usual locking order rules.  For
 * example, trying to get the IOLOCK while in transaction context, or just
 * plain breaking AG-order or inode-order inode locking rules.  Either way,
 * the only way to avoid an ABBA deadlock is to use trylock and back off if
 * we can't.
 */
int
D
Darrick J. Wong 已提交
871
xchk_ilock_inverted(
872 873 874 875 876 877 878 879 880 881 882 883
	struct xfs_inode	*ip,
	uint			lock_mode)
{
	int			i;

	for (i = 0; i < 20; i++) {
		if (xfs_ilock_nowait(ip, lock_mode))
			return 0;
		delay(1);
	}
	return -EDEADLOCK;
}
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

/* Pause background reaping of resources. */
void
xchk_stop_reaping(
	struct xfs_scrub	*sc)
{
	sc->flags |= XCHK_REAPING_DISABLED;
	xfs_stop_block_reaping(sc->mp);
}

/* Restart background reaping of resources. */
void
xchk_start_reaping(
	struct xfs_scrub	*sc)
{
	xfs_start_block_reaping(sc->mp);
	sc->flags &= ~XCHK_REAPING_DISABLED;
}