驱动中用到的重要的文件结构——文件操作结构体
时间:2010-11-06 来源:whyliyi
file_operations结构体定义在<linux/fs.h>中,其中包含了一组函数指针,每个打开的文件(在内部由一个file_operations结构的表示)和一组函数关联(通过包含指向一个file_operations结构的f_op字段)。这些操作主要用来实现系统调用,命名为open、read等等。我们可以认为文件时一个“对象”,而操作它的函数是“方法”,如果采用面向对象编程的术语来表达iushi:对象申明的动作将作用域本身。
下边是摘自Linux内核的源码:
struct file_operations {
1188 struct module *owner;
1189 loff_t (*llseek) (struct file *, loff_t, int);
1190 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1191 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1192 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1193 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1194 int (*readdir) (struct file *, void *, filldir_t);
1195 unsigned int (*poll) (struct file *, struct poll_table_struct *);
1196 int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
1197 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1198 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1199 int (*mmap) (struct file *, struct vm_area_struct *);
1200 int (*open) (struct inode *, struct file *);
1201 int (*flush) (struct file *, fl_owner_t id);
1202 int (*release) (struct inode *, struct file *);
1203 int (*fsync) (struct file *, struct dentry *, int datasync);
1204 int (*aio_fsync) (struct kiocb *, int datasync);
1205 int (*fasync) (int, struct file *, int);
1206 int (*lock) (struct file *, int, struct file_lock *);
1207 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1208 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1209 int (*check_flags)(int);
1210 int (*dir_notify)(struct file *filp, unsigned long arg);
1211 int (*flock) (struct file *, int, struct file_lock *);
1212 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1213 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1214 int (*setlease)(struct file *, long, struct file_lock **);
1215 };
其中的重要参数解释如下:
按照惯例,file_operations结构或指向这类结构的指针称为fops(或者相关的其他叫法)。这个结构体中的每一个字段都必须指向驱动程序中实现特定操作的函数,对于不支持的操作,对应的字段可以设置为NULL