18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2017 Oracle.  All Rights Reserved.
48c2ecf20Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include "xfs.h"
78c2ecf20Sopenharmony_ci#include "xfs_fs.h"
88c2ecf20Sopenharmony_ci#include "xfs_shared.h"
98c2ecf20Sopenharmony_ci#include "xfs_format.h"
108c2ecf20Sopenharmony_ci#include "xfs_trans_resv.h"
118c2ecf20Sopenharmony_ci#include "xfs_mount.h"
128c2ecf20Sopenharmony_ci#include "xfs_btree.h"
138c2ecf20Sopenharmony_ci#include "xfs_log_format.h"
148c2ecf20Sopenharmony_ci#include "xfs_trans.h"
158c2ecf20Sopenharmony_ci#include "xfs_sb.h"
168c2ecf20Sopenharmony_ci#include "xfs_inode.h"
178c2ecf20Sopenharmony_ci#include "xfs_icache.h"
188c2ecf20Sopenharmony_ci#include "xfs_alloc.h"
198c2ecf20Sopenharmony_ci#include "xfs_alloc_btree.h"
208c2ecf20Sopenharmony_ci#include "xfs_ialloc.h"
218c2ecf20Sopenharmony_ci#include "xfs_ialloc_btree.h"
228c2ecf20Sopenharmony_ci#include "xfs_refcount_btree.h"
238c2ecf20Sopenharmony_ci#include "xfs_rmap.h"
248c2ecf20Sopenharmony_ci#include "xfs_rmap_btree.h"
258c2ecf20Sopenharmony_ci#include "xfs_log.h"
268c2ecf20Sopenharmony_ci#include "xfs_trans_priv.h"
278c2ecf20Sopenharmony_ci#include "xfs_attr.h"
288c2ecf20Sopenharmony_ci#include "xfs_reflink.h"
298c2ecf20Sopenharmony_ci#include "scrub/scrub.h"
308c2ecf20Sopenharmony_ci#include "scrub/common.h"
318c2ecf20Sopenharmony_ci#include "scrub/trace.h"
328c2ecf20Sopenharmony_ci#include "scrub/repair.h"
338c2ecf20Sopenharmony_ci#include "scrub/health.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Common code for the metadata scrubbers. */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * Handling operational errors.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * The *_process_error() family of functions are used to process error return
418c2ecf20Sopenharmony_ci * codes from functions called as part of a scrub operation.
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * If there's no error, we return true to tell the caller that it's ok
448c2ecf20Sopenharmony_ci * to move on to the next check in its list.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * For non-verifier errors (e.g. ENOMEM) we return false to tell the
478c2ecf20Sopenharmony_ci * caller that something bad happened, and we preserve *error so that
488c2ecf20Sopenharmony_ci * the caller can return the *error up the stack to userspace.
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
518c2ecf20Sopenharmony_ci * OFLAG_CORRUPT in sm_flags and the *error is cleared.  In other words,
528c2ecf20Sopenharmony_ci * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
538c2ecf20Sopenharmony_ci * not via return codes.  We return false to tell the caller that
548c2ecf20Sopenharmony_ci * something bad happened.  Since the error has been cleared, the caller
558c2ecf20Sopenharmony_ci * will (presumably) return that zero and scrubbing will move on to
568c2ecf20Sopenharmony_ci * whatever's next.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * ftrace can be used to record the precise metadata location and the
598c2ecf20Sopenharmony_ci * approximate code location of the failed operation.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* Check for operational errors. */
638c2ecf20Sopenharmony_cistatic bool
648c2ecf20Sopenharmony_ci__xchk_process_error(
658c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
668c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno,
678c2ecf20Sopenharmony_ci	xfs_agblock_t		bno,
688c2ecf20Sopenharmony_ci	int			*error,
698c2ecf20Sopenharmony_ci	__u32			errflag,
708c2ecf20Sopenharmony_ci	void			*ret_ip)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	switch (*error) {
738c2ecf20Sopenharmony_ci	case 0:
748c2ecf20Sopenharmony_ci		return true;
758c2ecf20Sopenharmony_ci	case -EDEADLOCK:
768c2ecf20Sopenharmony_ci		/* Used to restart an op with deadlock avoidance. */
778c2ecf20Sopenharmony_ci		trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
788c2ecf20Sopenharmony_ci		break;
798c2ecf20Sopenharmony_ci	case -EFSBADCRC:
808c2ecf20Sopenharmony_ci	case -EFSCORRUPTED:
818c2ecf20Sopenharmony_ci		/* Note the badness but don't abort. */
828c2ecf20Sopenharmony_ci		sc->sm->sm_flags |= errflag;
838c2ecf20Sopenharmony_ci		*error = 0;
848c2ecf20Sopenharmony_ci		/* fall through */
858c2ecf20Sopenharmony_ci	default:
868c2ecf20Sopenharmony_ci		trace_xchk_op_error(sc, agno, bno, *error,
878c2ecf20Sopenharmony_ci				ret_ip);
888c2ecf20Sopenharmony_ci		break;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci	return false;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cibool
948c2ecf20Sopenharmony_cixchk_process_error(
958c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
968c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno,
978c2ecf20Sopenharmony_ci	xfs_agblock_t		bno,
988c2ecf20Sopenharmony_ci	int			*error)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	return __xchk_process_error(sc, agno, bno, error,
1018c2ecf20Sopenharmony_ci			XFS_SCRUB_OFLAG_CORRUPT, __return_address);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cibool
1058c2ecf20Sopenharmony_cixchk_xref_process_error(
1068c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1078c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno,
1088c2ecf20Sopenharmony_ci	xfs_agblock_t		bno,
1098c2ecf20Sopenharmony_ci	int			*error)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	return __xchk_process_error(sc, agno, bno, error,
1128c2ecf20Sopenharmony_ci			XFS_SCRUB_OFLAG_XFAIL, __return_address);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* Check for operational errors for a file offset. */
1168c2ecf20Sopenharmony_cistatic bool
1178c2ecf20Sopenharmony_ci__xchk_fblock_process_error(
1188c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1198c2ecf20Sopenharmony_ci	int			whichfork,
1208c2ecf20Sopenharmony_ci	xfs_fileoff_t		offset,
1218c2ecf20Sopenharmony_ci	int			*error,
1228c2ecf20Sopenharmony_ci	__u32			errflag,
1238c2ecf20Sopenharmony_ci	void			*ret_ip)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	switch (*error) {
1268c2ecf20Sopenharmony_ci	case 0:
1278c2ecf20Sopenharmony_ci		return true;
1288c2ecf20Sopenharmony_ci	case -EDEADLOCK:
1298c2ecf20Sopenharmony_ci		/* Used to restart an op with deadlock avoidance. */
1308c2ecf20Sopenharmony_ci		trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
1318c2ecf20Sopenharmony_ci		break;
1328c2ecf20Sopenharmony_ci	case -EFSBADCRC:
1338c2ecf20Sopenharmony_ci	case -EFSCORRUPTED:
1348c2ecf20Sopenharmony_ci		/* Note the badness but don't abort. */
1358c2ecf20Sopenharmony_ci		sc->sm->sm_flags |= errflag;
1368c2ecf20Sopenharmony_ci		*error = 0;
1378c2ecf20Sopenharmony_ci		/* fall through */
1388c2ecf20Sopenharmony_ci	default:
1398c2ecf20Sopenharmony_ci		trace_xchk_file_op_error(sc, whichfork, offset, *error,
1408c2ecf20Sopenharmony_ci				ret_ip);
1418c2ecf20Sopenharmony_ci		break;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci	return false;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cibool
1478c2ecf20Sopenharmony_cixchk_fblock_process_error(
1488c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1498c2ecf20Sopenharmony_ci	int			whichfork,
1508c2ecf20Sopenharmony_ci	xfs_fileoff_t		offset,
1518c2ecf20Sopenharmony_ci	int			*error)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	return __xchk_fblock_process_error(sc, whichfork, offset, error,
1548c2ecf20Sopenharmony_ci			XFS_SCRUB_OFLAG_CORRUPT, __return_address);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cibool
1588c2ecf20Sopenharmony_cixchk_fblock_xref_process_error(
1598c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1608c2ecf20Sopenharmony_ci	int			whichfork,
1618c2ecf20Sopenharmony_ci	xfs_fileoff_t		offset,
1628c2ecf20Sopenharmony_ci	int			*error)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return __xchk_fblock_process_error(sc, whichfork, offset, error,
1658c2ecf20Sopenharmony_ci			XFS_SCRUB_OFLAG_XFAIL, __return_address);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Handling scrub corruption/optimization/warning checks.
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * The *_set_{corrupt,preen,warning}() family of functions are used to
1728c2ecf20Sopenharmony_ci * record the presence of metadata that is incorrect (corrupt), could be
1738c2ecf20Sopenharmony_ci * optimized somehow (preen), or should be flagged for administrative
1748c2ecf20Sopenharmony_ci * review but is not incorrect (warn).
1758c2ecf20Sopenharmony_ci *
1768c2ecf20Sopenharmony_ci * ftrace can be used to record the precise metadata location and
1778c2ecf20Sopenharmony_ci * approximate code location of the failed check.
1788c2ecf20Sopenharmony_ci */
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/* Record a block which could be optimized. */
1818c2ecf20Sopenharmony_civoid
1828c2ecf20Sopenharmony_cixchk_block_set_preen(
1838c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1848c2ecf20Sopenharmony_ci	struct xfs_buf		*bp)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
1878c2ecf20Sopenharmony_ci	trace_xchk_block_preen(sc, bp->b_bn, __return_address);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/*
1918c2ecf20Sopenharmony_ci * Record an inode which could be optimized.  The trace data will
1928c2ecf20Sopenharmony_ci * include the block given by bp if bp is given; otherwise it will use
1938c2ecf20Sopenharmony_ci * the block location of the inode record itself.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_civoid
1968c2ecf20Sopenharmony_cixchk_ino_set_preen(
1978c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1988c2ecf20Sopenharmony_ci	xfs_ino_t		ino)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
2018c2ecf20Sopenharmony_ci	trace_xchk_ino_preen(sc, ino, __return_address);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* Record something being wrong with the filesystem primary superblock. */
2058c2ecf20Sopenharmony_civoid
2068c2ecf20Sopenharmony_cixchk_set_corrupt(
2078c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
2108c2ecf20Sopenharmony_ci	trace_xchk_fs_error(sc, 0, __return_address);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/* Record a corrupt block. */
2148c2ecf20Sopenharmony_civoid
2158c2ecf20Sopenharmony_cixchk_block_set_corrupt(
2168c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2178c2ecf20Sopenharmony_ci	struct xfs_buf		*bp)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
2208c2ecf20Sopenharmony_ci	trace_xchk_block_error(sc, bp->b_bn, __return_address);
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/* Record a corruption while cross-referencing. */
2248c2ecf20Sopenharmony_civoid
2258c2ecf20Sopenharmony_cixchk_block_xref_set_corrupt(
2268c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2278c2ecf20Sopenharmony_ci	struct xfs_buf		*bp)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
2308c2ecf20Sopenharmony_ci	trace_xchk_block_error(sc, bp->b_bn, __return_address);
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci/*
2348c2ecf20Sopenharmony_ci * Record a corrupt inode.  The trace data will include the block given
2358c2ecf20Sopenharmony_ci * by bp if bp is given; otherwise it will use the block location of the
2368c2ecf20Sopenharmony_ci * inode record itself.
2378c2ecf20Sopenharmony_ci */
2388c2ecf20Sopenharmony_civoid
2398c2ecf20Sopenharmony_cixchk_ino_set_corrupt(
2408c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2418c2ecf20Sopenharmony_ci	xfs_ino_t		ino)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
2448c2ecf20Sopenharmony_ci	trace_xchk_ino_error(sc, ino, __return_address);
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* Record a corruption while cross-referencing with an inode. */
2488c2ecf20Sopenharmony_civoid
2498c2ecf20Sopenharmony_cixchk_ino_xref_set_corrupt(
2508c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2518c2ecf20Sopenharmony_ci	xfs_ino_t		ino)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
2548c2ecf20Sopenharmony_ci	trace_xchk_ino_error(sc, ino, __return_address);
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci/* Record corruption in a block indexed by a file fork. */
2588c2ecf20Sopenharmony_civoid
2598c2ecf20Sopenharmony_cixchk_fblock_set_corrupt(
2608c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2618c2ecf20Sopenharmony_ci	int			whichfork,
2628c2ecf20Sopenharmony_ci	xfs_fileoff_t		offset)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
2658c2ecf20Sopenharmony_ci	trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci/* Record a corruption while cross-referencing a fork block. */
2698c2ecf20Sopenharmony_civoid
2708c2ecf20Sopenharmony_cixchk_fblock_xref_set_corrupt(
2718c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2728c2ecf20Sopenharmony_ci	int			whichfork,
2738c2ecf20Sopenharmony_ci	xfs_fileoff_t		offset)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
2768c2ecf20Sopenharmony_ci	trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci/*
2808c2ecf20Sopenharmony_ci * Warn about inodes that need administrative review but is not
2818c2ecf20Sopenharmony_ci * incorrect.
2828c2ecf20Sopenharmony_ci */
2838c2ecf20Sopenharmony_civoid
2848c2ecf20Sopenharmony_cixchk_ino_set_warning(
2858c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2868c2ecf20Sopenharmony_ci	xfs_ino_t		ino)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
2898c2ecf20Sopenharmony_ci	trace_xchk_ino_warning(sc, ino, __return_address);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/* Warn about a block indexed by a file fork that needs review. */
2938c2ecf20Sopenharmony_civoid
2948c2ecf20Sopenharmony_cixchk_fblock_set_warning(
2958c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2968c2ecf20Sopenharmony_ci	int			whichfork,
2978c2ecf20Sopenharmony_ci	xfs_fileoff_t		offset)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
3008c2ecf20Sopenharmony_ci	trace_xchk_fblock_warning(sc, whichfork, offset, __return_address);
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci/* Signal an incomplete scrub. */
3048c2ecf20Sopenharmony_civoid
3058c2ecf20Sopenharmony_cixchk_set_incomplete(
3068c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
3098c2ecf20Sopenharmony_ci	trace_xchk_incomplete(sc, __return_address);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/*
3138c2ecf20Sopenharmony_ci * rmap scrubbing -- compute the number of blocks with a given owner,
3148c2ecf20Sopenharmony_ci * at least according to the reverse mapping data.
3158c2ecf20Sopenharmony_ci */
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistruct xchk_rmap_ownedby_info {
3188c2ecf20Sopenharmony_ci	const struct xfs_owner_info	*oinfo;
3198c2ecf20Sopenharmony_ci	xfs_filblks_t			*blocks;
3208c2ecf20Sopenharmony_ci};
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ciSTATIC int
3238c2ecf20Sopenharmony_cixchk_count_rmap_ownedby_irec(
3248c2ecf20Sopenharmony_ci	struct xfs_btree_cur		*cur,
3258c2ecf20Sopenharmony_ci	struct xfs_rmap_irec		*rec,
3268c2ecf20Sopenharmony_ci	void				*priv)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	struct xchk_rmap_ownedby_info	*sroi = priv;
3298c2ecf20Sopenharmony_ci	bool				irec_attr;
3308c2ecf20Sopenharmony_ci	bool				oinfo_attr;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
3338c2ecf20Sopenharmony_ci	oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	if (rec->rm_owner != sroi->oinfo->oi_owner)
3368c2ecf20Sopenharmony_ci		return 0;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
3398c2ecf20Sopenharmony_ci		(*sroi->blocks) += rec->rm_blockcount;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	return 0;
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci/*
3458c2ecf20Sopenharmony_ci * Calculate the number of blocks the rmap thinks are owned by something.
3468c2ecf20Sopenharmony_ci * The caller should pass us an rmapbt cursor.
3478c2ecf20Sopenharmony_ci */
3488c2ecf20Sopenharmony_ciint
3498c2ecf20Sopenharmony_cixchk_count_rmap_ownedby_ag(
3508c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc,
3518c2ecf20Sopenharmony_ci	struct xfs_btree_cur		*cur,
3528c2ecf20Sopenharmony_ci	const struct xfs_owner_info	*oinfo,
3538c2ecf20Sopenharmony_ci	xfs_filblks_t			*blocks)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	struct xchk_rmap_ownedby_info	sroi = {
3568c2ecf20Sopenharmony_ci		.oinfo			= oinfo,
3578c2ecf20Sopenharmony_ci		.blocks			= blocks,
3588c2ecf20Sopenharmony_ci	};
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	*blocks = 0;
3618c2ecf20Sopenharmony_ci	return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec,
3628c2ecf20Sopenharmony_ci			&sroi);
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci/*
3668c2ecf20Sopenharmony_ci * AG scrubbing
3678c2ecf20Sopenharmony_ci *
3688c2ecf20Sopenharmony_ci * These helpers facilitate locking an allocation group's header
3698c2ecf20Sopenharmony_ci * buffers, setting up cursors for all btrees that are present, and
3708c2ecf20Sopenharmony_ci * cleaning everything up once we're through.
3718c2ecf20Sopenharmony_ci */
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/* Decide if we want to return an AG header read failure. */
3748c2ecf20Sopenharmony_cistatic inline bool
3758c2ecf20Sopenharmony_ciwant_ag_read_header_failure(
3768c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
3778c2ecf20Sopenharmony_ci	unsigned int		type)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	/* Return all AG header read failures when scanning btrees. */
3808c2ecf20Sopenharmony_ci	if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
3818c2ecf20Sopenharmony_ci	    sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
3828c2ecf20Sopenharmony_ci	    sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
3838c2ecf20Sopenharmony_ci		return true;
3848c2ecf20Sopenharmony_ci	/*
3858c2ecf20Sopenharmony_ci	 * If we're scanning a given type of AG header, we only want to
3868c2ecf20Sopenharmony_ci	 * see read failures from that specific header.  We'd like the
3878c2ecf20Sopenharmony_ci	 * other headers to cross-check them, but this isn't required.
3888c2ecf20Sopenharmony_ci	 */
3898c2ecf20Sopenharmony_ci	if (sc->sm->sm_type == type)
3908c2ecf20Sopenharmony_ci		return true;
3918c2ecf20Sopenharmony_ci	return false;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci/*
3958c2ecf20Sopenharmony_ci * Grab all the headers for an AG.
3968c2ecf20Sopenharmony_ci *
3978c2ecf20Sopenharmony_ci * The headers should be released by xchk_ag_free, but as a fail
3988c2ecf20Sopenharmony_ci * safe we attach all the buffers we grab to the scrub transaction so
3998c2ecf20Sopenharmony_ci * they'll all be freed when we cancel it.
4008c2ecf20Sopenharmony_ci */
4018c2ecf20Sopenharmony_ciint
4028c2ecf20Sopenharmony_cixchk_ag_read_headers(
4038c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
4048c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno,
4058c2ecf20Sopenharmony_ci	struct xfs_buf		**agi,
4068c2ecf20Sopenharmony_ci	struct xfs_buf		**agf,
4078c2ecf20Sopenharmony_ci	struct xfs_buf		**agfl)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
4108c2ecf20Sopenharmony_ci	int			error;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
4138c2ecf20Sopenharmony_ci	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
4148c2ecf20Sopenharmony_ci		goto out;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
4178c2ecf20Sopenharmony_ci	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
4188c2ecf20Sopenharmony_ci		goto out;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
4218c2ecf20Sopenharmony_ci	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
4228c2ecf20Sopenharmony_ci		goto out;
4238c2ecf20Sopenharmony_ci	error = 0;
4248c2ecf20Sopenharmony_ciout:
4258c2ecf20Sopenharmony_ci	return error;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci/* Release all the AG btree cursors. */
4298c2ecf20Sopenharmony_civoid
4308c2ecf20Sopenharmony_cixchk_ag_btcur_free(
4318c2ecf20Sopenharmony_ci	struct xchk_ag		*sa)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	if (sa->refc_cur)
4348c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
4358c2ecf20Sopenharmony_ci	if (sa->rmap_cur)
4368c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
4378c2ecf20Sopenharmony_ci	if (sa->fino_cur)
4388c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
4398c2ecf20Sopenharmony_ci	if (sa->ino_cur)
4408c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
4418c2ecf20Sopenharmony_ci	if (sa->cnt_cur)
4428c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
4438c2ecf20Sopenharmony_ci	if (sa->bno_cur)
4448c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	sa->refc_cur = NULL;
4478c2ecf20Sopenharmony_ci	sa->rmap_cur = NULL;
4488c2ecf20Sopenharmony_ci	sa->fino_cur = NULL;
4498c2ecf20Sopenharmony_ci	sa->ino_cur = NULL;
4508c2ecf20Sopenharmony_ci	sa->bno_cur = NULL;
4518c2ecf20Sopenharmony_ci	sa->cnt_cur = NULL;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci/* Initialize all the btree cursors for an AG. */
4558c2ecf20Sopenharmony_ciint
4568c2ecf20Sopenharmony_cixchk_ag_btcur_init(
4578c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
4588c2ecf20Sopenharmony_ci	struct xchk_ag		*sa)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
4618c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno = sa->agno;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	xchk_perag_get(sc->mp, sa);
4648c2ecf20Sopenharmony_ci	if (sa->agf_bp &&
4658c2ecf20Sopenharmony_ci	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_BNO)) {
4668c2ecf20Sopenharmony_ci		/* Set up a bnobt cursor for cross-referencing. */
4678c2ecf20Sopenharmony_ci		sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
4688c2ecf20Sopenharmony_ci				agno, XFS_BTNUM_BNO);
4698c2ecf20Sopenharmony_ci		if (!sa->bno_cur)
4708c2ecf20Sopenharmony_ci			goto err;
4718c2ecf20Sopenharmony_ci	}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	if (sa->agf_bp &&
4748c2ecf20Sopenharmony_ci	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_CNT)) {
4758c2ecf20Sopenharmony_ci		/* Set up a cntbt cursor for cross-referencing. */
4768c2ecf20Sopenharmony_ci		sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
4778c2ecf20Sopenharmony_ci				agno, XFS_BTNUM_CNT);
4788c2ecf20Sopenharmony_ci		if (!sa->cnt_cur)
4798c2ecf20Sopenharmony_ci			goto err;
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	/* Set up a inobt cursor for cross-referencing. */
4838c2ecf20Sopenharmony_ci	if (sa->agi_bp &&
4848c2ecf20Sopenharmony_ci	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_INO)) {
4858c2ecf20Sopenharmony_ci		sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
4868c2ecf20Sopenharmony_ci					agno, XFS_BTNUM_INO);
4878c2ecf20Sopenharmony_ci		if (!sa->ino_cur)
4888c2ecf20Sopenharmony_ci			goto err;
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	/* Set up a finobt cursor for cross-referencing. */
4928c2ecf20Sopenharmony_ci	if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb) &&
4938c2ecf20Sopenharmony_ci	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_FINO)) {
4948c2ecf20Sopenharmony_ci		sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
4958c2ecf20Sopenharmony_ci				agno, XFS_BTNUM_FINO);
4968c2ecf20Sopenharmony_ci		if (!sa->fino_cur)
4978c2ecf20Sopenharmony_ci			goto err;
4988c2ecf20Sopenharmony_ci	}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	/* Set up a rmapbt cursor for cross-referencing. */
5018c2ecf20Sopenharmony_ci	if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb) &&
5028c2ecf20Sopenharmony_ci	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_RMAP)) {
5038c2ecf20Sopenharmony_ci		sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
5048c2ecf20Sopenharmony_ci				agno);
5058c2ecf20Sopenharmony_ci		if (!sa->rmap_cur)
5068c2ecf20Sopenharmony_ci			goto err;
5078c2ecf20Sopenharmony_ci	}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	/* Set up a refcountbt cursor for cross-referencing. */
5108c2ecf20Sopenharmony_ci	if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb) &&
5118c2ecf20Sopenharmony_ci	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_REFC)) {
5128c2ecf20Sopenharmony_ci		sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
5138c2ecf20Sopenharmony_ci				sa->agf_bp, agno);
5148c2ecf20Sopenharmony_ci		if (!sa->refc_cur)
5158c2ecf20Sopenharmony_ci			goto err;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	return 0;
5198c2ecf20Sopenharmony_cierr:
5208c2ecf20Sopenharmony_ci	return -ENOMEM;
5218c2ecf20Sopenharmony_ci}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci/* Release the AG header context and btree cursors. */
5248c2ecf20Sopenharmony_civoid
5258c2ecf20Sopenharmony_cixchk_ag_free(
5268c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
5278c2ecf20Sopenharmony_ci	struct xchk_ag		*sa)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	xchk_ag_btcur_free(sa);
5308c2ecf20Sopenharmony_ci	if (sa->agfl_bp) {
5318c2ecf20Sopenharmony_ci		xfs_trans_brelse(sc->tp, sa->agfl_bp);
5328c2ecf20Sopenharmony_ci		sa->agfl_bp = NULL;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci	if (sa->agf_bp) {
5358c2ecf20Sopenharmony_ci		xfs_trans_brelse(sc->tp, sa->agf_bp);
5368c2ecf20Sopenharmony_ci		sa->agf_bp = NULL;
5378c2ecf20Sopenharmony_ci	}
5388c2ecf20Sopenharmony_ci	if (sa->agi_bp) {
5398c2ecf20Sopenharmony_ci		xfs_trans_brelse(sc->tp, sa->agi_bp);
5408c2ecf20Sopenharmony_ci		sa->agi_bp = NULL;
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci	if (sa->pag) {
5438c2ecf20Sopenharmony_ci		xfs_perag_put(sa->pag);
5448c2ecf20Sopenharmony_ci		sa->pag = NULL;
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci	sa->agno = NULLAGNUMBER;
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci/*
5508c2ecf20Sopenharmony_ci * For scrub, grab the AGI and the AGF headers, in that order.  Locking
5518c2ecf20Sopenharmony_ci * order requires us to get the AGI before the AGF.  We use the
5528c2ecf20Sopenharmony_ci * transaction to avoid deadlocking on crosslinked metadata buffers;
5538c2ecf20Sopenharmony_ci * either the caller passes one in (bmap scrub) or we have to create a
5548c2ecf20Sopenharmony_ci * transaction ourselves.
5558c2ecf20Sopenharmony_ci */
5568c2ecf20Sopenharmony_ciint
5578c2ecf20Sopenharmony_cixchk_ag_init(
5588c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
5598c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno,
5608c2ecf20Sopenharmony_ci	struct xchk_ag		*sa)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	int			error;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	sa->agno = agno;
5658c2ecf20Sopenharmony_ci	error = xchk_ag_read_headers(sc, agno, &sa->agi_bp,
5668c2ecf20Sopenharmony_ci			&sa->agf_bp, &sa->agfl_bp);
5678c2ecf20Sopenharmony_ci	if (error)
5688c2ecf20Sopenharmony_ci		return error;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	return xchk_ag_btcur_init(sc, sa);
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci/*
5748c2ecf20Sopenharmony_ci * Grab the per-ag structure if we haven't already gotten it.  Teardown of the
5758c2ecf20Sopenharmony_ci * xchk_ag will release it for us.
5768c2ecf20Sopenharmony_ci */
5778c2ecf20Sopenharmony_civoid
5788c2ecf20Sopenharmony_cixchk_perag_get(
5798c2ecf20Sopenharmony_ci	struct xfs_mount	*mp,
5808c2ecf20Sopenharmony_ci	struct xchk_ag		*sa)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	if (!sa->pag)
5838c2ecf20Sopenharmony_ci		sa->pag = xfs_perag_get(mp, sa->agno);
5848c2ecf20Sopenharmony_ci}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci/* Per-scrubber setup functions */
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci/*
5898c2ecf20Sopenharmony_ci * Grab an empty transaction so that we can re-grab locked buffers if
5908c2ecf20Sopenharmony_ci * one of our btrees turns out to be cyclic.
5918c2ecf20Sopenharmony_ci *
5928c2ecf20Sopenharmony_ci * If we're going to repair something, we need to ask for the largest possible
5938c2ecf20Sopenharmony_ci * log reservation so that we can handle the worst case scenario for metadata
5948c2ecf20Sopenharmony_ci * updates while rebuilding a metadata item.  We also need to reserve as many
5958c2ecf20Sopenharmony_ci * blocks in the head transaction as we think we're going to need to rebuild
5968c2ecf20Sopenharmony_ci * the metadata object.
5978c2ecf20Sopenharmony_ci */
5988c2ecf20Sopenharmony_ciint
5998c2ecf20Sopenharmony_cixchk_trans_alloc(
6008c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
6018c2ecf20Sopenharmony_ci	uint			resblks)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
6048c2ecf20Sopenharmony_ci		return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
6058c2ecf20Sopenharmony_ci				resblks, 0, 0, &sc->tp);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	return xfs_trans_alloc_empty(sc->mp, &sc->tp);
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci/* Set us up with a transaction and an empty context. */
6118c2ecf20Sopenharmony_ciint
6128c2ecf20Sopenharmony_cixchk_setup_fs(
6138c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
6148c2ecf20Sopenharmony_ci	struct xfs_inode	*ip)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	uint			resblks;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	resblks = xrep_calc_ag_resblks(sc);
6198c2ecf20Sopenharmony_ci	return xchk_trans_alloc(sc, resblks);
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci/* Set us up with AG headers and btree cursors. */
6238c2ecf20Sopenharmony_ciint
6248c2ecf20Sopenharmony_cixchk_setup_ag_btree(
6258c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
6268c2ecf20Sopenharmony_ci	struct xfs_inode	*ip,
6278c2ecf20Sopenharmony_ci	bool			force_log)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
6308c2ecf20Sopenharmony_ci	int			error;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	/*
6338c2ecf20Sopenharmony_ci	 * If the caller asks us to checkpont the log, do so.  This
6348c2ecf20Sopenharmony_ci	 * expensive operation should be performed infrequently and only
6358c2ecf20Sopenharmony_ci	 * as a last resort.  Any caller that sets force_log should
6368c2ecf20Sopenharmony_ci	 * document why they need to do so.
6378c2ecf20Sopenharmony_ci	 */
6388c2ecf20Sopenharmony_ci	if (force_log) {
6398c2ecf20Sopenharmony_ci		error = xchk_checkpoint_log(mp);
6408c2ecf20Sopenharmony_ci		if (error)
6418c2ecf20Sopenharmony_ci			return error;
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	error = xchk_setup_fs(sc, ip);
6458c2ecf20Sopenharmony_ci	if (error)
6468c2ecf20Sopenharmony_ci		return error;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa);
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci/* Push everything out of the log onto disk. */
6528c2ecf20Sopenharmony_ciint
6538c2ecf20Sopenharmony_cixchk_checkpoint_log(
6548c2ecf20Sopenharmony_ci	struct xfs_mount	*mp)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	int			error;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	error = xfs_log_force(mp, XFS_LOG_SYNC);
6598c2ecf20Sopenharmony_ci	if (error)
6608c2ecf20Sopenharmony_ci		return error;
6618c2ecf20Sopenharmony_ci	xfs_ail_push_all_sync(mp->m_ail);
6628c2ecf20Sopenharmony_ci	return 0;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci/*
6668c2ecf20Sopenharmony_ci * Given an inode and the scrub control structure, grab either the
6678c2ecf20Sopenharmony_ci * inode referenced in the control structure or the inode passed in.
6688c2ecf20Sopenharmony_ci * The inode is not locked.
6698c2ecf20Sopenharmony_ci */
6708c2ecf20Sopenharmony_ciint
6718c2ecf20Sopenharmony_cixchk_get_inode(
6728c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
6738c2ecf20Sopenharmony_ci	struct xfs_inode	*ip_in)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	struct xfs_imap		imap;
6768c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
6778c2ecf20Sopenharmony_ci	struct xfs_inode	*ip = NULL;
6788c2ecf20Sopenharmony_ci	int			error;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	/* We want to scan the inode we already had opened. */
6818c2ecf20Sopenharmony_ci	if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
6828c2ecf20Sopenharmony_ci		sc->ip = ip_in;
6838c2ecf20Sopenharmony_ci		return 0;
6848c2ecf20Sopenharmony_ci	}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	/* Look up the inode, see if the generation number matches. */
6878c2ecf20Sopenharmony_ci	if (xfs_internal_inum(mp, sc->sm->sm_ino))
6888c2ecf20Sopenharmony_ci		return -ENOENT;
6898c2ecf20Sopenharmony_ci	error = xfs_iget(mp, NULL, sc->sm->sm_ino,
6908c2ecf20Sopenharmony_ci			XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
6918c2ecf20Sopenharmony_ci	switch (error) {
6928c2ecf20Sopenharmony_ci	case -ENOENT:
6938c2ecf20Sopenharmony_ci		/* Inode doesn't exist, just bail out. */
6948c2ecf20Sopenharmony_ci		return error;
6958c2ecf20Sopenharmony_ci	case 0:
6968c2ecf20Sopenharmony_ci		/* Got an inode, continue. */
6978c2ecf20Sopenharmony_ci		break;
6988c2ecf20Sopenharmony_ci	case -EINVAL:
6998c2ecf20Sopenharmony_ci		/*
7008c2ecf20Sopenharmony_ci		 * -EINVAL with IGET_UNTRUSTED could mean one of several
7018c2ecf20Sopenharmony_ci		 * things: userspace gave us an inode number that doesn't
7028c2ecf20Sopenharmony_ci		 * correspond to fs space, or doesn't have an inobt entry;
7038c2ecf20Sopenharmony_ci		 * or it could simply mean that the inode buffer failed the
7048c2ecf20Sopenharmony_ci		 * read verifiers.
7058c2ecf20Sopenharmony_ci		 *
7068c2ecf20Sopenharmony_ci		 * Try just the inode mapping lookup -- if it succeeds, then
7078c2ecf20Sopenharmony_ci		 * the inode buffer verifier failed and something needs fixing.
7088c2ecf20Sopenharmony_ci		 * Otherwise, we really couldn't find it so tell userspace
7098c2ecf20Sopenharmony_ci		 * that it no longer exists.
7108c2ecf20Sopenharmony_ci		 */
7118c2ecf20Sopenharmony_ci		error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap,
7128c2ecf20Sopenharmony_ci				XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
7138c2ecf20Sopenharmony_ci		if (error)
7148c2ecf20Sopenharmony_ci			return -ENOENT;
7158c2ecf20Sopenharmony_ci		error = -EFSCORRUPTED;
7168c2ecf20Sopenharmony_ci		/* fall through */
7178c2ecf20Sopenharmony_ci	default:
7188c2ecf20Sopenharmony_ci		trace_xchk_op_error(sc,
7198c2ecf20Sopenharmony_ci				XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
7208c2ecf20Sopenharmony_ci				XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
7218c2ecf20Sopenharmony_ci				error, __return_address);
7228c2ecf20Sopenharmony_ci		return error;
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci	if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
7258c2ecf20Sopenharmony_ci		xfs_irele(ip);
7268c2ecf20Sopenharmony_ci		return -ENOENT;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	sc->ip = ip;
7308c2ecf20Sopenharmony_ci	return 0;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci/* Set us up to scrub a file's contents. */
7348c2ecf20Sopenharmony_ciint
7358c2ecf20Sopenharmony_cixchk_setup_inode_contents(
7368c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
7378c2ecf20Sopenharmony_ci	struct xfs_inode	*ip,
7388c2ecf20Sopenharmony_ci	unsigned int		resblks)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	int			error;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	error = xchk_get_inode(sc, ip);
7438c2ecf20Sopenharmony_ci	if (error)
7448c2ecf20Sopenharmony_ci		return error;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	/* Got the inode, lock it and we're ready to go. */
7478c2ecf20Sopenharmony_ci	sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
7488c2ecf20Sopenharmony_ci	xfs_ilock(sc->ip, sc->ilock_flags);
7498c2ecf20Sopenharmony_ci	error = xchk_trans_alloc(sc, resblks);
7508c2ecf20Sopenharmony_ci	if (error)
7518c2ecf20Sopenharmony_ci		goto out;
7528c2ecf20Sopenharmony_ci	sc->ilock_flags |= XFS_ILOCK_EXCL;
7538c2ecf20Sopenharmony_ci	xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ciout:
7568c2ecf20Sopenharmony_ci	/* scrub teardown will unlock and release the inode for us */
7578c2ecf20Sopenharmony_ci	return error;
7588c2ecf20Sopenharmony_ci}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci/*
7618c2ecf20Sopenharmony_ci * Predicate that decides if we need to evaluate the cross-reference check.
7628c2ecf20Sopenharmony_ci * If there was an error accessing the cross-reference btree, just delete
7638c2ecf20Sopenharmony_ci * the cursor and skip the check.
7648c2ecf20Sopenharmony_ci */
7658c2ecf20Sopenharmony_cibool
7668c2ecf20Sopenharmony_cixchk_should_check_xref(
7678c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
7688c2ecf20Sopenharmony_ci	int			*error,
7698c2ecf20Sopenharmony_ci	struct xfs_btree_cur	**curpp)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	/* No point in xref if we already know we're corrupt. */
7728c2ecf20Sopenharmony_ci	if (xchk_skip_xref(sc->sm))
7738c2ecf20Sopenharmony_ci		return false;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	if (*error == 0)
7768c2ecf20Sopenharmony_ci		return true;
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	if (curpp) {
7798c2ecf20Sopenharmony_ci		/* If we've already given up on xref, just bail out. */
7808c2ecf20Sopenharmony_ci		if (!*curpp)
7818c2ecf20Sopenharmony_ci			return false;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci		/* xref error, delete cursor and bail out. */
7848c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
7858c2ecf20Sopenharmony_ci		*curpp = NULL;
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
7898c2ecf20Sopenharmony_ci	trace_xchk_xref_error(sc, *error, __return_address);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	/*
7928c2ecf20Sopenharmony_ci	 * Errors encountered during cross-referencing with another
7938c2ecf20Sopenharmony_ci	 * data structure should not cause this scrubber to abort.
7948c2ecf20Sopenharmony_ci	 */
7958c2ecf20Sopenharmony_ci	*error = 0;
7968c2ecf20Sopenharmony_ci	return false;
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci/* Run the structure verifiers on in-memory buffers to detect bad memory. */
8008c2ecf20Sopenharmony_civoid
8018c2ecf20Sopenharmony_cixchk_buffer_recheck(
8028c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
8038c2ecf20Sopenharmony_ci	struct xfs_buf		*bp)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	xfs_failaddr_t		fa;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	if (bp->b_ops == NULL) {
8088c2ecf20Sopenharmony_ci		xchk_block_set_corrupt(sc, bp);
8098c2ecf20Sopenharmony_ci		return;
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci	if (bp->b_ops->verify_struct == NULL) {
8128c2ecf20Sopenharmony_ci		xchk_set_incomplete(sc);
8138c2ecf20Sopenharmony_ci		return;
8148c2ecf20Sopenharmony_ci	}
8158c2ecf20Sopenharmony_ci	fa = bp->b_ops->verify_struct(bp);
8168c2ecf20Sopenharmony_ci	if (!fa)
8178c2ecf20Sopenharmony_ci		return;
8188c2ecf20Sopenharmony_ci	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
8198c2ecf20Sopenharmony_ci	trace_xchk_block_error(sc, bp->b_bn, fa);
8208c2ecf20Sopenharmony_ci}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci/*
8238c2ecf20Sopenharmony_ci * Scrub the attr/data forks of a metadata inode.  The metadata inode must be
8248c2ecf20Sopenharmony_ci * pointed to by sc->ip and the ILOCK must be held.
8258c2ecf20Sopenharmony_ci */
8268c2ecf20Sopenharmony_ciint
8278c2ecf20Sopenharmony_cixchk_metadata_inode_forks(
8288c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
8298c2ecf20Sopenharmony_ci{
8308c2ecf20Sopenharmony_ci	__u32			smtype;
8318c2ecf20Sopenharmony_ci	bool			shared;
8328c2ecf20Sopenharmony_ci	int			error;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
8358c2ecf20Sopenharmony_ci		return 0;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	/* Metadata inodes don't live on the rt device. */
8388c2ecf20Sopenharmony_ci	if (sc->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
8398c2ecf20Sopenharmony_ci		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
8408c2ecf20Sopenharmony_ci		return 0;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	/* They should never participate in reflink. */
8448c2ecf20Sopenharmony_ci	if (xfs_is_reflink_inode(sc->ip)) {
8458c2ecf20Sopenharmony_ci		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
8468c2ecf20Sopenharmony_ci		return 0;
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	/* They also should never have extended attributes. */
8508c2ecf20Sopenharmony_ci	if (xfs_inode_hasattr(sc->ip)) {
8518c2ecf20Sopenharmony_ci		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
8528c2ecf20Sopenharmony_ci		return 0;
8538c2ecf20Sopenharmony_ci	}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	/* Invoke the data fork scrubber. */
8568c2ecf20Sopenharmony_ci	smtype = sc->sm->sm_type;
8578c2ecf20Sopenharmony_ci	sc->sm->sm_type = XFS_SCRUB_TYPE_BMBTD;
8588c2ecf20Sopenharmony_ci	error = xchk_bmap_data(sc);
8598c2ecf20Sopenharmony_ci	sc->sm->sm_type = smtype;
8608c2ecf20Sopenharmony_ci	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
8618c2ecf20Sopenharmony_ci		return error;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	/* Look for incorrect shared blocks. */
8648c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
8658c2ecf20Sopenharmony_ci		error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
8668c2ecf20Sopenharmony_ci				&shared);
8678c2ecf20Sopenharmony_ci		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
8688c2ecf20Sopenharmony_ci				&error))
8698c2ecf20Sopenharmony_ci			return error;
8708c2ecf20Sopenharmony_ci		if (shared)
8718c2ecf20Sopenharmony_ci			xchk_ino_set_corrupt(sc, sc->ip->i_ino);
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	return error;
8758c2ecf20Sopenharmony_ci}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci/*
8788c2ecf20Sopenharmony_ci * Try to lock an inode in violation of the usual locking order rules.  For
8798c2ecf20Sopenharmony_ci * example, trying to get the IOLOCK while in transaction context, or just
8808c2ecf20Sopenharmony_ci * plain breaking AG-order or inode-order inode locking rules.  Either way,
8818c2ecf20Sopenharmony_ci * the only way to avoid an ABBA deadlock is to use trylock and back off if
8828c2ecf20Sopenharmony_ci * we can't.
8838c2ecf20Sopenharmony_ci */
8848c2ecf20Sopenharmony_ciint
8858c2ecf20Sopenharmony_cixchk_ilock_inverted(
8868c2ecf20Sopenharmony_ci	struct xfs_inode	*ip,
8878c2ecf20Sopenharmony_ci	uint			lock_mode)
8888c2ecf20Sopenharmony_ci{
8898c2ecf20Sopenharmony_ci	int			i;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	for (i = 0; i < 20; i++) {
8928c2ecf20Sopenharmony_ci		if (xfs_ilock_nowait(ip, lock_mode))
8938c2ecf20Sopenharmony_ci			return 0;
8948c2ecf20Sopenharmony_ci		delay(1);
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci	return -EDEADLOCK;
8978c2ecf20Sopenharmony_ci}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci/* Pause background reaping of resources. */
9008c2ecf20Sopenharmony_civoid
9018c2ecf20Sopenharmony_cixchk_stop_reaping(
9028c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	sc->flags |= XCHK_REAPING_DISABLED;
9058c2ecf20Sopenharmony_ci	xfs_stop_block_reaping(sc->mp);
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci/* Restart background reaping of resources. */
9098c2ecf20Sopenharmony_civoid
9108c2ecf20Sopenharmony_cixchk_start_reaping(
9118c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
9128c2ecf20Sopenharmony_ci{
9138c2ecf20Sopenharmony_ci	xfs_start_block_reaping(sc->mp);
9148c2ecf20Sopenharmony_ci	sc->flags &= ~XCHK_REAPING_DISABLED;
9158c2ecf20Sopenharmony_ci}
916