浅谈file_operations结构体

1.概述

在Linux下,编写驱动程序实际上是实现对设备文件对应操作函数的编写,而这些操作函数是结构体file_operations中函数指针所指函数的具体实现。file_operations在Linux系统下的 /usr/include/linux/fs.h 中可以找到定义,该结构体的每个域都对应着驱动内核模块用来处理某个被请求的事件的函数的地址。(ps:这其实也就是函数指针的功能)

2.file_operations结构体

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 *);
	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 *, loff_t, loff_t, 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);
	int (*show_fdinfo)(struct seq_file *m, struct file *f);
};

其中比较常用的函数有:
第1行:owner拥有该拥有该结构体的模块指针,一般设置为 THIS_MODULE,防止模块还在被使用的时候被卸载。
第2行:lseek函数用于修改文件当前的读写位置。
第3行:read函数用于读取设备文件。
第4行:write函数用于向设备文件写入(发送 )数据。
第13行:open函数用于打开设备文件。
第15行:release函数用于释放 函数用于释放 函数用于释放 (关闭 )设备文件,与应用程序中的close()函数对应。

3.实现

具体哪些函数还看具体的驱动要求,一般最常用的是open、release、write、read等函数,这些函数也是必须要实现的。拿最简单的字符设备驱动开发举一个例子,其file_operations结构体的例化如下:

static struct file_operations test_fops = {
		.owner = THIS_MODULE,
		.open = chrtest_open,
		.read = chrtest_read,
		.write = chrtest_write,
		.release = chrtest_release,
}:

剩下的就是要根据具体是需求添加进去并实现了,具体的函数实现需要结合硬件信息以及相应的接口信息。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来鸟 鸣间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值