4.2.31Operating Systems

File system concepts — file, directory, path, inode

2,244 words10 min readdifficulty · medium2 backlinks

WHAT each thing is


WHY separate name (directory) from metadata (inode)?

directory "docs"                inode 5012                  data blocks
+-----------+--------+          +------------------+        +---------+
|  name     | inode# |          | size, owner,     |  --->  | block 9 |
+-----------+--------+          | perms, times,    |  --->  | block 40|
| a.txt     | 5012   |--------> | link_count = 2,  |  --->  | block 88|
| copy.txt  | 5012   |--------> | block ptrs       |        +---------+
+-----------+--------+          +------------------+

Two names, one inode, one copy of data → a.txt and copy.txt are the same file.

Figure — File system concepts — file, directory, path, inode

HOW path resolution actually works (derive it step by step)

To open /home/ana/a.txt, the kernel performs iterative directory lookup:

  1. Start at the root inode (its inode number is fixed/known, e.g. inode 2 on ext-style FS).
  2. Read root's directory data → find entry "home" → get its inode#.
  3. Load that inode, read its directory data → find "ana" → get inode#.
  4. Load that inode, read its directory data → find "a.txt" → get inode#.
  5. Load that inode → now we have the metadata + block pointers → check permissions → return a file handle.

HOW the inode addresses data blocks (the block-pointer scheme)


Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine a school library. The book is your file (the actual pages = data). The catalog card is the inode: it doesn't have the story, but it says how big the book is, who can borrow it, and which shelf the pages are on. The folder/list of titles is the directory: it just maps "title → card number". A path is the directions: "go to floor 2, room Science, shelf 5". You can put the same card number under two different titles — now one book has two names! When the last title pointing to that card is erased and nobody is currently reading it, the librarian throws the pages away.


Active recall

What is an inode?
An on-disk structure storing all metadata about a file except its name (size, owner, perms, timestamps, link count, data-block pointers).
Where is a file's name stored?
In its parent directory, as a (name → inode number) entry — not in the inode.
What is a directory, structurally?
A special file whose contents are a table mapping names to inode numbers.
Absolute vs relative path?
Absolute starts at root /; relative starts at the current working directory.
What is link_count in an inode?
The number of directory entries (hard links) pointing to that inode.
When are a file's data blocks freed?
When link_count = 0 AND no process has it open (open_count = 0).
Why is renaming a large file instant (same FS)?
Only one (name, inode#) directory entry changes; inode and data blocks don't move.
Hard link vs symbolic link?
Hard link = extra directory entry to the SAME inode#; symlink = a file storing a target PATH string, can dangle and cross filesystems.
Hard link vs copy?
Hard link shares one inode and one copy of data; copy creates a new inode and new data blocks.
Max file size formula with direct + 3 levels of indirect?
(D + k + k² + k³)·B, where k = block/pointer-size, B = block size, D = direct pointers.
How many directory lookups to resolve /a/b/c.txt?
3 (a, b, c.txt); root inode is already known.
What does stat read vs what shows the name?
stat reads the inode (size/perms/times); the name comes from the parent directory.

Connections

  • Hard links vs Symbolic links
  • ext2-ext4 inode structure
  • Virtual File System (VFS) layer
  • Block allocation and fragmentation
  • File permissions and UID-GID
  • open() read() write() system calls
  • Mount points and multiple filesystems

Concept Map

separates

separates

maps name to

identifies

stores

points to

is bytes plus

located by

each component is

resolves via

many entries share one inode

link count tracks

data freed when count hits 0

File System

Name in Directory

Metadata in Inode

Directory

Inode Number

Inode

Size owner perms times

Data Blocks

File

Path

Directory Lookup

Hard Link

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, file system ko ek library samjho. File matlab actual content (bytes) — jaise kisi book ke pages. Inode ek catalog card hai jo file ke baare mein sab kuch rakhta hai — size, owner, permissions, timestamps, aur sabse important: data blocks ke pointers — par filename store NAHI karta. Directory (folder) ek table hai jo simple mapping rakhta hai: naam → inode number. Aur path woh raasta hai (jaise /home/ana/a.txt) jisse OS root se chalkar file tak pahunchta hai, har / pe ek directory lookup karke.

Sabse bada concept yeh hai: naam aur data alag-alag rakhe jaate hain. Isi wajah se ek hi file ke do naam ho sakte hain — bas do directory entries same inode number ko point karein. Ise hard link kehte hain, aur inode ka link_count batata hai kitne naam point kar rahe hain. File ka data tabhi delete hota hai jab link_count = 0 ho AUR koi process use open na kiye baithi ho.

Yeh design itna powerful kyun hai? Kyunki rename ya move karna instant hota hai (same filesystem mein) — sirf ek (naam, inode#) entry badalti hai, data idhar-udhar nahi hota. Aur bade files ke liye inode mein direct pointers ke saath single/double/triple indirect pointers hote hain, jisse chhoti files fast rahein aur badi files bhi TB-scale tak ja sakein.

Exam aur real coding dono mein yeh kaam aata hai: ls -l, stat, ln, ln -s, rm ka behaviour samajhne ke liye yahi base hai. Yaad rakho mnemonic DIP-N: Directory rakhta hai Names, Inode rakhta hai metadata + Pointers, par Name nahi.

Test yourself — Operating Systems

Connections