Important data structures in character Devices
Most character-driven designs three important data structures
<Linux/fs. h> struct file_operations
Struct File
Struct inode
I. File Operations
In the previous article, I have already introduced how to use device numbers for personal characters, but without doing any work, I can only write one character device that cannot work;
The struct file_operations domain is used to connect devices and operations for system calls.
Important Field Introduction: struct file_operations {
Struct module * owner; // indicates the pointer to the structure module. Almost all drivers are set to this_module. <Linux/module. h>
Loff_t (* llseek) (struct file *, loff_t,Int); // File read/write location adjustment function
Ssize_t (* read) (struct file *, char _ User *, size_t, loff_t *); // read data from the device
Ssize_t (* write) (struct file *, const char _ User *, size_t, loff_t *); // write data to the device
Unsigned int (* poll) (struct file *, struct poll_table_struct *); // query whether read and write operations on the file descriptor are blocked
INT (* MMAP) (struct file *, struct vm_area_struct *); // map the device memory to the process space
INT (* open) (struct inode *, struct file *); // corresponds to the device that opens
INT (* release) (struct inode *, struct file *); // corresponding to disabling a device
.
.
.
};
There are still a lot of operations in the structure because I haven't learned it yet, so I haven't made much introduction to it. (ioctl function operations became unlocked_ioctl and compat_ioctl after 2.6.35. In fact, they are not very useful and have not been introduced yet, I will have the opportunity to introduce it later)
When the values of open and release are set to null, the default mode of INCORE is enabled or disabled without errors,
When other functions are not defined, application calls may fail.
Below is a definition of the most important device operation methods. Struct file_operations simple_fops = {
. Owner = this_module,
. Open = simple_open,
. Release = simple_close,
. Read = simple_read,
. Write = simple_write,
. Llseek = simple_llseek,
. Poll = simple_poll,
. MMAP = simple_mmap,
};
Ii. File structure
The file structure introduced here is not a file structure in C language. There is no association between the two. struct file is only a kernel structure. Each opened file corresponds to a struct file structure, A file can correspond to different struct file structures. struct file {
Struct path f_path; // File Location
Const struct file_operations * f_op;. // file Operator
Spinlock_t f_lock;
Atomic_long_t f_count;
Unsigned int f_flags; // File ID (o_nonblock, o_rdonly, etc)
Fmode_t f_mode; // file mode readable and writable
Loff_t f_pos; // file read/write location
Struct fown_struct f_owner;
Const struct cred * f_cred;
Struct file_ra_state f_ra;
.
.
.
Void * private_data; // Most Important !! Private Data, the driver can use it to point to any data structure
};
Iii. inode Structure
The Linux kernel uses the inode structure to represent a file. Unlike file, file can be understood as a structure used to represent the file descriptor. A file can correspond to many file descriptors, at last, it only points to the same inode structure.
Struct inode {
...
Dev_t I _rdev; // Save the device number
Union {
Struct pipe_inode_info * I _pipe;
Struct block_device * I _bdev;
Struct cdev * I _cdev; // points to the struct cdev Structure
};
...
};
Iv. File structure and inode Structure Diagram