2023-04-11 18:59:56 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-10-17 21:37:43 -07:00
|
|
|
/*
|
2023-04-11 18:59:57 -07:00
|
|
|
* Copyright (C) 2017-2023 Oracle. All Rights Reserved.
|
2023-04-11 18:59:56 -07:00
|
|
|
* Author: Darrick J. Wong <djwong@kernel.org>
|
2017-10-17 21:37:43 -07:00
|
|
|
*/
|
|
|
|
#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_bit.h"
|
|
|
|
#include "xfs_log_format.h"
|
|
|
|
#include "xfs_trans.h"
|
|
|
|
#include "xfs_inode.h"
|
|
|
|
#include "xfs_alloc.h"
|
|
|
|
#include "xfs_bmap.h"
|
|
|
|
#include "xfs_bmap_btree.h"
|
|
|
|
#include "xfs_rmap.h"
|
2018-03-23 10:06:53 -07:00
|
|
|
#include "xfs_rmap_btree.h"
|
2023-12-15 11:03:35 -07:00
|
|
|
#include "xfs_health.h"
|
2017-10-17 21:37:43 -07:00
|
|
|
#include "scrub/scrub.h"
|
|
|
|
#include "scrub/common.h"
|
|
|
|
#include "scrub/btree.h"
|
2023-12-15 11:03:35 -07:00
|
|
|
#include "scrub/health.h"
|
2021-06-01 17:48:24 -07:00
|
|
|
#include "xfs_ag.h"
|
2017-10-17 21:37:43 -07:00
|
|
|
|
|
|
|
/* Set us up with an inode's bmap. */
|
|
|
|
int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_setup_inode_bmap(
|
2021-04-07 17:59:39 -07:00
|
|
|
struct xfs_scrub *sc)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2018-07-19 12:29:12 -07:00
|
|
|
int error;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
2023-04-11 18:59:59 -07:00
|
|
|
if (xchk_need_intent_drain(sc))
|
|
|
|
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
|
|
|
|
|
2023-04-11 19:00:21 -07:00
|
|
|
error = xchk_iget_for_scrubbing(sc);
|
2017-10-17 21:37:43 -07:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
2023-08-10 07:48:08 -07:00
|
|
|
xchk_ilock(sc, XFS_IOLOCK_EXCL);
|
2017-10-17 21:37:43 -07:00
|
|
|
|
|
|
|
/*
|
2023-05-01 16:14:43 -07:00
|
|
|
* We don't want any ephemeral data/cow fork updates sitting around
|
2017-10-17 21:37:43 -07:00
|
|
|
* while we inspect block mappings, so wait for directio to finish
|
|
|
|
* and flush dirty data if we have delalloc reservations.
|
|
|
|
*/
|
|
|
|
if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
|
2023-05-01 16:14:43 -07:00
|
|
|
sc->sm->sm_type != XFS_SCRUB_TYPE_BMBTA) {
|
2020-06-29 14:47:17 -07:00
|
|
|
struct address_space *mapping = VFS_I(sc->ip)->i_mapping;
|
2023-12-15 11:03:39 -07:00
|
|
|
bool is_repair = xchk_could_repair(sc);
|
2020-06-29 14:47:17 -07:00
|
|
|
|
2023-08-10 07:48:08 -07:00
|
|
|
xchk_ilock(sc, XFS_MMAPLOCK_EXCL);
|
2023-04-11 19:00:22 -07:00
|
|
|
|
2023-12-15 11:03:39 -07:00
|
|
|
/* Break all our leases, we're going to mess with things. */
|
|
|
|
if (is_repair) {
|
|
|
|
error = xfs_break_layouts(VFS_I(sc->ip),
|
|
|
|
&sc->ilock_flags, BREAK_WRITE);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-10-17 21:37:43 -07:00
|
|
|
inode_dio_wait(VFS_I(sc->ip));
|
2020-06-29 14:47:17 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to flush all incore state to disk before we examine the
|
|
|
|
* space mappings for the data fork. Leave accumulated errors
|
|
|
|
* in the mapping for the writer threads to consume.
|
|
|
|
*
|
|
|
|
* On ENOSPC or EIO writeback errors, we continue into the
|
|
|
|
* extent mapping checks because write failures do not
|
|
|
|
* necessarily imply anything about the correctness of the file
|
|
|
|
* metadata. The metadata and the file data could be on
|
|
|
|
* completely separate devices; a media failure might only
|
|
|
|
* affect a subset of the disk, etc. We can handle delalloc
|
|
|
|
* extents in the scrubber, so leaving them in memory is fine.
|
|
|
|
*/
|
|
|
|
error = filemap_fdatawrite(mapping);
|
|
|
|
if (!error)
|
|
|
|
error = filemap_fdatawait_keep_errors(mapping);
|
|
|
|
if (error && (error != -ENOSPC && error != -EIO))
|
2017-10-17 21:37:43 -07:00
|
|
|
goto out;
|
2023-12-15 11:03:39 -07:00
|
|
|
|
|
|
|
/* Drop the page cache if we're repairing block mappings. */
|
|
|
|
if (is_repair) {
|
|
|
|
error = invalidate_inode_pages2(
|
|
|
|
VFS_I(sc->ip)->i_mapping);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Got the inode, lock it and we're ready to go. */
|
2018-07-19 12:29:11 -07:00
|
|
|
error = xchk_trans_alloc(sc, 0);
|
2017-10-17 21:37:43 -07:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
2023-12-15 11:03:34 -07:00
|
|
|
error = xchk_ino_dqattach(sc);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
2023-08-10 07:48:08 -07:00
|
|
|
xchk_ilock(sc, XFS_ILOCK_EXCL);
|
2017-10-17 21:37:43 -07:00
|
|
|
out:
|
|
|
|
/* scrub teardown will unlock and release the inode */
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inode fork block mapping (BMBT) scrubber.
|
|
|
|
* More complex than the others because we have to scrub
|
|
|
|
* all the extents regardless of whether or not the fork
|
|
|
|
* is in btree format.
|
|
|
|
*/
|
|
|
|
|
2018-07-19 12:29:11 -07:00
|
|
|
struct xchk_bmap_info {
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc;
|
2023-04-11 19:00:23 -07:00
|
|
|
|
|
|
|
/* Incore extent tree cursor */
|
2022-11-06 18:03:20 -07:00
|
|
|
struct xfs_iext_cursor icur;
|
2023-04-11 19:00:23 -07:00
|
|
|
|
|
|
|
/* Previous fork mapping that we examined */
|
|
|
|
struct xfs_bmbt_irec prev_rec;
|
|
|
|
|
|
|
|
/* Is this a realtime fork? */
|
2018-07-19 12:29:12 -07:00
|
|
|
bool is_rt;
|
2023-04-11 19:00:23 -07:00
|
|
|
|
|
|
|
/* May mappings point to shared space? */
|
2018-07-19 12:29:12 -07:00
|
|
|
bool is_shared;
|
2023-04-11 19:00:23 -07:00
|
|
|
|
|
|
|
/* Was the incore extent tree loaded? */
|
2019-08-26 17:06:00 -07:00
|
|
|
bool was_loaded;
|
2023-04-11 19:00:23 -07:00
|
|
|
|
|
|
|
/* Which inode fork are we checking? */
|
2018-07-19 12:29:12 -07:00
|
|
|
int whichfork;
|
2017-10-17 21:37:43 -07:00
|
|
|
};
|
|
|
|
|
2018-01-16 19:53:08 -07:00
|
|
|
/* Look for a corresponding rmap for this irec. */
|
|
|
|
static inline bool
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_get_rmap(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
struct xfs_bmbt_irec *irec,
|
|
|
|
xfs_agblock_t agbno,
|
|
|
|
uint64_t owner,
|
|
|
|
struct xfs_rmap_irec *rmap)
|
2018-01-16 19:53:08 -07:00
|
|
|
{
|
2018-07-19 12:29:12 -07:00
|
|
|
xfs_fileoff_t offset;
|
|
|
|
unsigned int rflags = 0;
|
|
|
|
int has_rmap;
|
|
|
|
int error;
|
2018-01-16 19:53:08 -07:00
|
|
|
|
|
|
|
if (info->whichfork == XFS_ATTR_FORK)
|
|
|
|
rflags |= XFS_RMAP_ATTR_FORK;
|
2020-11-08 17:32:43 -07:00
|
|
|
if (irec->br_state == XFS_EXT_UNWRITTEN)
|
|
|
|
rflags |= XFS_RMAP_UNWRITTEN;
|
2018-01-16 19:53:08 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CoW staging extents are owned (on disk) by the refcountbt, so
|
|
|
|
* their rmaps do not have offsets.
|
|
|
|
*/
|
|
|
|
if (info->whichfork == XFS_COW_FORK)
|
|
|
|
offset = 0;
|
|
|
|
else
|
|
|
|
offset = irec->br_startoff;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the caller thinks this could be a shared bmbt extent (IOWs,
|
|
|
|
* any data fork extent of a reflink inode) then we have to use the
|
|
|
|
* range rmap lookup to make sure we get the correct owner/offset.
|
|
|
|
*/
|
|
|
|
if (info->is_shared) {
|
|
|
|
error = xfs_rmap_lookup_le_range(info->sc->sa.rmap_cur, agbno,
|
|
|
|
owner, offset, rflags, rmap, &has_rmap);
|
2022-04-25 18:37:06 -07:00
|
|
|
} else {
|
|
|
|
error = xfs_rmap_lookup_le(info->sc->sa.rmap_cur, agbno,
|
|
|
|
owner, offset, rflags, rmap, &has_rmap);
|
2018-01-16 19:53:08 -07:00
|
|
|
}
|
2022-04-25 18:37:06 -07:00
|
|
|
if (!xchk_should_check_xref(info->sc, &error, &info->sc->sa.rmap_cur))
|
2018-01-16 19:53:08 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!has_rmap)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
2018-01-16 19:53:08 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
return has_rmap;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
/* Make sure that we have rmapbt records for this data/attr fork extent. */
|
2018-01-16 19:53:08 -07:00
|
|
|
STATIC void
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_xref_rmap(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
struct xfs_bmbt_irec *irec,
|
|
|
|
xfs_agblock_t agbno)
|
2018-01-16 19:53:08 -07:00
|
|
|
{
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_rmap_irec rmap;
|
|
|
|
unsigned long long rmap_end;
|
2023-04-11 19:00:24 -07:00
|
|
|
uint64_t owner = info->sc->ip->i_ino;
|
2018-01-16 19:53:08 -07:00
|
|
|
|
2018-07-19 12:29:11 -07:00
|
|
|
if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
|
2018-01-16 19:53:08 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Find the rmap record for this irec. */
|
2018-07-19 12:29:11 -07:00
|
|
|
if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
|
2018-01-16 19:53:08 -07:00
|
|
|
return;
|
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
/*
|
|
|
|
* The rmap must be an exact match for this incore file mapping record,
|
|
|
|
* which may have arisen from multiple ondisk records.
|
|
|
|
*/
|
|
|
|
if (rmap.rm_startblock != agbno)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
2018-01-16 19:53:08 -07:00
|
|
|
rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
|
2023-04-11 19:00:24 -07:00
|
|
|
if (rmap_end != agbno + irec->br_blockcount)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
2018-01-16 19:53:08 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
/* Check the logical offsets. */
|
|
|
|
if (rmap.rm_offset != irec->br_startoff)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
2018-01-16 19:53:08 -07:00
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
rmap_end = (unsigned long long)rmap.rm_offset + rmap.rm_blockcount;
|
|
|
|
if (rmap_end != irec->br_startoff + irec->br_blockcount)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
/* Check the owner */
|
2018-01-16 19:53:08 -07:00
|
|
|
if (rmap.rm_owner != owner)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
2018-01-16 19:53:08 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for discrepancies between the unwritten flag in the irec and
|
|
|
|
* the rmap. Note that the (in-memory) CoW fork distinguishes between
|
|
|
|
* unwritten and written extents, but we don't track that in the rmap
|
|
|
|
* records because the blocks are owned (on-disk) by the refcountbt,
|
|
|
|
* which doesn't track unwritten state.
|
|
|
|
*/
|
2023-04-11 19:00:24 -07:00
|
|
|
if (!!(irec->br_state == XFS_EXT_UNWRITTEN) !=
|
2020-11-08 17:32:41 -07:00
|
|
|
!!(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
2018-01-16 19:53:08 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
|
2020-11-08 17:32:41 -07:00
|
|
|
if (!!(info->whichfork == XFS_ATTR_FORK) !=
|
|
|
|
!!(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
2018-01-16 19:53:08 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
2018-01-16 19:53:08 -07:00
|
|
|
irec->br_startoff);
|
2023-04-11 19:00:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that we have rmapbt records for this COW fork extent. */
|
|
|
|
STATIC void
|
|
|
|
xchk_bmap_xref_rmap_cow(
|
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
struct xfs_bmbt_irec *irec,
|
|
|
|
xfs_agblock_t agbno)
|
|
|
|
{
|
|
|
|
struct xfs_rmap_irec rmap;
|
|
|
|
unsigned long long rmap_end;
|
|
|
|
uint64_t owner = XFS_RMAP_OWN_COW;
|
|
|
|
|
|
|
|
if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Find the rmap record for this irec. */
|
|
|
|
if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CoW staging extents are owned by the refcount btree, so the rmap
|
|
|
|
* can start before and end after the physical space allocated to this
|
|
|
|
* mapping. There are no offsets to check.
|
|
|
|
*/
|
|
|
|
if (rmap.rm_startblock > agbno)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
|
|
|
|
if (rmap_end < agbno + irec->br_blockcount)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
/* Check the owner */
|
|
|
|
if (rmap.rm_owner != owner)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
2022-11-06 18:03:20 -07:00
|
|
|
|
|
|
|
/*
|
2023-04-11 19:00:24 -07:00
|
|
|
* No flags allowed. Note that the (in-memory) CoW fork distinguishes
|
|
|
|
* between unwritten and written extents, but we don't track that in
|
|
|
|
* the rmap records because the blocks are owned (on-disk) by the
|
|
|
|
* refcountbt, which doesn't track unwritten state.
|
2022-11-06 18:03:20 -07:00
|
|
|
*/
|
2023-04-11 19:00:24 -07:00
|
|
|
if (rmap.rm_flags & XFS_RMAP_ATTR_FORK)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
if (rmap.rm_flags & XFS_RMAP_UNWRITTEN)
|
|
|
|
xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
2018-01-16 19:53:08 -07:00
|
|
|
}
|
|
|
|
|
2018-01-16 19:53:05 -07:00
|
|
|
/* Cross-reference a single rtdev extent record. */
|
|
|
|
STATIC void
|
2019-08-26 17:06:00 -07:00
|
|
|
xchk_bmap_rt_iextent_xref(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_inode *ip,
|
2019-08-26 17:06:00 -07:00
|
|
|
struct xchk_bmap_info *info,
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_bmbt_irec *irec)
|
2018-01-16 19:53:05 -07:00
|
|
|
{
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_xref_is_used_rt_space(info->sc, irec->br_startblock,
|
2018-01-16 19:53:10 -07:00
|
|
|
irec->br_blockcount);
|
2018-01-16 19:53:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cross-reference a single datadev extent record. */
|
|
|
|
STATIC void
|
2019-08-26 17:06:00 -07:00
|
|
|
xchk_bmap_iextent_xref(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_inode *ip,
|
2019-08-26 17:06:00 -07:00
|
|
|
struct xchk_bmap_info *info,
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_bmbt_irec *irec)
|
2018-01-16 19:53:05 -07:00
|
|
|
{
|
2023-04-11 19:00:16 -07:00
|
|
|
struct xfs_owner_info oinfo;
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_mount *mp = info->sc->mp;
|
|
|
|
xfs_agnumber_t agno;
|
|
|
|
xfs_agblock_t agbno;
|
|
|
|
xfs_extlen_t len;
|
|
|
|
int error;
|
2018-01-16 19:53:06 -07:00
|
|
|
|
|
|
|
agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
|
|
|
|
agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
|
|
|
|
len = irec->br_blockcount;
|
|
|
|
|
2021-08-06 11:06:35 -07:00
|
|
|
error = xchk_ag_init_existing(info->sc, agno, &info->sc->sa);
|
2018-07-19 12:29:11 -07:00
|
|
|
if (!xchk_fblock_process_error(info->sc, info->whichfork,
|
2018-01-16 19:53:06 -07:00
|
|
|
irec->br_startoff, &error))
|
xfs: fix perag structure refcounting error when scrub fails
The kernel test robot found the following bug when running xfs/355 to
scrub a bmap btree:
XFS: Assertion failed: !sa->pag, file: fs/xfs/scrub/common.c, line: 412
------------[ cut here ]------------
kernel BUG at fs/xfs/xfs_message.c:110!
invalid opcode: 0000 [#1] SMP PTI
CPU: 2 PID: 1415 Comm: xfs_scrub Not tainted 5.14.0-rc4-00021-g48c6615cc557 #1
Hardware name: Hewlett-Packard p6-1451cx/2ADA, BIOS 8.15 02/05/2013
RIP: 0010:assfail+0x23/0x28 [xfs]
RSP: 0018:ffffc9000aacb890 EFLAGS: 00010202
RAX: 0000000000000000 RBX: ffffc9000aacbcc8 RCX: 0000000000000000
RDX: 00000000ffffffc0 RSI: 000000000000000a RDI: ffffffffc09e7dcd
RBP: ffffc9000aacbc80 R08: ffff8881fdf17d50 R09: 0000000000000000
R10: 000000000000000a R11: f000000000000000 R12: 0000000000000000
R13: ffff88820c7ed000 R14: 0000000000000001 R15: ffffc9000aacb980
FS: 00007f185b955700(0000) GS:ffff8881fdf00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f7f6ef43000 CR3: 000000020de38002 CR4: 00000000001706e0
Call Trace:
xchk_ag_read_headers+0xda/0x100 [xfs]
xchk_ag_init+0x15/0x40 [xfs]
xchk_btree_check_block_owner+0x76/0x180 [xfs]
xchk_btree_get_block+0xd0/0x140 [xfs]
xchk_btree+0x32e/0x440 [xfs]
xchk_bmap_btree+0xd4/0x140 [xfs]
xchk_bmap+0x1eb/0x3c0 [xfs]
xfs_scrub_metadata+0x227/0x4c0 [xfs]
xfs_ioc_scrub_metadata+0x50/0xc0 [xfs]
xfs_file_ioctl+0x90c/0xc40 [xfs]
__x64_sys_ioctl+0x83/0xc0
do_syscall_64+0x3b/0xc0
The unusual handling of errors while initializing struct xchk_ag is the
root cause here. Since the beginning of xfs_scrub, the goal of
xchk_ag_read_headers has been to read all three AG header buffers and
attach them both to the xchk_ag structure and the scrub transaction.
Corruption errors on any of the three headers doesn't necessarily
trigger an immediate return to userspace, because xfs_scrub can also
tell us to /fix/ the problem.
In other words, it's possible for the xchk_ag init functions to return
an error code and a partially filled out structure so that scrub can use
however much information it managed to pull. Before 5.15, it was
sufficient to cancel (or commit) the scrub transaction on the way out of
the scrub code to release the buffers.
Ccommit 48c6615cc557 added a reference to the perag structure to struct
xchk_ag. Since perag structures are not attached to transactions like
buffers are, this adds the requirement that the perag ref be released
explicitly. The scrub teardown function xchk_teardown was amended to do
this for the xchk_ag embedded in struct xfs_scrub.
Unfortunately, I forgot that certain parts of the scrub code probe
multiple AGs and therefore handle the initialization and cleanup on
their own. Specifically, the bmbt scrubber will initialize it long
enough to cross-reference AG metadata for btree blocks and for the
extent mappings in the bmbt.
If one of the AG headers is corrupt, the init function returns with a
live perag structure reference and some of the AG header buffers. If an
error occurs, the cross referencing will be noted as XCORRUPTion and
skipped, but the main scrub process will move on to the next record.
It is now necessary to release the perag reference before we try to
analyze something from a different AG, or else we'll trip over the
assertion noted above.
Fixes: 48c6615cc557 ("xfs: grab active perag ref when reading AG headers")
Reported-by: kernel test robot <oliver.sang@intel.com>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>
2021-08-19 12:07:49 -07:00
|
|
|
goto out_free;
|
2018-01-16 19:53:06 -07:00
|
|
|
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_xref_is_used_space(info->sc, agbno, len);
|
|
|
|
xchk_xref_is_not_inode_chunk(info->sc, agbno, len);
|
2018-01-16 19:53:09 -07:00
|
|
|
switch (info->whichfork) {
|
|
|
|
case XFS_DATA_FORK:
|
2023-04-11 19:00:24 -07:00
|
|
|
xchk_bmap_xref_rmap(info, irec, agbno);
|
2023-04-11 19:00:16 -07:00
|
|
|
if (!xfs_is_reflink_inode(info->sc->ip)) {
|
|
|
|
xfs_rmap_ino_owner(&oinfo, info->sc->ip->i_ino,
|
|
|
|
info->whichfork, irec->br_startoff);
|
|
|
|
xchk_xref_is_only_owned_by(info->sc, agbno,
|
|
|
|
irec->br_blockcount, &oinfo);
|
2023-04-11 19:00:12 -07:00
|
|
|
xchk_xref_is_not_shared(info->sc, agbno,
|
|
|
|
irec->br_blockcount);
|
2023-04-11 19:00:16 -07:00
|
|
|
}
|
2023-04-11 19:00:12 -07:00
|
|
|
xchk_xref_is_not_cow_staging(info->sc, agbno,
|
|
|
|
irec->br_blockcount);
|
|
|
|
break;
|
2018-01-16 19:53:09 -07:00
|
|
|
case XFS_ATTR_FORK:
|
2023-04-11 19:00:24 -07:00
|
|
|
xchk_bmap_xref_rmap(info, irec, agbno);
|
2023-04-11 19:00:16 -07:00
|
|
|
xfs_rmap_ino_owner(&oinfo, info->sc->ip->i_ino,
|
|
|
|
info->whichfork, irec->br_startoff);
|
|
|
|
xchk_xref_is_only_owned_by(info->sc, agbno, irec->br_blockcount,
|
|
|
|
&oinfo);
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_xref_is_not_shared(info->sc, agbno,
|
2018-01-16 19:53:09 -07:00
|
|
|
irec->br_blockcount);
|
2023-04-11 19:00:12 -07:00
|
|
|
xchk_xref_is_not_cow_staging(info->sc, agbno,
|
|
|
|
irec->br_blockcount);
|
2018-01-16 19:53:09 -07:00
|
|
|
break;
|
|
|
|
case XFS_COW_FORK:
|
2023-04-11 19:00:24 -07:00
|
|
|
xchk_bmap_xref_rmap_cow(info, irec, agbno);
|
2023-04-11 19:00:16 -07:00
|
|
|
xchk_xref_is_only_owned_by(info->sc, agbno, irec->br_blockcount,
|
|
|
|
&XFS_RMAP_OINFO_COW);
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_xref_is_cow_staging(info->sc, agbno,
|
2018-01-16 19:53:09 -07:00
|
|
|
irec->br_blockcount);
|
2022-11-06 18:03:21 -07:00
|
|
|
xchk_xref_is_not_shared(info->sc, agbno,
|
|
|
|
irec->br_blockcount);
|
2018-01-16 19:53:09 -07:00
|
|
|
break;
|
|
|
|
}
|
2018-01-16 19:53:06 -07:00
|
|
|
|
xfs: fix perag structure refcounting error when scrub fails
The kernel test robot found the following bug when running xfs/355 to
scrub a bmap btree:
XFS: Assertion failed: !sa->pag, file: fs/xfs/scrub/common.c, line: 412
------------[ cut here ]------------
kernel BUG at fs/xfs/xfs_message.c:110!
invalid opcode: 0000 [#1] SMP PTI
CPU: 2 PID: 1415 Comm: xfs_scrub Not tainted 5.14.0-rc4-00021-g48c6615cc557 #1
Hardware name: Hewlett-Packard p6-1451cx/2ADA, BIOS 8.15 02/05/2013
RIP: 0010:assfail+0x23/0x28 [xfs]
RSP: 0018:ffffc9000aacb890 EFLAGS: 00010202
RAX: 0000000000000000 RBX: ffffc9000aacbcc8 RCX: 0000000000000000
RDX: 00000000ffffffc0 RSI: 000000000000000a RDI: ffffffffc09e7dcd
RBP: ffffc9000aacbc80 R08: ffff8881fdf17d50 R09: 0000000000000000
R10: 000000000000000a R11: f000000000000000 R12: 0000000000000000
R13: ffff88820c7ed000 R14: 0000000000000001 R15: ffffc9000aacb980
FS: 00007f185b955700(0000) GS:ffff8881fdf00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f7f6ef43000 CR3: 000000020de38002 CR4: 00000000001706e0
Call Trace:
xchk_ag_read_headers+0xda/0x100 [xfs]
xchk_ag_init+0x15/0x40 [xfs]
xchk_btree_check_block_owner+0x76/0x180 [xfs]
xchk_btree_get_block+0xd0/0x140 [xfs]
xchk_btree+0x32e/0x440 [xfs]
xchk_bmap_btree+0xd4/0x140 [xfs]
xchk_bmap+0x1eb/0x3c0 [xfs]
xfs_scrub_metadata+0x227/0x4c0 [xfs]
xfs_ioc_scrub_metadata+0x50/0xc0 [xfs]
xfs_file_ioctl+0x90c/0xc40 [xfs]
__x64_sys_ioctl+0x83/0xc0
do_syscall_64+0x3b/0xc0
The unusual handling of errors while initializing struct xchk_ag is the
root cause here. Since the beginning of xfs_scrub, the goal of
xchk_ag_read_headers has been to read all three AG header buffers and
attach them both to the xchk_ag structure and the scrub transaction.
Corruption errors on any of the three headers doesn't necessarily
trigger an immediate return to userspace, because xfs_scrub can also
tell us to /fix/ the problem.
In other words, it's possible for the xchk_ag init functions to return
an error code and a partially filled out structure so that scrub can use
however much information it managed to pull. Before 5.15, it was
sufficient to cancel (or commit) the scrub transaction on the way out of
the scrub code to release the buffers.
Ccommit 48c6615cc557 added a reference to the perag structure to struct
xchk_ag. Since perag structures are not attached to transactions like
buffers are, this adds the requirement that the perag ref be released
explicitly. The scrub teardown function xchk_teardown was amended to do
this for the xchk_ag embedded in struct xfs_scrub.
Unfortunately, I forgot that certain parts of the scrub code probe
multiple AGs and therefore handle the initialization and cleanup on
their own. Specifically, the bmbt scrubber will initialize it long
enough to cross-reference AG metadata for btree blocks and for the
extent mappings in the bmbt.
If one of the AG headers is corrupt, the init function returns with a
live perag structure reference and some of the AG header buffers. If an
error occurs, the cross referencing will be noted as XCORRUPTion and
skipped, but the main scrub process will move on to the next record.
It is now necessary to release the perag reference before we try to
analyze something from a different AG, or else we'll trip over the
assertion noted above.
Fixes: 48c6615cc557 ("xfs: grab active perag ref when reading AG headers")
Reported-by: kernel test robot <oliver.sang@intel.com>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>
2021-08-19 12:07:49 -07:00
|
|
|
out_free:
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_ag_free(info->sc, &info->sc->sa);
|
2018-01-16 19:53:05 -07:00
|
|
|
}
|
|
|
|
|
2019-02-01 10:08:53 -07:00
|
|
|
/*
|
|
|
|
* Directories and attr forks should never have blocks that can't be addressed
|
|
|
|
* by a xfs_dablk_t.
|
|
|
|
*/
|
|
|
|
STATIC void
|
|
|
|
xchk_bmap_dirattr_extent(
|
|
|
|
struct xfs_inode *ip,
|
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
struct xfs_bmbt_irec *irec)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = ip->i_mount;
|
|
|
|
xfs_fileoff_t off;
|
|
|
|
|
|
|
|
if (!S_ISDIR(VFS_I(ip)->i_mode) && info->whichfork != XFS_ATTR_FORK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!xfs_verify_dablk(mp, irec->br_startoff))
|
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
off = irec->br_startoff + irec->br_blockcount - 1;
|
|
|
|
if (!xfs_verify_dablk(mp, off))
|
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork, off);
|
|
|
|
}
|
|
|
|
|
2017-10-17 21:37:43 -07:00
|
|
|
/* Scrub a single extent record. */
|
2022-11-06 18:03:20 -07:00
|
|
|
STATIC void
|
2019-08-26 17:06:00 -07:00
|
|
|
xchk_bmap_iextent(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_inode *ip,
|
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
struct xfs_bmbt_irec *irec)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_mount *mp = info->sc->mp;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for out-of-order extents. This record could have come
|
|
|
|
* from the incore list, for which there is no ordering check.
|
|
|
|
*/
|
2023-04-11 19:00:23 -07:00
|
|
|
if (irec->br_startoff < info->prev_rec.br_startoff +
|
|
|
|
info->prev_rec.br_blockcount)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
2017-10-17 21:37:43 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
|
2020-12-04 14:28:35 -07:00
|
|
|
if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
|
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
2019-02-01 10:08:53 -07:00
|
|
|
xchk_bmap_dirattr_extent(ip, info, irec);
|
|
|
|
|
2017-10-17 21:37:43 -07:00
|
|
|
/* Make sure the extent points to a valid place. */
|
|
|
|
if (info->is_rt &&
|
2023-10-16 09:31:22 -07:00
|
|
|
!xfs_verify_rtbext(mp, irec->br_startblock, irec->br_blockcount))
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
2017-10-17 21:37:43 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
if (!info->is_rt &&
|
2020-12-04 14:20:00 -07:00
|
|
|
!xfs_verify_fsbext(mp, irec->br_startblock, irec->br_blockcount))
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
2017-10-17 21:37:43 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
/* We don't allow unwritten extents on attr forks. */
|
|
|
|
if (irec->br_state == XFS_EXT_UNWRITTEN &&
|
|
|
|
info->whichfork == XFS_ATTR_FORK)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
2017-10-17 21:37:43 -07:00
|
|
|
irec->br_startoff);
|
|
|
|
|
2019-08-26 17:06:00 -07:00
|
|
|
if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
2022-11-06 18:03:20 -07:00
|
|
|
return;
|
2019-08-26 17:06:00 -07:00
|
|
|
|
2018-01-16 19:53:05 -07:00
|
|
|
if (info->is_rt)
|
2019-08-26 17:06:00 -07:00
|
|
|
xchk_bmap_rt_iextent_xref(ip, info, irec);
|
2018-01-16 19:53:05 -07:00
|
|
|
else
|
2019-08-26 17:06:00 -07:00
|
|
|
xchk_bmap_iextent_xref(ip, info, irec);
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scrub a bmbt record. */
|
|
|
|
STATIC int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmapbt_rec(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xchk_btree *bs,
|
2021-08-10 17:02:17 -07:00
|
|
|
const union xfs_btree_rec *rec)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_bmbt_irec irec;
|
2019-08-26 17:06:00 -07:00
|
|
|
struct xfs_bmbt_irec iext_irec;
|
|
|
|
struct xfs_iext_cursor icur;
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xchk_bmap_info *info = bs->private;
|
2020-03-10 17:52:53 -07:00
|
|
|
struct xfs_inode *ip = bs->cur->bc_ino.ip;
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_buf *bp = NULL;
|
|
|
|
struct xfs_btree_block *block;
|
2022-07-09 10:56:05 -07:00
|
|
|
struct xfs_ifork *ifp = xfs_ifork_ptr(ip, info->whichfork);
|
2018-07-19 12:29:12 -07:00
|
|
|
uint64_t owner;
|
|
|
|
int i;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the owners of the btree blocks up to the level below
|
|
|
|
* the root since the verifiers don't do that.
|
|
|
|
*/
|
2021-08-18 18:46:37 -07:00
|
|
|
if (xfs_has_crc(bs->cur->bc_mp) &&
|
2021-09-16 12:24:04 -07:00
|
|
|
bs->cur->bc_levels[0].ptr == 1) {
|
2017-10-17 21:37:43 -07:00
|
|
|
for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
|
|
|
|
block = xfs_btree_get_block(bs->cur, i, &bp);
|
|
|
|
owner = be64_to_cpu(block->bb_u.l.bb_owner);
|
|
|
|
if (owner != ip->i_ino)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(bs->sc,
|
2017-10-17 21:37:43 -07:00
|
|
|
info->whichfork, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:06:00 -07:00
|
|
|
/*
|
|
|
|
* Check that the incore extent tree contains an extent that matches
|
|
|
|
* this one exactly. We validate those cached bmaps later, so we don't
|
|
|
|
* need to check them here. If the incore extent tree was just loaded
|
|
|
|
* from disk by the scrubber, we assume that its contents match what's
|
|
|
|
* on disk (we still hold the ILOCK) and skip the equivalence check.
|
|
|
|
*/
|
|
|
|
if (!info->was_loaded)
|
|
|
|
return 0;
|
|
|
|
|
2017-11-03 10:34:46 -07:00
|
|
|
xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
|
2023-04-11 19:00:04 -07:00
|
|
|
if (xfs_bmap_validate_extent(ip, info->whichfork, &irec) != NULL) {
|
|
|
|
xchk_fblock_set_corrupt(bs->sc, info->whichfork,
|
|
|
|
irec.br_startoff);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:06:00 -07:00
|
|
|
if (!xfs_iext_lookup_extent(ip, ifp, irec.br_startoff, &icur,
|
|
|
|
&iext_irec) ||
|
|
|
|
irec.br_startoff != iext_irec.br_startoff ||
|
|
|
|
irec.br_startblock != iext_irec.br_startblock ||
|
|
|
|
irec.br_blockcount != iext_irec.br_blockcount ||
|
|
|
|
irec.br_state != iext_irec.br_state)
|
|
|
|
xchk_fblock_set_corrupt(bs->sc, info->whichfork,
|
|
|
|
irec.br_startoff);
|
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan the btree records. */
|
|
|
|
STATIC int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_btree(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc,
|
2018-07-19 12:29:12 -07:00
|
|
|
int whichfork,
|
|
|
|
struct xchk_bmap_info *info)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_owner_info oinfo;
|
2022-07-09 10:56:05 -07:00
|
|
|
struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, whichfork);
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
struct xfs_inode *ip = sc->ip;
|
|
|
|
struct xfs_btree_cur *cur;
|
|
|
|
int error;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
2019-08-26 17:06:00 -07:00
|
|
|
/* Load the incore bmap cache if it's not loaded. */
|
2021-04-13 11:15:12 -07:00
|
|
|
info->was_loaded = !xfs_need_iread_extents(ifp);
|
2021-04-13 11:15:09 -07:00
|
|
|
|
|
|
|
error = xfs_iread_extents(sc->tp, ip, whichfork);
|
|
|
|
if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
|
|
|
|
goto out;
|
2019-08-26 17:06:00 -07:00
|
|
|
|
|
|
|
/* Check the btree structure. */
|
2017-10-17 21:37:43 -07:00
|
|
|
cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
|
|
|
|
xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
|
2018-07-19 12:29:11 -07:00
|
|
|
error = xchk_btree(sc, cur, xchk_bmapbt_rec, &oinfo, info);
|
2018-07-19 12:26:31 -07:00
|
|
|
xfs_btree_del_cursor(cur, error);
|
2019-08-26 17:06:00 -07:00
|
|
|
out:
|
2017-10-17 21:37:43 -07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:29:11 -07:00
|
|
|
struct xchk_bmap_check_rmap_info {
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc;
|
2018-07-19 12:29:12 -07:00
|
|
|
int whichfork;
|
|
|
|
struct xfs_iext_cursor icur;
|
2018-03-23 10:06:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Can we find bmaps that fit this rmap? */
|
|
|
|
STATIC int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_check_rmap(
|
2018-03-23 10:06:53 -07:00
|
|
|
struct xfs_btree_cur *cur,
|
2021-08-10 17:02:16 -07:00
|
|
|
const struct xfs_rmap_irec *rec,
|
2018-03-23 10:06:53 -07:00
|
|
|
void *priv)
|
|
|
|
{
|
|
|
|
struct xfs_bmbt_irec irec;
|
2021-08-10 17:02:16 -07:00
|
|
|
struct xfs_rmap_irec check_rec;
|
2018-07-19 12:29:11 -07:00
|
|
|
struct xchk_bmap_check_rmap_info *sbcri = priv;
|
2018-03-23 10:06:53 -07:00
|
|
|
struct xfs_ifork *ifp;
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc = sbcri->sc;
|
2018-03-23 10:06:53 -07:00
|
|
|
bool have_map;
|
|
|
|
|
|
|
|
/* Is this even the right fork? */
|
|
|
|
if (rec->rm_owner != sc->ip->i_ino)
|
|
|
|
return 0;
|
|
|
|
if ((sbcri->whichfork == XFS_ATTR_FORK) ^
|
|
|
|
!!(rec->rm_flags & XFS_RMAP_ATTR_FORK))
|
|
|
|
return 0;
|
|
|
|
if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Now look up the bmbt record. */
|
2022-07-09 10:56:05 -07:00
|
|
|
ifp = xfs_ifork_ptr(sc->ip, sbcri->whichfork);
|
2018-03-23 10:06:53 -07:00
|
|
|
if (!ifp) {
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, sbcri->whichfork,
|
2018-03-23 10:06:53 -07:00
|
|
|
rec->rm_offset);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset,
|
|
|
|
&sbcri->icur, &irec);
|
|
|
|
if (!have_map)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, sbcri->whichfork,
|
2018-03-23 10:06:53 -07:00
|
|
|
rec->rm_offset);
|
|
|
|
/*
|
|
|
|
* bmap extent record lengths are constrained to 2^21 blocks in length
|
|
|
|
* because of space constraints in the on-disk metadata structure.
|
|
|
|
* However, rmap extent record lengths are constrained only by AG
|
|
|
|
* length, so we have to loop through the bmbt to make sure that the
|
|
|
|
* entire rmap is covered by bmbt records.
|
|
|
|
*/
|
2021-08-10 17:02:16 -07:00
|
|
|
check_rec = *rec;
|
2018-03-23 10:06:53 -07:00
|
|
|
while (have_map) {
|
2021-08-10 17:02:16 -07:00
|
|
|
if (irec.br_startoff != check_rec.rm_offset)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, sbcri->whichfork,
|
2021-08-10 17:02:16 -07:00
|
|
|
check_rec.rm_offset);
|
2018-03-23 10:06:53 -07:00
|
|
|
if (irec.br_startblock != XFS_AGB_TO_FSB(sc->mp,
|
2021-08-10 17:02:16 -07:00
|
|
|
cur->bc_ag.pag->pag_agno,
|
|
|
|
check_rec.rm_startblock))
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, sbcri->whichfork,
|
2021-08-10 17:02:16 -07:00
|
|
|
check_rec.rm_offset);
|
|
|
|
if (irec.br_blockcount > check_rec.rm_blockcount)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, sbcri->whichfork,
|
2021-08-10 17:02:16 -07:00
|
|
|
check_rec.rm_offset);
|
2018-03-23 10:06:53 -07:00
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
break;
|
2021-08-10 17:02:16 -07:00
|
|
|
check_rec.rm_startblock += irec.br_blockcount;
|
|
|
|
check_rec.rm_offset += irec.br_blockcount;
|
|
|
|
check_rec.rm_blockcount -= irec.br_blockcount;
|
|
|
|
if (check_rec.rm_blockcount == 0)
|
2018-03-23 10:06:53 -07:00
|
|
|
break;
|
|
|
|
have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec);
|
|
|
|
if (!have_map)
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, sbcri->whichfork,
|
2021-08-10 17:02:16 -07:00
|
|
|
check_rec.rm_offset);
|
2018-03-23 10:06:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
2019-08-28 14:37:57 -07:00
|
|
|
return -ECANCELED;
|
2018-03-23 10:06:53 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure each rmap has a corresponding bmbt entry. */
|
|
|
|
STATIC int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_check_ag_rmaps(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc,
|
2018-03-23 10:06:53 -07:00
|
|
|
int whichfork,
|
2021-06-01 17:48:24 -07:00
|
|
|
struct xfs_perag *pag)
|
2018-03-23 10:06:53 -07:00
|
|
|
{
|
2018-07-19 12:29:11 -07:00
|
|
|
struct xchk_bmap_check_rmap_info sbcri;
|
2018-03-23 10:06:53 -07:00
|
|
|
struct xfs_btree_cur *cur;
|
|
|
|
struct xfs_buf *agf;
|
|
|
|
int error;
|
|
|
|
|
2022-07-07 02:07:40 -07:00
|
|
|
error = xfs_alloc_read_agf(pag, sc->tp, 0, &agf);
|
2018-03-23 10:06:53 -07:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2021-06-01 17:48:24 -07:00
|
|
|
cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, pag);
|
2018-03-23 10:06:53 -07:00
|
|
|
|
|
|
|
sbcri.sc = sc;
|
|
|
|
sbcri.whichfork = whichfork;
|
2018-07-19 12:29:11 -07:00
|
|
|
error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri);
|
2019-08-28 14:37:57 -07:00
|
|
|
if (error == -ECANCELED)
|
2018-03-23 10:06:53 -07:00
|
|
|
error = 0;
|
|
|
|
|
2018-07-19 12:26:31 -07:00
|
|
|
xfs_btree_del_cursor(cur, error);
|
2018-03-23 10:06:53 -07:00
|
|
|
xfs_trans_brelse(sc->tp, agf);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2023-12-15 11:03:38 -07:00
|
|
|
/*
|
|
|
|
* Decide if we want to scan the reverse mappings to determine if the attr
|
|
|
|
* fork /really/ has zero space mappings.
|
|
|
|
*/
|
|
|
|
STATIC bool
|
|
|
|
xchk_bmap_check_empty_attrfork(
|
|
|
|
struct xfs_inode *ip)
|
|
|
|
{
|
|
|
|
struct xfs_ifork *ifp = &ip->i_af;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the dinode repair found a bad attr fork, it will reset the fork
|
|
|
|
* to extents format with zero records and wait for the this scrubber
|
|
|
|
* to reconstruct the block mappings. If the fork is not in this
|
|
|
|
* state, then the fork cannot have been zapped.
|
|
|
|
*/
|
|
|
|
if (ifp->if_format != XFS_DINODE_FMT_EXTENTS || ifp->if_nextents != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Files can have an attr fork in EXTENTS format with zero records for
|
|
|
|
* several reasons:
|
|
|
|
*
|
|
|
|
* a) an attr set created a fork but ran out of space
|
|
|
|
* b) attr replace deleted an old attr but failed during the set step
|
|
|
|
* c) the data fork was in btree format when all attrs were deleted, so
|
|
|
|
* the fork was left in place
|
|
|
|
* d) the inode repair code zapped the fork
|
|
|
|
*
|
|
|
|
* Only in case (d) do we want to scan the rmapbt to see if we need to
|
|
|
|
* rebuild the attr fork. The fork zap code clears all DAC permission
|
|
|
|
* bits and zeroes the uid and gid, so avoid the scan if any of those
|
|
|
|
* three conditions are not met.
|
|
|
|
*/
|
|
|
|
if ((VFS_I(ip)->i_mode & 0777) != 0)
|
|
|
|
return false;
|
|
|
|
if (!uid_eq(VFS_I(ip)->i_uid, GLOBAL_ROOT_UID))
|
|
|
|
return false;
|
|
|
|
if (!gid_eq(VFS_I(ip)->i_gid, GLOBAL_ROOT_GID))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decide if we want to scan the reverse mappings to determine if the data
|
|
|
|
* fork /really/ has zero space mappings.
|
|
|
|
*/
|
|
|
|
STATIC bool
|
|
|
|
xchk_bmap_check_empty_datafork(
|
|
|
|
struct xfs_inode *ip)
|
|
|
|
{
|
|
|
|
struct xfs_ifork *ifp = &ip->i_df;
|
|
|
|
|
|
|
|
/* Don't support realtime rmap checks yet. */
|
|
|
|
if (XFS_IS_REALTIME_INODE(ip))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the dinode repair found a bad data fork, it will reset the fork
|
|
|
|
* to extents format with zero records and wait for the this scrubber
|
|
|
|
* to reconstruct the block mappings. If the fork is not in this
|
|
|
|
* state, then the fork cannot have been zapped.
|
|
|
|
*/
|
|
|
|
if (ifp->if_format != XFS_DINODE_FMT_EXTENTS || ifp->if_nextents != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we encounter an empty data fork along with evidence that the fork
|
|
|
|
* might not really be empty, we need to scan the reverse mappings to
|
|
|
|
* decide if we're going to rebuild the fork. Data forks with nonzero
|
|
|
|
* file size are scanned.
|
|
|
|
*/
|
|
|
|
return i_size_read(VFS_I(ip)) != 0;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:00:25 -07:00
|
|
|
/*
|
|
|
|
* Decide if we want to walk every rmap btree in the fs to make sure that each
|
|
|
|
* rmap for this file fork has corresponding bmbt entries.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
xchk_bmap_want_check_rmaps(
|
|
|
|
struct xchk_bmap_info *info)
|
2018-03-23 10:06:53 -07:00
|
|
|
{
|
2023-04-11 19:00:25 -07:00
|
|
|
struct xfs_scrub *sc = info->sc;
|
2018-03-23 10:06:53 -07:00
|
|
|
|
2023-04-11 19:00:25 -07:00
|
|
|
if (!xfs_has_rmapbt(sc->mp))
|
|
|
|
return false;
|
|
|
|
if (info->whichfork == XFS_COW_FORK)
|
|
|
|
return false;
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
return false;
|
2018-03-23 10:06:53 -07:00
|
|
|
|
2023-12-15 11:03:38 -07:00
|
|
|
if (info->whichfork == XFS_ATTR_FORK)
|
|
|
|
return xchk_bmap_check_empty_attrfork(sc->ip);
|
2023-04-11 19:00:25 -07:00
|
|
|
|
2023-12-15 11:03:38 -07:00
|
|
|
return xchk_bmap_check_empty_datafork(sc->ip);
|
2023-04-11 19:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure each rmap has a corresponding bmbt entry. */
|
|
|
|
STATIC int
|
|
|
|
xchk_bmap_check_rmaps(
|
|
|
|
struct xfs_scrub *sc,
|
|
|
|
int whichfork)
|
|
|
|
{
|
|
|
|
struct xfs_perag *pag;
|
|
|
|
xfs_agnumber_t agno;
|
|
|
|
int error;
|
2018-03-23 10:06:53 -07:00
|
|
|
|
2021-06-01 17:48:24 -07:00
|
|
|
for_each_perag(sc->mp, agno, pag) {
|
2021-06-01 17:48:24 -07:00
|
|
|
error = xchk_bmap_check_ag_rmaps(sc, whichfork, pag);
|
2022-11-06 18:03:19 -07:00
|
|
|
if (error ||
|
|
|
|
(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
|
2023-02-12 15:14:42 -07:00
|
|
|
xfs_perag_rele(pag);
|
2022-11-06 18:03:19 -07:00
|
|
|
return error;
|
|
|
|
}
|
2018-03-23 10:06:53 -07:00
|
|
|
}
|
2022-11-06 18:03:19 -07:00
|
|
|
|
|
|
|
return 0;
|
2018-03-23 10:06:53 -07:00
|
|
|
}
|
|
|
|
|
2022-11-06 18:03:20 -07:00
|
|
|
/* Scrub a delalloc reservation from the incore extent map tree. */
|
|
|
|
STATIC void
|
|
|
|
xchk_bmap_iextent_delalloc(
|
|
|
|
struct xfs_inode *ip,
|
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
struct xfs_bmbt_irec *irec)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = info->sc->mp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for out-of-order extents. This record could have come
|
|
|
|
* from the incore list, for which there is no ordering check.
|
|
|
|
*/
|
2023-04-11 19:00:23 -07:00
|
|
|
if (irec->br_startoff < info->prev_rec.br_startoff +
|
|
|
|
info->prev_rec.br_blockcount)
|
2022-11-06 18:03:20 -07:00
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
|
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
|
|
|
|
/* Make sure the extent points to a valid place. */
|
|
|
|
if (irec->br_blockcount > XFS_MAX_BMBT_EXTLEN)
|
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
/* Decide if this individual fork mapping is ok. */
|
|
|
|
static bool
|
|
|
|
xchk_bmap_iext_mapping(
|
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
const struct xfs_bmbt_irec *irec)
|
|
|
|
{
|
|
|
|
/* There should never be a "hole" extent in either extent list. */
|
|
|
|
if (irec->br_startblock == HOLESTARTBLOCK)
|
|
|
|
return false;
|
|
|
|
if (irec->br_blockcount > XFS_MAX_BMBT_EXTLEN)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are these two mappings contiguous with each other? */
|
|
|
|
static inline bool
|
|
|
|
xchk_are_bmaps_contiguous(
|
|
|
|
const struct xfs_bmbt_irec *b1,
|
|
|
|
const struct xfs_bmbt_irec *b2)
|
|
|
|
{
|
|
|
|
/* Don't try to combine unallocated mappings. */
|
|
|
|
if (!xfs_bmap_is_real_extent(b1))
|
|
|
|
return false;
|
|
|
|
if (!xfs_bmap_is_real_extent(b2))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Does b2 come right after b1 in the logical and physical range? */
|
|
|
|
if (b1->br_startoff + b1->br_blockcount != b2->br_startoff)
|
|
|
|
return false;
|
|
|
|
if (b1->br_startblock + b1->br_blockcount != b2->br_startblock)
|
|
|
|
return false;
|
|
|
|
if (b1->br_state != b2->br_state)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the incore extent records, accumulating consecutive contiguous records
|
|
|
|
* into a single incore mapping. Returns true if @irec has been set to a
|
|
|
|
* mapping or false if there are no more mappings. Caller must ensure that
|
|
|
|
* @info.icur is zeroed before the first call.
|
|
|
|
*/
|
2023-06-04 21:48:12 -07:00
|
|
|
static bool
|
2023-04-11 19:00:24 -07:00
|
|
|
xchk_bmap_iext_iter(
|
|
|
|
struct xchk_bmap_info *info,
|
|
|
|
struct xfs_bmbt_irec *irec)
|
|
|
|
{
|
|
|
|
struct xfs_bmbt_irec got;
|
|
|
|
struct xfs_ifork *ifp;
|
2023-06-04 21:48:12 -07:00
|
|
|
unsigned int nr = 0;
|
2023-04-11 19:00:24 -07:00
|
|
|
|
|
|
|
ifp = xfs_ifork_ptr(info->sc->ip, info->whichfork);
|
|
|
|
|
|
|
|
/* Advance to the next iextent record and check the mapping. */
|
|
|
|
xfs_iext_next(ifp, &info->icur);
|
|
|
|
if (!xfs_iext_get_extent(ifp, &info->icur, irec))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!xchk_bmap_iext_mapping(info, irec)) {
|
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
|
|
|
irec->br_startoff);
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-04 21:48:12 -07:00
|
|
|
nr++;
|
2023-04-11 19:00:24 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate subsequent iextent records and merge them with the one
|
|
|
|
* that we just read, if possible.
|
|
|
|
*/
|
|
|
|
while (xfs_iext_peek_next_extent(ifp, &info->icur, &got)) {
|
|
|
|
if (!xchk_are_bmaps_contiguous(irec, &got))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!xchk_bmap_iext_mapping(info, &got)) {
|
|
|
|
xchk_fblock_set_corrupt(info->sc, info->whichfork,
|
|
|
|
got.br_startoff);
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-04 21:48:12 -07:00
|
|
|
nr++;
|
2023-04-11 19:00:25 -07:00
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
irec->br_blockcount += got.br_blockcount;
|
|
|
|
xfs_iext_next(ifp, &info->icur);
|
|
|
|
}
|
|
|
|
|
2023-06-04 21:48:12 -07:00
|
|
|
/*
|
|
|
|
* If the merged mapping could be expressed with fewer bmbt records
|
|
|
|
* than we actually found, notify the user that this fork could be
|
|
|
|
* optimized. CoW forks only exist in memory so we ignore them.
|
|
|
|
*/
|
|
|
|
if (nr > 1 && info->whichfork != XFS_COW_FORK &&
|
|
|
|
howmany_64(irec->br_blockcount, XFS_MAX_BMBT_EXTLEN) < nr)
|
|
|
|
xchk_ino_set_preen(info->sc, info->sc->ip->i_ino);
|
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-17 21:37:43 -07:00
|
|
|
/*
|
|
|
|
* Scrub an inode fork's block mappings.
|
|
|
|
*
|
|
|
|
* First we scan every record in every btree block, if applicable.
|
|
|
|
* Then we unconditionally scan the incore extent cache.
|
|
|
|
*/
|
|
|
|
STATIC int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc,
|
2018-07-19 12:29:12 -07:00
|
|
|
int whichfork)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_bmbt_irec irec;
|
|
|
|
struct xchk_bmap_info info = { NULL };
|
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
struct xfs_inode *ip = sc->ip;
|
2022-07-09 10:56:05 -07:00
|
|
|
struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
|
2018-07-19 12:29:12 -07:00
|
|
|
xfs_fileoff_t endoff;
|
|
|
|
int error = 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
2020-05-16 10:19:29 -07:00
|
|
|
/* Non-existent forks can be ignored. */
|
|
|
|
if (!ifp)
|
2023-08-10 07:48:13 -07:00
|
|
|
return -ENOENT;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
2024-02-22 13:43:36 -07:00
|
|
|
info.is_rt = xfs_ifork_is_realtime(ip, whichfork);
|
2017-10-17 21:37:43 -07:00
|
|
|
info.whichfork = whichfork;
|
|
|
|
info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
|
|
|
|
info.sc = sc;
|
|
|
|
|
|
|
|
switch (whichfork) {
|
|
|
|
case XFS_COW_FORK:
|
2023-08-10 07:48:13 -07:00
|
|
|
/* No CoW forks on non-reflink filesystems. */
|
|
|
|
if (!xfs_has_reflink(mp)) {
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
|
2023-08-10 07:48:13 -07:00
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case XFS_ATTR_FORK:
|
2024-07-29 13:44:33 -07:00
|
|
|
/*
|
|
|
|
* "attr" means that an attr fork was created at some point in
|
|
|
|
* the life of this filesystem. "attr2" means that inodes have
|
|
|
|
* variable-sized data/attr fork areas. Hence we only check
|
|
|
|
* attr here.
|
|
|
|
*/
|
|
|
|
if (!xfs_has_attr(mp))
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
|
2017-10-17 21:37:43 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(whichfork == XFS_DATA_FORK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the fork values */
|
2020-05-18 10:28:05 -07:00
|
|
|
switch (ifp->if_format) {
|
2017-10-17 21:37:43 -07:00
|
|
|
case XFS_DINODE_FMT_UUID:
|
|
|
|
case XFS_DINODE_FMT_DEV:
|
|
|
|
case XFS_DINODE_FMT_LOCAL:
|
|
|
|
/* No mappings to check. */
|
2022-11-06 18:03:22 -07:00
|
|
|
if (whichfork == XFS_COW_FORK)
|
|
|
|
xchk_fblock_set_corrupt(sc, whichfork, 0);
|
2023-08-10 07:48:13 -07:00
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
case XFS_DINODE_FMT_EXTENTS:
|
|
|
|
break;
|
|
|
|
case XFS_DINODE_FMT_BTREE:
|
|
|
|
if (whichfork == XFS_COW_FORK) {
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, whichfork, 0);
|
2023-08-10 07:48:13 -07:00
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
2018-07-19 12:29:11 -07:00
|
|
|
error = xchk_bmap_btree(sc, whichfork, &info);
|
2017-10-17 21:37:43 -07:00
|
|
|
if (error)
|
2023-08-10 07:48:13 -07:00
|
|
|
return error;
|
2017-10-17 21:37:43 -07:00
|
|
|
break;
|
|
|
|
default:
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, whichfork, 0);
|
2023-08-10 07:48:13 -07:00
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
2023-08-10 07:48:13 -07:00
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
|
|
|
/* Find the offset of the last extent in the mapping. */
|
|
|
|
error = xfs_bmap_last_offset(ip, &endoff, whichfork);
|
2018-07-19 12:29:11 -07:00
|
|
|
if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
|
2023-08-10 07:48:13 -07:00
|
|
|
return error;
|
2017-10-17 21:37:43 -07:00
|
|
|
|
2023-04-11 19:00:24 -07:00
|
|
|
/*
|
|
|
|
* Scrub extent records. We use a special iterator function here that
|
|
|
|
* combines adjacent mappings if they are logically and physically
|
|
|
|
* contiguous. For large allocations that require multiple bmbt
|
|
|
|
* records, this reduces the number of cross-referencing calls, which
|
|
|
|
* reduces runtime. Cross referencing with the rmap is simpler because
|
|
|
|
* the rmap must match the combined mapping exactly.
|
|
|
|
*/
|
|
|
|
while (xchk_bmap_iext_iter(&info, &irec)) {
|
2018-07-19 12:29:11 -07:00
|
|
|
if (xchk_should_terminate(sc, &error) ||
|
2018-05-14 06:34:32 -07:00
|
|
|
(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
|
2023-08-10 07:48:13 -07:00
|
|
|
return 0;
|
2022-11-06 18:03:20 -07:00
|
|
|
|
2017-10-17 21:37:43 -07:00
|
|
|
if (irec.br_startoff >= endoff) {
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_fblock_set_corrupt(sc, whichfork,
|
2017-10-17 21:37:43 -07:00
|
|
|
irec.br_startoff);
|
2023-08-10 07:48:13 -07:00
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
2022-11-06 18:03:20 -07:00
|
|
|
|
|
|
|
if (isnullstartblock(irec.br_startblock))
|
|
|
|
xchk_bmap_iextent_delalloc(ip, &info, &irec);
|
|
|
|
else
|
|
|
|
xchk_bmap_iextent(ip, &info, &irec);
|
2023-04-11 19:00:23 -07:00
|
|
|
memcpy(&info.prev_rec, &irec, sizeof(struct xfs_bmbt_irec));
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:00:25 -07:00
|
|
|
if (xchk_bmap_want_check_rmaps(&info)) {
|
|
|
|
error = xchk_bmap_check_rmaps(sc, whichfork);
|
|
|
|
if (!xchk_fblock_xref_process_error(sc, whichfork, 0, &error))
|
2023-08-10 07:48:13 -07:00
|
|
|
return error;
|
2023-04-11 19:00:25 -07:00
|
|
|
}
|
2023-08-10 07:48:13 -07:00
|
|
|
|
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scrub an inode's data fork. */
|
|
|
|
int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_data(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2023-12-15 11:03:35 -07:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (xchk_file_looks_zapped(sc, XFS_SICK_INO_BMBTD_ZAPPED)) {
|
|
|
|
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = xchk_bmap(sc, XFS_DATA_FORK);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/* If the data fork is clean, it is clearly not zapped. */
|
|
|
|
xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_BMBTD_ZAPPED);
|
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scrub an inode's attr fork. */
|
|
|
|
int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_attr(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2023-12-15 11:03:35 -07:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the attr fork has been zapped, it's possible that forkoff was
|
|
|
|
* reset to zero and hence sc->ip->i_afp is NULL. We don't want the
|
|
|
|
* NULL ifp check in xchk_bmap to conclude that the attr fork is ok,
|
|
|
|
* so short circuit that logic by setting the corruption flag and
|
|
|
|
* returning immediately.
|
|
|
|
*/
|
|
|
|
if (xchk_file_looks_zapped(sc, XFS_SICK_INO_BMBTA_ZAPPED)) {
|
|
|
|
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = xchk_bmap(sc, XFS_ATTR_FORK);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/* If the attr fork is clean, it is clearly not zapped. */
|
|
|
|
xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_BMBTA_ZAPPED);
|
|
|
|
return 0;
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scrub an inode's CoW fork. */
|
|
|
|
int
|
2018-07-19 12:29:11 -07:00
|
|
|
xchk_bmap_cow(
|
2018-07-19 12:29:12 -07:00
|
|
|
struct xfs_scrub *sc)
|
2017-10-17 21:37:43 -07:00
|
|
|
{
|
2018-07-19 12:29:11 -07:00
|
|
|
return xchk_bmap(sc, XFS_COW_FORK);
|
2017-10-17 21:37:43 -07:00
|
|
|
}
|