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_READ and FMODE_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 ...

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.