Part 3 of 7 · 5 min read
The VFS
Think of a universal remote control again, but for file systems. The buttons are open, read, write and close. Under the cover, each television brand has its own code for each button. The analogy stops in one place: a remote sends a signal, and the VFS calls a function in the kernel.
In plain words: The virtual file system (VFS) is the layer of the kernel between the system calls and the file systems. It defines the objects that all file systems share: the superblock (one mounted file system), the inode (one file), the dentry (one name, with the dentry cache of page 1) and the file (one open file, with its offset). Each object has a table of function pointers, and each file system fills in its own functions. When a program calls read, the VFS finds the file object, and calls the read function of the file system that owns it. The program never knows which one it was.
A worked example. The miniature VFS below has three file systems that fill in the same table. The generic function finds the mount with the longest matching prefix, and calls its function.
#include <stdio.h>
#include <string.h>
/* A miniature VFS: every file system fills in the same table of functions. */
struct fs_ops {
const char *name;
int (*read)(const char *path, char *buf, int n);
};
static int mem_read(const char *path, char *buf, int n) { /* "stored" text */
const char *text = strcmp(path, "/note") == 0 ? "kept in memory\n" : NULL;
if (!text) return -1;
strncpy(buf, text, (size_t)n);
return (int)strlen(text);
}
static int zero_read(const char *path, char *buf, int n) { /* a device-like source */
(void)path; memset(buf, 0, (size_t)n); return n;
}
static int clock_read(const char *path, char *buf, int n) { /* a value made on the fly */
(void)path; return snprintf(buf, (size_t)n, "tick %d\n", 42);
}
static const struct fs_ops memfs = {"memfs", mem_read};
static const struct fs_ops zerofs = {"zerofs", zero_read};
static const struct fs_ops clockfs = {"clockfs", clock_read};
struct mount { const char *point; const struct fs_ops *ops; };
static const struct mount table[] = {{"/", &memfs}, {"/zero", &zerofs}, {"/clock", &clockfs}};
/* The generic layer: find the mount with the longest matching prefix, then call its function. */
static int vfs_read(const char *path, char *buf, int n) {
const struct mount *best = &table[0];
for (size_t i = 0; i < sizeof table / sizeof table[0]; i++) {
size_t len = strlen(table[i].point);
if (strncmp(path, table[i].point, len) == 0 && len > strlen(best->point)) best = &table[i];
}
const char *rest = path + strlen(best->point);
if (best != &table[0] && *rest == 0) rest = "/";
if (best == &table[0]) rest = path;
int r = best->ops->read(rest, buf, n);
printf("%-9s -> %-8s read returned %d\n", path, best->ops->name, r);
return r;
}
int main(void) {
char buf[32];
vfs_read("/note", buf, sizeof buf);
vfs_read("/zero/x", buf, 4);
vfs_read("/clock", buf, sizeof buf);
vfs_read("/missing", buf, sizeof buf);
return 0;
}
Output:
/note -> memfs read returned 15
/zero/x -> zerofs read returned 4
/clock -> clockfs read returned 8
/missing -> memfs read returned -1
The caller used one function, vfs_read, for four requests. The longest-prefix rule sent /zero/x to zerofs and /clock to clockfs, and everything else went to the root file system.
The key lines. This is the same technique as the device drivers of Level 8, page 5: a table of function pointers that hides many implementations behind one interface. In C it is the way to write "polymorphism".
Virtual file system (VFS)
The virtual file system is the layer of an OS kernel that gives all file systems one interface. It defines common objects and a table of operations for each, and it calls the file system's own function for each request. This lets a program use the same system calls on any file system.
Example: a read on a file in /proc and a read on a file in /home both enter the VFS, which calls different functions.
Check yourself:
1. What does the VFS give a program?
2. How does the miniature VFS decide which file system gets /zero/x?
3. What C feature builds the table of operations?