A filesystem under the bonnet
A filesystem turns storage into named objects. A traditional UFS-style layout is a useful mental model, not a literal map of ext4, XFS or ZFS. UFS distributes metadata and data across cylinder groups.
Superblock → filesystem geometry and state Inode table → metadata and block references Data blocks → file contents and directory entries
A directory maps names to inode numbers. The inode records the object’s type, ownership, permissions, timestamps, link count and data mapping. The filename is not normally stored in the inode.
Directory entry Inode Data report.txt ──────────→ 42 ──────────→ contents another-name ────────→ 42
Classic UFS uses direct pointers and single, double and triple indirect pointers. Indirect blocks contain further block addresses rather than user data. Modern filesystems often use extents or trees instead.
inode ── direct ───────────────────→ data
├─ single ── pointer block ──→ data
├─ double ── block ── block ─→ data
└─ triple ── block ── block ── block → data
The seven Linux file types
The first character of
ls -l
identifies the object type. Each of these named objects has an inode in the filesystem namespace; anonymous pipes and sockets are a separate matter.
| Type | Marker | Purpose |
|---|---|---|
| Regular file |
-
|
Stores bytes: text, binaries, images. |
| Directory |
d
|
Maps names to inode numbers. |
| Symbolic link |
l
|
Stores a target pathname; may dangle. |
| Block device |
b
|
Access to block-oriented devices, e.g. disks. |
| Character device |
c
|
Driver-defined character or stream-style access. |
| Named pipe |
p
|
Kernel-buffered byte stream between processes. |
| Unix domain socket |
s
|
Local IPC; supports bidirectional communication. |
Device nodes store major and minor device numbers, not the device contents. A FIFO or socket pathname is a rendezvous point, not a persistent store for messages.
Links and deletion
A hard link is another directory entry for the same inode. Hard links normally cannot cross filesystems or refer to directories. A symbolic link has its own inode and stores a pathname, which can cross filesystems.
mkdir -p ~/fs-lab cd ~/fs-lab printf 'hello\n' > original ln original hard ln -s original symbolic ls -li rm original cat hard cat symbolic # fails: dangling symlink
Unlinking the last name does not immediately reclaim an open file. Its data remains accessible through open descriptors until the last relevant reference is released. This explains some discrepancies between df and du.
printf 'still here\n' > held exec 3<held rm held cat <&3 exec 3<&-
Metadata, timestamps and blocks
stat hard ls -li hard chmod 600 hard stat hard
mtime records data modification; ctime records inode status changes, including changes to mtime and permissions. atime records access subject to mount policy. Birth time, where supported, records creation of the inode, not necessarily the original document: editors may replace files with new inodes.
GNU stat reports allocated blocks in units of 512 bytes (check %B), while IO Block is a preferred I/O size, not necessarily the filesystem allocation unit. Sparse files can have a large logical size but few allocated blocks.
truncate -s 1G sparse stat sparse du -h sparse ls -lh sparse
Device files: a safe demonstration
On a disposable Linux VM, create a character node referring to the same driver as /dev/null. This requires privileges and may be restricted in containers.
cd ~/fs-lab ls -l /dev/null sudo mknod demo-null c 1 3 sudo chmod 666 demo-null printf 'discard me\n' > demo-null cat demo-null sudo rm demo-null
Nothing is stored in the node. The kernel dispatches operations to the null driver. Never experiment by writing to a real disk device.
Named pipes and Unix sockets
A FIFO is useful for streaming data between unrelated processes without a temporary regular file. A Unix domain socket is preferable for request/response protocols, multiple clients or bidirectional communication.
In terminal A:
cd ~/fs-lab mkfifo feed cat feed
In terminal B:
cd ~/fs-lab printf 'hello from another process\n' > feed
cat exits when all writers close. A FIFO is a byte stream, not a message queue. For sockets, install socat if necessary and use two terminals:
# Terminal A cd ~/fs-lab socat UNIX-LISTEN:demo.sock STDIO # Terminal B cd ~/fs-lab socat STDIO UNIX-CONNECT:demo.sock
Type in either terminal to send data to the other. Stop both processes before removing the socket and FIFO.
rm -f ~/fs-lab/demo.sock ~/fs-lab/feed
Test your knowledge
Can a file exist after its last name is deleted?
Yes, while an open reference remains.
Can two names share an inode?
Yes, hard links.
Does a symlink share its target inode?
No. It has its own inode.
Does chmod change mtime?
Normally no; it changes ctime.
Can a 1 GiB file consume almost no disk space?
Yes, a sparse file.
Does a device node contain disk data?
No, it identifies a device driver interface.
Does a FIFO preserve messages after all processes close it?
No.
Can a Unix socket work without a network interface?
Yes, it is local IPC.