The file Structure
struct file, defined in <linux/fs.h>, is the
second most important data structure used in device drivers. Note
that a file has nothing to do with the FILEs of
user-space programs. A FILE is defined in the C library and never
appears in kernel code. A struct file, on the other hand, is a kernel
structure that never appears in user programs.
The file structure represents an ``open file.'' It is
created by the kernel on open and is passed to any function
that operates on the file, until close. After the file is
closed, the kernel releases the data structure. An ``open file'' is
different from a ``disk file,'' which is represented by struct inode.
In the kernel sources, a pointer to struct file is usually
called either file or filp (``file
pointer''). I’ll consistently
call the pointer filp to prevent ambiguities with the structure
itself--filp is a pointer (as such, it is one of the arguments to device
methods), while file is the structure itself.
The most important fields of struct file are shown below.
As in the previous section, the list can be skipped on a first
reading. In the next section though, when we face some real C code,
I’ll discuss some of the fields, so they are here for you to
refer back to.
-
mode_t f_mode; The file mode is identified by the bits
FMODE_READandFMODE_WRITE. You might want to check this field for read/write permission in your ioctl function, but you don’t need to check permissions for read and write because the kernel checks before ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access