The filesystem is not just a place to store files; it’s an active participant in how your data is accessed and protected.

Let’s see what happens when we create a file on a typical Linux system. When you run echo "hello" > test.txt, it’s not a simple copy operation. The kernel first needs to find free blocks on the disk. This involves consulting the filesystem’s metadata structures – think of them as the filesystem’s internal index. Once free blocks are identified, the data itself is written, and then the metadata is updated to point to those blocks and record the file’s attributes (like ownership, permissions, and timestamps). This metadata update is crucial and often involves multiple writes to ensure consistency.

This dance between data and metadata is where ext4, XFS, and Btrfs diverge significantly, each with its own philosophy on how to manage this, leading to different performance characteristics and resilience.

ext4: The Reliable Workhorse

ext4 is the default for many Linux distributions for a reason: it’s robust, well-tested, and offers a good balance of features and performance. It uses a journaling approach, meaning it logs intended metadata changes to a journal area before they are applied to the main filesystem. If the system crashes mid-operation, the journal can be replayed to bring the filesystem back to a consistent state quickly, minimizing the risk of corruption.

Example Scenario: Imagine a sudden power loss while writing a large file.

  1. What happened: The system crashed during a metadata update for a new inode.
  2. Common Causes & Fixes:
    • Power Outage/System Crash: This is the most common cause for filesystem corruption. The fix is not to "fix" the cause but to recover.
      • Diagnosis: Boot from a Live CD/USB or another OS. Mount the affected partition read-only. If it doesn’t mount, proceed to fsck.
      • Fix: Run sudo fsck -y /dev/sdXn (replace /dev/sdXn with your partition). The -y flag automatically answers "yes" to repair prompts.
      • Why it works: fsck replays the journal and checks metadata structures for inconsistencies, repairing them based on the logged operations or by intelligently guessing the correct state.
    • Bad Sectors on Disk: Physical disk errors can lead to unreadable metadata.
      • Diagnosis: sudo smartctl -a /dev/sdX (install smartmontools). Look for high counts in "Reallocated_Sector_Ct" or "Current_Pending_Sector".
      • Fix: If disk errors are found, the primary fix is to back up data immediately and replace the failing drive. fsck might be able to work around minor errors, but it’s a temporary solution.
      • Why it works: smartctl reports on the drive’s health. Replacing the drive eliminates the source of the read errors.
    • Incorrect Mount Options: Mounting a filesystem with incorrect options can prevent it from being written to, leading to apparent "errors."
      • Diagnosis: Check /etc/fstab. Ensure the entry for the partition has defaults or appropriate write options (e.g., rw, noatime).
      • Fix: Correct the /etc/fstab entry. For example, change ro,user to rw,user. Then remount: sudo mount -o remount,rw /mount/point.
      • Why it works: Ensures the filesystem is mounted with read-write permissions, allowing normal operations.
    • Filesystem Corruption (Journal Issues): The journal itself can become corrupted, hindering recovery.
      • Diagnosis: During boot, you might see messages about journal errors. sudo tune2fs -l /dev/sdXn | grep 'Journal Features' and look for has_journal. If it’s missing, the journal was removed or is severely damaged.
      • Fix: Recreate the journal: sudo tune2fs -O has_journal /dev/sdXn, followed by sudo fsck -y /dev/sdXn.
      • Why it works: Re-enables the journaling feature, allowing fsck to perform its recovery operations.
    • Full Disk: Running out of space can cause write operations to fail, sometimes leading to inconsistent states.
      • Diagnosis: df -h /mount/point. Look for 100% usage.
      • Fix: Free up space by deleting unnecessary files or expanding the partition.
      • Why it works: Provides the necessary free blocks for the filesystem to write new data and metadata.
    • Hardware Issues (RAM/Controller): Faulty RAM or a failing storage controller can introduce data corruption during writes.
      • Diagnosis: Run memtest86+ from a bootable USB. Check system logs (dmesg) for disk-related errors that don’t point to specific bad sectors.
      • Fix: Replace faulty RAM or the storage controller.
      • Why it works: Ensures data integrity during transit from the CPU to the disk.

The next error you’ll likely see after fixing ext4 corruption is a "Stale file handle" if a process was holding a reference to a file that was deleted or moved during the repair.

XFS: The High-Performance Powerhouse

XFS is designed for high performance, especially with large files and many concurrent I/O operations. It uses a similar journaling approach but with a more sophisticated allocation strategy and metadata handling. XFS excels in scenarios where throughput is critical, like large databases or media servers. It’s known for its speed and scalability.

Example Scenario: A busy database server with many threads writing to different files simultaneously.

  1. What happened: A concurrent write operation to a large file was interrupted by a system crash.
  2. Common Causes & Fixes:
    • System Crash/Power Loss: Similar to ext4, this is the primary cause of needing recovery.
      • Diagnosis: Boot from a Live CD/USB. Attempt to mount the XFS partition. If it fails with a "clean" error, proceed to xfs_repair.
      • Fix: Run sudo xfs_repair /dev/sdXn. If it fails with a dirty filesystem, run sudo xfs_repair -L /dev/sdXn.
      • Why it works: xfs_repair checks the filesystem’s consistency. The -L option forces log zeroing, which is necessary if the journal is inconsistent but potentially risky, as it might discard metadata that wasn’t fully committed.
    • Disk Hardware Failure: Bad sectors can corrupt XFS metadata, which is more complex than ext4’s.
      • Diagnosis: sudo smartctl -a /dev/sdX. Look for reallocated sectors. Also, sudo xfs_info /dev/sdXn might report read errors if the filesystem is aware of them.
      • Fix: Back up data and replace the drive.
      • Why it works: Eliminates the faulty hardware causing data integrity issues.
    • Improper Shutdown/Unmount: Forcing an unmount without proper fsync can leave data in an inconsistent state.
      • Diagnosis: Check dmesg for messages indicating an unclean shutdown or XFS errors.
      • Fix: Ensure proper unmounting (umount -f is a last resort, but sync before unmounting is good practice). If corruption occurred, use xfs_repair.
      • Why it works: Proper unmounting ensures all cached writes are flushed to disk, maintaining consistency.
    • Filesystem Full: XFS, like any filesystem, will fail writes if it’s full.
      • Diagnosis: df -h /mount/point.
      • Fix: Free up space or resize the XFS filesystem using xfs_growfs /mount/point (if the underlying partition was expanded).
      • Why it works: Provides the necessary space for metadata and data allocation.
    • Journal Corruption: The XFS log can become corrupted.
      • Diagnosis: sudo xfs_repair /dev/sdXn will report if the log is bad.
      • Fix: Use sudo xfs_repair -L /dev/sdXn to zero the log. This is the most common fix for a dirty XFS filesystem and can recover from many states.
      • Why it works: Zeroing the log forces XFS to rebuild its internal state from the primary metadata structures, bypassing the corrupted journal.

The next common issue after fixing XFS corruption might be "Input/output error" if xfs_repair -L had to discard some unrecoverable metadata, leading to orphaned inodes or data blocks.

Btrfs: The Modern, Feature-Rich Option

Btrfs (B-tree Filesystem) is a more modern filesystem that introduces advanced features like snapshots, built-in RAID, data checksumming, and transparent compression. It uses a copy-on-write (CoW) mechanism, meaning that when data is modified, Btrfs writes the new data to a different location on the disk rather than overwriting the old data in place. This inherently makes it resilient to power loss during writes, as the old data remains intact until the new data is fully committed.

Example Scenario: Taking a snapshot of a filesystem before a risky software upgrade.

  1. What happened: A CoW operation for a file was interrupted, but the original data remains untouched.
  2. Common Causes & Fixes:
    • System Crash/Power Loss: Btrfs’s CoW design makes it very robust against this.
      • Diagnosis: Boot normally. If Btrfs detects an inconsistency, it will often attempt self-healing or report errors in dmesg.
      • Fix: In most cases, Btrfs will automatically recover using its CoW properties. If there are persistent issues, you might need sudo btrfs check /dev/sdXn. If that fails, sudo btrfs check --repair /dev/sdXn (use with extreme caution, as it’s less mature than fsck or xfs_repair).
      • Why it works: CoW ensures that a write operation is atomic; either the new data is fully in place and referenced, or the old data is still there and referenced. No partial writes of new data overwrite old data.
    • Disk Errors / Data Corruption: Btrfs’s checksumming is its primary defense.
      • Diagnosis: sudo btrfs scrub start /mount/point. This checks all data and metadata checksums. Errors will be reported in dmesg or sudo btrfs scrub status /mount/point.
      • Fix: If the filesystem is configured with redundancy (e.g., RAID1), Btrfs will automatically try to repair the corrupted block using a good copy. If no redundancy exists, you’ll need to restore from backup.
      • Why it works: Checksumming detects bit rot or block corruption. If redundancy is available, it provides a mechanism to correct the bad block.
    • Running Out of Space / Allocation Failures: Btrfs needs free space to perform CoW operations.
      • Diagnosis: df -h /mount/point and sudo btrfs filesystem usage /mount/point.
      • Fix: Free up space. Btrfs can sometimes get into a state where it reports 100% full even if df shows some space, due to metadata allocation. A btrfs balance operation might help.
      • Why it works: Ensures that there’s physical space on the disk for new data blocks to be written to during CoW.
    • Metadata Corruption (Rare): While CoW helps, severe hardware issues could still corrupt metadata.
      • Diagnosis: dmesg will show Btrfs-specific errors.
      • Fix: btrfs check --repair is a last resort. Often, if data is on a redundant configuration, Btrfs can self-heal.
      • Why it works: The --repair flag attempts to fix structural inconsistencies in the B-tree metadata.
    • Suboptimal Configuration: Using Btrfs on a single drive with no redundancy is less resilient than on a multi-disk setup.
      • Diagnosis: sudo btrfs filesystem show /dev/sdXn. Look at the "Profile" for data and metadata.
      • Fix: Reconfigure the filesystem if possible (e.g., convert single to RAID1 using btrfs balance convert).
      • Why it works: Leverages redundancy to protect against single-disk failures.

The next challenge you’ll likely encounter with Btrfs is understanding its advanced features like snapshots and subvolumes, and how to manage them effectively.

Want structured learning?

Take the full Storage course →