Appearance
2.6.1 — Inodes, Directories & Permissions
The photo-upload service saves a photo as album/beach.jpg. Days later it reads the same bytes back, with the same name, the same size and the date it was saved. That feels obvious. It is not. A disk knows nothing about names, folders, sizes or dates. To a disk, there is only an enormous row of numbered blocks, each a few kilobytes, and two commands: "give me block 91,442" and "write these bytes into block 4,001".
The file system is the part of the kernel that turns that row of numbered blocks into named files in folders. This page builds it up from the blocks. Here is the upload service poking at what the file system keeps about one photo. Every line of output will make sense by the end of the page.
ts
// inode-and-links.ts — one file, two names, and what the kernel stores about it
import { writeFileSync, statSync, linkSync, renameSync, unlinkSync, readFileSync, rmSync, mkdirSync } from "node:fs";
rmSync("album", { recursive: true, force: true });
mkdirSync("album");
writeFileSync("album/beach.jpg", Buffer.alloc(10_000, 1)); // (1) a 10,000-byte photo
const s = statSync("album/beach.jpg"); // (2)
console.log("inode number:", s.ino, " size:", s.size, " links:", s.nlink);
linkSync("album/beach.jpg", "album/best-of-summer.jpg"); // (3) a second name, same file
const t = statSync("album/best-of-summer.jpg");
console.log("second name -> inode", t.ino, " links now:", t.nlink, " same file?", t.ino === s.ino);
renameSync("album/beach.jpg", "album/goa-beach.jpg"); // (4) rename: data untouched
console.log("after rename, inode:", statSync("album/goa-beach.jpg").ino);
unlinkSync("album/goa-beach.jpg"); // (5) "delete" one name
console.log("after deleting one name, the other still reads",
readFileSync("album/best-of-summer.jpg").length, "bytes; links:",
statSync("album/best-of-summer.jpg").nlink);
// Printed on Windows (NTFS); on Linux the inode number is a much smaller number:
// inode number: 16325548650865800 size: 10000 links: 1
// second name -> inode 16325548650865800 links now: 2 same file? true
// after rename, inode: 16325548650865800
// after deleting one name, the other still reads 10000 bytes; links: 1(1)We save a 10,000-byte photo.(2)statSyncmakes thestatsystem call, which asks the file system for everything it knows about a file except its contents. The answer includes a number called the inode number, the size, and a count of links.(3)linkSyncgives the same file a second name. The output shows both names have the same inode number. There is one file with two names, not two files.(4)Renaming changes a name. The inode number does not change, and no data was copied.(5)unlinkSyncis how Node deletes a file. Yet after "deleting"goa-beach.jpg, the photo is still fully readable through its other name.
Two facts explain all of that. A file's name and the file itself are stored in different places. And "delete" removes a name, not a file.
1. From numbered blocks to a file
A storage device, whether a spinning hard disk or a solid-state drive, presents itself to the kernel as a long row of fixed-size blocks. Each block is usually 4 KB, and each has a number. That is all the hardware offers.
To turn that into album/beach.jpg, the file system must keep track of two quite different things for every file.
- The data: the bytes of the photo itself. A 10,000-byte photo needs three 4 KB blocks. The blocks need not be next to each other. They go wherever free space was available, just as virtual memory scattered pages across frames (2.5.1).
- The metadata: everything about the file that is not its content. Its size, its owner, who may read or change it, when it was changed, and, most important of all, the list of which blocks hold its data.
On Unix file systems, the record that holds a file's metadata has a name worth knowing.
2. The inode: what a file really is
An inode, short for index node, is a small fixed-size record, typically 256 bytes, with one inode per file. It holds everything about a file except its name and its contents:
- the file's size in bytes;
- its owner, as a user number and a group number;
- its permission bits (section 6);
- three timestamps: last changed, last accessed, last metadata change;
- a link count: how many names point to it;
- the pointers to its data blocks.
Inodes are numbered. Each file system keeps a table of them, and an inode number says where in that table to look. In a very real sense, the inode is the file. The name is only a label stored somewhere else, as section 4 shows. That is why stat could tell us everything about the photo without reading its data: it only read the inode.
(Windows's file system, NTFS, has the same split. Its equivalent of the inode table is called the Master File Table, and the huge number Node printed is NTFS's ID for the file's record there. Page 2.6.3 explains why that table makes some tools astonishingly fast.)
3. How a small inode points at a huge file
An inode has a fixed size, but a file can be any size. A 10 GB video needs about 2.6 million blocks. A 256-byte inode cannot list 2.6 million block numbers. So how does it describe the file?
The classic Unix answer is a tree of pointers, exactly the idea behind the multi-level page tables of 2.5.2. The inode holds fifteen block pointers, used in four different ways.
- 12 direct pointers each name one data block. With 4 KB blocks, they cover the first 48 KB of the file. Most files are small, so for most files these are the only pointers used, and reading any block needs no extra lookups.
- 1 single indirect pointer names a block that holds nothing but more block numbers. With 4-byte block numbers, a 4 KB block holds 4096 \div 4 = 1024 of them. So this one pointer reaches 1,024 more data blocks, another 4 MB.
- 1 double indirect pointer names a block of 1,024 pointers, each to a block of 1,024 pointers. That is 1024 \times 1024 = 1{,}048{,}576 data blocks, about 4 GB.
- 1 triple indirect pointer adds one more level: 1024^3, about a billion data blocks, about 4 TB.
Add the four together:
(12 + 1024 + 1024^2 + 1024^3) \times 4\text{ KB} \approx 4\text{ TB}
Read it as "twelve, plus 1,024, plus 1,024 squared, plus 1,024 cubed, all times 4 kilobytes". A 256-byte record can describe a 4-terabyte file, while a small file pays almost nothing, because it never uses the indirect levels. This is a pattern you will see again and again in this book: a small fixed root, plus a tree of pointers, to reach something enormous. Page tables use it, databases use it in B-trees (7.3.2), and file systems use it here.
Modern Linux file systems improve on this. Instead of listing every block, they record ranges, saying "blocks 50,000 to 52,047 belong to this file" in one entry, called an extent. Page 2.6.3 shows why that helps.
4. Directories: names are just data
If the inode holds everything except the name, where does the name beach.jpg live? In the directory. And here is the surprise: a directory is just a special file whose contents are a table of names and inode numbers.
A folder does not "contain" files the way a box contains things. It is a small file holding a list:
| Name | Inode number |
|---|---|
. | 5000 (this directory) |
.. | 2 (the parent directory) |
beach.jpg | 5012 |
notes.txt | 88 |
Every directory has the entries ., meaning itself, and .., meaning its parent. That is how cd .. works: it simply follows the .. entry.
Walking a path
When the upload service opens /home/svc/album/beach.jpg, the kernel walks the path one name at a time. This is called path resolution.
- Start at the root directory,
/, whose inode number is fixed, usually 2. - Read the root directory's table. Find
home. It points to, say, inode 130. - Read inode 130's table. Find
svc. It points to inode 4100. - Read inode 4100's table. Find
album. It points to inode 5000. - Read inode 5000's table. Find
beach.jpg. It points to inode 5012. - Read inode 5012. Now the kernel knows the file's size, permissions and data blocks.
At every step, the kernel also checks the permissions of each directory on the way (section 6). Walking long paths again and again would be slow, so the kernel keeps a cache of recent name lookups in memory, called the dentry cache on Linux. Most path walks never touch the disk.
What this design explains
Once you know that names live in directories and point at inodes, several everyday puzzles answer themselves.
Renaming is instant, however big the file. Renaming beach.jpg to goa-beach.jpg changes one string in one directory's table. The inode and the gigabytes of data are not touched. That is why the inode number in the output did not change. Moving a file to another directory on the same file system is equally cheap: remove the entry from one table and add it to another. Moving it to a different disk is slow, because the data really must be copied.
A hard link is a second name for the same inode. linkSync added a second row, best-of-summer.jpg → 5012, to the directory table. Both rows point at one inode. Neither name is "the original". The inode's link count went from 1 to 2.
"Delete" removes a name. Deleting is called unlinking on Unix, and the name is exact. It removes one row from a directory and lowers the inode's link count by one. Only when the link count reaches 0, and no running program still has the file open, does the file system free the inode and its data blocks. That is why the photo survived: one name was removed, one remained, so the link count went to 1, not 0.
That second condition causes a classic production puzzle. A server's disk is full. Someone deletes a huge log file. The disk is still full. The reason: the program writing the log still has it open, so its data cannot be freed, even though it no longer has any name. The space comes back only when that program closes the file or is restarted. On Linux, lsof can find such files; they are marked "(deleted)" (2.8.5).
A symbolic link is different. A symbolic link, or symlink, is a tiny separate file whose content is a path to another file. It points by name, not by inode. So if you delete the target, the symlink breaks, because the path it holds no longer leads anywhere. A hard link cannot break that way, because it points straight at the inode.
| Hard link | Symbolic link | |
|---|---|---|
| Points to | the inode | a path, as text |
| Target deleted | still works | broken |
| Across disks | not allowed | allowed |
| To a directory | not allowed | allowed |
The last two rows follow from the design. Inode numbers only mean something inside one file system, so a hard link cannot point onto another disk. And hard links to directories are forbidden because they could create loops in the directory tree that would confuse every program that walks it.
5. How the file system finds free space
To save a new photo, the file system needs free blocks and a free inode. It tracks which are free with bitmaps: long strings of bits, one bit per block or per inode, where 1 means used and 0 means free. Finding free space means finding 0 bits. A 1 TB disk with 4 KB blocks has about 268 million blocks, so its block bitmap is about 32 MB, small enough to search quickly.
Saving a file therefore touches several places on the disk: the inode bitmap, the inode, the block bitmap, the data blocks, and the directory's table. If the power fails halfway through those writes, the file system could be left inconsistent. Page 2.6.2 is entirely about that problem.
6. Permissions: rwx and the octal numbers
Every inode holds a set of permission bits that say who may do what with the file. On Linux, ls -l shows them at the start of each line:
text
-rw-r--r-- 1 svc svc 10000 Oct 19 11:20 beach.jpg
drwxr-x--- 2 svc svc 4096 Oct 19 11:20 albumThe first character is the type: - for a file, d for a directory, l for a symbolic link. Then come nine characters in three groups of three, for three kinds of person:
- the owner of the file, here the user
svc; - the group, here also called
svc, meaning other users in that group; - everyone else.
Each group has three letters, and a - means that permission is missing:
| Letter | On a file | On a directory |
|---|---|---|
r | read its contents | list the names in it |
w | change its contents | create, delete and rename names in it |
x | run it as a program | pass through it to reach what is inside |
So -rw-r--r-- means the owner may read and write, and the group and everyone else may only read.
Notice the directory row of that table. Deleting a file needs write permission on the directory, not on the file, because deleting changes the directory's table, not the file. And reaching album/beach.jpg needs x on album, even if you have no r on it: you can then open files whose names you already know, but not list the folder.
The octal numbers
People usually write these permissions as three digits, as in chmod 644 beach.jpg. Each digit is one group, made by adding up its letters: r is 4, w is 2 and x is 1.
| Digit | Letters | Calculation |
|---|---|---|
| 7 | rwx | 4 + 2 + 1 |
| 6 | rw- | 4 + 2 |
| 5 | r-x | 4 + 1 |
| 4 | r-- | 4 |
| 0 | --- | nothing |
So 644 is rw-r--r--, the usual setting for a file. 755 is rwxr-xr-x, the usual setting for a directory or a program. 600 is rw-------, private to the owner, which is what a file holding a password or a private key should be. These digits are base 8, called octal, because each digit holds exactly three bits.
When a program creates a file, it asks for some permissions, usually 666 for files. The kernel then removes any bits set in the process's umask, a mask that each process inherits from its parent. The common umask is 022, which removes w for the group and for everyone else. So 666 becomes 644. That is why files you create come out readable by others but writable only by you.
The kernel checks these bits on every open (the checking step of 2.1.2). The user called root, the administrator, skips almost all of these checks. 2.8.4 covers users, root and some extra bits not shown here.
Windows uses a richer system called access control lists, where each file can list any number of users and groups, each with its own permissions. 2.9.4 covers it. Linux file systems support access control lists too, but the three-group rwx bits are what you will meet every day.
What the interviewer will push on
"What is an inode, and what does it contain?" They are checking that you know it holds everything about a file except its name and its contents: size, owner, permissions, timestamps, link count and the block pointers. The tell is saying "the inode is the file; the name lives in the directory".
"What actually is a directory?" A file whose contents are a table of names and inode numbers. Strong answers use it to explain why rename is instant and why a folder's own size is tiny.
"What is the difference between a hard link and a symbolic link?" A hard link is a second directory entry pointing at the same inode; a symlink is a file containing a path. Know the consequences: a symlink breaks when its target is deleted and can cross disks; a hard link does neither.
"I deleted a 50 GB log file and the disk is still full. Why?" A program still has it open. The name is gone, but the inode and its data stay until the last open descriptor is closed. Find it with lsof and restart or signal the program. This question separates people who have run servers from people who have not.
"What does chmod 755 mean?" Owner rwx, group r-x, others r-x, from 4 for read, 2 for write and 1 for execute. Bonus: deleting a file needs write permission on its directory.
Something to volunteer that nobody asks for: a 256-byte inode reaches a 4 TB file through direct, single-, double- and triple-indirect pointers, the same "small root plus a tree of pointers" idea as page tables and database B-trees. Pointing out that one idea runs through all three shows you see the structure, not just the facts.
Recall
- A disk is only numbered blocks; the file system builds files from them. Each file has data, scattered across blocks, and metadata.
- The inode holds all metadata except the name: size, owner, permissions, timestamps, link count and block pointers. With 12 direct, one single-, one double- and one triple-indirect pointer, it reaches about 4 TB.
- A directory is a file holding a table of names and inode numbers, with
.and... Path resolution walks it one name at a time, cached in memory. Rename within one disk only edits a table. - A hard link is a second name for the same inode. "Delete" unlinks one name; data is freed only when the link count is 0 and no program has the file open. A symbolic link stores a path and breaks if the target goes.
- Free space is tracked with bitmaps. Permission bits give
rwxfor owner, group and others, written in octal with r = 4, w = 2 and x = 1. The umask removes bits at creation. Deleting needs write permission on the directory.
Self-test: Why could stat describe the photo without reading its data? Work out how much data the double-indirect pointer reaches. Why did deleting goa-beach.jpg not delete the photo? Why can a deleted log file still fill a disk? Decode chmod 640, and say who can delete the file.
Quiz Bank
FoundationalWhat is an inode, and what does it contain?
An inode is a small fixed-size record, one per file, that holds all of the file's metadata: its size, its owner and group, its permission bits, its timestamps, its link count, and the pointers to the blocks that hold its data. It does not hold the file's name, which lives in a directory, or its contents, which live in the data blocks. Each inode has a number, and in effect the inode is the file's identity.
FoundationalWhat actually is a directory?
A directory is a special file whose contents are a table mapping names to inode numbers, such as beach.jpg → 5012. It also has the entries . for itself and .. for its parent. Opening a path means walking these tables from the root: read each directory, find the next name, follow its inode number. This is why renaming within one file system only edits a table entry, and why a folder's own size is small no matter how large the files listed in it are.
AppliedHow can a small, fixed-size inode describe a multi-terabyte file?
Through levels of indirection. The classic inode holds 12 direct pointers, each naming one data block, enough for small files. Then a single-indirect pointer names a block of 1,024 further block numbers. A double-indirect pointer names a block of pointers to such blocks, reaching about a million blocks. A triple-indirect pointer adds a third level, about a billion blocks. With 4 KB blocks, the total is about 4 TB. Small files only use the direct pointers and pay nothing for the levels. Modern file systems often use extents, ranges of blocks, instead.
AppliedWhat is the difference between a hard link and a symbolic link?
A hard link is an extra directory entry pointing at the same inode as an existing name: two names, one file. The inode's link count tracks how many names point at it, and the data is freed only when it reaches 0. A symbolic link is a separate small file whose content is a path to another file; it points by name.
So deleting the original name leaves a hard link working but breaks a symlink. Hard links cannot cross file systems, because inode numbers are only meaningful inside one, and cannot point at directories; symlinks can do both.
InterviewYou deleted a huge log file but the disk usage did not go down. Why, and how do you fix it?
Deleting only removes the name: it unlinks the directory entry and lowers the inode's link count. The file system frees the inode and its blocks only when the link count is 0 and no process still has the file open. A program that was writing the log still holds an open descriptor to it, so its data stays on disk, nameless.
Find the process with lsof, which lists open files and marks this one "(deleted)". Then make the program close it, by restarting it or by sending the signal it uses to reopen its logs, often SIGHUP. In future, rotate logs by renaming and signalling, or truncate the file in place, instead of deleting a file that is still open.
StaffA deployment script moves a 20 GB data directory to a new location and it takes forty minutes, although moving it on a colleague's machine was instant. Explain using this page.
A move within one file system is a rename: the kernel adds the directory's name to the new parent's table and removes it from the old one. No inodes or data blocks are touched, so it is instant regardless of size. On the colleague's machine the source and destination were on the same file system.
On the server, the destination is on a different file system, perhaps a separate disk, partition or mounted volume. Inode numbers mean nothing across file systems, so a rename is impossible. The move becomes a copy of every file, creating new inodes and writing 20 GB of data, followed by deleting the originals. That takes as long as the disks can transfer the data.
To keep such moves instant, place the directory and its destination on the same file system, or switch where a path points, for example by renaming a symbolic link, rather than moving the data.
Scenario Drill
DrillThe photo-upload service stores originals in one directory and creates an album view in another directory, by copying each photo. Storage use doubles. Using this page, propose a better design and its trade-offs.
The album view does not need copies, only names. Two options follow from the design of inodes and directories.
Hard links. Add a second directory entry for each photo in the album directory. Both names point at the same inode, so no data is duplicated, and deleting the original name leaves the album entry working, since the data is freed only when the last name is gone. The limits: both directories must be on the same file system, and an edit to the file through either name changes the one shared file.
Symbolic links. Create a small link file in the album directory holding the path of the original. This can cross disks and points visibly at the original. But deleting or moving the original breaks the link.
For a real service, a third option is usually best: store each photo once, keyed by its ID, and keep albums as rows in a database that refer to photo IDs. The file system then holds data, and the database holds the relationships, which is what it is good at.
Next: saving one photo touched a bitmap, an inode, data blocks and a directory, in several separate writes. Page 2.6.2 asks what happens if the power fails between them, and how file systems, and your own code, make sure a crash leaves either the old state or the new one, never a broken mixture.