SkillGridLearn computer science by doing

Files and the file system

What actually happens when you save a file, and why an inode is not the file name.

12 min · 36 XP

A file system organises blocks on a device into named files and directories. A directory is itself a file that maps names to inode numbers — the name is not the file; it is a label pointing at one.

An inode stores a file's metadata and where its data blocks live: size, permissions, timestamps, block pointers. It does not store the name. That is why one file can have several names (hard links) and none of them is "the real one".

AppKernel (VFS)Page cacheDiskopen("/etc/hosts")walk directories, find inode 42file descriptor 3read(fd, buf, 4096)is block already cached?no — read it from diskhere is the blockcopy into your buffer

Disk reads are thousands of times slower than memory, so the kernel keeps recently used file data in a page cache. A repeated read may never touch the disk at all.

# Путь — это просто имена каталогов, которые ядро проходит шаг за шагом.
# Конкретный ФАЙЛ живёт в inode, а не в имени.

$ ls -i /etc/hosts
42 /etc/hosts          # 42 — номер inode

$ ln /etc/hosts /tmp/alias     # жёсткая ссылка: +1 имя у ТОГО ЖЕ inode
$ ln -s /etc/hosts /tmp/soft    # симлинк: отдельный файл со ссылкой

# Место на диске занимают ДАННЫЕ, а не имя файла.

Practice

  1. You write to a file and the call returns successfully. Is the data on disk?

    Hints
    • Where does the kernel put writes before they reach the disk?
    • What is the point of fsync if writes already reached the disk?

    12 XP

  2. Put the steps of reading a file in order.

    Hints
    • A path is a sequence of names, each resolved in turn.
    • Check the fast path before the slow one.

    13 XP

  3. Explain why deleting a file that another process has open does not necessarily free its disk space.

    Hints
    • What keeps the inode alive — the name, or something else?
    • Count the references, not the directory entries.

    13 XP