各位大神,谁能跟我讲讲file_Operation结构里面各个函数的具体用法,特别是函数参数的用法,感激不尽
3条回答 默认 最新
- JansonZhe 2015-08-07 07:10关注
file_operation是Linux内核驱动中一个非常重要的结构体,它建立了用户空间与内核空间之间的联系。file_operation结构体定义在linux内核的include\linux\fs.h文件中,定义是这样的:
```struct file_operations {
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,里面会有详细介绍哦。
解决 无用评论 打赏 举报
悬赏问题
- ¥20 delta降尺度方法,未来数据怎么降尺度
- ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
- ¥15 高德地图点聚合中Marker的位置无法实时更新
- ¥15 DIFY API Endpoint 问题。
- ¥20 sub地址DHCP问题
- ¥15 delta降尺度计算的一些细节,有偿
- ¥15 Arduino红外遥控代码有问题
- ¥15 数值计算离散正交多项式
- ¥30 数值计算均差系数编程
- ¥15 redis-full-check比较 两个集群的数据出错