Sub topic 17
Indexed Allocation and Inodes
This chapter assumes you know contiguous and linked allocation, and how a disk is divided into fixed-size blocks. It covers the structure of an index, the Unix inode, and the maximum-file-size and access-count calculations that GATE CS asks about.
Why indexed allocation exists
Contiguous allocation suffers from external fragmentation and from files that cannot grow. Linked allocation, including FAT, grows easily but makes direct access slow, because reaching block k means following k links. Indexed allocation fixes both problems. Each file has an index block, which is an array of pointers to the file's data blocks. Entry i of the index points to logical block i of the file.
- Direct access is fast. You read the index entry and jump straight to the block.
- There is no external fragmentation, because data blocks can be anywhere.
- The cost is the index itself. A one-block file still needs an index block, which is wasteful for tiny files.
The Unix inode
A Unix-style file system keeps one inode (index node) per file. The inode holds the file's metadata and a fixed number of block pointers. The file name is not in the inode. It lives in the directory entry, which maps a name to an inode number.
A classic inode contains:
- the file type and permission bits
- the owner and group
- the size
- timestamps
- the link count
- the block pointers
The pointers fall into four groups:
- Direct pointers (typically 12). Each points straight to a data block.
- Single indirect pointer. It points to a block full of pointers to data blocks.
- Double indirect pointer. It points to a block of pointers to single-indirect blocks.
- Triple indirect pointer. It adds one more level.
Small files, which are the common case, are served entirely by direct pointers. The inode is read once, so no extra index blocks are needed. Only large files pay for indirection.
Computing the maximum file size
Let the block size be B bytes and the pointer size be s bytes. One index block holds
p = B / s
pointers. With d direct pointers and one pointer at each indirect level, the number of data blocks a file can address is
blocks = d + p + p² + p³
The maximum file size is blocks × B. Drop the terms for levels the inode does not have.
def max_file_size_bytes(block_size, pointer_size, direct, single, double, triple):
p = block_size // pointer_size # pointers that fit in one index block
blocks = direct + single * p + double * p**2 + triple * p**3
return blocks * block_size
# 4 KB blocks, 4-byte pointers, 12 direct + single + double + triple indirect
size = max_file_size_bytes(4096, 4, 12, 1, 1, 1)
print(size)
print(size / 2**40) # slightly more than 4 (TiB)Here p = 4096 / 4 = 1024. The block count is 12 + 1024 + 1,048,576 + 1,073,741,824 = 1,074,791,436. Multiplying by 4096 gives 4,402,345,721,856 bytes, which is just over 4 TiB.
Almost all of that capacity comes from the triple-indirect term. The 12 direct pointers cover only 48 KB.
A second limit: pointer width
A pointer of s bytes can name at most 2^(8s) blocks. With 4-byte pointers and 4 KB blocks, the disk can have at most 2³² blocks, which is 16 TiB. The real maximum file size is the smaller of the inode formula and the disk-size limit. In the example above, the inode limit (about 4 TiB) is the smaller one. If a question says the disk has only 2^20 blocks, the disk limit wins.
Counting disk accesses
Assume the inode is already in memory. The number of disk reads needed to fetch logical block n (counting from 0) depends on where n falls:
| Block number n | Region | Disk reads |
|---|---|---|
| 0 to d − 1 | Direct | 1 (the data block) |
| d to d + p − 1 | Single indirect | 2 |
| next p² blocks | Double indirect | 3 |
| next p³ blocks | Triple indirect | 4 |
If the inode is not cached, add one read for the inode.
def accesses_for_block(n, direct, p):
# Inode assumed to be in memory; counts index reads plus the data read.
if n < direct:
return 1
n -= direct
if n < p:
return 2
n -= p
if n < p**2:
return 3
return 4
p = 1024
print(accesses_for_block(5, 12, p)) # 1
print(accesses_for_block(12, 12, p)) # 2
print(accesses_for_block(12 + p, 12, p)) # 3
print(accesses_for_block(12 + p + p**2, 12, p)) # 4Space overhead for a given file
To find the total blocks a file uses, count data blocks and then the index blocks that the data blocks require.
Take a 100 KB file with 4 KB blocks and p = 1024. It needs 25 data blocks. The first 12 use direct pointers. The other 13 need one single-indirect block. The file therefore occupies 25 + 1 = 26 blocks. Only a file larger than 12 + 1024 blocks needs a double-indirect block, and that file also needs the first-level index block plus as many second-level blocks as it has data blocks to cover, divided by p, rounded up.
Comparing the three allocation schemes
| Property | Contiguous | Linked / FAT | Indexed (inode) |
|---|---|---|---|
| Sequential access | Fast | Fast | Fast |
| Direct access | Fast | Slow (FAT in memory helps) | Fast |
| External fragmentation | Yes | No | No |
| Growing a file | Hard | Easy | Easy |
| Per-file overhead | None | One pointer per block | Index blocks |
| Large files | Needs a large free extent | Long chains | Extra reads per level |
Common errors
- Using 1 KB = 1000 bytes. GATE questions use powers of two.
- Forgetting that the inode itself holds the direct pointers, so they cost no extra index block.
- Assuming the name is stored in the inode. Hard links work because several directory entries can point to one inode, and the inode's link count tracks them.
- Counting the inode read in some parts of a problem but not in others. State your assumption first.
Exercise
A file system uses 1 KB blocks and 4-byte pointers. Each inode has 10 direct pointers, one single indirect and one double indirect pointer. What is the maximum file size, and how many disk reads (inode already in memory) are needed to read a byte in the double-indirect region?
Show answer
p = 1024 / 4 = 256. Data blocks = 10 + 256 + 256² = 10 + 256 + 65,536 = 65,802. The maximum size is 65,802 KB, which is about 64.26 MB. A byte in the double-indirect region needs 3 reads: the first-level index block, the second-level index block, and the data block.
Exercise
With 4 KB blocks and 4-byte pointers, a file is 100 KB and the inode has 12 direct pointers plus single, double and triple indirect pointers. How many disk blocks does the file occupy in total, counting index blocks but not the inode?
Show answer
100 KB is 25 data blocks. 12 are reached through direct pointers. The other 13 need one single-indirect index block. Total = 25 + 1 = 26 blocks.