各位大神,谁能跟我讲讲file_Operation结构里面各个函数的具体用法,特别是函数参数的用法,感激不尽
file_operation是Linux内核驱动中一个非常重要的结构体,它建立了用户空间与内核空间之间的联系。file_operation结构体定义在linux内核的includelinuxfs.h文件中,定义是这样的:
struct module owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
/ remove by cym 20130408 support for MT660.ko /
#if 0
//#ifdef CONFIG_SMM6260_MODEM
#if 1// liang, Pixtree also need to use ioctl interface...
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
#endif
#endif
/ end remove /
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
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);
/ add by cym 20130408 support for MT6260 and Pixtree /
#if defined(CONFIG_SMM6260_MODEM) || defined(CONFIG_USE_GPIO_AS_I2C)
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
#endif
/ end add */
};
由定义可以看出,该结构体里面定义了许多的函数指针,例如我们在写驱动程序时经常会用到的open、ioctl、read、write等。同时也指明了函数指针里面的参数,这也是为什么我们在写Linux驱动程序的open等函数时,里面的参数都是固定的,不能自己额外添加。当我们在驱动程序中完成open、close等函数之后,就必须在file_operation里面进行映射,使file_operation里面的函数指针一一对应你自己编写的open、colse等方法。这样我们在用户空间中就可以通过调用close、open等函数来调用Linux内核你自己编写的驱动程序啦,不过要成功调用这些函数,你还必须include<fcntl.h>头文件,关于这部分的内容,你可以详细阅读linux的VFS,里面会有详细介绍哦。
可以参考《Linux设备驱动开发详解_宋宝华》这本书里面的
struct file _ operations
{
struct module owner;
// 拥有该结构的模块的指针,一般为 THIS _ MODULES
loff _ t(*llseek)(struct file *, loff _ t, int);
// 用来修改文件当前的读写位置
ssize _ t(*read)(struct file *, char _ _ user *, size _ t, loff _ t);
// 从设备中同步读取数据
ssize _ t(*aio _ read)(struct kiocb , char _ _ user *, size _ t, loff _ t);
// 初始化一个异步的读取操作
ssize _ t(*write)(struct file *, const char _ _ user *, size _ t,
loff _ t);
// 向设备发送数据
ssize _ t(*aio _ write)(struct kiocb , const char _ _ user *, size _ t,
loff _ t);
// 初始化一个异步的写入操作
int(*readdir)(struct file *, void *, filldir _ t);
// 仅用于读取目录,对于设备文件,该字段为 NULL
unsigned int(*poll)(struct file *, struct poll _ table _ struct);
// 轮询函数,判断目前是否可以进行非阻塞的读取或写入
int(*ioctl)(struct inode , struct file *, unsigned int, unsigned
long);
// 执行设备 I/O 控制命令
long(*unlocked _ ioctl)(struct file *, unsigned int, unsigned long);
// 不使用 BLK 文件系统,将使用此种函数指针代替 ioctl
long(*compat _ ioctl)(struct file *, unsigned int, unsigned long);
// 在 64 位系统上,32 位的 ioctl 调用将使用此函数指针代替
int(*mmap)(struct file *, struct vm _ area _ struct);
// 用于请求将设备内存映射到进程地址空间
int(*open)(struct inode , struct file);
// 打开
int(*flush)(struct file*);
int(*release)(struct inode , struct file);
// 关闭
int(*synch)(struct file , struct dentry *, int datasync);
// 刷新待处理的数据
int(*aio _ fsync)(struct kiocb *, int datasync);
// 异步 fsync
int(*fasync)(int, struct file *, int);
// 通知设备 FASYNC 标志发生变化
int(*lock)(struct file *, int, struct file _ lock);
ssize _ t(*readv)(struct file , const struct iovec *, unsigned long,
loff _ t);
ssize _ t(*writev)(struct file , const struct iovec *, unsigned long,
loff _ t);
// readv 和 writev:分散/聚集型的读写操作
ssize _ t(*sendfile)(struct file , loff _ t *, size _ t, read _ actor _ t,
void);
// 通常为 NULL
ssize _ t(*sendpage)(struct file , struct page *, int, size _ t,
loff _ t *, int);
// 通常为 NULL
unsigned long(*get _ unmapped _ area)(struct file *,unsigned long,
unsigned long,
unsigned long, unsigned long);
// 在进程地址空间找到一个将底层设备中的内存段映射的位置
int(*check _ flags)(int);
// 允许模块检查传递给 fcntl(F _ SETEL...)调用的标志
int(*dir _ notify)(struct file *filp, unsigned long arg);
// 仅对文件系统有效,驱动程序不必实现
int(*flock)(struct file *, int, struct file _ lock);
};
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。