Preface
In the previous section, we introduced the important structure of character device cdev, and some operation functions related to cdev, such as cdev_alloc(), cdev_init(), etc., but a parameter fops closely related to cdev_init() is not introduced. This section focuses on the file_operations structure.
text
The member functions of the file_operations structure are the main contents of the character device driver design, which are actually called through Linux system calls, such as open(), write().
// (include\linux\Fs.h)
/*
* NOTE:
* all file operations except setlease can be called without
* the big kernel lock held in all filesystems.
*/
struct file_operations {
//A pointer to a module with this structure, usually set to THIS_MODULE
struct module *owner;
//Used to modify the read and write location of the current file
loff_t (*llseek) (struct file *, loff_t, int);
//Read data synchronously from the device
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
//Send data to the device
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
//Asynchronous read operation
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
//Asynchronous write operations
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
//Used only to read directories, generally NULL for device files
int (*readdir) (struct file *, void *, filldir_t);
//Polling function to determine whether non-blocking reading and writing is currently possible
unsigned int (*poll) (struct file *, struct poll_table_struct *);
//Execute device I/O control commands
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
//Request for mapping device memory to process address space
int (*mmap) (struct file *, struct vm_area_struct *);
//open
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
//Close
int (*release) (struct inode *, struct file *);
//Refresh the pending buffer
int (*fsync) (struct file *, int datasync);
//Asynchronous sync
int (*aio_fsync) (struct kiocb *, int datasync);
//Notify equipment FASYNC identification changes
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
};
Summarize the more file_operations member functions involved in character devices
- The open() function is used to open character devices
- release() function is used to close character devices
- The read() function is used to read data from the device. The function returns the number of bytes read when it succeeds and a negative number when it fails.
- The write() function sends data to the device and returns the number of bytes written when it succeeds.
- The unlocked_ioctl() function provides device-related control commands
- mmap() function maps device memory to process memory
- poll() function The general user asks if the device can be read or written immediately without blocking it.
summary
When system calls are executed in user space, such as open(), member functions of file_operations of the corresponding device are called. In this way, the operation of user space can be realized by the member function of file_opeations of the corresponding character device.
Parametric literature
Linux Device Driver Development Details (Song Baohua) 2nd Edition