Figure 10.1. Overview of the Virtual Filesystem (VFS)
The VFS layer works by dispatching requests for filesystem operations (such as opening, reading, or writing a file) to the appropriate filesystem implementation. The VFS works by defining several abstract datatypes representing mounted filesystem instances, open files, and open directories. Each of these datatypes contains a virtual function table which contains pointers to functions in the filesystem that the object belongs to. For example, File objects created by the GOSFS filesystem have a virtual function table whose Read() entry points to the function GOSFS_Read().
To give you an idea of how VFS works, here is what happens when a user process reads data from an open file in a GOSFS filesystem.
-
The process calls the Read() in the C library, which generates a software interrupts to notify the kernel that a system call is requested. It passes a file descriptor identifying the open file, the address of the buffer to store the data read from the file, and the number of bytes requested.
-
The kernel calls the Sys_Read() system call handler function. This function will look at the process's open file list (in User_Context) to find the File object representing the open file. It will also need to allocate a kernel buffer to temporarily hold the data read from the file. It will then call the kernel VFS function Read().
-
The VFS Read() will find the address of the Read function in the file's virtual function table. Because the file is part of a GOSFS filesystem, this will resolve to the GOSFS_Read() function.
-
GOSFS_Read() will do whatever work is necessary to read the requested data from the file, at the current file position, copying the data into the kernel buffer created by Sys_Read().
-
When control returns to Sys_Read(), it will copy the data read from the kernel buffer to the user buffer (using the Copy_To_User() function), and destroy the kernel buffer.
-
Finally, Sys_Read() will return, and the process will resume execution.