Sub topic 15
Indexed Allocation and Inodes
Prerequisites and the Basic Model
This chapter assumes you know binary powers, block sizes, system calls, and the difference between a file's data and its metadata.
Indexed allocation stores a file's block addresses in one or more index blocks. An index block contains pointers to data blocks. Unix-like file systems commonly use an inode, which is a metadata structure that stores file attributes and pointers to the file's data.
An inode typically stores:
- File type, such as regular file or directory
- File permissions and owner
- File size
- Timestamps
- Direct pointers to data blocks
- Pointers to indirect index blocks
The inode itself does not usually contain the file's contents. It tells the file system where to find those contents.
Direct and Indirect Pointers
A direct pointer points straight to one data block. An indirect pointer points to an index block, which contains more pointers.
Suppose:
- Block size is
Bbytes. - Each pointer uses
Pbytes. - One index block holds
q = B / Ppointers. - The inode has
ddirect pointers. - It has one single, one double, and one triple indirect pointer.
The number of reachable data blocks is:
\[ d + q + q^2 + q^3 \]
The maximum file size is:
\[ (d + q + q^2 + q^3)B \]
The powers appear because each additional indirect level adds another layer of pointers.
Worked Calculation
With 4 KiB blocks and 4-byte pointers:
\[ q = 4096 / 4 = 1024 \]
For 12 direct pointers and one pointer of each indirect type:
\[ (12 + 1024 + 1024^2 + 1024^3) \times 4096 \]
def max_file_size_bytes(block_size, pointer_size, direct):
pointers_per_index = block_size // pointer_size
data_blocks = (
direct
+ pointers_per_index
+ pointers_per_index ** 2
+ pointers_per_index ** 3
)
return data_blocks * block_size
size = max_file_size_bytes(4096, 4, 12)
print(size)
print(size / 2**40, "TiB")Common Errors
- Counting index blocks as file data blocks. They consume storage, but they do not increase the file's data capacity.
- Using
Binstead ofB / Pfor the number of pointers in an index block. - Forgetting that a double indirect pointer reaches
q²data blocks. - Confusing the inode number with the inode contents. The number identifies the inode; the inode stores metadata and block references.
Exercise
A file system uses 1 KiB blocks, 4-byte pointers, and an inode with 10 direct pointers plus one single and one double indirect pointer. How many data blocks can the inode address?
Show answer
Each index block holds 1024 / 4 = 256 pointers. The inode addresses 10 + 256 + 256² = 65,802 data blocks.