Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 8: The Virtual Filesystem


The kernel provides more than 50 system calls for the above manipulations. We look only at the most
important calls to demonstrate the key principles.^2

Files must be opened with theopenoropenatsystem call before they can be used. The kernel returns a
non-negative integer number to userland after opening the file successfully. The assigned file descriptor
numbers start at 3. Recall that numbering does not start at 0 because the first three file descriptors are
reserved for all processes, although no explicit instructions need be given. 0 represents standard input, 1
standard output, and 2 standard error.

Once a file has been opened, its name has no further significance. It is now uniquely identified by its file
descriptor, which is passed as a parameter to all further library functions (and therefore to system calls).
While file descriptors were traditionally sufficient to identify a file within the kernel, this is not the case
anymore. Since the introduction of multiple namespaces and containers, multiple file descriptors with
the same numerical value can coexist in the kernel. Aunique representation is provided by a special data
structure (struct file), discussed below.

We see this in theclosepart of the sample program that closes the ‘‘connection‘‘ to a file (and returns
the file descriptor so that it can be used for files to open other files in the future).readalso expects the
file descriptor as its first parameter so that it can identify the source from which to read data.

The current position within an open file is held in thefile pointer, which is an integer that specifies the
offset from the start of the file. The pointer can be set to any value forrandom access filesas long as the
value remains within the file limits. This supports random access to the file data. Other file types —named
pipesordevice filesfor character devices, for instance — prohibit this. They may only be read sequentially
from beginning to end.

Various flags (such asO_RDONLY) are specified to define theaccess modewhen a file is opened. More
detailed explanations are given in all works on system programming.

8.2.4 Files as a Universal Interface


Unixis based on just a few judiciously selected paradigms. A very important ‘‘metaphor‘‘ threads its way
through the kernel (and particularly through the VFS), particularly with regard to the implementation of
input and output mechanisms.

Everything is a file.


OK, let’s admit it: There are, of course, a few exceptions to this rule (e.g., network devices), but most
functions exported by the kernel and employed by user programs can be reached via the file interface
defined by the VFS. The following is a selection of kernel subsystems that use files as their central means
of communication:

❑ Character and block devices
❑ Pipes between processes

(^2) Communication with files is carried out not only by means of file descriptors but also with the help ofstreams. The latter provide
a convenient interface. They are, however, implemented in the C standard library and not in the kernel. Internally they make use of
normal file descriptors.

Free download pdf