How a file system finds your file
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.
Step by step
- A path that starts with
/begins at the root directory; any other path begins at the current directory. - The system looks for
homein the root directory. It must be a directory, and you need search permission on it (the execute bit). - It repeats this for
anainsidehome. A name that is missing gives the error ENOENT. - In the last directory, the entry for
notes.txtgives an inode number. - The inode holds the file's size, owner and timestamps, and it points to the file's data blocks.
- 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
- path_resolution(7): how a path is walked one component at a time, the search permission and ENOENT
- inode(7): what an inode holds (size, owner, timestamps, hard link count) and that inode numbers are unique only within a file system
- ext4 directory entries: an entry is a name plus an inode number