How Mounting Works
Block devices differ from char devices and normal files in that
they can be mounted on the computer’s filesystem. This is different
from the normal access through a struct file
, where the
structure is bound to a specific process and exists only from
open to close. When a filesystem is mounted,
there’s no process holding a filp
.
When the kernel mounts a device in the filesystem, it invokes
the normal open method to access the driver. However, in
this case the filp
argument to open is a
dummy variable, almost a placeholder, whose only meaningful field is
f_mode
. The remaining fields hold random values and should
not be used. The value of f_mode
tells the driver whether
the device is to be mounted read-only (f_mode == FMODE_READ
) or read/write (f_mode == (FMODE_READ|FMODE_WRITE)
). A dummy variable is used instead of a
file
structure because a real struct file
would be released at process termination, while a mounted filesystem
survives after the mount command has done its job.
At mount time, the only thing that is invoked in the driver is the
open method. While
the disk is mounted, the kernel invokes the read and write
methods in the device (which map to request_fn
) to manage files
in the filesystem. The driver can’t tell if request_fn
is
servicing a process (like fsck) or the filesystem layer of the
kernel.
As far as umount is concerned, it just flushes the buffer
cache and calls the release (close) driver method. Since
there is no meaningful filp
to pass to ...
Get Linux Device Drivers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.