2023-08-04 15:20:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-04-26 01:26:24 +00:00
|
|
|
/*
|
2023-04-12 01:59:57 +00:00
|
|
|
* Copyright (C) 2019-2023 Oracle. All Rights Reserved.
|
2023-04-12 01:59:56 +00:00
|
|
|
* Author: Darrick J. Wong <djwong@kernel.org>
|
2019-04-26 01:26:24 +00:00
|
|
|
*/
|
|
|
|
#include "xfs.h"
|
|
|
|
#include "xfs_fs.h"
|
|
|
|
#include "xfs_shared.h"
|
|
|
|
#include "xfs_format.h"
|
|
|
|
#include "xfs_trans_resv.h"
|
2023-08-04 15:20:57 +00:00
|
|
|
#include "xfs_log_format.h"
|
|
|
|
#include "xfs_trans.h"
|
2019-04-26 01:26:24 +00:00
|
|
|
#include "xfs_mount.h"
|
|
|
|
#include "xfs_alloc.h"
|
|
|
|
#include "xfs_ialloc.h"
|
|
|
|
#include "xfs_health.h"
|
2021-04-27 02:06:58 +00:00
|
|
|
#include "xfs_btree.h"
|
2021-06-02 00:48:24 +00:00
|
|
|
#include "xfs_ag.h"
|
2023-10-16 16:21:47 +00:00
|
|
|
#include "xfs_rtbitmap.h"
|
2022-11-07 01:03:19 +00:00
|
|
|
#include "xfs_inode.h"
|
2023-08-04 15:20:57 +00:00
|
|
|
#include "xfs_icache.h"
|
2019-04-26 01:26:24 +00:00
|
|
|
#include "scrub/scrub.h"
|
|
|
|
#include "scrub/common.h"
|
|
|
|
#include "scrub/trace.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FS Summary Counters
|
|
|
|
* ===================
|
|
|
|
*
|
|
|
|
* The basics of filesystem summary counter checking are that we iterate the
|
|
|
|
* AGs counting the number of free blocks, free space btree blocks, per-AG
|
|
|
|
* reservations, inodes, delayed allocation reservations, and free inodes.
|
|
|
|
* Then we compare what we computed against the in-core counters.
|
|
|
|
*
|
|
|
|
* However, the reality is that summary counters are a tricky beast to check.
|
|
|
|
* While we /could/ freeze the filesystem and scramble around the AGs counting
|
|
|
|
* the free blocks, in practice we prefer not do that for a scan because
|
|
|
|
* freezing is costly. To get around this, we added a per-cpu counter of the
|
|
|
|
* delalloc reservations so that we can rotor around the AGs relatively
|
|
|
|
* quickly, and we allow the counts to be slightly off because we're not taking
|
|
|
|
* any locks while we do this.
|
|
|
|
*
|
|
|
|
* So the first thing we do is warm up the buffer cache in the setup routine by
|
|
|
|
* walking all the AGs to make sure the incore per-AG structure has been
|
|
|
|
* initialized. The expected value calculation then iterates the incore per-AG
|
|
|
|
* structures as quickly as it can. We snapshot the percpu counters before and
|
|
|
|
* after this operation and use the difference in counter values to guess at
|
|
|
|
* our tolerance for mismatch between expected and actual counter values.
|
|
|
|
*/
|
|
|
|
|
2022-11-07 01:03:19 +00:00
|
|
|
struct xchk_fscounters {
|
|
|
|
struct xfs_scrub *sc;
|
|
|
|
uint64_t icount;
|
|
|
|
uint64_t ifree;
|
|
|
|
uint64_t fdblocks;
|
|
|
|
uint64_t frextents;
|
|
|
|
unsigned long long icount_min;
|
|
|
|
unsigned long long icount_max;
|
2023-08-04 15:20:57 +00:00
|
|
|
bool frozen;
|
2022-11-07 01:03:19 +00:00
|
|
|
};
|
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
/*
|
|
|
|
* Since the expected value computation is lockless but only browses incore
|
|
|
|
* values, the percpu counters should be fairly close to each other. However,
|
|
|
|
* we'll allow ourselves to be off by at least this (arbitrary) amount.
|
|
|
|
*/
|
|
|
|
#define XCHK_FSCOUNT_MIN_VARIANCE (512)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the per-AG structure has been initialized from the on-disk header
|
|
|
|
* contents and trust that the incore counters match the ondisk counters. (The
|
|
|
|
* AGF and AGI scrubbers check them, and a normal xfs_scrub run checks the
|
|
|
|
* summary counters after checking all AG headers). Do this from the setup
|
|
|
|
* function so that the inner AG aggregation loop runs as quickly as possible.
|
|
|
|
*
|
|
|
|
* This function runs during the setup phase /before/ we start checking any
|
|
|
|
* metadata.
|
|
|
|
*/
|
|
|
|
STATIC int
|
|
|
|
xchk_fscount_warmup(
|
|
|
|
struct xfs_scrub *sc)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
struct xfs_buf *agi_bp = NULL;
|
|
|
|
struct xfs_buf *agf_bp = NULL;
|
|
|
|
struct xfs_perag *pag = NULL;
|
|
|
|
xfs_agnumber_t agno;
|
|
|
|
int error = 0;
|
|
|
|
|
2021-06-02 00:48:24 +00:00
|
|
|
for_each_perag(mp, agno, pag) {
|
|
|
|
if (xchk_should_terminate(sc, &error))
|
|
|
|
break;
|
2023-02-12 22:14:52 +00:00
|
|
|
if (xfs_perag_initialised_agi(pag) &&
|
|
|
|
xfs_perag_initialised_agf(pag))
|
2021-06-02 00:48:24 +00:00
|
|
|
continue;
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
/* Lock both AG headers. */
|
2022-07-07 09:07:24 +00:00
|
|
|
error = xfs_ialloc_read_agi(pag, sc->tp, &agi_bp);
|
2019-04-26 01:26:24 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2022-07-07 09:07:40 +00:00
|
|
|
error = xfs_alloc_read_agf(pag, sc->tp, 0, &agf_bp);
|
2019-04-26 01:26:24 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are supposed to be initialized by the header read
|
|
|
|
* function.
|
|
|
|
*/
|
2023-02-12 22:14:52 +00:00
|
|
|
if (!xfs_perag_initialised_agi(pag) ||
|
|
|
|
!xfs_perag_initialised_agf(pag)) {
|
2021-06-02 00:48:24 +00:00
|
|
|
error = -EFSCORRUPTED;
|
2019-04-26 01:26:24 +00:00
|
|
|
break;
|
2021-06-02 00:48:24 +00:00
|
|
|
}
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
xfs_buf_relse(agf_bp);
|
|
|
|
agf_bp = NULL;
|
|
|
|
xfs_buf_relse(agi_bp);
|
|
|
|
agi_bp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (agf_bp)
|
|
|
|
xfs_buf_relse(agf_bp);
|
|
|
|
if (agi_bp)
|
|
|
|
xfs_buf_relse(agi_bp);
|
|
|
|
if (pag)
|
2023-02-12 22:14:42 +00:00
|
|
|
xfs_perag_rele(pag);
|
2019-04-26 01:26:24 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2023-08-04 15:20:57 +00:00
|
|
|
static inline int
|
|
|
|
xchk_fsfreeze(
|
|
|
|
struct xfs_scrub *sc)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = freeze_super(sc->mp->m_super, FREEZE_HOLDER_KERNEL);
|
|
|
|
trace_xchk_fsfreeze(sc, error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
xchk_fsthaw(
|
|
|
|
struct xfs_scrub *sc)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* This should always succeed, we have a kernel freeze */
|
|
|
|
error = thaw_super(sc->mp->m_super, FREEZE_HOLDER_KERNEL);
|
|
|
|
trace_xchk_fsthaw(sc, error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We couldn't stabilize the filesystem long enough to sample all the variables
|
|
|
|
* that comprise the summary counters and compare them to the percpu counters.
|
|
|
|
* We need to disable all writer threads, which means taking the first two
|
|
|
|
* freeze levels to put userspace to sleep, and the third freeze level to
|
|
|
|
* prevent background threads from starting new transactions. Take one level
|
|
|
|
* more to prevent other callers from unfreezing the filesystem while we run.
|
|
|
|
*/
|
|
|
|
STATIC int
|
|
|
|
xchk_fscounters_freeze(
|
|
|
|
struct xfs_scrub *sc)
|
|
|
|
{
|
|
|
|
struct xchk_fscounters *fsc = sc->buf;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (sc->flags & XCHK_HAVE_FREEZE_PROT) {
|
|
|
|
sc->flags &= ~XCHK_HAVE_FREEZE_PROT;
|
|
|
|
mnt_drop_write_file(sc->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to grab a kernel freeze. */
|
|
|
|
while ((error = xchk_fsfreeze(sc)) == -EBUSY) {
|
|
|
|
if (xchk_should_terminate(sc, &error))
|
|
|
|
return error;
|
|
|
|
|
|
|
|
delay(HZ / 10);
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
fsc->frozen = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thaw the filesystem after checking or repairing fscounters. */
|
|
|
|
STATIC void
|
|
|
|
xchk_fscounters_cleanup(
|
|
|
|
void *buf)
|
|
|
|
{
|
|
|
|
struct xchk_fscounters *fsc = buf;
|
|
|
|
struct xfs_scrub *sc = fsc->sc;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (!fsc->frozen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
error = xchk_fsthaw(sc);
|
|
|
|
if (error)
|
|
|
|
xfs_emerg(sc->mp, "still frozen after scrub, err=%d", error);
|
|
|
|
else
|
|
|
|
fsc->frozen = false;
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
int
|
|
|
|
xchk_setup_fscounters(
|
2021-04-08 00:59:39 +00:00
|
|
|
struct xfs_scrub *sc)
|
2019-04-26 01:26:24 +00:00
|
|
|
{
|
|
|
|
struct xchk_fscounters *fsc;
|
|
|
|
int error;
|
|
|
|
|
2023-04-12 01:59:59 +00:00
|
|
|
/*
|
|
|
|
* If the AGF doesn't track btreeblks, we have to lock the AGF to count
|
|
|
|
* btree block usage by walking the actual btrees.
|
|
|
|
*/
|
|
|
|
if (!xfs_has_lazysbcount(sc->mp))
|
|
|
|
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
|
|
|
|
|
2022-11-07 01:03:16 +00:00
|
|
|
sc->buf = kzalloc(sizeof(struct xchk_fscounters), XCHK_GFP_FLAGS);
|
2019-04-26 01:26:24 +00:00
|
|
|
if (!sc->buf)
|
|
|
|
return -ENOMEM;
|
2023-08-04 15:20:57 +00:00
|
|
|
sc->buf_cleanup = xchk_fscounters_cleanup;
|
2019-04-26 01:26:24 +00:00
|
|
|
fsc = sc->buf;
|
2022-11-07 01:03:19 +00:00
|
|
|
fsc->sc = sc;
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
xfs_icount_range(sc->mp, &fsc->icount_min, &fsc->icount_max);
|
|
|
|
|
|
|
|
/* We must get the incore counters set up before we can proceed. */
|
|
|
|
error = xchk_fscount_warmup(sc);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2023-08-04 15:20:57 +00:00
|
|
|
/*
|
|
|
|
* Pause all writer activity in the filesystem while we're scrubbing to
|
|
|
|
* reduce the likelihood of background perturbations to the counters
|
|
|
|
* throwing off our calculations.
|
|
|
|
*/
|
|
|
|
if (sc->flags & XCHK_TRY_HARDER) {
|
|
|
|
error = xchk_fscounters_freeze(sc);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xfs_trans_alloc_empty(sc->mp, &sc->tp);
|
2019-04-26 01:26:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 01:03:19 +00:00
|
|
|
/*
|
|
|
|
* Part 1: Collecting filesystem summary counts. For each AG, we add its
|
|
|
|
* summary counts (total inodes, free inodes, free data blocks) to an incore
|
|
|
|
* copy of the overall filesystem summary counts.
|
|
|
|
*
|
|
|
|
* To avoid false corruption reports in part 2, any failure in this part must
|
|
|
|
* set the INCOMPLETE flag even when a negative errno is returned. This care
|
|
|
|
* must be taken with certain errno values (i.e. EFSBADCRC, EFSCORRUPTED,
|
|
|
|
* ECANCELED) that are absorbed into a scrub state flag update by
|
|
|
|
* xchk_*_process_error.
|
|
|
|
*/
|
|
|
|
|
2021-04-27 02:06:58 +00:00
|
|
|
/* Count free space btree blocks manually for pre-lazysbcount filesystems. */
|
|
|
|
static int
|
|
|
|
xchk_fscount_btreeblks(
|
|
|
|
struct xfs_scrub *sc,
|
|
|
|
struct xchk_fscounters *fsc,
|
|
|
|
xfs_agnumber_t agno)
|
|
|
|
{
|
|
|
|
xfs_extlen_t blocks;
|
|
|
|
int error;
|
|
|
|
|
2021-08-06 18:06:35 +00:00
|
|
|
error = xchk_ag_init_existing(sc, agno, &sc->sa);
|
2021-04-27 02:06:58 +00:00
|
|
|
if (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 19:07:49 +00:00
|
|
|
goto out_free;
|
2021-04-27 02:06:58 +00:00
|
|
|
|
|
|
|
error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
|
|
|
|
if (error)
|
|
|
|
goto out_free;
|
|
|
|
fsc->fdblocks += blocks - 1;
|
|
|
|
|
|
|
|
error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
|
|
|
|
if (error)
|
|
|
|
goto out_free;
|
|
|
|
fsc->fdblocks += blocks - 1;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
xchk_ag_free(sc, &sc->sa);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
/*
|
|
|
|
* Calculate what the global in-core counters ought to be from the incore
|
|
|
|
* per-AG structure. Callers can compare this to the actual in-core counters
|
|
|
|
* to estimate by how much both in-core and on-disk counters need to be
|
|
|
|
* adjusted.
|
|
|
|
*/
|
|
|
|
STATIC int
|
|
|
|
xchk_fscount_aggregate_agcounts(
|
|
|
|
struct xfs_scrub *sc,
|
|
|
|
struct xchk_fscounters *fsc)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
struct xfs_perag *pag;
|
|
|
|
uint64_t delayed;
|
|
|
|
xfs_agnumber_t agno;
|
|
|
|
int tries = 8;
|
2019-11-05 23:33:56 +00:00
|
|
|
int error = 0;
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
retry:
|
|
|
|
fsc->icount = 0;
|
|
|
|
fsc->ifree = 0;
|
|
|
|
fsc->fdblocks = 0;
|
|
|
|
|
2021-06-02 00:48:24 +00:00
|
|
|
for_each_perag(mp, agno, pag) {
|
|
|
|
if (xchk_should_terminate(sc, &error))
|
|
|
|
break;
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
/* This somehow got unset since the warmup? */
|
2023-02-12 22:14:52 +00:00
|
|
|
if (!xfs_perag_initialised_agi(pag) ||
|
|
|
|
!xfs_perag_initialised_agf(pag)) {
|
2021-06-02 00:48:24 +00:00
|
|
|
error = -EFSCORRUPTED;
|
|
|
|
break;
|
2019-04-26 01:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Count all the inodes */
|
|
|
|
fsc->icount += pag->pagi_count;
|
|
|
|
fsc->ifree += pag->pagi_freecount;
|
|
|
|
|
|
|
|
/* Add up the free/freelist/bnobt/cntbt blocks */
|
|
|
|
fsc->fdblocks += pag->pagf_freeblks;
|
|
|
|
fsc->fdblocks += pag->pagf_flcount;
|
2021-08-19 01:46:55 +00:00
|
|
|
if (xfs_has_lazysbcount(sc->mp)) {
|
2021-04-23 23:02:01 +00:00
|
|
|
fsc->fdblocks += pag->pagf_btreeblks;
|
2021-04-27 02:06:58 +00:00
|
|
|
} else {
|
|
|
|
error = xchk_fscount_btreeblks(sc, fsc, agno);
|
2021-06-02 00:48:24 +00:00
|
|
|
if (error)
|
2021-04-27 02:06:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Per-AG reservations are taken out of the incore counters,
|
|
|
|
* so they must be left out of the free blocks computation.
|
|
|
|
*/
|
|
|
|
fsc->fdblocks -= pag->pag_meta_resv.ar_reserved;
|
|
|
|
fsc->fdblocks -= pag->pag_rmapbt_resv.ar_orig_reserved;
|
|
|
|
|
|
|
|
}
|
2021-06-02 00:48:24 +00:00
|
|
|
if (pag)
|
2023-02-12 22:14:42 +00:00
|
|
|
xfs_perag_rele(pag);
|
2022-11-07 01:03:19 +00:00
|
|
|
if (error) {
|
|
|
|
xchk_set_incomplete(sc);
|
2019-11-05 23:33:56 +00:00
|
|
|
return error;
|
2022-11-07 01:03:19 +00:00
|
|
|
}
|
2019-11-05 23:33:56 +00:00
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
/*
|
|
|
|
* The global incore space reservation is taken from the incore
|
|
|
|
* counters, so leave that out of the computation.
|
|
|
|
*/
|
|
|
|
fsc->fdblocks -= mp->m_resblks_avail;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delayed allocation reservations are taken out of the incore counters
|
|
|
|
* but not recorded on disk, so leave them and their indlen blocks out
|
|
|
|
* of the computation.
|
|
|
|
*/
|
|
|
|
delayed = percpu_counter_sum(&mp->m_delalloc_blks);
|
|
|
|
fsc->fdblocks -= delayed;
|
|
|
|
|
|
|
|
trace_xchk_fscounters_calc(mp, fsc->icount, fsc->ifree, fsc->fdblocks,
|
|
|
|
delayed);
|
|
|
|
|
|
|
|
|
|
|
|
/* Bail out if the values we compute are totally nonsense. */
|
|
|
|
if (fsc->icount < fsc->icount_min || fsc->icount > fsc->icount_max ||
|
|
|
|
fsc->fdblocks > mp->m_sb.sb_dblocks ||
|
|
|
|
fsc->ifree > fsc->icount_max)
|
|
|
|
return -EFSCORRUPTED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If ifree > icount then we probably had some perturbation in the
|
|
|
|
* counters while we were calculating things. We'll try a few times
|
|
|
|
* to maintain ifree <= icount before giving up.
|
|
|
|
*/
|
|
|
|
if (fsc->ifree > fsc->icount) {
|
|
|
|
if (tries--)
|
|
|
|
goto retry;
|
2023-08-04 15:20:57 +00:00
|
|
|
return -EDEADLOCK;
|
2019-04-26 01:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-07 01:03:19 +00:00
|
|
|
#ifdef CONFIG_XFS_RT
|
|
|
|
STATIC int
|
|
|
|
xchk_fscount_add_frextent(
|
|
|
|
struct xfs_mount *mp,
|
|
|
|
struct xfs_trans *tp,
|
|
|
|
const struct xfs_rtalloc_rec *rec,
|
|
|
|
void *priv)
|
|
|
|
{
|
|
|
|
struct xchk_fscounters *fsc = priv;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
fsc->frextents += rec->ar_extcount;
|
|
|
|
|
|
|
|
xchk_should_terminate(fsc->sc, &error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the number of free realtime extents from the realtime bitmap. */
|
|
|
|
STATIC int
|
|
|
|
xchk_fscount_count_frextents(
|
|
|
|
struct xfs_scrub *sc,
|
|
|
|
struct xchk_fscounters *fsc)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
fsc->frextents = 0;
|
|
|
|
if (!xfs_has_realtime(mp))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
|
|
|
|
error = xfs_rtalloc_query_all(sc->mp, sc->tp,
|
|
|
|
xchk_fscount_add_frextent, fsc);
|
|
|
|
if (error) {
|
|
|
|
xchk_set_incomplete(sc);
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
STATIC int
|
|
|
|
xchk_fscount_count_frextents(
|
|
|
|
struct xfs_scrub *sc,
|
|
|
|
struct xchk_fscounters *fsc)
|
|
|
|
{
|
|
|
|
fsc->frextents = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_XFS_RT */
|
|
|
|
|
2022-11-07 01:03:19 +00:00
|
|
|
/*
|
|
|
|
* Part 2: Comparing filesystem summary counters. All we have to do here is
|
|
|
|
* sum the percpu counters and compare them to what we've observed.
|
|
|
|
*/
|
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
/*
|
|
|
|
* Is the @counter reasonably close to the @expected value?
|
|
|
|
*
|
|
|
|
* We neither locked nor froze anything in the filesystem while aggregating the
|
|
|
|
* per-AG data to compute the @expected value, which means that the counter
|
|
|
|
* could have changed. We know the @old_value of the summation of the counter
|
|
|
|
* before the aggregation, and we re-sum the counter now. If the expected
|
|
|
|
* value falls between the two summations, we're ok.
|
|
|
|
*
|
|
|
|
* Otherwise, we /might/ have a problem. If the change in the summations is
|
|
|
|
* more than we want to tolerate, the filesystem is probably busy and we should
|
|
|
|
* just send back INCOMPLETE and see if userspace will try again.
|
2023-08-04 15:20:57 +00:00
|
|
|
*
|
|
|
|
* If we're repairing then we require an exact match.
|
2019-04-26 01:26:24 +00:00
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
xchk_fscount_within_range(
|
|
|
|
struct xfs_scrub *sc,
|
|
|
|
const int64_t old_value,
|
|
|
|
struct percpu_counter *counter,
|
|
|
|
uint64_t expected)
|
|
|
|
{
|
|
|
|
int64_t min_value, max_value;
|
|
|
|
int64_t curr_value = percpu_counter_sum(counter);
|
|
|
|
|
|
|
|
trace_xchk_fscounters_within_range(sc->mp, expected, curr_value,
|
|
|
|
old_value);
|
|
|
|
|
|
|
|
/* Negative values are always wrong. */
|
|
|
|
if (curr_value < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Exact matches are always ok. */
|
|
|
|
if (curr_value == expected)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
min_value = min(old_value, curr_value);
|
|
|
|
max_value = max(old_value, curr_value);
|
|
|
|
|
|
|
|
/* Within the before-and-after range is ok. */
|
|
|
|
if (expected >= min_value && expected <= max_value)
|
|
|
|
return true;
|
|
|
|
|
2023-08-04 15:20:57 +00:00
|
|
|
/* Everything else is bad. */
|
2019-04-26 01:26:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the superblock counters. */
|
|
|
|
int
|
|
|
|
xchk_fscounters(
|
|
|
|
struct xfs_scrub *sc)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
struct xchk_fscounters *fsc = sc->buf;
|
2022-11-07 01:03:19 +00:00
|
|
|
int64_t icount, ifree, fdblocks, frextents;
|
2023-08-04 15:20:57 +00:00
|
|
|
bool try_again = false;
|
2019-04-26 01:26:24 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Snapshot the percpu counters. */
|
|
|
|
icount = percpu_counter_sum(&mp->m_icount);
|
|
|
|
ifree = percpu_counter_sum(&mp->m_ifree);
|
|
|
|
fdblocks = percpu_counter_sum(&mp->m_fdblocks);
|
2022-11-07 01:03:19 +00:00
|
|
|
frextents = percpu_counter_sum(&mp->m_frextents);
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
/* No negative values, please! */
|
2023-08-04 15:20:57 +00:00
|
|
|
if (icount < 0 || ifree < 0)
|
2019-04-26 01:26:24 +00:00
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
|
2023-08-04 15:20:57 +00:00
|
|
|
/*
|
|
|
|
* If the filesystem is not frozen, the counter summation calls above
|
|
|
|
* can race with xfs_mod_freecounter, which subtracts a requested space
|
|
|
|
* reservation from the counter and undoes the subtraction if that made
|
|
|
|
* the counter go negative. Therefore, it's possible to see negative
|
|
|
|
* values here, and we should only flag that as a corruption if we
|
|
|
|
* froze the fs. This is much more likely to happen with frextents
|
|
|
|
* since there are no reserved pools.
|
|
|
|
*/
|
|
|
|
if (fdblocks < 0 || frextents < 0) {
|
|
|
|
if (!fsc->frozen)
|
|
|
|
return -EDEADLOCK;
|
|
|
|
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
/* See if icount is obviously wrong. */
|
|
|
|
if (icount < fsc->icount_min || icount > fsc->icount_max)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
|
|
|
|
/* See if fdblocks is obviously wrong. */
|
|
|
|
if (fdblocks > mp->m_sb.sb_dblocks)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
|
2022-11-07 01:03:19 +00:00
|
|
|
/* See if frextents is obviously wrong. */
|
|
|
|
if (frextents > mp->m_sb.sb_rextents)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
/*
|
|
|
|
* If ifree exceeds icount by more than the minimum variance then
|
|
|
|
* something's probably wrong with the counters.
|
|
|
|
*/
|
|
|
|
if (ifree > icount && ifree - icount > XCHK_FSCOUNT_MIN_VARIANCE)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
|
|
|
|
/* Walk the incore AG headers to calculate the expected counters. */
|
|
|
|
error = xchk_fscount_aggregate_agcounts(sc, fsc);
|
|
|
|
if (!xchk_process_error(sc, 0, XFS_SB_BLOCK(mp), &error))
|
|
|
|
return error;
|
|
|
|
|
2022-11-07 01:03:19 +00:00
|
|
|
/* Count the free extents counter for rt volumes. */
|
|
|
|
error = xchk_fscount_count_frextents(sc, fsc);
|
|
|
|
if (!xchk_process_error(sc, 0, XFS_SB_BLOCK(mp), &error))
|
|
|
|
return error;
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE)
|
|
|
|
return 0;
|
|
|
|
|
2023-08-04 15:20:57 +00:00
|
|
|
/*
|
|
|
|
* Compare the in-core counters with whatever we counted. If the fs is
|
|
|
|
* frozen, we treat the discrepancy as a corruption because the freeze
|
|
|
|
* should have stabilized the counter values. Otherwise, we need
|
|
|
|
* userspace to call us back having granted us freeze permission.
|
|
|
|
*/
|
|
|
|
if (!xchk_fscount_within_range(sc, icount, &mp->m_icount,
|
|
|
|
fsc->icount)) {
|
|
|
|
if (fsc->frozen)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
else
|
|
|
|
try_again = true;
|
|
|
|
}
|
2019-04-26 01:26:24 +00:00
|
|
|
|
2023-08-04 15:20:57 +00:00
|
|
|
if (!xchk_fscount_within_range(sc, ifree, &mp->m_ifree, fsc->ifree)) {
|
|
|
|
if (fsc->frozen)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
else
|
|
|
|
try_again = true;
|
|
|
|
}
|
2019-04-26 01:26:24 +00:00
|
|
|
|
|
|
|
if (!xchk_fscount_within_range(sc, fdblocks, &mp->m_fdblocks,
|
2023-08-04 15:20:57 +00:00
|
|
|
fsc->fdblocks)) {
|
|
|
|
if (fsc->frozen)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
else
|
|
|
|
try_again = true;
|
|
|
|
}
|
2019-04-26 01:26:24 +00:00
|
|
|
|
2022-11-07 01:03:19 +00:00
|
|
|
if (!xchk_fscount_within_range(sc, frextents, &mp->m_frextents,
|
2023-08-04 15:20:57 +00:00
|
|
|
fsc->frextents)) {
|
|
|
|
if (fsc->frozen)
|
|
|
|
xchk_set_corrupt(sc);
|
|
|
|
else
|
|
|
|
try_again = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (try_again)
|
|
|
|
return -EDEADLOCK;
|
2022-11-07 01:03:19 +00:00
|
|
|
|
2019-04-26 01:26:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|