A disk does not know about files. It knows about blocks: a long row of equal-sized pieces, each with a number, that you can read or write one at a time. Yet you see folders, names, sizes and files that can grow without you telling anyone. Where is all that kept?
Everything a file system does is built from a few ideas about how to arrange numbered blocks. This lesson opens the box and shows the layout, one structure at a time. The simulator draws a tiny disk of 64 blocks so that you can see all of them at once.
The layout of the disk
A file system sets aside some blocks for its own bookkeeping and uses the rest for your data. The simulator’s disk has this layout (real file systems are more elaborate, but follow the same plan):
- the superblock (block 0): the file system’s own description, such as its block size and where everything else starts;
- the free-space bitmap (block 1): one bit per data block, saying whether it is in use;
- the inode table (blocks 2 to 5): a fixed array of inodes, 16 to a block here;
- a journal (blocks 6 to 13): the subject of the next lesson, empty for now;
- the data blocks (from block 14): the contents of files and directories.
The inode
An inode (index node) is a small record that describes one file. It holds everything the kernel needs to know about the file except its name:
- the type (regular file, directory, symbolic link), the owner, the permissions and the timestamps;
- the size in bytes;
- the link count: how many names point to it (Lesson 7.3);
- the block pointers: where the file’s data is.
Everything is found by inode number, which is just the position in the inode table. When a process opens a file, the kernel reads its inode and keeps it in memory, and the position stored in the open file entry (Lesson 7.3) is only a number into the file’s bytes. The inode is where those bytes are turned into block numbers.
Directories are files too
A directory is a file whose data is a list of entries, each a name and an inode number. That is the only place a name is stored. The root directory is always the same inode (2 on the usual Linux file systems), so the kernel knows where to start.
To find /home/ana/notes.txt, the kernel reads the root inode, then the root’s data block to find home; reads home’s inode, then home’s data to find ana; reads ana’s inode, then ana’s data to find notes.txt; and finally reads the inode of notes.txt. Step through it. The Blocks view shows which block is being read.
Looking up /home/ana/notes.txt, one block read at a time
-
Read the root inode
A path is read from the root. The root directory is always inode 2, so the kernel can start there: it reads the inode from block 2 of the inode table. The inode says where the directory's data lives: block 14.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 1 so far
- Disk consistent
- yes
-
Read the data of /
The inode of / says its data is in block 14, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 12.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 2 so far
- Disk consistent
- yes
-
Read inode 12 (home)
The directory entry gave the inode number 12. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 16.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 3 so far
- Disk consistent
- yes
-
Read the data of /home
The inode of /home says its data is in block 16, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 13.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 4 so far
- Disk consistent
- yes
-
Read inode 13 (ana)
The directory entry gave the inode number 13. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 17.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 5 so far
- Disk consistent
- yes
-
Read the data of /home/ana
The inode of /home/ana says its data is in block 17, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 14.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 6 so far
- Disk consistent
- yes
-
Read inode 14 (notes.txt)
The directory entry gave the inode number 14. The kernel reads that inode from block 2 of the inode table. It is a file of 6K (2 blocks). The lookup is done: the name never needed to be stored in the inode.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 7 so far
- Disk consistent
- yes
-
Found /home/ana/notes.txt: inode 14
The lookup took 7 block reads: one inode read for the root, then a directory read and an inode read for every name in the path. Real kernels keep recent lookups in memory (the dentry and inode caches), so most lookups cost no disk reads at all.
- Operation
- lookup /home/ana/notes.txt
- Block reads
- 7
- Inode number
- 14
- Disk consistent
- yes
The lookup took 7 block reads: one for the root inode, then a directory read and an inode read for each of the three names. The file turned out to be inode 14, of 2 blocks. Each name in the path costs two more reads. A deeper path shows it:
A deeper path costs two more reads for every name
-
Read the root inode
A path is read from the root. The root directory is always inode 2, so the kernel can start there: it reads the inode from block 2 of the inode table. The inode says where the directory's data lives: block 14.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 1 so far
- Disk consistent
- yes
-
Read the data of /
The inode of / says its data is in block 14, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 12.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 2 so far
- Disk consistent
- yes
-
Read inode 12 (home)
The directory entry gave the inode number 12. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 16.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 3 so far
- Disk consistent
- yes
-
Read the data of /home
The inode of /home says its data is in block 16, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 13.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 4 so far
- Disk consistent
- yes
-
Read inode 13 (ana)
The directory entry gave the inode number 13. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 17.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 5 so far
- Disk consistent
- yes
-
Read the data of /home/ana
The inode of /home/ana says its data is in block 17, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 14.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 6 so far
- Disk consistent
- yes
-
Read inode 14 (projects)
The directory entry gave the inode number 14. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 18.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 7 so far
- Disk consistent
- yes
-
Read the data of /home/ana/projects
The inode of /home/ana/projects says its data is in block 18, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 15.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 8 so far
- Disk consistent
- yes
-
Read inode 15 (os)
The directory entry gave the inode number 15. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 19.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 9 so far
- Disk consistent
- yes
-
Read the data of /home/ana/projects/os
The inode of /home/ana/projects/os says its data is in block 19, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 16.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 10 so far
- Disk consistent
- yes
-
Read inode 16 (notes.txt)
The directory entry gave the inode number 16. The kernel reads that inode from block 2 of the inode table. It is a file of 6K (2 blocks). The lookup is done: the name never needed to be stored in the inode.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 11 so far
- Disk consistent
- yes
-
Found /home/ana/projects/os/notes.txt: inode 16
The lookup took 11 block reads: one inode read for the root, then a directory read and an inode read for every name in the path. Real kernels keep recent lookups in memory (the dentry and inode caches), so most lookups cost no disk reads at all.
- Operation
- lookup /home/ana/projects/os/notes.txt
- Block reads
- 11
- Inode number
- 16
- Disk consistent
- yes
Five names, 11 reads. This simulator has no cache, so every read goes to the disk. A real kernel keeps recently used directory entries and inodes in memory (the dentry cache and the inode cache), so a path that you use often costs no disk reads at all. The cost on a cold cache is still the reason file systems try to keep directories small and shallow in the hot paths of a program.
Creating a file
Making a file is not one action on the disk. The kernel has to write the file’s data, the new inode, the bitmap that says which blocks are now used, and the directory entry that gives it a name. Switch to the Blocks view and press play. The blue ring marks a block being read, the orange ring a block being written, and the coloured blocks belong to files.
Creating a 6 KB file takes five block writes
-
Find where the name goes
The kernel reads the directory /home/ana (6 block reads) to find the inode and check the name. Then it works out every block it must write.
- Operation
- create /home/ana/todo.txt 6K
- Block reads
- 6
- Disk consistent
- yes
-
Plan: 5 block writes
One create is not one write. It is 5 separate block writes, done in this order: data block 1 of the new file; data block 2 of the new file; the new inode 15: size 6K and its block pointers; the free-space bitmap: mark blocks 20 and 21 as used; the directory block: add the entry "todo.txt" pointing to inode 15. The disk can lose power between any two of them. With no journal, the disk holds a half-done change at every point in between.
- Operation
- create /home/ana/todo.txt 6K
- Block writes
- 0 of 5
- Journal
- off
- Disk consistent
- yes
-
Write 1 of 5: data block
Write data block 1 of the new file.
- Operation
- create /home/ana/todo.txt 6K
- Block writes
- 1 of 5
- Journal
- off
- Disk consistent
- yes
-
Write 2 of 5: data block
Write data block 2 of the new file.
- Operation
- create /home/ana/todo.txt 6K
- Block writes
- 2 of 5
- Journal
- off
- Disk consistent
- yes
-
Write 3 of 5: inode
Write the new inode 15: size 6K and its block pointers. Right now the disk is inconsistent: inode 15 is in use, but no directory entry names it.
- Operation
- create /home/ana/todo.txt 6K
- Block writes
- 3 of 5
- Journal
- off
- Disk consistent
- no (3 problems)
-
Write 4 of 5: bitmap
Write the free-space bitmap: mark blocks 20 and 21 as used. Right now the disk is inconsistent: inode 15 is in use, but no directory entry names it.
- Operation
- create /home/ana/todo.txt 6K
- Block writes
- 4 of 5
- Journal
- off
- Disk consistent
- no (1 problem)
-
Write 5 of 5: directory block
Write the directory block: add the entry "todo.txt" pointing to inode 15.
- Operation
- create /home/ana/todo.txt 6K
- Block writes
- 5 of 5
- Journal
- off
- Disk consistent
- yes
-
Done: create /home/ana/todo.txt 6K
All 5 block writes finished.
- Operation
- create /home/ana/todo.txt 6K
- Block writes
- 5 of 5
- Disk consistent
- yes
Look at the plan step: five writes, in a fixed order: two data blocks, the inode, the bitmap and the directory. The file got inode 15 and 2 blocks (6 KB rounds up to two 4 KB blocks, and the second one is only half used). In the middle of the sequence the disk really is inconsistent: the fields panel says so, and the text of each write step tells you what is wrong at that moment. The next lesson is about what happens if the power fails there.
Block pointers, and what a big file needs
An inode is small, so it cannot hold a list of every block of a big file. Instead it holds a short list of direct pointers, each the number of one data block, and a pointer to an indirect block: a block that holds nothing but more block numbers. In this simulator an inode has 12 direct pointers. Here is a file of exactly 48 KB:
Twelve direct pointers cover a 48 KB file
-
Read the root inode
A path is read from the root. The root directory is always inode 2, so the kernel can start there: it reads the inode from block 2 of the inode table. The inode says where the directory's data lives: block 14.
- Operation
- lookup /home/ana/full
- Block reads
- 1 so far
- Disk consistent
- yes
-
Read the data of /
The inode of / says its data is in block 14, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 12.
- Operation
- lookup /home/ana/full
- Block reads
- 2 so far
- Disk consistent
- yes
-
Read inode 12 (home)
The directory entry gave the inode number 12. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 16.
- Operation
- lookup /home/ana/full
- Block reads
- 3 so far
- Disk consistent
- yes
-
Read the data of /home
The inode of /home says its data is in block 16, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 13.
- Operation
- lookup /home/ana/full
- Block reads
- 4 so far
- Disk consistent
- yes
-
Read inode 13 (ana)
The directory entry gave the inode number 13. The kernel reads that inode from block 2 of the inode table. It is a directory, with its data in block 17.
- Operation
- lookup /home/ana/full
- Block reads
- 5 so far
- Disk consistent
- yes
-
Read the data of /home/ana
The inode of /home/ana says its data is in block 17, so the kernel reads that block. A directory's data is a list of names and inode numbers. The entry it wants gives inode 14.
- Operation
- lookup /home/ana/full
- Block reads
- 6 so far
- Disk consistent
- yes
-
Read inode 14 (full)
The directory entry gave the inode number 14. The kernel reads that inode from block 2 of the inode table. It is a file of 48K (12 blocks). The lookup is done: the name never needed to be stored in the inode.
- Operation
- lookup /home/ana/full
- Block reads
- 7 so far
- Disk consistent
- yes
-
Found /home/ana/full: inode 14
The lookup took 7 block reads: one inode read for the root, then a directory read and an inode read for every name in the path. Real kernels keep recent lookups in memory (the dentry and inode caches), so most lookups cost no disk reads at all.
- Operation
- lookup /home/ana/full
- Block reads
- 7
- Inode number
- 14
- Disk consistent
- yes
It uses 12 data blocks and no indirect block at all, so 12 direct pointers with 4 KB blocks reach 48 KB. A bigger file spills into an indirect block.
Watch a 40 KB file (10 blocks) grow by 12 KB. The pointer card under the grid shows the inode’s 12 direct slots and its indirect pointer.
A file outgrows its twelve direct pointers
-
Find where the name goes
The kernel reads /home/ana/log (7 block reads) to find the inode and check the name. Then it works out every block it must write.
- Operation
- append /home/ana/log 12K
- Block reads
- 7
- Disk consistent
- yes
-
Plan: 6 block writes
One append is not one write. It is 6 separate block writes, done in this order: data block 11 of the file; data block 12 of the file; data block 13 of the file; the new indirect block: the file no longer fits in 12 direct pointers; inode 14: the size is now 52K and it has new block pointers; the free-space bitmap: mark blocks 28, 29, 30 and 31 as used. The disk can lose power between any two of them. With no journal, the disk holds a half-done change at every point in between.
- Operation
- append /home/ana/log 12K
- Block writes
- 0 of 6
- Journal
- off
- Disk consistent
- yes
-
Write 1 of 6: data block
Write data block 11 of the file.
- Operation
- append /home/ana/log 12K
- Block writes
- 1 of 6
- Journal
- off
- Disk consistent
- yes
-
Write 2 of 6: data block
Write data block 12 of the file.
- Operation
- append /home/ana/log 12K
- Block writes
- 2 of 6
- Journal
- off
- Disk consistent
- yes
-
Write 3 of 6: data block
Write data block 13 of the file.
- Operation
- append /home/ana/log 12K
- Block writes
- 3 of 6
- Journal
- off
- Disk consistent
- yes
-
Write 4 of 6: indirect block
Write the new indirect block: the file no longer fits in 12 direct pointers.
- Operation
- append /home/ana/log 12K
- Block writes
- 4 of 6
- Journal
- off
- Disk consistent
- yes
-
Write 5 of 6: inode
Write inode 14: the size is now 52K and it has new block pointers. Right now the disk is inconsistent: inode 14 uses block 28, but the bitmap says block 28 is free.
- Operation
- append /home/ana/log 12K
- Block writes
- 5 of 6
- Journal
- off
- Disk consistent
- no (4 problems)
-
Write 6 of 6: bitmap
Write the free-space bitmap: mark blocks 28, 29, 30 and 31 as used.
- Operation
- append /home/ana/log 12K
- Block writes
- 6 of 6
- Journal
- off
- Disk consistent
- yes
-
Done: append /home/ana/log 12K
All 6 block writes finished.
- Operation
- append /home/ana/log 12K
- Block writes
- 6 of 6
- Disk consistent
- yes
The file now has 13 data blocks, and uses 14 blocks on the disk, because the indirect block is overhead. Reading its last block needs one extra read: the kernel reads the indirect block to find the block number, then the block itself.
A 4 KB indirect block holds four-byte pointers, and the sum of what one block can reach is easy to check:
#include <stdio.h>
int main(void) {
long block = 4096, pointer = 4;
long per_block = block / pointer;
printf("pointers in one block: %ld\n", per_block);
printf("file reached: %ld MB\n", per_block * block / (1024 * 1024));
return 0;
}
Output (one run)
pointers in one block: 1024 file reached: 4 MB
So one indirect block adds 1024 pointers and 4 MB of file. Real file systems of this design add a double indirect block (an indirect block of indirect blocks), and a triple one, to reach very large files. Newer file systems, such as ext4, replace the pointer list with extents: one record saying “this many blocks, starting at this block”. A large file in one piece needs a handful of extents instead of thousands of pointers.
Press Try it and change the path and size. Create a file of 48K and then of 52K and look at the Blocks view for the indirect pointer. Look up a path that does not exist, and note the error.
In the real world
You can look at all of this on a Linux file system with ordinary commands:
$ ls -i notes.txt
$ stat notes.txt
$ df -i
$ sudo dumpe2fs -h /dev/sda1
ls -i puts the inode number next to the name. stat prints the inode’s contents: size, number of blocks, link count, permissions and timestamps, and notice that the name is not one of them. df -i shows how many inodes are used and free on each file system: a file system can run out of inodes while it still has free space, if you make millions of tiny files. dumpe2fs -h prints the superblock of an ext2, ext3 or ext4 file system, including its block size and inode count. NTFS keeps the same information in a master file table, and Apple’s APFS uses different structures for the same job. :::
Quick check
Quick check
-
The name lives in the directory entry, which pairs a name with an inode number. That is why one inode can have several names.
-
Past the direct pointers, the inode points to a block of more pointers, so a small inode can still describe a very large file.
-
The directory's data holds name-to-inode entries. Finding an entry costs a data read, and then the next inode must be read to continue.
-
The bitmap has one bit per block. Creating a file marks its blocks as used, and deleting one marks them free again.
Join the discussion