Skip to content

Commit 2e9e648

Browse files
committed
xfs: detect and fix bad summary counts at mount
Filippo Giunchedi complained that xfs doesn't even perform basic sanity checks of the fs summary counters at mount time. Therefore, recalculate the summary counters from the AGFs after log recovery if the counts were bad (or we had to recover the fs). Enhance the recalculation routine to fail the mount entirely if the new values are also obviously incorrect. We use a mount state flag to record the "bad summary count" state so that the (subsequent) online fsck patches can detect subtlely incorrect counts and set the flag; clear it userspace asks for a repair; or force a recalculation at the next mount if nobody fixes it by unmount time. Reported-by: Filippo Giunchedi <fgiunchedi@wikimedia.org> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Christoph Hellwig <hch@lst.de>
1 parent 032d91f commit 2e9e648

3 files changed

Lines changed: 73 additions & 29 deletions

File tree

‎fs/xfs/libxfs/xfs_sb.c‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ xfs_initialize_perag_data(
804804
uint64_t bfree = 0;
805805
uint64_t bfreelst = 0;
806806
uint64_t btree = 0;
807+
uint64_t fdblocks;
807808
int error;
808809

809810
for (index = 0; index < agcount; index++) {
@@ -827,17 +828,31 @@ xfs_initialize_perag_data(
827828
btree += pag->pagf_btreeblks;
828829
xfs_perag_put(pag);
829830
}
831+
fdblocks = bfree + bfreelst + btree;
832+
833+
/*
834+
* If the new summary counts are obviously incorrect, fail the
835+
* mount operation because that implies the AGFs are also corrupt.
836+
* Clear BAD_SUMMARY so that we don't unmount with a dirty log, which
837+
* will prevent xfs_repair from fixing anything.
838+
*/
839+
if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
840+
xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
841+
error = -EFSCORRUPTED;
842+
goto out;
843+
}
830844

831845
/* Overwrite incore superblock counters with just-read data */
832846
spin_lock(&mp->m_sb_lock);
833847
sbp->sb_ifree = ifree;
834848
sbp->sb_icount = ialloc;
835-
sbp->sb_fdblocks = bfree + bfreelst + btree;
849+
sbp->sb_fdblocks = fdblocks;
836850
spin_unlock(&mp->m_sb_lock);
837851

838852
xfs_reinit_percpu_counters(mp);
839-
840-
return 0;
853+
out:
854+
mp->m_flags &= ~XFS_MOUNT_BAD_SUMMARY;
855+
return error;
841856
}
842857

843858
/*

‎fs/xfs/xfs_mount.c‎

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,56 @@ xfs_default_resblks(xfs_mount_t *mp)
606606
return resblks;
607607
}
608608

609+
/* Ensure the summary counts are correct. */
610+
STATIC int
611+
xfs_check_summary_counts(
612+
struct xfs_mount *mp)
613+
{
614+
/*
615+
* The AG0 superblock verifier rejects in-progress filesystems,
616+
* so we should never see the flag set this far into mounting.
617+
*/
618+
if (mp->m_sb.sb_inprogress) {
619+
xfs_err(mp, "sb_inprogress set after log recovery??");
620+
WARN_ON(1);
621+
return -EFSCORRUPTED;
622+
}
623+
624+
/*
625+
* Now the log is mounted, we know if it was an unclean shutdown or
626+
* not. If it was, with the first phase of recovery has completed, we
627+
* have consistent AG blocks on disk. We have not recovered EFIs yet,
628+
* but they are recovered transactionally in the second recovery phase
629+
* later.
630+
*
631+
* If the log was clean when we mounted, we can check the summary
632+
* counters. If any of them are obviously incorrect, we can recompute
633+
* them from the AGF headers in the next step.
634+
*/
635+
if (XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
636+
(mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks ||
637+
mp->m_sb.sb_ifree > mp->m_sb.sb_icount))
638+
mp->m_flags |= XFS_MOUNT_BAD_SUMMARY;
639+
640+
/*
641+
* We can safely re-initialise incore superblock counters from the
642+
* per-ag data. These may not be correct if the filesystem was not
643+
* cleanly unmounted, so we waited for recovery to finish before doing
644+
* this.
645+
*
646+
* If the filesystem was cleanly unmounted or the previous check did
647+
* not flag anything weird, then we can trust the values in the
648+
* superblock to be correct and we don't need to do anything here.
649+
* Otherwise, recalculate the summary counters.
650+
*/
651+
if ((!xfs_sb_version_haslazysbcount(&mp->m_sb) ||
652+
XFS_LAST_UNMOUNT_WAS_CLEAN(mp)) &&
653+
!(mp->m_flags & XFS_MOUNT_BAD_SUMMARY))
654+
return 0;
655+
656+
return xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount);
657+
}
658+
609659
/*
610660
* This function does the following on an initial mount of a file system:
611661
* - reads the superblock from disk and init the mount struct
@@ -831,32 +881,10 @@ xfs_mountfs(
831881
goto out_fail_wait;
832882
}
833883

834-
/*
835-
* Now the log is mounted, we know if it was an unclean shutdown or
836-
* not. If it was, with the first phase of recovery has completed, we
837-
* have consistent AG blocks on disk. We have not recovered EFIs yet,
838-
* but they are recovered transactionally in the second recovery phase
839-
* later.
840-
*
841-
* Hence we can safely re-initialise incore superblock counters from
842-
* the per-ag data. These may not be correct if the filesystem was not
843-
* cleanly unmounted, so we need to wait for recovery to finish before
844-
* doing this.
845-
*
846-
* If the filesystem was cleanly unmounted, then we can trust the
847-
* values in the superblock to be correct and we don't need to do
848-
* anything here.
849-
*
850-
* If we are currently making the filesystem, the initialisation will
851-
* fail as the perag data is in an undefined state.
852-
*/
853-
if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
854-
!XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
855-
!mp->m_sb.sb_inprogress) {
856-
error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
857-
if (error)
858-
goto out_log_dealloc;
859-
}
884+
/* Make sure the summary counts are ok. */
885+
error = xfs_check_summary_counts(mp);
886+
if (error)
887+
goto out_log_dealloc;
860888

861889
/*
862890
* Get and sanity-check the root inode.

‎fs/xfs/xfs_mount.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ typedef struct xfs_mount {
202202
must be synchronous except
203203
for space allocations */
204204
#define XFS_MOUNT_UNMOUNTING (1ULL << 1) /* filesystem is unmounting */
205+
#define XFS_MOUNT_BAD_SUMMARY (1ULL << 2) /* summary counters are bad */
205206
#define XFS_MOUNT_WAS_CLEAN (1ULL << 3)
206207
#define XFS_MOUNT_FS_SHUTDOWN (1ULL << 4) /* atomic stop of all filesystem
207208
operations, typically for

0 commit comments

Comments
 (0)