CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 3 of 7 · 2 min read

The inode

Think of the library card of a book. It gives the size, the owner, the dates and the shelf place, and it has no title in it, because the title is in the catalogue. The analogy stops in one place: a library card is on paper, while an inode is a fixed number of bytes at a fixed place that the OS finds by arithmetic.

In plain words: An inode is a fixed-size record, 256 bytes in this file system, that describes one file. It holds the type and the permission bits (the mode), the owner and group, the size in bytes, the times of access, change and modification, the link count, and the pointers to the data blocks. It does not hold the name of the file: names are in directories (page 1). It also does not hold the data of an ordinary file, except for very small files in some file systems.

A worked example. debugfs printed the inode of a file of 10,000 bytes (the file ten.bin, inode 13), and the lines that matter are these:

Inode: 13   Type: regular    Mode:  0644   Flags: 0x80000
User:     0   Group:     0   Size: 10000
Links: 1   Blockcount: 24
EXTENTS:
(0-2):293-295

The size is 10,000 bytes. 10,000 / 4,096 = 2.44, so the file needs 3 blocks. The Blockcount is counted in units of 512 bytes, so 24 units are 12,288 bytes, which is 3 blocks. The line (0-2):293-295 says that the file's blocks 0 to 2 are the disk blocks 293 to 295. The last block is only partly used, and the unused 2,288 bytes of it are wasted, a loss that is called internal fragmentation.

The key lines. A file of one byte (one.txt, inode 12) printed a size of 1 and a Blockcount of 8: one whole block of 4 KiB for one byte. A file of exactly 1 MiB (meg.bin, inode 14) printed one extent (0-255):804-1059, which means 256 blocks in a row.

Inode

An inode is the fixed-size on-disk record that describes one file or directory: its type, permissions, owner, size, times, link count and the pointers to its data blocks. It is identified by its inode number, and it contains no file name.

Example: inode 13 on the test image describes a regular file of 10,000 bytes whose three blocks are 293 to 295.

Check yourself: 1. Which items does an inode not contain? 2. How many data blocks does a file of 10,000 bytes need at 4 KiB per block? 3. What is internal fragmentation, and how many bytes does it waste in the example?