[PATCH 37/41] xfs_repair: truncate and unmark orphaned metadata inodes

Darrick J. Wong
2024-12-06T23:49:17Z

From: Darrick J. Wong <djwong@kernel.org>

If an inode claims to be a metadata inode but wasn't linked in either
directory tree, remove the attr fork and reset the data fork if the
contents weren't regular extent mappings before moving the inode to the
lost+found.

We don't ifree the inode, because it's possible that the inode was not
actually a metadata inode but simply got corrupted due to bitflips or
something, and we'd rather let the sysadmin examine what's left of the
file instead of photorec'ing it.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
repair/phase6.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)

diff --git a/repair/phase6.c b/repair/phase6.c
index dd17e8a60d05a3..cc81f5c8b013f4 100644
--- a/repair/phase6.c
+++ b/repair/phase6.c
@@ -855,6 +855,53 @@ mk_orphanage(
return(ino);
}

+/* Don't let metadata inode contents leak to lost+found. */
+static void
+trunc_metadata_inode(
+ struct xfs_inode *ip)
+{
+ struct xfs_trans *tp;
+ struct xfs_mount *mp = ip->i_mount;
+ int err;
+
+ err = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
+ if (err)
+ do_error(
+ _("space reservation failed (%d), filesystem may be out of space\n"),
+ err);
+
+ libxfs_trans_ijoin(tp, ip, 0);
+ ip->i_diflags2 &= ~XFS_DIFLAG2_METADATA;
+
+ switch (VFS_I(ip)->i_mode & S_IFMT) {
+ case S_IFIFO:
+ case S_IFCHR:
+ case S_IFBLK:
+ case S_IFSOCK:
+ ip->i_df.if_format = XFS_DINODE_FMT_DEV;
+ break;
+ case S_IFREG:
+ switch (ip->i_df.if_format) {
+ case XFS_DINODE_FMT_EXTENTS:
+ case XFS_DINODE_FMT_BTREE:
+ break;
+ default:
+ ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
+ ip->i_df.if_nextents = 0;
+ break;
+ }
+ break;
+ }
+
+ libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
+
+ err = -libxfs_trans_commit(tp);
+ if (err)
+ do_error(
+ _("truncation of metadata inode 0x%llx failed, err=%d\n"),
+ (unsigned long long)ip->i_ino, err);
+}
+
/*
* Add a parent pointer back to the orphanage for any file we're moving into
* the orphanage, being careful not to trip over any existing parent pointer.
@@ -945,6 +992,9 @@ mv_orphanage(
if (err)
do_error(_("%d - couldn't iget disconnected inode\n"), err);

+ if (xfs_is_metadir_inode(ino_p))
+ trunc_metadata_inode(ino_p);
+
xname.type = libxfs_mode_to_ftype(VFS_I(ino_p)->i_mode);

if (isa_dir) {