FAT and ext4: two on-disk layouts
About this lesson
A disk only stores numbered blocks. A file system is the map that says which blocks belong to which file. Save the same 10,000-byte file on two disks and compare the maps: FAT32 keeps a linked list in a table, one entry per cluster, while ext4 gives each file an inode whose extents say where whole runs of blocks start. Then read byte 9000 on both and watch FAT walk its chain while ext4 does one sum. The Linux lessons The file system and Disks, partitions and mounts show the user's side; Inodes and data blocks covers the inode itself.
One file, two layouts
A disk is a long row of numbered blocks. A file system decides which blocks hold a file and how to find them again. Here notes.txt, 10,000 bytes, sits on two disks with 4 KiB blocks: FAT32 and ext4. It needs 3 blocks.
file notes.txt size 10000 bytes block 4096 bytes (4 KiB) on both disks blocks ceil(10000 / 4096) = 3 left FAT32: directory entry -> table of next pointers right ext4: directory entry -> inode -> extents
Read the lesson step by step
One file, two layouts
A disk is a long row of numbered blocks. A file system decides which blocks hold a file and how to find them again. Here notes.txt, 10,000 bytes, sits on two disks with 4 KiB blocks: FAT32 and ext4. It needs 3 blocks.
file notes.txt size 10000 bytes block 4096 bytes (4 KiB) on both disks blocks ceil(10000 / 4096) = 3 left FAT32: directory entry -> table of next pointers right ext4: directory entry -> inode -> extentsFAT32's layout
FAT32 starts with a boot sector, then the file allocation table (FAT), then data clusters numbered from 2. The FAT has one entry per cluster. Each entry holds 0 (free), the next cluster of the same file, or end of chain.
on disk, in order: boot sector + reserved sectors FAT (2 copies by default) data area: cluster 2, 3, 4, ... FAT entry n describes cluster n: 0 free m next cluster of the same file is m EOF last cluster of the file (end of chain) FAT32 entries are 32 bits; the top 4 are reservedThe directory points to cluster 5
A FAT directory is a list of 32-byte entries. NOTES.TXT's entry holds its name, its size and its first cluster: 5. There is no inode: everything FAT knows about the file sits in this entry.
32-byte directory entry (short name): name NOTES + TXT (8 + 3 bytes) attributes 1 byte time, date first cluster 5 size 10000 bytes no inode: the entry itself is the metadataFollowing the chain
For the rest, the kernel follows the chain in the FAT: entry 5 says 6, entry 6 says 9, entry 9 says end of chain. So the file lives in clusters 5, 6 and 9: a linked list kept in a table.
FAT[5] = 6 FAT[6] = 9 FAT[9] = EOF end of chain chain 5 -> 6 -> 9 clusters 7 and 8 belong to other files or are free, so the file is in pieces (fragmented)Byte 9000: walk to it
Now read byte 9000. 9000 ÷ 4096 = 2, so it is in the file's third cluster. FAT cannot jump there: it must follow 2 links, 5 to 6 to 9. In a big file, the walk grows with the offset.
the program asks the same way on both disks (C99, POSIX): #define _POSIX_C_SOURCE 200809L #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { char c; int fd = open("notes.txt", O_RDONLY); if (fd < 0) { perror("open"); return 1; } ssize_t n = pread(fd, &c, 1, 9000); /* one byte at offset 9000 */ printf("read %zd byte\n", n); close(fd); return n == 1 ? 0 : 1; } block index 9000 / 4096 = 2 (third block, from 0) FAT walk 5 -> 6 -> 9 2 hops in cluster byte 808ext4's layout
ext4 splits the disk into block groups of 32,768 blocks (128 MiB). A group has a block bitmap, an inode bitmap, an inode table (inode 12 is one row in it) and data blocks. Some groups also keep a backup superblock and group descriptors.
one block group (4 KiB blocks): super block backup copy, only in some groups group descriptors backup copy, only in some groups data block bitmap 1 block inode bitmap 1 block inode table many blocks (inode 12 is one row) data blocks the rest 32768 blocks x 4096 bytes = 128 MiB with flex_bg (on by default) the bitmaps and inode tables of several groups sit together in the first oneName to inode number
An ext4 directory entry holds just a name and an inode number: notes.txt is inode 12. The size, owner, times and block map live in the inode. So two names can share one inode, a hard link, which FAT cannot do.
ext4_dir_entry_2: inode 12 rec_len length of this entry name_len 9 file_type regular file name notes.txt inode 2 is the root directory; 11 is lost+found, the first ordinary inode, so files start at 12An extent: start and length
Inode 12's record (256 bytes) has room for four extents. One is enough: logical block 0, length 3, starting at block 40960. An extent says these 3 blocks sit side by side, instead of one pointer per block.
inode 12 size 10000 bytes i_block extent tree root (60 bytes): header (magic 0xF30A) + up to 4 extents extent 0 ee_block = 0 first file block ee_len = 3 blocks ee_start = 40960 covers blocks 40960 .. 40962Byte 9000: one sum
Byte 9000 is again block 2 of the file. The extent covers blocks 0 to 2, so the kernel adds: 40960 + 2 = 40962. No chain to walk, however far into the file the byte is.
block index 9000 / 4096 = 2 extent 0 <= 2 < 3 yes disk block 40960 + (2 - 0) = 40962 in block byte 808 a file with many pieces has many extents, kept in a small tree; one extent covers up to 32,768 blocksFree space: a bitmap
To find free blocks, ext4 reads the block bitmap: one bit per block, 1 used, 0 free. Here 40958 to 40962 are used and 40963 is free. FAT keeps the same fact inside its table: a 0 entry, like FAT[8].
block bitmap (one bit per block in the group): 40958 1 used 40959 1 used 40960 1 used 40961 1 used 40962 1 used 40963 0 free FAT: free space is entries that hold 0 FAT[8] = 0 cluster 8 is freeTwo maps of the same bytes
FAT: the directory gives the first cluster, the table links the rest. Simple, and readable almost everywhere. ext4: name, then inode, then extents, plus bitmaps. Random access is one sum, and one inode can have many names.
both seeks as a Python simulation: BLOCK = 4096 fat = {2: "EOF", 3: 4, 4: "EOF", 5: 6, 6: 9, 7: "EOF", 8: 0, 9: "EOF"} extents = [(0, 3, 40960)] # (first logical block, length, start) def fat_seek(first, offset): # walk the chain, one entry per block c, hops = first, 0 for _ in range(offset // BLOCK): c, hops = fat[c], hops + 1 return c, hops def ext4_seek(offset): # find the extent, then add n = offset // BLOCK for first, length, start in extents: if first <= n < first + length: return start + (n - first) print("FAT cluster, hops:", fat_seek(5, 9000)) print("ext4 block:", ext4_seek(9000)) prints: FAT cluster, hops: (9, 2) ext4 block: 40962
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01On a FAT32 disk, what does the FAT entry for cluster 5 hold?
The table has one entry per cluster, and each entry is a next pointer. Following them from the first cluster (stored in the directory entry) gives the whole file: here 5, 6, 9.
02With 4 KiB blocks, which block of a file holds byte 9000 (counting blocks from 0)?
9000 ÷ 4096 = 2 with a remainder of 808, so the byte is at offset 808 of the file's third block, block 2.
03Why can't FAT jump straight to the cluster that holds byte 9000?
A FAT file is a linked list. To reach the n-th cluster the kernel follows n links, here 5 to 6 to 9. Keeping the table in memory makes the walk quick, but it still grows with the offset.
04Inode 12 has one extent: first block 0, length 3, start 40960. On which disk block is the file's block 2?
Block 2 lies inside the extent (0 to 2), so the disk block is 40960 + (2 − 0) = 40962. No chain is walked.
05Which statement about directory entries is true?
In ext4 the metadata and the extents live in the inode, and the directory only maps names to inode numbers. FAT has no inodes: its 32-byte entry holds the name, size and first cluster, which is why it cannot have hard links.
Glossary
- Block (cluster)
- The unit a file system hands out space in, fixed when the disk is formatted; 4 KiB is typical. FAT calls it a cluster.
- File allocation table (FAT)
- A table with one entry per cluster. Each entry holds 0 (free), the next cluster of the same file, or an end-of-chain mark.
- Directory entry
- One record in a directory. In FAT: 32 bytes with name, size and first cluster. In ext4: a name and an inode number.
- Inode
- A file's record in ext4: size, owner, times and where its data is. Directory entries point to it by number.
- Extent
- A run of blocks side by side, written as a start block and a length (plus which file block it begins at).
- Block group
- A slice of an ext4 disk (128 MiB with 4 KiB blocks) with its own data blocks, a block bitmap, an inode bitmap and an inode table (with flex_bg these may be stored in a neighbouring group).
- Block bitmap
- One bit per block in a group: 1 if the block is used, 0 if it is free.
- Fragmentation
- When a file's blocks are scattered instead of side by side, as in clusters 5, 6 and 9.
Notes and sources
- The cluster numbers 5, 6 and 9, inode 12 and block 40960 are example values chosen to be typical; how the FAT file came to be split around clusters 7 and 8 is not shown.
- FAT calls a block a cluster. Both the cluster size and ext4's block size (1 to 64 KiB, typically 4 KiB) are fixed when the disk is formatted; this example uses 4 KiB on both.
- FAT keeps two copies of the table by default. On FAT12 and FAT16 the root directory has its own fixed area before the data; on FAT32 it is an ordinary chain of clusters, usually starting at cluster 2. Long file names are stored in extra directory entries next to the short one.
- The FAT walk is fast in practice because the kernel keeps the table in memory: OSTEP notes that random access then means scanning the in-memory table first, then one disk read.
- ext4 also has an inode bitmap, a journal, and backup copies of the superblock and group descriptors in some groups only (sparse_super); the figure leaves them out. With flex_bg, which mke2fs turns on by default for ext4, the bitmaps and inode tables of several groups are packed together in the first group of the set. Large directories use a hashed tree instead of a plain list of entries.
- Updating a file touches several places at once (directory, table or inode, bitmap, data). If the power fails half way, they disagree: the lesson Crash consistency and journaling shows how ext4's journal fixes that.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 40: File System Implementation
- A. Brouwer, The FAT file system
- Berkeley CS162, Lecture 20: Filesystems 1 (FAT case study)
- MIT 6.1810 (2024), lecture notes: File systems
- Linux kernel documentation: VFAT
- Linux kernel documentation: ext4 blocks
- Linux kernel documentation: ext4 block groups
- Linux kernel documentation: ext4 block and inode bitmaps
- Linux kernel documentation: ext4 inodes
- Linux kernel documentation: ext4 extent tree
- Linux kernel documentation: ext4 directory entries
- Linux kernel documentation: ext4 special inodes
- Linux man-pages: open(2)
- Linux man-pages: pread(2)
Quiz Unlocks after the last step.