2023-04-12 09:59:56 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-10-18 12:37:44 +08:00
|
|
|
/*
|
2023-04-12 09:59:57 +08:00
|
|
|
* Copyright (C) 2017-2023 Oracle. All Rights Reserved.
|
2023-04-12 09:59:56 +08:00
|
|
|
* Author: Darrick J. Wong <djwong@kernel.org>
|
2017-10-18 12:37:44 +08: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_log_format.h"
|
|
|
|
#include "xfs_trans.h"
|
|
|
|
#include "xfs_inode.h"
|
|
|
|
#include "xfs_icache.h"
|
|
|
|
#include "xfs_dir2.h"
|
|
|
|
#include "xfs_dir2_priv.h"
|
2023-12-16 02:03:35 +08:00
|
|
|
#include "xfs_health.h"
|
2017-10-18 12:37:44 +08:00
|
|
|
#include "scrub/scrub.h"
|
|
|
|
#include "scrub/common.h"
|
|
|
|
#include "scrub/dabtree.h"
|
2023-04-12 10:00:17 +08:00
|
|
|
#include "scrub/readdir.h"
|
2023-12-16 02:03:35 +08:00
|
|
|
#include "scrub/health.h"
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/* Set us up to scrub directories. */
|
|
|
|
int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_setup_directory(
|
2021-04-08 08:59:39 +08:00
|
|
|
struct xfs_scrub *sc)
|
2017-10-18 12:37:44 +08:00
|
|
|
{
|
2021-04-08 08:59:39 +08:00
|
|
|
return xchk_setup_inode_contents(sc, 0);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Directories */
|
|
|
|
|
|
|
|
/* Scrub a directory entry. */
|
|
|
|
|
2023-04-12 10:00:17 +08:00
|
|
|
/* Check that an inode's mode matches a given XFS_DIR3_FT_* type. */
|
2023-04-12 10:00:18 +08:00
|
|
|
STATIC void
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_dir_check_ftype(
|
2023-04-12 10:00:17 +08:00
|
|
|
struct xfs_scrub *sc,
|
2018-07-20 03:29:12 +08:00
|
|
|
xfs_fileoff_t offset,
|
2023-04-12 10:00:18 +08:00
|
|
|
struct xfs_inode *ip,
|
2023-04-12 10:00:17 +08:00
|
|
|
int ftype)
|
2017-10-18 12:37:44 +08:00
|
|
|
{
|
2023-04-12 10:00:17 +08:00
|
|
|
struct xfs_mount *mp = sc->mp;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2021-08-19 09:46:37 +08:00
|
|
|
if (!xfs_has_ftype(mp)) {
|
2023-04-12 10:00:17 +08:00
|
|
|
if (ftype != XFS_DIR3_FT_UNKNOWN && ftype != XFS_DIR3_FT_DIR)
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
|
2023-04-12 10:00:18 +08:00
|
|
|
return;
|
2020-12-03 04:25:44 +08:00
|
|
|
}
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2023-04-12 10:00:17 +08:00
|
|
|
if (xfs_mode_to_ftype(VFS_I(ip)->i_mode) != ftype)
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scrub a single directory entry.
|
|
|
|
*
|
2023-04-12 10:00:17 +08:00
|
|
|
* Check the inode number to make sure it's sane, then we check that we can
|
|
|
|
* look up this filename. Finally, we check the ftype.
|
2017-10-18 12:37:44 +08:00
|
|
|
*/
|
2023-04-12 10:00:17 +08:00
|
|
|
STATIC int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_dir_actor(
|
2023-04-12 10:00:17 +08:00
|
|
|
struct xfs_scrub *sc,
|
|
|
|
struct xfs_inode *dp,
|
|
|
|
xfs_dir2_dataptr_t dapos,
|
|
|
|
const struct xfs_name *name,
|
|
|
|
xfs_ino_t ino,
|
|
|
|
void *priv)
|
2017-10-18 12:37:44 +08:00
|
|
|
{
|
2023-04-12 10:00:17 +08:00
|
|
|
struct xfs_mount *mp = dp->i_mount;
|
2023-04-12 10:00:18 +08:00
|
|
|
struct xfs_inode *ip;
|
2018-07-20 03:29:12 +08:00
|
|
|
xfs_ino_t lookup_ino;
|
|
|
|
xfs_dablk_t offset;
|
|
|
|
int error = 0;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
offset = xfs_dir2_db_to_da(mp->m_dir_geo,
|
2023-04-12 10:00:17 +08:00
|
|
|
xfs_dir2_dataptr_to_db(mp->m_dir_geo, dapos));
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2023-04-12 10:00:17 +08:00
|
|
|
if (xchk_should_terminate(sc, &error))
|
|
|
|
return error;
|
2019-11-06 07:33:56 +08:00
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/* Does this inode number make sense? */
|
|
|
|
if (!xfs_verify_dir_ino(mp, ino)) {
|
2023-04-12 10:00:17 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
|
|
|
|
return -ECANCELED;
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:08:54 +08:00
|
|
|
/* Does this name make sense? */
|
2023-04-12 10:00:17 +08:00
|
|
|
if (!xfs_dir2_namecheck(name->name, name->len)) {
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
|
|
|
|
return -ECANCELED;
|
2019-02-02 01:08:54 +08:00
|
|
|
}
|
|
|
|
|
2023-04-12 10:00:17 +08:00
|
|
|
if (!strncmp(".", name->name, name->len)) {
|
2017-10-18 12:37:44 +08:00
|
|
|
/* If this is "." then check that the inum matches the dir. */
|
2023-04-12 10:00:17 +08:00
|
|
|
if (ino != dp->i_ino)
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
|
|
|
|
} else if (!strncmp("..", name->name, name->len)) {
|
2017-10-18 12:37:44 +08:00
|
|
|
/*
|
|
|
|
* If this is ".." in the root inode, check that the inum
|
|
|
|
* matches this dir.
|
|
|
|
*/
|
2023-04-12 10:00:17 +08:00
|
|
|
if (dp->i_ino == mp->m_sb.sb_rootino && ino != dp->i_ino)
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that we can look up this name by hash. */
|
2023-04-12 10:00:17 +08:00
|
|
|
error = xchk_dir_lookup(sc, dp, name, &lookup_ino);
|
2020-03-12 01:37:57 +08:00
|
|
|
/* ENOENT means the hash lookup failed and the dir is corrupt */
|
|
|
|
if (error == -ENOENT)
|
|
|
|
error = -EFSCORRUPTED;
|
2023-04-12 10:00:17 +08:00
|
|
|
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, offset, &error))
|
2018-05-14 21:34:32 +08:00
|
|
|
goto out;
|
2017-10-18 12:37:44 +08:00
|
|
|
if (lookup_ino != ino) {
|
2023-04-12 10:00:17 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
|
|
|
|
return -ECANCELED;
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
2023-04-12 10:00:18 +08:00
|
|
|
/*
|
xfs: manage inode DONTCACHE status at irele time
Right now, there are statements scattered all over the online fsck
codebase about how we can't use XFS_IGET_DONTCACHE because of concerns
about scrub's unusual practice of releasing inodes with transactions
held.
However, iget is the wrong place to handle this -- the DONTCACHE state
doesn't matter at all until we try to *release* the inode, and here we
get things wrong in multiple ways:
First, if we /do/ have a transaction, we must NOT drop the inode,
because the inode could have dirty pages, dropping the inode will
trigger writeback, and writeback can trigger a nested transaction.
Second, if the inode already had an active reference and the DONTCACHE
flag set, the icache hit when scrub grabs another ref will not clear
DONTCACHE. This is sort of by design, since DONTCACHE is now used to
initiate cache drops so that sysadmins can change a file's access mode
between pagecache and DAX.
Third, if we do actually have the last active reference to the inode, we
can set DONTCACHE to avoid polluting the cache. This is the /one/ case
where we actually want that flag.
Create an xchk_irele helper to encode all that logic and switch the
online fsck code to use it. Since this now means that nearly all
scrubbers use the same xfs_iget flags, we can wrap them too.
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
2023-04-12 10:00:20 +08:00
|
|
|
* Grab the inode pointed to by the dirent. We release the inode
|
|
|
|
* before we cancel the scrub transaction.
|
2023-04-12 10:00:18 +08:00
|
|
|
*
|
|
|
|
* If _iget returns -EINVAL or -ENOENT then the child inode number is
|
|
|
|
* garbage and the directory is corrupt. If the _iget returns
|
|
|
|
* -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
|
|
|
|
* cross referencing error. Any other error is an operational error.
|
|
|
|
*/
|
xfs: manage inode DONTCACHE status at irele time
Right now, there are statements scattered all over the online fsck
codebase about how we can't use XFS_IGET_DONTCACHE because of concerns
about scrub's unusual practice of releasing inodes with transactions
held.
However, iget is the wrong place to handle this -- the DONTCACHE state
doesn't matter at all until we try to *release* the inode, and here we
get things wrong in multiple ways:
First, if we /do/ have a transaction, we must NOT drop the inode,
because the inode could have dirty pages, dropping the inode will
trigger writeback, and writeback can trigger a nested transaction.
Second, if the inode already had an active reference and the DONTCACHE
flag set, the icache hit when scrub grabs another ref will not clear
DONTCACHE. This is sort of by design, since DONTCACHE is now used to
initiate cache drops so that sysadmins can change a file's access mode
between pagecache and DAX.
Third, if we do actually have the last active reference to the inode, we
can set DONTCACHE to avoid polluting the cache. This is the /one/ case
where we actually want that flag.
Create an xchk_irele helper to encode all that logic and switch the
online fsck code to use it. Since this now means that nearly all
scrubbers use the same xfs_iget flags, we can wrap them too.
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
2023-04-12 10:00:20 +08:00
|
|
|
error = xchk_iget(sc, ino, &ip);
|
2023-04-12 10:00:18 +08:00
|
|
|
if (error == -EINVAL || error == -ENOENT) {
|
|
|
|
error = -EFSCORRUPTED;
|
|
|
|
xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
|
|
|
|
goto out;
|
2020-12-03 04:25:44 +08:00
|
|
|
}
|
2023-04-12 10:00:18 +08:00
|
|
|
if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset, &error))
|
|
|
|
goto out;
|
2023-04-12 10:00:17 +08:00
|
|
|
|
2023-04-12 10:00:18 +08:00
|
|
|
xchk_dir_check_ftype(sc, offset, ip, name->type);
|
xfs: manage inode DONTCACHE status at irele time
Right now, there are statements scattered all over the online fsck
codebase about how we can't use XFS_IGET_DONTCACHE because of concerns
about scrub's unusual practice of releasing inodes with transactions
held.
However, iget is the wrong place to handle this -- the DONTCACHE state
doesn't matter at all until we try to *release* the inode, and here we
get things wrong in multiple ways:
First, if we /do/ have a transaction, we must NOT drop the inode,
because the inode could have dirty pages, dropping the inode will
trigger writeback, and writeback can trigger a nested transaction.
Second, if the inode already had an active reference and the DONTCACHE
flag set, the icache hit when scrub grabs another ref will not clear
DONTCACHE. This is sort of by design, since DONTCACHE is now used to
initiate cache drops so that sysadmins can change a file's access mode
between pagecache and DAX.
Third, if we do actually have the last active reference to the inode, we
can set DONTCACHE to avoid polluting the cache. This is the /one/ case
where we actually want that flag.
Create an xchk_irele helper to encode all that logic and switch the
online fsck code to use it. Since this now means that nearly all
scrubbers use the same xfs_iget flags, we can wrap them too.
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
2023-04-12 10:00:20 +08:00
|
|
|
xchk_irele(sc, ip);
|
2017-10-18 12:37:44 +08:00
|
|
|
out:
|
2023-04-12 10:00:17 +08:00
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
return -ECANCELED;
|
|
|
|
return error;
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scrub a directory btree record. */
|
|
|
|
STATIC int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_dir_rec(
|
|
|
|
struct xchk_da_btree *ds,
|
2019-11-09 06:52:07 +08:00
|
|
|
int level)
|
2017-10-18 12:37:44 +08:00
|
|
|
{
|
2023-04-12 10:00:16 +08:00
|
|
|
struct xfs_name dname = { };
|
2019-11-09 06:52:07 +08:00
|
|
|
struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
|
2017-10-18 12:37:44 +08:00
|
|
|
struct xfs_mount *mp = ds->state->mp;
|
|
|
|
struct xfs_inode *dp = ds->dargs.dp;
|
2019-11-09 07:05:38 +08:00
|
|
|
struct xfs_da_geometry *geo = mp->m_dir_geo;
|
2017-10-18 12:37:44 +08:00
|
|
|
struct xfs_dir2_data_entry *dent;
|
|
|
|
struct xfs_buf *bp;
|
2019-11-09 06:52:07 +08:00
|
|
|
struct xfs_dir2_leaf_entry *ent;
|
2019-11-09 07:05:36 +08:00
|
|
|
unsigned int end;
|
2019-11-09 07:05:33 +08:00
|
|
|
unsigned int iter_off;
|
2017-10-18 12:37:44 +08:00
|
|
|
xfs_ino_t ino;
|
|
|
|
xfs_dablk_t rec_bno;
|
|
|
|
xfs_dir2_db_t db;
|
|
|
|
xfs_dir2_data_aoff_t off;
|
|
|
|
xfs_dir2_dataptr_t ptr;
|
|
|
|
xfs_dahash_t calc_hash;
|
|
|
|
xfs_dahash_t hash;
|
2019-11-09 06:57:50 +08:00
|
|
|
struct xfs_dir3_icleaf_hdr hdr;
|
2017-10-18 12:37:44 +08:00
|
|
|
unsigned int tag;
|
|
|
|
int error;
|
|
|
|
|
2019-11-09 06:52:07 +08:00
|
|
|
ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
|
|
|
|
blk->magic == XFS_DIR2_LEAFN_MAGIC);
|
|
|
|
|
2019-11-09 06:57:50 +08:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
|
|
|
|
ent = hdr.ents + blk->index;
|
2019-11-09 06:52:07 +08:00
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/* Check the hash of the entry. */
|
2018-07-20 03:29:11 +08:00
|
|
|
error = xchk_da_btree_hash(ds, level, &ent->hashval);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Valid hash pointer? */
|
|
|
|
ptr = be32_to_cpu(ent->address);
|
|
|
|
if (ptr == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Find the directory entry's location. */
|
2019-11-09 07:05:38 +08:00
|
|
|
db = xfs_dir2_dataptr_to_db(geo, ptr);
|
|
|
|
off = xfs_dir2_dataptr_to_off(geo, ptr);
|
|
|
|
rec_bno = xfs_dir2_db_to_da(geo, db);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2019-11-09 07:05:38 +08:00
|
|
|
if (rec_bno >= geo->leafblk) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_da_set_corrupt(ds, level);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2019-11-21 01:46:04 +08:00
|
|
|
error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno,
|
|
|
|
XFS_DABUF_MAP_HOLE_OK, &bp);
|
2018-07-20 03:29:11 +08:00
|
|
|
if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
|
2017-10-18 12:37:44 +08:00
|
|
|
&error))
|
|
|
|
goto out;
|
|
|
|
if (!bp) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_buffer_recheck(ds->sc, bp);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2018-05-14 21:34:32 +08:00
|
|
|
if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
goto out_relse;
|
|
|
|
|
2019-11-09 07:05:33 +08:00
|
|
|
dent = bp->b_addr + off;
|
2018-01-17 10:54:12 +08:00
|
|
|
|
|
|
|
/* Make sure we got a real directory entry. */
|
2019-11-09 07:05:38 +08:00
|
|
|
iter_off = geo->data_entry_offset;
|
|
|
|
end = xfs_dir3_data_end_offset(geo, bp->b_addr);
|
2019-11-09 07:05:36 +08:00
|
|
|
if (!end) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
|
2018-01-17 10:54:12 +08:00
|
|
|
goto out_relse;
|
|
|
|
}
|
2019-11-09 07:05:33 +08:00
|
|
|
for (;;) {
|
|
|
|
struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off;
|
|
|
|
struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off;
|
|
|
|
|
2019-11-09 07:05:36 +08:00
|
|
|
if (iter_off >= end) {
|
2019-11-09 07:05:33 +08:00
|
|
|
xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
|
|
|
|
goto out_relse;
|
|
|
|
}
|
2018-01-17 10:54:12 +08:00
|
|
|
|
|
|
|
if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
|
2019-11-09 07:05:33 +08:00
|
|
|
iter_off += be16_to_cpu(dup->length);
|
2018-01-17 10:54:12 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dep == dent)
|
|
|
|
break;
|
2019-11-09 07:05:37 +08:00
|
|
|
iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
|
2018-01-17 10:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve the entry, sanity check it, and compare hashes. */
|
2017-10-18 12:37:44 +08:00
|
|
|
ino = be64_to_cpu(dent->inumber);
|
|
|
|
hash = be32_to_cpu(ent->hashval);
|
2019-11-09 07:05:37 +08:00
|
|
|
tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
|
2017-10-18 12:37:44 +08:00
|
|
|
if (!xfs_verify_dir_ino(mp, ino) || tag != off)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (dent->namelen == 0) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out_relse;
|
|
|
|
}
|
2023-04-12 10:00:16 +08:00
|
|
|
|
|
|
|
/* Does the directory hash match? */
|
|
|
|
dname.name = dent->name;
|
|
|
|
dname.len = dent->namelen;
|
|
|
|
calc_hash = xfs_dir2_hashname(mp, &dname);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (calc_hash != hash)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
out_relse:
|
|
|
|
xfs_trans_brelse(ds->dargs.trans, bp);
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/*
|
|
|
|
* Is this unused entry either in the bestfree or smaller than all of
|
|
|
|
* them? We've already checked that the bestfrees are sorted longest to
|
|
|
|
* shortest, and that there aren't any bogus entries.
|
|
|
|
*/
|
|
|
|
STATIC void
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_check_free_entry(
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_scrub *sc,
|
2017-10-18 12:37:44 +08:00
|
|
|
xfs_dablk_t lblk,
|
|
|
|
struct xfs_dir2_data_free *bf,
|
|
|
|
struct xfs_dir2_data_unused *dup)
|
|
|
|
{
|
|
|
|
struct xfs_dir2_data_free *dfp;
|
|
|
|
unsigned int dup_length;
|
|
|
|
|
|
|
|
dup_length = be16_to_cpu(dup->length);
|
|
|
|
|
|
|
|
/* Unused entry is shorter than any of the bestfrees */
|
|
|
|
if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
|
|
|
|
if (dup_length == be16_to_cpu(dfp->length))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Unused entry should be in the bestfrees but wasn't found. */
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check free space info in a directory data block. */
|
|
|
|
STATIC int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_data_bestfree(
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_scrub *sc,
|
2017-10-18 12:37:44 +08:00
|
|
|
xfs_dablk_t lblk,
|
|
|
|
bool is_block)
|
|
|
|
{
|
|
|
|
struct xfs_dir2_data_unused *dup;
|
|
|
|
struct xfs_dir2_data_free *dfp;
|
|
|
|
struct xfs_buf *bp;
|
|
|
|
struct xfs_dir2_data_free *bf;
|
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
u16 tag;
|
|
|
|
unsigned int nr_bestfrees = 0;
|
|
|
|
unsigned int nr_frees = 0;
|
|
|
|
unsigned int smallest_bestfree;
|
|
|
|
int newlen;
|
2019-11-09 07:05:33 +08:00
|
|
|
unsigned int offset;
|
|
|
|
unsigned int end;
|
2017-10-18 12:37:44 +08:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (is_block) {
|
|
|
|
/* dir block format */
|
|
|
|
if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
|
|
|
|
} else {
|
|
|
|
/* dir data format */
|
2019-11-21 01:46:04 +08:00
|
|
|
error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, 0, &bp);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
2018-07-20 03:29:11 +08:00
|
|
|
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_buffer_recheck(sc, bp);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
|
|
|
|
|
2018-05-14 21:34:32 +08:00
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
goto out_buf;
|
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/* Do the bestfrees correspond to actual free space? */
|
2019-11-09 07:05:39 +08:00
|
|
|
bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
|
2017-10-18 12:37:44 +08:00
|
|
|
smallest_bestfree = UINT_MAX;
|
|
|
|
for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
|
|
|
|
offset = be16_to_cpu(dfp->offset);
|
|
|
|
if (offset == 0)
|
|
|
|
continue;
|
|
|
|
if (offset >= mp->m_dir_geo->blksize) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out_buf;
|
|
|
|
}
|
2019-11-09 07:05:33 +08:00
|
|
|
dup = bp->b_addr + offset;
|
2017-10-18 12:37:44 +08:00
|
|
|
tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
|
|
|
|
|
|
|
|
/* bestfree doesn't match the entry it points at? */
|
|
|
|
if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
|
|
|
|
be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
|
2019-11-09 07:05:33 +08:00
|
|
|
tag != offset) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bestfree records should be ordered largest to smallest */
|
|
|
|
if (smallest_bestfree < be16_to_cpu(dfp->length)) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
smallest_bestfree = be16_to_cpu(dfp->length);
|
|
|
|
nr_bestfrees++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the bestfrees are actually the best free spaces. */
|
2019-11-09 07:05:38 +08:00
|
|
|
offset = mp->m_dir_geo->data_entry_offset;
|
2019-11-09 07:05:36 +08:00
|
|
|
end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/* Iterate the entries, stopping when we hit or go past the end. */
|
2019-11-09 07:05:33 +08:00
|
|
|
while (offset < end) {
|
|
|
|
dup = bp->b_addr + offset;
|
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/* Skip real entries */
|
|
|
|
if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
|
2019-11-09 07:05:33 +08:00
|
|
|
struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2019-11-09 07:05:37 +08:00
|
|
|
newlen = xfs_dir2_data_entsize(mp, dep->namelen);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (newlen <= 0) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
|
2017-10-18 12:37:44 +08:00
|
|
|
lblk);
|
|
|
|
goto out_buf;
|
|
|
|
}
|
2019-11-09 07:05:33 +08:00
|
|
|
offset += newlen;
|
2017-10-18 12:37:44 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Spot check this free entry */
|
|
|
|
tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
|
2019-11-09 07:05:33 +08:00
|
|
|
if (tag != offset) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2018-05-14 21:34:32 +08:00
|
|
|
goto out_buf;
|
|
|
|
}
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Either this entry is a bestfree or it's smaller than
|
|
|
|
* any of the bestfrees.
|
|
|
|
*/
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_check_free_entry(sc, lblk, bf, dup);
|
2018-05-14 21:34:32 +08:00
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
goto out_buf;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/* Move on. */
|
|
|
|
newlen = be16_to_cpu(dup->length);
|
|
|
|
if (newlen <= 0) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out_buf;
|
|
|
|
}
|
2019-11-09 07:05:33 +08:00
|
|
|
offset += newlen;
|
|
|
|
if (offset <= end)
|
2017-10-18 12:37:44 +08:00
|
|
|
nr_frees++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're required to fill all the space. */
|
2019-11-09 07:05:33 +08:00
|
|
|
if (offset != end)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/* Did we see at least as many free slots as there are bestfrees? */
|
|
|
|
if (nr_frees < nr_bestfrees)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
out_buf:
|
|
|
|
xfs_trans_brelse(sc->tp, bp);
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Does the free space length in the free space index block ($len) match
|
|
|
|
* the longest length in the directory data block's bestfree array?
|
|
|
|
* Assume that we've already checked that the data block's bestfree
|
|
|
|
* array is in order.
|
|
|
|
*/
|
|
|
|
STATIC void
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_check_freesp(
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_scrub *sc,
|
2017-10-18 12:37:44 +08:00
|
|
|
xfs_dablk_t lblk,
|
|
|
|
struct xfs_buf *dbp,
|
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
struct xfs_dir2_data_free *dfp;
|
|
|
|
|
2019-11-09 07:05:39 +08:00
|
|
|
dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2017-11-07 03:37:46 +08:00
|
|
|
if (len != be16_to_cpu(dfp->length))
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2017-11-07 03:37:46 +08:00
|
|
|
if (len > 0 && be16_to_cpu(dfp->offset) == 0)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check free space info in a directory leaf1 block. */
|
|
|
|
STATIC int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_leaf1_bestfree(
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_scrub *sc,
|
2017-10-18 12:37:44 +08:00
|
|
|
struct xfs_da_args *args,
|
2021-12-16 03:53:15 +08:00
|
|
|
xfs_dir2_db_t last_data_db,
|
2017-10-18 12:37:44 +08:00
|
|
|
xfs_dablk_t lblk)
|
|
|
|
{
|
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
|
|
|
struct xfs_dir2_leaf_tail *ltp;
|
|
|
|
struct xfs_dir2_leaf *leaf;
|
|
|
|
struct xfs_buf *dbp;
|
|
|
|
struct xfs_buf *bp;
|
|
|
|
struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
|
|
|
|
__be16 *bestp;
|
|
|
|
__u16 best;
|
|
|
|
__u32 hash;
|
|
|
|
__u32 lasthash = 0;
|
|
|
|
__u32 bestcount;
|
|
|
|
unsigned int stale = 0;
|
|
|
|
int i;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Read the free space block. */
|
2019-11-21 01:46:03 +08:00
|
|
|
error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, &bp);
|
2018-07-20 03:29:11 +08:00
|
|
|
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
|
2020-03-25 11:10:56 +08:00
|
|
|
return error;
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_buffer_recheck(sc, bp);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
leaf = bp->b_addr;
|
2019-11-09 06:57:49 +08:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
|
2017-10-18 12:37:44 +08:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(geo, leaf);
|
|
|
|
bestcount = be32_to_cpu(ltp->bestcount);
|
|
|
|
bestp = xfs_dir2_leaf_bests_p(ltp);
|
|
|
|
|
2021-08-19 09:46:37 +08:00
|
|
|
if (xfs_has_crc(sc->mp)) {
|
2017-10-18 12:37:44 +08:00
|
|
|
struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
|
|
|
|
|
|
|
|
if (hdr3->pad != cpu_to_be32(0))
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-16 03:53:15 +08:00
|
|
|
* There must be enough bestfree slots to cover all the directory data
|
|
|
|
* blocks that we scanned. It is possible for there to be a hole
|
|
|
|
* between the last data block and i_disk_size. This seems like an
|
|
|
|
* oversight to the scrub author, but as we have been writing out
|
|
|
|
* directories like this (and xfs_repair doesn't mind them) for years,
|
|
|
|
* that's what we have to check.
|
2017-10-18 12:37:44 +08:00
|
|
|
*/
|
2021-12-16 03:53:15 +08:00
|
|
|
if (bestcount != last_data_db + 1) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the leaf count even remotely sane? */
|
2019-11-09 06:57:51 +08:00
|
|
|
if (leafhdr.count > geo->leaf_max_ents) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Leaves and bests don't overlap in leaf format. */
|
2019-11-09 06:57:50 +08:00
|
|
|
if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check hash value order, count stale entries. */
|
|
|
|
for (i = 0; i < leafhdr.count; i++) {
|
2019-11-09 06:57:50 +08:00
|
|
|
hash = be32_to_cpu(leafhdr.ents[i].hashval);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (i > 0 && lasthash > hash)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
lasthash = hash;
|
2019-11-09 06:57:50 +08:00
|
|
|
if (leafhdr.ents[i].address ==
|
|
|
|
cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
|
2017-10-18 12:37:44 +08:00
|
|
|
stale++;
|
|
|
|
}
|
|
|
|
if (leafhdr.stale != stale)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2018-05-14 21:34:32 +08:00
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
goto out;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/* Check all the bestfree entries. */
|
|
|
|
for (i = 0; i < bestcount; i++, bestp++) {
|
|
|
|
best = be16_to_cpu(*bestp);
|
|
|
|
error = xfs_dir3_data_read(sc->tp, sc->ip,
|
2020-11-09 08:32:42 +08:00
|
|
|
xfs_dir2_db_to_da(args->geo, i),
|
|
|
|
XFS_DABUF_MAP_HOLE_OK,
|
|
|
|
&dbp);
|
2018-07-20 03:29:11 +08:00
|
|
|
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
|
2017-10-18 12:37:44 +08:00
|
|
|
&error))
|
2018-05-14 21:34:32 +08:00
|
|
|
break;
|
2020-11-09 08:32:42 +08:00
|
|
|
|
|
|
|
if (!dbp) {
|
|
|
|
if (best != NULLDATAOFF) {
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
|
|
|
|
lblk);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best == NULLDATAOFF)
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
|
|
|
else
|
|
|
|
xchk_directory_check_freesp(sc, lblk, dbp, best);
|
2017-10-18 12:37:44 +08:00
|
|
|
xfs_trans_brelse(sc->tp, dbp);
|
2018-05-14 21:34:32 +08:00
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
2020-03-25 11:10:56 +08:00
|
|
|
break;
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
out:
|
2020-03-25 11:10:56 +08:00
|
|
|
xfs_trans_brelse(sc->tp, bp);
|
2017-10-18 12:37:44 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check free space info in a directory freespace block. */
|
|
|
|
STATIC int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_free_bestfree(
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_scrub *sc,
|
2017-10-18 12:37:44 +08:00
|
|
|
struct xfs_da_args *args,
|
|
|
|
xfs_dablk_t lblk)
|
|
|
|
{
|
|
|
|
struct xfs_dir3_icfree_hdr freehdr;
|
|
|
|
struct xfs_buf *dbp;
|
|
|
|
struct xfs_buf *bp;
|
2017-11-07 03:53:58 +08:00
|
|
|
__u16 best;
|
2017-10-18 12:37:44 +08:00
|
|
|
unsigned int stale = 0;
|
|
|
|
int i;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Read the free space block */
|
|
|
|
error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
|
2018-07-20 03:29:11 +08:00
|
|
|
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
|
2020-03-25 11:10:56 +08:00
|
|
|
return error;
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_buffer_recheck(sc, bp);
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2021-08-19 09:46:37 +08:00
|
|
|
if (xfs_has_crc(sc->mp)) {
|
2017-10-18 12:37:44 +08:00
|
|
|
struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
|
|
|
|
|
|
|
|
if (hdr3->pad != cpu_to_be32(0))
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check all the entries. */
|
2019-11-09 06:57:52 +08:00
|
|
|
xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
|
2019-11-09 06:58:05 +08:00
|
|
|
for (i = 0; i < freehdr.nvalid; i++) {
|
|
|
|
best = be16_to_cpu(freehdr.bests[i]);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (best == NULLDATAOFF) {
|
|
|
|
stale++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
error = xfs_dir3_data_read(sc->tp, sc->ip,
|
|
|
|
(freehdr.firstdb + i) * args->geo->fsbcount,
|
2019-11-21 01:46:04 +08:00
|
|
|
0, &dbp);
|
2018-07-20 03:29:11 +08:00
|
|
|
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
|
2017-10-18 12:37:44 +08:00
|
|
|
&error))
|
2020-03-25 11:10:56 +08:00
|
|
|
goto out;
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_check_freesp(sc, lblk, dbp, best);
|
2017-10-18 12:37:44 +08:00
|
|
|
xfs_trans_brelse(sc->tp, dbp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freehdr.nused + stale != freehdr.nvalid)
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
out:
|
2020-03-25 11:10:56 +08:00
|
|
|
xfs_trans_brelse(sc->tp, bp);
|
2017-10-18 12:37:44 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check free space information in directories. */
|
|
|
|
STATIC int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory_blocks(
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_scrub *sc)
|
2017-10-18 12:37:44 +08:00
|
|
|
{
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_bmbt_irec got;
|
xfs: fully initialize xfs_da_args in xchk_directory_blocks
While running the online fsck test suite, I noticed the following
assertion in the kernel log (edited for brevity):
XFS: Assertion failed: 0, file: fs/xfs/xfs_health.c, line: 571
------------[ cut here ]------------
WARNING: CPU: 3 PID: 11667 at fs/xfs/xfs_message.c:104 assfail+0x46/0x4a [xfs]
CPU: 3 PID: 11667 Comm: xfs_scrub Tainted: G W 5.19.0-rc7-xfsx #rc7 6e6475eb29fd9dda3181f81b7ca7ff961d277a40
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.15.0-1 04/01/2014
RIP: 0010:assfail+0x46/0x4a [xfs]
Call Trace:
<TASK>
xfs_dir2_isblock+0xcc/0xe0
xchk_directory_blocks+0xc7/0x420
xchk_directory+0x53/0xb0
xfs_scrub_metadata+0x2b6/0x6b0
xfs_scrubv_metadata+0x35e/0x4d0
xfs_ioc_scrubv_metadata+0x111/0x160
xfs_file_ioctl+0x4ec/0xef0
__x64_sys_ioctl+0x82/0xa0
do_syscall_64+0x2b/0x80
entry_SYSCALL_64_after_hwframe+0x46/0xb0
This assertion triggers in xfs_dirattr_mark_sick when the caller passes
in a whichfork value that is neither of XFS_{DATA,ATTR}_FORK. The cause
of this is that xchk_directory_blocks only partially initializes the
xfs_da_args structure that is passed to xfs_dir2_isblock. If the data
fork is not correct, the XFS_IS_CORRUPT clause will trigger. My
development branch reports this failure to the health monitoring
subsystem, which accesses the uninitialized args->whichfork field,
leading the the assertion tripping. We really shouldn't be passing
random stack contents around, so the solution here is to force the
compiler to zero-initialize the struct.
Found by fuzzing u3.bmx[0].blockcount = middlebit on xfs/1554.
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
2022-11-07 09:03:13 +08:00
|
|
|
struct xfs_da_args args = {
|
|
|
|
.dp = sc ->ip,
|
|
|
|
.whichfork = XFS_DATA_FORK,
|
|
|
|
.geo = sc->mp->m_dir_geo,
|
|
|
|
.trans = sc->tp,
|
|
|
|
};
|
2022-07-10 01:56:05 +08:00
|
|
|
struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_mount *mp = sc->mp;
|
|
|
|
xfs_fileoff_t leaf_lblk;
|
|
|
|
xfs_fileoff_t free_lblk;
|
|
|
|
xfs_fileoff_t lblk;
|
|
|
|
struct xfs_iext_cursor icur;
|
|
|
|
xfs_dablk_t dabno;
|
2021-12-16 03:53:15 +08:00
|
|
|
xfs_dir2_db_t last_data_db = 0;
|
2018-07-20 03:29:12 +08:00
|
|
|
bool found;
|
2022-10-04 13:39:58 +08:00
|
|
|
bool is_block = false;
|
2018-07-20 03:29:12 +08:00
|
|
|
int error;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
/* Ignore local format directories. */
|
2020-05-19 01:28:05 +08:00
|
|
|
if (ifp->if_format != XFS_DINODE_FMT_EXTENTS &&
|
|
|
|
ifp->if_format != XFS_DINODE_FMT_BTREE)
|
2017-10-18 12:37:44 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
|
|
|
|
leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
|
|
|
|
free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
|
|
|
|
|
|
|
|
/* Is this a block dir? */
|
|
|
|
error = xfs_dir2_isblock(&args, &is_block);
|
2018-07-20 03:29:11 +08:00
|
|
|
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Iterate all the data extents in the directory... */
|
2017-11-04 01:34:43 +08:00
|
|
|
found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
|
2018-05-14 21:34:32 +08:00
|
|
|
while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
|
2017-10-18 12:37:44 +08:00
|
|
|
/* No more data blocks... */
|
|
|
|
if (got.br_startoff >= leaf_lblk)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check each data block's bestfree data.
|
|
|
|
*
|
|
|
|
* Iterate all the fsbcount-aligned block offsets in
|
|
|
|
* this directory. The directory block reading code is
|
|
|
|
* smart enough to do its own bmap lookups to handle
|
|
|
|
* discontiguous directory blocks. When we're done
|
|
|
|
* with the extent record, re-query the bmap at the
|
|
|
|
* next fsbcount-aligned offset to avoid redundant
|
|
|
|
* block checks.
|
|
|
|
*/
|
|
|
|
for (lblk = roundup((xfs_dablk_t)got.br_startoff,
|
|
|
|
args.geo->fsbcount);
|
|
|
|
lblk < got.br_startoff + got.br_blockcount;
|
|
|
|
lblk += args.geo->fsbcount) {
|
2021-12-16 03:53:15 +08:00
|
|
|
last_data_db = xfs_dir2_da_to_db(args.geo, lblk);
|
2018-07-20 03:29:11 +08:00
|
|
|
error = xchk_directory_data_bestfree(sc, lblk,
|
2017-10-18 12:37:44 +08:00
|
|
|
is_block);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
dabno = got.br_startoff + got.br_blockcount;
|
|
|
|
lblk = roundup(dabno, args.geo->fsbcount);
|
2017-11-04 01:34:43 +08:00
|
|
|
found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Look for a leaf1 block, which has free info. */
|
2017-11-04 01:34:43 +08:00
|
|
|
if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
|
2017-10-18 12:37:44 +08:00
|
|
|
got.br_startoff == leaf_lblk &&
|
|
|
|
got.br_blockcount == args.geo->fsbcount &&
|
2017-11-04 01:34:43 +08:00
|
|
|
!xfs_iext_next_extent(ifp, &icur, &got)) {
|
2017-10-18 12:37:44 +08:00
|
|
|
if (is_block) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2021-12-16 03:53:15 +08:00
|
|
|
error = xchk_directory_leaf1_bestfree(sc, &args, last_data_db,
|
2017-10-18 12:37:44 +08:00
|
|
|
leaf_lblk);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Scan for free blocks */
|
|
|
|
lblk = free_lblk;
|
2017-11-04 01:34:43 +08:00
|
|
|
found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
|
2018-05-14 21:34:32 +08:00
|
|
|
while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
|
2017-10-18 12:37:44 +08:00
|
|
|
/*
|
|
|
|
* Dirs can't have blocks mapped above 2^32.
|
|
|
|
* Single-block dirs shouldn't even be here.
|
|
|
|
*/
|
|
|
|
lblk = got.br_startoff;
|
|
|
|
if (lblk & ~0xFFFFFFFFULL) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (is_block) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
|
2017-10-18 12:37:44 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check each dir free block's bestfree data.
|
|
|
|
*
|
|
|
|
* Iterate all the fsbcount-aligned block offsets in
|
|
|
|
* this directory. The directory block reading code is
|
|
|
|
* smart enough to do its own bmap lookups to handle
|
|
|
|
* discontiguous directory blocks. When we're done
|
|
|
|
* with the extent record, re-query the bmap at the
|
|
|
|
* next fsbcount-aligned offset to avoid redundant
|
|
|
|
* block checks.
|
|
|
|
*/
|
|
|
|
for (lblk = roundup((xfs_dablk_t)got.br_startoff,
|
|
|
|
args.geo->fsbcount);
|
|
|
|
lblk < got.br_startoff + got.br_blockcount;
|
|
|
|
lblk += args.geo->fsbcount) {
|
2018-07-20 03:29:11 +08:00
|
|
|
error = xchk_directory_free_bestfree(sc, &args,
|
2017-10-18 12:37:44 +08:00
|
|
|
lblk);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
dabno = got.br_startoff + got.br_blockcount;
|
|
|
|
lblk = roundup(dabno, args.geo->fsbcount);
|
2017-11-04 01:34:43 +08:00
|
|
|
found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/* Scrub a whole directory. */
|
|
|
|
int
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_directory(
|
2018-07-20 03:29:12 +08:00
|
|
|
struct xfs_scrub *sc)
|
2017-10-18 12:37:44 +08:00
|
|
|
{
|
2023-04-12 10:00:17 +08:00
|
|
|
int error;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
|
|
|
if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
|
|
|
|
return -ENOENT;
|
|
|
|
|
2023-12-16 02:03:35 +08:00
|
|
|
if (xchk_file_looks_zapped(sc, XFS_SICK_INO_DIR_ZAPPED)) {
|
|
|
|
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/* Plausible size? */
|
2021-03-30 02:11:40 +08:00
|
|
|
if (sc->ip->i_disk_size < xfs_dir2_sf_hdr_size(0)) {
|
2018-07-20 03:29:11 +08:00
|
|
|
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
|
2023-04-12 10:00:17 +08:00
|
|
|
return 0;
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check directory tree structure */
|
2018-07-20 03:29:11 +08:00
|
|
|
error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
2023-04-12 10:00:17 +08:00
|
|
|
return 0;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2017-10-18 12:37:44 +08:00
|
|
|
/* Check the freespace. */
|
2018-07-20 03:29:11 +08:00
|
|
|
error = xchk_directory_blocks(sc);
|
2017-10-18 12:37:44 +08:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
|
2023-04-12 10:00:17 +08:00
|
|
|
return 0;
|
2017-10-18 12:37:44 +08:00
|
|
|
|
2023-04-12 10:00:17 +08:00
|
|
|
/* Look up every name in this directory by hash. */
|
|
|
|
error = xchk_dir_walk(sc, sc->ip, xchk_dir_actor, NULL);
|
2023-12-16 02:03:35 +08:00
|
|
|
if (error && error != -ECANCELED)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/* If the dir is clean, it is clearly not zapped. */
|
|
|
|
xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_DIR_ZAPPED);
|
|
|
|
return 0;
|
2017-10-18 12:37:44 +08:00
|
|
|
}
|
2023-12-16 02:03:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decide if this directory has been zapped to satisfy the inode and ifork
|
|
|
|
* verifiers. Checking and repairing should be postponed until the directory
|
|
|
|
* is fixed.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
xchk_dir_looks_zapped(
|
|
|
|
struct xfs_inode *dp)
|
|
|
|
{
|
|
|
|
/* Repair zapped this dir's data fork a short time ago */
|
|
|
|
if (xfs_ifork_zapped(dp, XFS_DATA_FORK))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the dinode repair found a bad data fork, it will reset the fork
|
|
|
|
* to extents format with zero records and wait for the bmapbtd
|
|
|
|
* scrubber to reconstruct the block mappings. Directories always
|
|
|
|
* contain some content, so this is a clear sign of a zapped directory.
|
|
|
|
* The state checked by xfs_ifork_zapped is not persisted, so this is
|
|
|
|
* the secondary strategy if repairs are interrupted by a crash or an
|
|
|
|
* unmount.
|
|
|
|
*/
|
|
|
|
return dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS &&
|
|
|
|
dp->i_df.if_nextents == 0;
|
|
|
|
}
|