4.2.33Operating Systems

Directory structure — tree, DAG (hard links, symbolic links)

2,272 words10 min readdifficulty · medium1 backlinks

1. What is a directory, really?


2. Tree structure (the default)

WHY a tree is convenient: unique path ⇒ unique name ⇒ deletion is trivial (one entry, free the inode). HOW we walk it: start at /, split path on /, look up each component's inode in the current directory, repeat.

Even a "pure tree" secretly has two special entries in every directory:

  • . → the directory's own inode
  • .. → the parent's inode

These make the directory graph technically cyclic for directories, which is exactly why we ban user-created directory hard links (more below).


Constraints on hard links (and WHY):

  1. Cannot cross filesystems — inode numbers are only meaningful within one filesystem.
  2. Cannot hard-link directories (for users) — would create cycles, breaking traversal & making link count undefined for "reachability". (Kernel itself uses ./.. carefully.)

Figure — Directory structure — tree, DAG (hard links, symbolic links)

5. Side-by-side (the 80/20 table)

Property Hard link Symbolic link
Points to inode path string
Own inode? No (shares target's) Yes (separate inode)
Bumps target link count? Yes No
Survives target deletion? Yes (until count 0) No → dangling
Cross-filesystem? No Yes
Link to directory? No (users) Yes
ls -l type char - (normal) ==l==


Recall Feynman: explain it to a 12-year-old

Imagine a big toy box (the data) with a label number painted on it. A hard link is a sticker on the wall that says "toy box #42." You can put many stickers in different rooms all saying "#42" — they all open the same box. The box keeps a little counter of how many stickers point to it; the box is only thrown away when the last sticker is removed. A symbolic link is different: it's a sticky note that just says "go to the kitchen and look for the box called teddy." If someone renames the kitchen box, your sticky note now points nowhere — a dead note. Because many stickers can point to one box, the map of names isn't a simple family tree anymore; it's a web that never loops back on itself — a DAG.


Connections

  • Inodes and File Metadata
  • File System Implementation — Block Allocation
  • Path Resolution and the namei() routine
  • Mount points and Virtual File System (VFS)
  • Graphs — Trees vs DAGs
  • Reference Counting and Garbage Collection
  • Unix system calls — link, unlink, symlink, stat

What does a directory entry (dentry) map?
A human-readable name to an inode number.
Where is a file's name stored — inode or directory?
In the directory (the inode holds metadata + data pointers, not the name).
Why is a filesystem with hard links a DAG instead of a tree?
Multiple paths/names can reach the same inode (in-degree >1), but directory cycles are forbidden, so it's acyclic.
What is a hard link?
An additional directory entry pointing to the same inode; a true co-equal name.
What is the inode link count and when are data blocks freed?
Number of dentries referencing the inode; blocks freed only when link count = 0 AND no process has it open.
Does creating a symlink change the target's link count?
No — a symlink has its own inode and only stores the target's path as text.
What is a dangling symlink?
A symlink whose target path no longer exists, so resolution fails.
Two things a symlink can do that a hard link cannot?
Cross filesystems, and link to a directory.
Why can't users hard-link directories?
It could create cycles, breaking traversal and reference counting.
What does rm actually call, and what does it do?
unlink(); removes one name and decrements the inode link count.
ls -l first character for a symlink?
'l'.
Mnemonic for hard vs soft links?
Hard points at the HEART (inode); Soft points at the SPOT (path). Hard counts, Soft doesn't.

Concept Map

answered by

maps name to

stores

separates name from

each has one parent

many names one inode

adds dentry to same inode

increments

freed when zero

requires

keeps traversal finite

stores path not inode

Name to data lookup

Directory as lookup table

Inode metadata plus data

Directory entry name to inode

Tree namespace

Directed Acyclic Graph

Hard link

Symbolic link

Link count n

Ban directory cycles

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, filesystem ka asli kaam ek hi hai: naam do, data dhoondh ke do. Iske liye Unix mein name aur data ko alag rakhte hain. Data + metadata ek structure mein hota hai jise inode kehte hain, aur directory sirf ek table hai jisme (naam, inode number) ke pairs hote hain. Isi separation ki wajah se ek hi file ke kai naam ho sakte hain.

Hard link ka matlab — ek aur directory entry jo same inode ko point karti hai. Dono naam barabar hain, koi "asli" koi "copy" nahi. Inode ek link count rakhta hai = kitne naam usse point kar rahe hain. rm actually unlink call karta hai jo count ko 1 kam karta hai; data tabhi delete hota hai jab count 0 ho jaye (aur koi process file open na rakhe). Isiliye agar a.txt aur b.txt hard link hain, rm a.txt ke baad bhi b.txt se data padh sakte ho.

Symbolic link (symlink) thoda alag hai — ye ek alag inode wali special file hai jiske andar bas ek path ka text likha hota hai, jaise sticky note "wahan jao". Symlink target ki link count nahi badhata. Isliye agar target delete ho gaya to symlink dangling (toot jaata) ho jaata hai. Plus point: symlink dusre filesystem par bhi point kar sakta hai aur directory ko bhi link kar sakta hai — hard link ye dono nahi kar sakta.

Ek line yaad rakho: Hard = HEART (inode) pe point, counted; Soft = SPOT (path) pe point, not counted. Jaise hi ek file ke do alag path ban jaate hain, structure tree nahi rehta — wo DAG ban jaata hai (cycles allowed nahi, isliye directory hard links ban allowed nahi).

Go deeper — visual, from zero

Test yourself — Operating Systems

Connections