How Ext4 File System Manages Data in Linux
The fourth extended filesystem (ext4) is the default and most widely adopted storage system in modern Linux distributions, engineered to organize, store, and retrieve files with high reliability and speed. This article examines the core mechanics of ext4 data management, detailing how it uses block groups, inodes, extents, advanced allocation techniques, and journaling to maintain file system integrity and optimize read/write performance.
Block Groups
To minimize fragmentation and reduce disk seek times, ext4 divides a storage partition into segments known as block groups. Rather than scattering file data and metadata across the entire drive, ext4 groups related information together. Each block group contains:
- Superblock Backup: Stores critical file system parameters, such as total block counts and system state.
- Group Descriptors: Tracks the locations of bitmaps and tables within the group.
- Block and Inode Bitmaps: Tracks which data blocks and inodes are free or allocated.
- Inode Table: Stores the actual metadata structures for files located in the group.
- Data Blocks: The physical space where actual file payloads reside.
Inodes and Metadata Management
Every file and directory in ext4 is represented by an inode (index node). Inodes store metadata, including file size, permissions, owner, timestamps, and access control lists (ACLs). Crucially, inodes do not store the filename or the file content itself. Filenames are maintained in directory data blocks, which map human-readable names to their corresponding inode numbers. In ext4, default inodes are 256 bytes, providing ample room for extended attributes and timestamps accurate down to the nanosecond.
Extents
Older Linux filesystems like ext3 used indirect block mapping, requiring individual pointers for every data block. Ext4 replaces this mechanism with extents. An extent represents a range of contiguous physical storage blocks mapped via a single descriptor (up to 128 MB per extent using standard 4 KB blocks). A single inode can store up to four extents directly. For larger files requiring more than four extents, ext4 organizes them into an efficient H-tree structure. This approach drastically reduces metadata overhead and accelerates large file read and write operations.
Allocation Techniques
Ext4 improves write performance and prevents fragmentation using two key allocation strategies:
- Multiblock Allocation (mballoc): Instead of allocating storage one block at a time, ext4 allows the kernel to allocate multiple contiguous blocks in a single request.
- Delayed Allocation (delalloc): When an application writes data, the file system caches it in RAM and delays physical block allocation until the data is actually flushed to the disk. This delay allows the allocator to aggregate writes and allocate large contiguous chunks of space efficiently, rather than guessing block requirements in advance.
Journaling and Data Integrity
To protect against corruption during unexpected shutdowns or power failures, ext4 relies on the Journaling Block Device (JBD2) layer. Before changes are committed to the main storage area, they are recorded in a dedicated journal space. Ext4 supports three journaling modes:
- Ordered (Default): File metadata is written to the journal, but only after the actual file data is written to disk. This prevents stale data exposure and balances safety with performance.
- Journal: Both metadata and file contents are written to the journal before being committed to storage, offering the highest level of integrity at the cost of slower write performance.
- Writeback: Only metadata is journaled, with no strict ordering guarantees relative to data blocks, providing the fastest write speeds but a higher risk of data corruption following a crash.