【驱动】Linux驱动程序中的file,inode,file_operations三大结构体

这三个结构体均在文件linux-2.6.22.6>include>linux>fs.h中定义,大部分驱动程序操作都涉及三个重要的内核数据结构,分别是file_operations,file,inode。第一个是文件操作,file_operations结构就是用来连接驱动程序操作连接到我们前面给自己保留的编号的工作的。结构定义在<linux/fs.h>中,其中包含一组指针,每个打开的文件和一组函数关联(通过包含指向一个file_operations结构的f_op字段)。这些操作主要用来实现系统调用,也可以说文件可以认为是一个对象,而操作它的函数是方法。

惯例,file_operations 结构或者指向这类结构的指针称为fops,这个结构中每个字段都必须指向驱动中实现特定操作的函数。

对于不支持的操作,可以对应的字段设置为NULL。

file结构是设备驱动程序所使用的第二个重要的数据结构。它是一个内核结构,不会出现在用户程序中。它不仅仅代表一个打开的文件。它由内核在open时创建,并传递给该文件上进行操作的所有函数,知道最后的close函数,在文件的所有实例都被关闭后,内核会释放这个数据结构。

inode结构它表示打开的文件描述符。他包含了大量有关文件的信息。而只有 dev_t i_rdev;     struct cdev *i_cdev 与驱动程序代码有关用。
前者表示了设备文件的inode结构,包含了真正的设备编号,而后者表示字符设备的内核的内部结构,当其指向一个字符设备文件时,则包含了指向struct cdev结构的指针。

 

三者之间关系:
    struct file结构体中包含有struct file_operations结构体,struct file_operations是struct file的一个域;我们在使用系统调用open()打开一个设备节点struct inode时,我们会得到一个文件struct file,同时返回一个文件描述符,该文件描述符是一个整数,我们称之为句柄,通过访问句柄我们能够访问设备文件struct file,描述符是一个有着特殊含义的整数,特定位都有一定的意义或属性。
---------------------
作者:SkyHandy
来源:CSDN
原文:https://blog.csdn.net/u010944778/article/details/45077565
版权声明:本文为博主原创文章,转载请附上博文链接!

 

1. struct  file_operations{ ... }结构体

 1 /*
 2  * NOTE:
 3  * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
 4  * can be called without the big kernel lock held in all filesystems.
 5  */
 6 struct file_operations {
 7     struct module *owner;
 8     loff_t (*llseek) (struct file *, loff_t, int);
 9     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
10     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
11     ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
12     ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
13     int (*readdir) (struct file *, void *, filldir_t);
14     unsigned int (*poll) (struct file *, struct poll_table_struct *);
15     int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
16     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
17     long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
18     int (*mmap) (struct file *, struct vm_area_struct *);
19     int (*open) (struct inode *, struct file *);
20     int (*flush) (struct file *, fl_owner_t id);
21     int (*release) (struct inode *, struct file *);
22     int (*fsync) (struct file *, struct dentry *, int datasync);
23     int (*aio_fsync) (struct kiocb *, int datasync);
24     int (*fasync) (int, struct file *, int);
25     int (*lock) (struct file *, int, struct file_lock *);
26     ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
27     ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
28     unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
29     int (*check_flags)(int);
30     int (*dir_notify)(struct file *filp, unsigned long arg);
31     int (*flock) (struct file *, int, struct file_lock *);
32     ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
33     ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
34 };

 

 

2.struct  file{ ... }结构体

 1 struct file 
 2 {
 3     /*
 4      * fu_list becomes invalid after file_free is called and queued via
 5      * fu_rcuhead for RCU freeing
 6      */
 7     union 
 8     {
 9         struct list_head    fu_list;
10         struct rcu_head     fu_rcuhead;
11     } f_u;
12     struct path        f_path;
13 #define f_dentry    f_path.dentry
14 #define f_vfsmnt    f_path.mnt
15     const struct file_operations    *f_op;
16 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值