File Operations
In the next few sections, we’ll look at the various operations a
driver can perform on the devices it manages. An open device is
identified internally by a file structure, and the
kernel uses the file_operations structure to access
the driver’s functions. The structure, defined in
<linux/fs.h>, is an array of function
pointers. Each file is associated with its own set of functions (by
including a field called f_op that points to a
file_operations structure). The operations are
mostly in charge of implementing the system calls and are thus named
open, read, and so on. We
can consider the file to be an “object” and the functions operating
on it to be its “methods,” using object-oriented programming
terminology to denote actions declared by an object to act on itself.
This is the first sign of object-oriented programming we see in the
Linux kernel, and we’ll see more in later chapters.
Conventionally, a file_operations structure or a
pointer to one is called fops (or some variation
thereof); we’ve already seen one such pointer as an argument to the
register_chrdev call. Each field in the structure
must point to the function in the driver that implements a specific
operation, or be left NULL for unsupported
operations. The exact behavior of the kernel when a
NULL pointer is specified is different for each
function, as the list later in this section shows.
The file_operations structure has been slowly getting bigger as new functionality is added to the kernel. ...