Sub topic 16
Linked allocation and FAT
This chapter assumes you know what a disk block is, how seek time and rotational latency are computed, and the basic idea of contiguous allocation. It covers how linked allocation stores a file, why it has a random-access problem, and how the File Allocation Table (FAT) fixes that problem.
The linked allocation idea
In linked allocation, a file is a chain of disk blocks that can be anywhere on the disk. Each block holds some file data and a pointer to the next block. The directory entry stores the address of the first block. The last block holds a special end-of-file marker instead of a pointer.
Advantages over contiguous allocation:
- There is no external fragmentation. Any free block can be used.
- A file can grow without knowing its final size in advance.
- No compaction is needed.
Problems:
- Slow direct access. To reach block k you must read blocks 0 to k-1 in order, because each block's address is stored in the block before it.
- Pointer overhead. The pointer reduces the data space in every block.
- Fragile chains. One damaged pointer loses the rest of the file.
- Scattered blocks. Consecutive blocks of a file are often far apart, so sequential reads cause extra seeks.
Worked calculation
Take a 512-byte block and a 4-byte pointer. Each block holds 512 − 4 = 508 bytes of data. To read the byte at offset 5000, compute 5000 // 508 = 9. That byte is in logical block 9 (counting from 0). Reaching it takes 10 disk reads, one for each block in the chain up to and including block 9.
From linked blocks to the FAT
The File Allocation Table moves the pointers out of the data blocks and into one table at the start of the volume. The table has one entry per block (or per cluster, a fixed group of blocks). Entry i says what follows block i in its file.
An entry holds one of these values:
- a free marker, usually 0
- the number of the next block in the file
- an end-of-chain marker
- a bad-block marker
The directory entry stores only the file's first block. Data blocks now hold only data, so a block of size B holds B bytes of file content.
Suppose file A is the chain 4 → 7 → 2, and file B is the chain 1 → 5.
| Block | FAT entry |
|---|---|
| 0 | free |
| 1 | 5 |
| 2 | EOF |
| 3 | free |
| 4 | 7 |
| 5 | EOF |
| 6 | free |
| 7 | 2 |
Why this helps random access
The FAT is small enough to cache in memory. To reach logical block k, the OS follows k entries in RAM, with no disk access. It then does one disk read for the data block. The chain is still followed one link at a time, so direct access is O(k) in memory steps. It no longer costs k disk reads.
For the same offset 5000 with 512-byte blocks, the logical block is 5000 // 512 = 9. That takes 9 in-memory hops and 1 disk read.
EOF = -1
def chain(fat, start):
"""Return the list of blocks of a file, following FAT entries."""
blocks, seen, b = [], set(), start
while b != EOF:
if b in seen:
raise ValueError("cycle in FAT chain")
seen.add(b)
blocks.append(b)
b = fat[b]
return blocks
def linked_reads(offset, block_size, ptr_size):
"""Disk reads to reach a byte offset with pointers inside blocks."""
return offset // (block_size - ptr_size) + 1
def fat_disk_reads(offset, block_size):
"""With a cached FAT, only the target data block is read from disk."""
return 1
fat = {4: 7, 7: 2, 2: EOF, 1: 5, 5: EOF}
print(chain(fat, 4)) # [4, 7, 2]
print(chain(fat, 1)) # [1, 5]
print(linked_reads(5000, 512, 4)) # 10
print(fat_disk_reads(5000, 512)) # 1Sizing the FAT
The FAT needs one entry per block, so:
FAT size = (disk size / block size) × entry size
For a 16 GB disk with 4 KB blocks and 4-byte entries, there are 2^34 / 2^12 = 2^22 entries. The table is 2^22 × 4 B = 16 MB.
The entry width limits the number of addressable blocks. An n-bit entry can address at most 2^n blocks, minus the values reserved for free, EOF and bad. So FAT16 with 32 KB clusters tops out near 2^16 × 32 KB = 2 GB. FAT32 uses 28 of its 32 bits for the cluster number. Its maximum file size is 4 GiB − 1 byte, because the directory entry stores the size in 32 bits.
A larger cluster shrinks the table but wastes more space. On average, half a cluster per file is lost to internal fragmentation in the last cluster.
Comparison
| Property | Contiguous | Linked | FAT |
|---|---|---|---|
| External fragmentation | Yes | No | No |
| Direct access cost | One seek | k disk reads | k RAM hops, 1 disk read |
| Space overhead | None | Pointer per block | Table per volume |
| File growth | Hard | Easy | Easy |
| Failure impact | Local | Chain breaks | Table damage hits all files |
Gotchas
Other points that come up in exam questions:
- FAT does not fix scattered blocks. A fragmented file still causes seeks, which is why defragmentation exists.
- Free space is found by scanning the FAT for free entries, so the FAT also serves as the free-space structure.
- Real FAT numbers clusters from 2, because 0 and 1 are reserved. Exam problems usually simplify this.
Exercise
A disk of 8 GB uses 4 KB blocks and a FAT with 4-byte entries. How large is the FAT? Then, for a plain linked scheme with 1 KB blocks and 4-byte pointers, how many blocks does a 10,000-byte file need, and how many disk reads are needed to read its last byte?
Show answer
FAT: 2^33 / 2^12 = 2^21 entries, times 4 B = 8 MB. Linked scheme: each block holds 1024 − 4 = 1020 data bytes. 10,000 / 1020 is about 9.8, so the file needs 10 blocks. Reaching the last byte means reading the whole chain, which is 10 disk reads.
Exercise
A FAT volume has 2^20 clusters of 8 KB each. What is the smallest entry size, in whole bytes, that can address every cluster? What is the total FAT size with that entry size?
Show answer
Addressing 2^20 clusters needs at least 20 bits, plus room for special values, which fits in 3 bytes (24 bits). The FAT is 2^20 × 3 B = 3 MB. Many systems round up to 4 bytes for alignment, giving 4 MB.