驱动中重要的三个结构体介绍:struct inode、struct file、struct file_operations

1、struct inode结构体

struct inode {
	······
	struct hlist_node	i_hash;
	struct list_head	i_list;		/* backing dev IO list */
	struct list_head	i_sb_list;

	//主次设备号
	dev_t			i_rdev;

	struct list_head	i_devices;
	//用联合体是因为该文件可能是块设备文件或者字符设备文件
	union {
		struct pipe_inode_info	*i_pipe;	//管道文件
		struct block_device	*i_bdev;	//块设备文件
		struct cdev		*i_cdev;	//字符设备文件
	};
	
	//私有数据
	void			*i_private; /* fs or device private pointer */
};

struct inode结构体是用来表示一个静态文件的,每个文件都会对应唯一的struct inode结构体,结构体里描述了文件的详细信息。

2、struct file结构体

struct file {
	union {
		struct list_head	fu_list;
		struct rcu_head 	fu_rcuhead;
	} f_u;
  ······
  
	const struct file_operations	*f_op;	//该文件对应的操作方法
	
	unsigned int 		f_flags;	
	fmode_t			f_mode;	//打开文件的权限,比如:只读打开、只写打开、读写打开
	loff_t			f_pos;	//文件指针的偏移量
	
	/* needed for tty driver, and maybe others */
	void			*private_data;	//私有数据
};

struct file结构体 用来表示一个动态的设备,每当open打开一个文件时就会产生一个struct file结构体 与之对应。

3、struct 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 *);
	int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	int (*open) (struct inode *, struct file *);
	int (*release) (struct inode *, struct file *);
};

struct file_operations结构体是用来描述设备的操作方法的。在linux中一切皆是文件,不管是普通文件还是设备驱动文件上层应用都是用同一的open、read函数去操作。每一个struct file结构体 中都保存了一个struct file_operations结构体,虽然上层应用都是调用的read函数,但是在内部实际通过函数指针调用了不同的read函数。

4、struct cdev结构体

struct cdev {
	struct kobject kobj;
	struct module *owner;
	const struct file_operations *ops; //设备的操作方法
	struct list_head list;
	dev_t dev;	//主次设备号
	unsigned int count;	//引用计数
};

struct cdev结构体是用来描述一个字符设备的,每个字符设备都对应一个struct cdev结构体。

5、结构体之间的关联

5.1、struct inode结构体和struct file结构体

(1)struct inode结构体和struct file结构体 都是用来描述文件信息的,struct inode结构体是描述静态的文件,struct file结构体描述动态的文件(也就是打开的文件);
(2)每个文件只有唯一的struct inode结构体,但是可以有多个struct file结构体,文件每被打开一次就多一个 struct file结构体 ;

5.2、struct file_operations结构体和struct cdev结构体

(1)struct file_operations结构体描述设备的操作方法,struct cdev结构体描述字符设备全部的信息;
(2)struct cdev结构体包含struct file_operations结构体,在注册驱动时需要将struct file_operations结构体指针赋值给struct cdev结构体;

5.3、struct inode结构体和struct cdev结构体

(1)上层应用访问设备驱动是通过设备节点,设备节点就是一个文件,在创建设备节点时需要指明主次设备号,主次设备号就会保存在设备节点对应的 struct inode结构体的i_rdev变量中;
(2)在向内核注册字符设备驱动时就是将对应的struct cdev结构体注册到chrdevs全局变量中,其中struct cdev结构体就保存了主次设备号;
(3)struct inode结构体的联合体中有struct cdev结构体指针,将来找到对应的struct cdev结构体会对该指针赋值;
(4)联系:在用open打开设备节点时从struct inode结构体中获取初次设备号,然后用这个主次设备号去chrdevs全局变量中找到对应的struct cdev结构体。
总结:依靠主次设备号联系起来。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

正在起飞的蜗牛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值