File Systems

Files, directories and inodes; allocation methods; hard and soft links; journaling and crash consistency; copy-on-write file systems; and fsync.

Intermediate 4 min readLesson 13 of 16 Interview Q&A

A file system turns a disk — a giant array of numbered blocks — into named files in a hierarchy of directories, with permissions, timestamps, and (hopefully) no corruption when the power fails.

The abstractions

  • A file is a sequence of bytes plus metadata (size, owner, permissions, timestamps).
  • A directory is a special file mapping names → inode numbers.
  • A path like /home/ada/notes.txt is resolved one component at a time from the root directory.

Inodes

In Unix file systems, each file's metadata lives in an inode (index node). The name is not in the inode; it's in the directory entry that points to it.

directory /home/adanotes.txt → inode 4182inode 4182size · owner · mode · times · block pointersdata blockdata blockindirect block→ more blocks

Classic Unix inodes hold 12 direct block pointers plus single, double and triple indirect pointers, so small files are fast to access and large files are still supported. ext4 and most modern file systems use extents instead — (start block, length) runs — which are far more compact for large contiguous files.

Allocation methods

MethodHowProsCons
ContiguousEach file is one run of blocksFast sequential and random accessExternal fragmentation; files can't grow easily
Linked (FAT)Each block points to the next (in a table)No fragmentationRandom access is O(n)
Indexed (inodes)An index block lists the data blocksRandom access, growthIndex overhead
ExtentsLists of contiguous runsCompact, fastMore complex

Free space is tracked with bitmaps (one bit per block) or extent trees.

Hard link

Another directory entry pointing to the same inode. The file only disappears when the inode's link count drops to 0. It can't cross file systems or point to directories.

Shell
ln notes.txt backup.txt
Symbolic (soft) link

A small file containing a path. It can point anywhere, including other file systems and directories, but it breaks ("dangles") if the target moves.

Shell
ln -s /opt/app/v2 current

Deleting a file is really unlink: removing a name. If a process still has the file open, the data survives until it's closed — which is why deleting a huge log file sometimes frees no space until the process writing to it restarts.

Crash consistency and journaling

Appending one block to a file needs several separate writes: the data block, the inode (size, pointers) and the free-space bitmap. If power fails halfway, the file system can be left inconsistent: a block marked used but owned by no file, or an inode pointing to garbage.

  1. fsck (old approach): after a crash, scan the whole disk to repair it. Takes hours on large disks.
  2. Journaling (write-ahead logging): first write the intended changes to a journal, then a commit record, then apply them in place. After a crash, replay committed journal entries and discard incomplete ones. Recovery takes seconds.
Journaling mode (ext4)JournalsTrade-off
journalMetadata and dataSafest, slowest (data written twice)
ordered (default)Metadata; data is written before its metadata commitsGood balance
writebackMetadata only, no orderingFastest; files may contain stale data after a crash

Copy-on-write file systems

ZFS, Btrfs and Apple's APFS never overwrite data in place. They write new blocks and then atomically switch a pointer at the top of the tree. That gives:

  • Consistency without a journal (the old tree stays valid until the switch).
  • Cheap snapshots and clones (keep the old root).
  • Checksums on every block to detect silent corruption (bit rot), and self-healing with redundancy.

The trade-off is fragmentation for workloads with many random overwrites, such as databases.

Durability: the fsync story

write() only copies data into the OS page cache. It reaches the disk later. To make it durable you must call fsync (or fdatasync), which waits until the data is on stable storage.

try (FileChannel ch = FileChannel.open(path, CREATE, WRITE)) {
    ch.write(ByteBuffer.wrap(bytes));
    ch.force(true);              // fsync: data + metadata on disk
}

Virtual file system and "everything is a file"

Linux's VFS layer gives every file system (ext4, XFS, NFS, tmpfs, FUSE…) the same interface. Many non-disk things look like files too: /proc (process information), /sys (kernel and device settings), /dev (devices), pipes and sockets. One set of tools (cat, ls, read, write) works on all of them.

Key takeaways
  • Directories map names to inodes; inodes hold metadata and block pointers (or extents).
  • Hard links share an inode; symlinks store a path.
  • Journaling writes intent to a log first so crashes can be recovered quickly.
  • Copy-on-write file systems give atomic updates, snapshots and checksums.
  • write isn't durable; fsync is. Use write-temp + fsync + rename for atomic updates.
Interview questions
QWhy might deleting a big file not free disk space?medium

Deletion removes the directory entry (unlink). If a process still has the file open, the inode and its data blocks remain until the last file descriptor is closed. lsof +L1 lists such deleted-but-open files.