Files, directories, paths and the VFS
Follow a path one name at a time to an object, see where each failure stops, and see how the VFS sends one set of calls to different filesystems. Filesystems and storage · Operating systems.
A program opens /srv/atlas/reports/today.txt and gets "No such file or directory". The report was there yesterday. What did the kernel do with that string, which different things can go wrong, and why do the same open and read calls work on a local disk, in memory and across a network?
A path is a walk, one name at a time
The kernel never looks up a whole path at once. It splits the path at the slashes into components and looks them up one by one, starting from a directory. A directory is a table of entries, and each entry binds a name to an inode number. The inode is the object that name leads to. It holds the file type, the owner, the permission bits, the size, a link count and the pointers to the data blocks, but not the name. An inode number is meaningful only inside one filesystem, so an object is identified by the pair (filesystem, inode number).
Walk the path for Ana, user 1001 in group 100. The root directory / is inode 2 of the System disk. Looking up srv there gives inode 14, a directory. Looking up atlas in it gives inode 21, a directory on which another disk is mounted. The walk continues in the root of the Atlas disk, which is inode 2 of that disk and a different object from the System disk's inode 2. There reports is inode 30, a directory, and today.txt is inode 42, a file. The walk ends at (Atlas disk, 42). The names Ana typed are not part of that identity.
Before the kernel reads a directory for a name, it checks that the user may search it, which is the execute bit (x) on a directory. Permission bits come in three classes: owner, group and other. The kernel picks the first class that fits the user and uses that class alone. Suppose reports has mode 750, owned by user 1001 and group 100. Ana is the owner, so she has rwx. Ben, user 1002 in group 200, falls in the other class, which has no bits, so his walk stops at reports with EACCES, access denied. The check comes before the lookup, so Ben cannot even learn whether today.txt exists.
That gives three different errors. ENOENT, "no such file or directory", means the name is absent from a directory the user may search, as with yesterday.txt in reports. EACCES means a permission check was refused. In this walk it came from a refused search, before the name was tried, and it can also come from the last step: once today.txt is found, the kernel checks the requested access (read here) on the file itself, and a refusal there is EACCES too. ENOTDIR means a name in the middle of the path exists but is not a directory.
A path walked one name at a time
Read the steps as text
- Atlas is mounted at /srv/atlas. Ana today: Ana wants to read /srv/atlas/reports/today.txt. Ana yesterday: Ana wants to read /srv/atlas/reports/yesterday.txt. Ben today: Ben wants to read /srv/atlas/reports/today.txt.
- All lookups: the walk starts at the root.
- All lookups: searching the root directory is allowed as other (r-x).
- All lookups: the walk finds srv in the root directory, a directory.
- All lookups: searching /srv is allowed as other (r-x).
- All lookups: the walk finds atlas in /srv, a directory, which is a mount point, so it enters Atlas.
- All lookups: searching /srv/atlas is allowed as other (r-x).
- All lookups: the walk finds reports in /srv/atlas, a directory.
- Ana today and Ana yesterday: searching /srv/atlas/reports is allowed as the owner (rwx). Ben today: searching /srv/atlas/reports is denied as other (---), so the lookup fails with EACCES.
- Ana today: the walk finds today.txt in /srv/atlas/reports, a file. Ana yesterday: the walk finds nothing named yesterday.txt in /srv/atlas/reports, so it fails with ENOENT.
- Ana today: read access on /srv/atlas/reports/today.txt is allowed as the owner (rw-).
- Ana today: the walk reaches /srv/atlas/reports/today.txt, inode 42 of Atlas.
The same message has more than one cause. A name may really be absent: misspelled or removed. The walk may have started in the wrong place, as the next section shows. Or the mount may be missing: while the Atlas disk is not mounted, atlas is only an empty directory on the System disk, reports is not found in it, and the error is again ENOENT.
Where the walk starts
A path that begins with / starts at the process's root directory. Any other path is relative and starts at the process's current working directory, or, in calls such as openat, at a directory the caller holds open through a descriptor. So one piece of text can reach different objects. From /srv, atlas/reports/today.txt ends at (Atlas disk, 42). From /home the same text fails at its first name with ENOENT, because /home has no atlas. From /srv, plan.txt/old fails with ENOTDIR, because plan.txt is a file and a file cannot be searched.
The same text from other starting points
Read the steps as text
- Atlas is mounted at /srv/atlas. Srv start: Ana wants to read atlas/reports/today.txt. Home start: Ana wants to read atlas/reports/today.txt. Plan file path: Ana wants to read plan.txt/old.
- Srv start and Plan file path: the walk starts at the current directory /srv. Home start: the walk starts at the current directory /home.
- Srv start and Plan file path: searching /srv is allowed as other (r-x). Home start: searching /home is allowed as other (r-x).
- Srv start: the walk finds atlas in /srv, a directory, which is a mount point, so it enters Atlas. Home start: the walk finds nothing named atlas in /home, so it fails with ENOENT. Plan file path: the walk finds plan.txt in /srv, a file.
- Srv start: searching /srv/atlas is allowed as other (r-x). Plan file path: /srv/plan.txt is not a directory, so the lookup fails with ENOTDIR.
- Srv start: the walk finds reports in /srv/atlas, a directory.
- Srv start: searching /srv/atlas/reports is allowed as the owner (rwx).
- Srv start: the walk finds today.txt in /srv/atlas/reports, a file.
- Srv start: read access on /srv/atlas/reports/today.txt is allowed as the owner (rw-).
- Srv start: the walk reaches /srv/atlas/reports/today.txt, inode 42 of Atlas.
A . component stays where it is and .. moves to the parent directory. The root's parent is the root itself, so extra .. components at the top do no harm. A caller holding a descriptor for /home/ana and another holding one for /srv can both reach /srv/plan.txt with ../../srv/plan.txt, and both end at the same object.
Starting from a directory descriptor
Read the steps as text
- Home descriptor: Ana wants to read ../../srv/plan.txt. Srv descriptor: Ana wants to read ../../srv/plan.txt.
- Home descriptor: the walk starts at the directory /home/ana held by a descriptor. Srv descriptor: the walk starts at the directory /srv held by a descriptor.
- Home descriptor: searching /home/ana is allowed as the owner (rwx). Srv descriptor: searching /srv is allowed as other (r-x).
- Home descriptor: the walk moves up to /home. Srv descriptor: the walk moves up to the root directory.
- Home descriptor: searching /home is allowed as other (r-x). Srv descriptor: searching the root directory is allowed as other (r-x).
- Home descriptor: the walk moves up to the root directory. Srv descriptor: the root has no parent above it, so going up stays at the root directory.
- Both lookups: searching the root directory is allowed as other (r-x).
- Both lookups: the walk finds srv in the root directory, a directory.
- Both lookups: searching /srv is allowed as other (r-x).
- Both lookups: the walk finds plan.txt in /srv, a file.
- Both lookups: read access on /srv/plan.txt is allowed as other (r--).
- Both lookups: the walk reaches /srv/plan.txt, inode 15 of System.
One set of operations, many filesystems
Every lookup, and the open, read, write and close that follow it, goes through the VFS, the kernel's virtual filesystem switch. The VFS defines one set of operations on in-memory objects: the mounted filesystem, the inode, the directory entry and the open file. During path lookup, mount crossings determine which filesystem supplies each component. Once a file is open, operations such as read, write and close use the open file reached through the descriptor; they do not resolve the original path again. The VFS dispatches these operations to the corresponding filesystem implementation. A local disk filesystem reads blocks, or serves them from its cache. A memory filesystem answers from RAM. A network filesystem sends a request to a server.
The VFS stores no file data itself, and it does not make the implementations behave alike. A network read can fail or time out in ways a local read cannot. Depending on the mount options, a server that never answers may end the call with EIO, an input/output error, or keep the caller waiting. The share in the example below is mounted to give up instead of waiting; its requested data requires a server response. The scene models routing by path as a shorthand for opening each file before reading it.
One read, three filesystems
Read the steps as text
- Mounts send / to Disk, /tmp to Temp and /srv/share to Share. Disk: a program reads 80 bytes of /var/log/app.log. Temp: a program reads 80 bytes of /tmp/scratch.bin. Share: a program reads 80 bytes of /srv/share/team/plan.txt.
- Disk: the program reads /var/log/app.log. Temp: the program reads /tmp/scratch.bin. Share: the program reads /srv/share/team/plan.txt.
- Disk: the longest matching mount point is /, so the VFS picks Disk. Temp: the longest matching mount point is /tmp, so the VFS picks Temp. Share: the longest matching mount point is /srv/share, so the VFS picks Share.
- Disk: the VFS calls the local filesystem's own read. Temp: the VFS calls the memory filesystem's own read. Share: the VFS calls the network filesystem's own read.
- Disk: the data is read from the disk. Temp: the data is answered from memory. Share: a request goes to the server.
- Disk and Temp: the call returns 80 bytes. Share: no reply arrives before this mount gives up waiting, so the call fails with EIO.
- Share: the error goes back to the program.
Names can share and loop
Two directories can each hold a name report.txt, because a name has to be unique only inside its directory, so /ana/report.txt and /ben/report.txt are different objects (inode 31 and 32). A hard link gives one object a second name, so /ben/latest.txt reaches inode 31 as well. A symbolic link is a name whose content is another path, looked up again each time it is followed, so two links can point at each other, and the walk follows only a fixed number of them (the examples use 8) before it fails with ELOOP.
Names, directories and links
Read the steps as text
- Ana report: Ana wants to read /ana/report.txt. Ben report: Ben wants to read /ben/report.txt. Ben latest: Ben wants to read /ben/latest.txt.
- All lookups: the walk starts at the root.
- All lookups: searching the root directory is allowed as other (r-x).
- Ana report: the walk finds ana in the root directory, a directory. Ben report and Ben latest: the walk finds ben in the root directory, a directory.
- Ana report: searching /ana is allowed as the owner (rwx). Ben report and Ben latest: searching /ben is allowed as the owner (rwx).
- Ana report: the walk finds report.txt in /ana, a file. Ben report: the walk finds report.txt in /ben, a file. Ben latest: the walk finds latest.txt in /ben, a file.
- Ana report: read access on /ana/report.txt is allowed as the owner (rw-). Ben report: read access on /ben/report.txt is allowed as the owner (rw-). Ben latest: read access on /ben/latest.txt is allowed as other (r--).
- Ana report: the walk reaches /ana/report.txt, inode 31 of Disk. Ben report: the walk reaches /ben/report.txt, inode 32 of Disk. Ben latest: the walk reaches /ben/latest.txt, inode 31 of Disk.
Links that point at each other
Read the steps as text
- Short link: Ana wants to read /ana/current. Loop: Ana wants to stat /loop-a.
- Both lookups: the walk starts at the root.
- Both lookups: searching the root directory is allowed as other (r-x).
- Short link: the walk finds ana in the root directory, a directory. Loop: the walk finds loop-a in the root directory, a link to loop-b, followed (link 1 of the 8 allowed here).
- Short link: searching /ana is allowed as the owner (rwx). Loop: searching the root directory is allowed as other (r-x).
- Short link: the walk finds current in /ana, a link to report.txt, followed (link 1 of the 8 allowed here). Loop: the walk finds loop-b in the root directory, a link to loop-a, followed (link 2 of the 8 allowed here).
- Short link: searching /ana is allowed as the owner (rwx). Loop: searching the root directory is allowed as other (r-x).
- Short link: the walk finds report.txt in /ana, a file. Loop: the walk finds loop-a in the root directory, a link to loop-b, followed (link 3 of the 8 allowed here).
- Short link: read access on /ana/report.txt is allowed as the owner (rw-). Loop: searching the root directory is allowed as other (r-x).
- Short link: the walk reaches /ana/report.txt, inode 31 of Disk. Loop: the walk finds loop-b in the root directory, a link to loop-a, followed (link 4 of the 8 allowed here).
- Loop: searching the root directory is allowed as other (r-x).
- Loop: the walk finds loop-a in the root directory, a link to loop-b, followed (link 5 of the 8 allowed here).
- Loop: searching the root directory is allowed as other (r-x).
- Loop: the walk finds loop-b in the root directory, a link to loop-a, followed (link 6 of the 8 allowed here).
- Loop: searching the root directory is allowed as other (r-x).
- Loop: the walk finds loop-a in the root directory, a link to loop-b, followed (link 7 of the 8 allowed here).
- Loop: searching the root directory is allowed as other (r-x).
- Loop: the walk finds loop-b in the root directory, a link to loop-a, followed (link 8 of the 8 allowed here).
- Loop: searching the root directory is allowed as other (r-x).
- Loop: the walk finds loop-a in the root directory, a link to loop-b, so the lookup gives up at the limit of 8 links allowed here, with ELOOP.
Recap
- The kernel resolves a path one component at a time from a starting directory, checks search permission on each directory, and ends at an object identified by its filesystem and inode number.
ENOENT,EACCESandENOTDIRmark different breaks in the walk, and the starting point and the mounts decide which directories the walk searches.- The VFS gives callers one set of operations and dispatches to the relevant filesystem, whether it reads a disk, answers from memory or asks a server, without making them behave alike.
What the walk leaves out
A mount changes only which filesystem answers. While it is mounted, the previous contents of the mount point are hidden, lookup continues in the mounted filesystem's root, and .. from that root leads back to the parent of the mount point. Nothing is copied.
The superuser's bypass, access control lists, setuid bits and the sticky bit on shared directories change who may do what, and they are left out here. Creating, deleting or renaming an entry needs write and search permission on the directory, not on the file. Open files, descriptors and the offset they carry are the next lesson: File descriptors and object lifetime.