Sub topic 14
Linked Allocation and FAT
Prerequisites and working model
Assume you know what a file, disk block, pointer, and directory entry are. A disk block is a fixed-size region that stores part of a file. A pointer identifies another block.
In linked allocation, a file is stored as a chain of blocks. Each block contains file data and a pointer to the next block. The directory entry stores the first block, and sometimes the last block. The final block contains an end-of-file marker.
The blocks do not need to be next to one another. Therefore, linked allocation avoids external fragmentation, which occurs when enough total free space exists but it is split into separated regions.
Strengths and limitations
Linked allocation works well for sequential access. To read a file from the beginning, the system follows each pointer in order. Appending data is also simple when the last block is known.
Direct access is slow. To read block 100 of a file, the system may need to follow 99 earlier pointers. Each pointer also consumes space that could otherwise store data. If one pointer is corrupted, the rest of the chain may become unreachable. A cycle in the pointers can make traversal continue forever unless the file system detects it.
File Allocation Table
The File Allocation Table, or FAT, stores the links in a separate table instead of inside the data blocks. Each disk block has a corresponding FAT entry.
For example, this chain can be represented as follows:
| Block | FAT entry | Meaning |
|---|---|---|
| 9 | 16 | The next block is 16 |
| 16 | 1 | The next block is 1 |
| 1 | EOF | This is the final block |
The directory entry records block 9 as the file's first block. To follow the file, the system reads FAT[9], then FAT[16], then FAT[1]. A FAT entry can also mark a block as free or reserved.
Because the table contains all links, the data blocks do not need pointer fields. The operating system can cache frequently used FAT entries in memory. This improves access, but large disks require a large table. FAT corruption can affect many files because the table is shared.
Tracing a file chain
EOF = -1
fat = {
9: 16,
16: 1,
1: EOF,
}
def follow_chain(first_block, table):
chain = []
block = first_block
while block != EOF:
if block in chain:
raise ValueError("cycle detected")
chain.append(block)
block = table[block]
return chain
print(follow_chain(9, fat)) # [9, 16, 1]Comparison with contiguous allocation
Contiguous allocation gives fast sequential and direct access, but file growth can be difficult. Linked allocation grows easily and avoids external fragmentation, but direct access is slower and pointer corruption is dangerous.
Exercise
A FAT chain is 7 → 12 → 4 → EOF. Which blocks contain the file, and how many FAT links must be read to find the final block?
Show answer
The file uses blocks 7, 12, and 4. Two next-block links must be read: FAT[7] and FAT[12].