Linux filesystem repair: e2fsck, xfs_repair, btrfs check
Filesystem repair tools restore metadata consistency after corruption. They are a last resort, repair prioritizes filesystem structure over data preservation, and a recent backup restore is always safer when available.
When to repair vs restore from backup
| Situation | Action |
|---|---|
| Recent backup exists, corruption is severe | Restore from backup |
| Hardware is failing (SMART errors, I/O errors) | Replace hardware, then restore from backup |
| No backup available, corruption is limited | Attempt repair |
| Filesystem won't mount but data is critical | Extract data first (btrfs restore, photorec), then repair |
Corruption symptoms
# Check kernel logs for filesystem errors
dmesg | grep -E "(EXT4|XFS|BTRFS|I/O|corruption)"
journalctl -b | grep -E "(EXT4|XFS|BTRFS|I/O|corruption)"Common indicators: files go missing or contain wrong data, Input/output error on access, Structure needs cleaning on mount, elevated iowait.
Unmounting safely before repair
The repair tool and the kernel both read and write the block device without coordination. Running fsck on a mounted filesystem can corrupt an otherwise healthy filesystem. The only safe exception is e2fsck -n (read-only, no changes).
# Find processes using the mount
lsof /mnt/point
fuser -m /mnt/point
# Stop processes, then unmount
umount /mnt/point
# Lazy unmount if processes can't be stopped
umount -l /mnt/point
# Force unmount (NFS/FUSE only)
umount -f /mnt/point
# For root filesystem: boot from live USB or single-user mode
# Or schedule fsck at next boot:
touch /forcefsck
# Or force check via mount count:
tune2fs -C 1 /dev/sdX1Ext2/3/4 repair, e2fsck
Always check first (read-only)
# Read-only check, no changes
e2fsck -n /dev/sdX1Automatic repair (preen)
# Safe automatic repairs only
e2fsck -p /dev/sdX1
# Yes to all (more aggressive)
e2fsck -y /dev/sdX1
# Force check even if clean flag is set
e2fsck -f /dev/sdX1Bad block scanning
# Read-only bad block scan
e2fsck -c /dev/sdX1
# Non-destructive read-write scan (more thorough)
e2fsck -cc /dev/sdX1Bad blocks are added to the bad block inode and won't be allocated to files. On SSDs, prefer SMART monitoring, the firmware remaps bad blocks transparently.
Directory optimization
# Reindex and sort directories
e2fsck -D /dev/sdX1The lost+found directory
fsck places orphaned inodes (files whose directory entries were corrupted) in /lost+found/, named by inode number. After repair, examine and rehome them:
ls -la /mnt/lost+found
# Identify files by content, UID/GID, or timestamps
# Recreate lost+found if deleted:
mklost+found /mntExt4 superblock recovery
Ext4 stores backup superblocks in specific block groups. If the primary superblock is damaged, repair from a backup.
Find backup superblock locations
# Method 1: dumpe2fs (if primary is readable)
dumpe2fs /dev/sdX1 | grep -i superblock
# Method 2: mke2fs -n (dry run, shows where backups would be)
mke2fs -n /dev/sdX1Typical backup locations for 4K block filesystems with sparse_super:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872Repair from a backup superblock
# Try each backup until one works
e2fsck -b 32768 -y /dev/sdX1
e2fsck -b 98304 -y /dev/sdX1
e2fsck -b 163840 -y /dev/sdX1On success, e2fsck updates the primary superblock automatically.
XFS repair, xfs_repair
Always dry-run first
# No-modify mode, shows what would be repaired
xfs_repair -n /dev/sdX1Actual repair
xfs_repair /dev/sdX1Log recovery
XFS replays its journal in the kernel at mount time. If the log is corrupt and mount fails, try mounting and unmounting first to replay it:
mount /dev/sdX1 /mnt
umount /mnt
xfs_repair /dev/sdX1-L forces log zeroing even if the log contains uncommitted metadata changes. This can cause data loss. Use only as a last resort when the log is corrupt and cannot be replayed.
# LAST RESORT, force log zeroing
xfs_repair -L /dev/sdX1XFS superblock architecture
XFS has one superblock per Allocation Group (AG). The primary is in AG 0; copies in other AGs serve as backups. Unlike ext4, secondary superblocks are not updated after mkfs.xfs. They may have stale icount/fdblocks values.
# Inspect with xfs_db
xfs_db -c "sb 0" -c p /dev/sdX1 # primary
xfs_db -c "sb 1" -c p /dev/sdX1 # AG 1 backupxfs_logprint
# Transactional view (for debugging recovery)
xfs_logprint -t /dev/sdX1
# Copy log to file
xfs_logprint -C /tmp/xfs.log /dev/sdX1Btrfs repair, btrfs check
Read-only check (default, safe)
btrfs check --readonly /dev/sdX1
# or simply:
btrfs check /dev/sdX1Recovery path (in order of safety)
# 1. Try mount with rescue options (kernel 5.9+)
mount -o ro,rescue=usebackuproot /dev/sdX1 /mnt
mount -o ro,rescue=nologreplay /dev/sdX1 /mnt
mount -o ro,rescue=ignorebadroots /dev/sdX1 /mnt
mount -o ro,rescue=all /dev/sdX1 /mnt
# 2. Extract data with btrfs restore (does not modify filesystem)
btrfs restore -l /dev/sdX1 # list subvolume roots
btrfs restore -D /dev/sdX1 /target # dry run
btrfs restore -m -x -S /dev/sdX1 /target # metadata, xattrs, symlinks
btrfs restore -r <rootid> /dev/sdX1 /target # specific subvolume
# 3. Last resort: btrfs check --repair
btrfs check --repair --force /dev/sdX1The official man page warns: "Do not use --repair unless you are advised to do so by a developer or an experienced user." The tool has a 10-second delay when run without --force to give you time to reconsider. Always extract data with btrfs restore first.
Rescue mount options (kernel 5.9+)
| Option | Effect | Since |
|---|---|---|
rescue=usebackuproot | Use backup root slots in superblock | 5.9 |
rescue=nologreplay | Skip dirty log replay | 5.9 |
rescue=ignorebadroots | Ignore bad tree roots | 5.11 |
rescue=ignoredatacsums | Ignore data checksum verification | 5.11 |
rescue=ignoremetacsums | Ignore metadata checksum verification | 6.12 |
rescue=all | Enable all supported rescue options | - |
FAT/VFAT repair, fsck.fat
# Check only
fsck.fat -n /dev/sdX1
# Automatic repair (least destructive approach)
fsck.fat -a /dev/sdX1
# With bad sector test
fsck.fat -t -a /dev/sdX1
# Verbose
fsck.fat -v -a /dev/sdX1Useful for EFI System Partitions (ESP), which must be FAT32 per the UEFI specification.
Journal recovery
Most "corruption" after an unclean shutdown is just an un-replayed journal. Journaling filesystems replay the journal automatically at mount time and restore consistency, no manual repair needed.
| Filesystem | Journal replay |
|---|---|
| ext4 | Kernel at mount, or e2fsck in userspace at boot |
| XFS | Kernel at mount only, fsck.xfs is a stub that does nothing |
| Btrfs | CoW transaction replay at mount (no separate journal) |
Actual corruption (not just unclean shutdown) is caused by hardware faults, firmware bugs, driver bugs, or something overwriting filesystem structures. These require the repair tools above.
Bad block management
HDDs
# Read-only scan
badblocks -sv /dev/sdX
# Non-destructive read-write scan
badblocks -nsv /dev/sdX
# Destructive write test (wipes all data!)
badblocks -wsv /dev/sdXSSDs and NVMe
SSDs remap bad blocks transparently at the firmware level. badblocks is largely meaningless on SSDs because logical block numbers don't map to fixed physical locations. Use SMART instead:
# ATA/SATA SMART
smartctl -a /dev/sdX | grep -i reallocated
smartctl -t long /dev/sdX
# NVMe SMART
nvme smart-log /dev/nvme0Prevention
- Proper shutdown. Use
shutdown -h noworsystemctl poweroff; never pull the power cord. - UPS: prevents power loss during writes. Configure automatic shutdown on low battery.
- Journaling. ext4, XFS, Btrfs, and ZFS all journal metadata; ext4
data=journalmode journals data too (at a performance cost). - Checksums and scrub. Btrfs and ZFS checksum every block and self-heal from redundancy; schedule monthly scrubs.
- SMART monitoring. Run
smartdwith email alerts and replace drives proactively when reallocated sectors grow. - Regular backups: the only real protection against catastrophic failure.
Emergency quick reference
| Filesystem | Read-only check | Repair |
|---|---|---|
| ext4 | e2fsck -n /dev/sdX1 | e2fsck -y /dev/sdX1 |
| XFS | xfs_repair -n /dev/sdX1 | xfs_repair /dev/sdX1 |
| Btrfs | btrfs check /dev/sdX1 | btrfs check --repair --force /dev/sdX1 |
| FAT | fsck.fat -n /dev/sdX1 | fsck.fat -a /dev/sdX1 |