Chapter 12: Recovery, Corruption Detection and Repair

Updated

October 31, 2026

Chapter 12: Recovery, Corruption Detection and Repair

Storage media is physically volatile. Cosmic rays, firmware bugs in drive controllers, failing SATA/NVMe lanes, cabling faults, and abrupt power loss can induce silent data corruption (bit rot) or leave metadata pointers pointing to corrupt blocks.

A production filesystem must provide robust mechanisms to detect corruption immediately, isolate damaged structures before damage spreads, and offer deterministic repair tools capable of restoring structural invariants.

This chapter explores ext4’s resilience architecture: metadata checksumming (metadata_csum), the internal five-pass mechanics of e2fsck, runtime kernel error handling, locating and mounting backup superblocks, and low-level forensic disk analysis with debugfs.

Checksum Offsets and Verification (crc32c)

In early ext2 and ext3 filesystems, corruption often went unnoticed until a process attempted to traverse an invalid pointer and crashed.

Modern ext4 incorporates Full Metadata Checksumming (metadata_csum), protecting every critical structural element with a hardware-accelerated CRC32c (Castagnoli) checksum:

┌────────────────────────────────────────────────────────────────────────┐
│                        METADATA CSUM CALCULATION                       │
│                                                                        │
│   Superblock UUID (128-bit) ──► Initial CRC32c Seed (s_csum_seed)      │
│                                           │                            │
│                                           ▼                            │
│   Target Metadata Block / Inode Bytes (minus checksum field itself)    │
│                                           │                            │
│                                           ▼ Hardware CRC32c            │
│   Calculated Checksum ──► Matches Stored Checksum?                     │
│         ├─► YES: Process operation safely                              │
│         └─► NO:  Trigger ext4_error(); block operation; log to dmesg   │
└────────────────────────────────────────────────────────────────────────┘

Checksum Locations Across On-Disk Structures

  • Superblock: s_checksum (offset 0x3FC in the superblock).
  • Group Descriptors: bg_checksum (16-bit) and bg_block_bitmap_csum_hi/lo (32-bit).
  • Inodes: i_checksum_lo (offset 0x7E) and i_checksum_hi (offset 0x82).
  • Extent Tree Nodes: Final 4 bytes of leaf and index blocks (ext4_extent_tail.et_checksum).
  • Directory Blocks: Final 12 bytes of leaf blocks (ext4_dir_entry_tail.det_checksum).

By initializing the CRC32c generator with the filesystem’s UUID, ext4 prevents cross-volume contamination: if a block from drive A is accidentally copied to drive B, its checksum will fail validation immediately.

Corruption Detection in Production (errors=)

When the Linux kernel detects a corrupted extent tree, an invalid checksum, or an unreadable inode during normal runtime operations, it invokes ext4_error().

The host’s response is governed by the errors= mount option:

# Configured in /etc/fstab:
UUID=...  /data  ext4  errors=remount-ro  0 2
  • errors=continue: Records an error in the system log and continues running. Highly dangerous; allows corrupted metadata to overwrite adjacent valid data.
  • errors=remount-ro (Recommended Production Default): Immediately sets the filesystem state to Read-Only (MS_RDONLY). Active applications can read existing data, but write operations fail immediately, halting corruption propagation until an administrator investigates.
  • errors=panic: Triggers a full Linux kernel panic immediately. Essential for high-availability database failover clusters where fencing (STONITH) must switch traffic to a standby node before corrupted state can persist.

Backup Superblocks and Redundancy

If Block Group 0 suffers catastrophic physical bad-sector failure, the primary superblock becomes unreadable, preventing normal mounting.

As established in Chapter 2, ext4 maintains redundant backup superblocks in Block Groups 1, 3, 5, 7, 9, 25, 49, etc.

To locate and utilize backup superblocks:

# Query all backup superblock locations
sudo dumpe2fs /dev/nvme0n1p2 | grep -i "backup"
# Backup superblock at 32768, Group descriptors at 32769-32770 ...
# Backup superblock at 98304, Group descriptors at 98305-98306 ...

# Force e2fsck to restore filesystem using the backup superblock at Block 32768
sudo e2fsck -b 32768 -y /dev/nvme0n1p2

e2fsck reads the intact backup superblock from Group 1, reconstructs the missing master superblock in Group 0, and restores normal mountability.

Forensic Analysis of ext4 Images

The debugfs utility provides an interactive, read-only window into raw ext4 data structures without mounting the filesystem:

# Launch interactive debugfs shell on unmounted volume
sudo debugfs /dev/nvme0n1p2

# Inside debugfs:
debugfs:  stat <2>               # Inspect Root Directory Inode 2
debugfs:  stat /etc/passwd       # Inspect inode metadata and extent tree
debugfs:  extents /var/log/syslog# Print exact physical block mappings
debugfs:  dump_unused            # Recover deleted files from free blocks
debugfs:  bdump 1048576          # Hex dump physical block 1048576

Using debugfs, forensics investigators can extract deleted files from unallocated inodes, map corrupted bad blocks back to owning filenames, and inspect raw journal transaction streams.