How a file system finds your file

· 2 min read

#how-it-works#os

A file system turns a path like /home/ana/notes.txt into the disk blocks that hold the file. The idea to keep: a file's name is not stored in the file, it is stored in a directory.

The short version

A directory is a list of entries, and each entry pairs a name with an inode number. The system starts at the root and looks up one name at a time, so each lookup leads into the next directory. The last name leads to an inode, and the inode says where the data blocks are.

A path is followed from the root directory to the data blocks The path /home/ana/notes.txt is looked up in the root directory, then in the home directory, then in the ana directory. The entry for notes.txt gives an inode, and the inode points to the data blocks. Root directory Directory: home Directory: ana Inode Data blocks 1 home 2 ana 3 notes.txt 4 blocks Path: /home/ana/notes.txt
Simplified (ext4 on Linux): each arrow is one lookup, and only the inode knows where the data is.

Step by step

  1. A path that starts with / begins at the root directory; any other path begins at the current directory.
  2. The system looks for home in the root directory. It must be a directory, and you need search permission on it (the execute bit).
  3. It repeats this for ana inside home. A name that is missing gives the error ENOENT.
  4. In the last directory, the entry for notes.txt gives an inode number.
  5. The inode holds the file's size, owner and timestamps, and it points to the file's data blocks.
  6. The system reads those blocks.
What Where it is stored
Name Directory entry
Size, owner, timestamps Inode
Content Data blocks

Why it matters

Because the name sits in the directory and not in the inode, two names can point to the same inode; that is a hard link. The downside is that inode numbers are only unique inside one file system, so a hard link cannot span two. My take: a deep path costs one lookup per part.

Sources