字符设备驱动程序中重要的三个数据结构file_operations、inode、file
在学习字符设备驱动的开始,我们必须了解的是三个很重要的数据结构,他们分别是file_operations、inode、file。下面陶毛毛同学就和大家一起来学习这三个数据结构。
struct _file_operations在Fs.h这个文件里面被定义的,如下所示:
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 (*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);//仅用于读取目录,对于设备文件,该字段为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 *, fl_owner_t id);
int (*release) (struct inode *, struct file *); //关闭
int (*fsync) (struct file *, struct dentry *, int datasync); //刷新待处理的数据
int (*aio_fsync) (struct kiocb *, int datasync); //异步刷新待处理的数据
int (*fasync) (int, struct file *, int); //通知设备FASYNC标志发生变化
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 **);
};Linux使用file_operations结构访问驱动程序的函数,这个结构的每一个成员的名字都对应着一个调用。用户进程利用在对设备文件进行诸如read/write操作的时候,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交给该函数,这是Linux的设备驱动程序工作的基本原理。
下面是struct inode结构体。
struct inode {
struct hlist_node i_hash; //哈希表
struct list_head i_list; //索引节点链表
struct list_head i_sb_list;//目录项链表
struct list_head i_dentry;
unsigned long i_ino; //节点号
atomic_t i_count; //引用记数
unsigned int i_nlink; //硬链接数
uid_t i_uid; /*使用者id */
gid_t i_gid; /*使用者id组*/
dev_t i_rdev; //该成员表示设备文件的inode结构,它包含了真正的设备编号。
u64 i_version;
loff_t i_size; /*inode多代表的文件的大小*/
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t i_size_seqcount;
#endif
struct timespec i_atime; /*inode最后一次存取的时间*/
struct timespec i_mtime; /*inode最后一次修改的时间*/
struct timespec i_ctime; /*inode的创建时间*/
unsigned int i_blkbits; /*inode在做I/O时的区块大小*/
blkcnt_t i_blocks; /*inode所石油的block块数*/
unsigned short i_bytes;
umode_t i_mode; /*inode的权限*/
spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
struct mutex i_mutex;
struct rw_semaphore i_alloc_sem;
const struct inode_operations *i_op;
const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
struct super_block *i_sb;
struct file_lock *i_flock;
struct address_space *i_mapping;
struct address_space i_data;
#ifdef CONFIG_QUOTA
struct dquot *i_dquot[MAXQUOTAS];
#endif
struct list_head i_devices; /*若是字符设备,为其对应的cdev结构体指针*/
union {
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev; /*若是块设备,为其对应的cdev结构体指针*/
struct cdev *i_cdev; //该成员表示字符设备的内核的 内部结构。当inode指向一个字符设备文件时,该成员包含了指向struct cdev结构的指针,其中cdev结构是字符设备结构体。
};
int i_cindex;
__u32 i_generation;
#ifdef CONFIG_DNOTIFY
unsigned long i_dnotify_mask; /* Directory notify events */
struct dnotify_struct *i_dnotify; /* for directory notifications */
#endif
#ifdef CONFIG_INOTIFY
struct list_head inotify_watches; /* watches on this inode */
struct mutex inotify_mutex; /* protects the watches list */
#endif
unsigned long i_state;
unsigned long dirtied_when; /* jiffies of first dirtying */
unsigned int i_flags;
atomic_t i_writecount;
#ifdef CONFIG_SECURITY
void *i_security;
#endif
void *i_private; /* fs or device private pointer */
};
内核中用inode结构表示具体的文件,也就是对应与硬盘上面具体的文件。提供了设备文件的信息。inode译成中文就是索引节点。每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘 ... ... )被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。而inode呢,就是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件。
最后是struct file。
struct file {
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head fu_list;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
#define f_dentry f_path.dentry //该成员是对应的 目录结构 。
#define f_vfsmnt f_path.mnt
const struct file_operations *f_op; //该操作 是定义文件关联的操作的。内核在执行open时对这个指针赋值。
atomic_long_t f_count;//文件的引用计数(有多少进程打开该文件)
unsigned int f_flags; //该成员是文件标志。
mode_t f_mode;//读写模式:open的mod_t mode参数
loff_t f_pos;//该文件在当前进程中的文件偏移量
struct fown_struct f_owner;//该结构的作用是通过信号进行I/O时间通知的数据
unsigned int f_uid, f_gid;//文件所有者id,所有者组id
struct file_ra_state f_ra;
u64 f_version;
#ifdef CONFIG_SECURITY
void *f_security;
#endif
/* needed for tty driver, and maybe others */
void *private_data;//该成员是系统调用时保存状态信息非常有用的资源。
#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links;
spinlock_t f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};
文件结构代表一个打开的文件描述符,它不是专门给驱动程序使用的,系统中每一个打开的文件在内核中都有一个关联的struct file。它由内核在open时创建,并传递给在文件上操作的任何函数,知道最后关闭。当文件的所有实例都关闭之后,内核释放这个数据结构。
最后:总结一下,struct operations存在于设备驱动程序内部,起着联系应用程序和设备驱动的作用。struct inode存在于磁盘上,作为描述设备驱动文件的信息的作用。struct file是在执行open函数时产生的,每打开一个文件就产生一个struct file,供设备驱动关联的函数使用。
转载于:https://my.oschina.net/uvwxyz/blog/102814
字符设备驱动程序中重要的三个数据结构file_operations、inode、file相关推荐
- linux设备驱动程序中的阻塞机制
阻塞与非阻塞是设备访问的两种方式.在写阻塞与非阻塞的驱动程序时,经常用到等待队列. 一.阻塞与非阻塞 阻塞调用是指调用结果返回之前,当前线程会被挂起,函数只有在得到结果之后才会返回. 非阻塞指不能立刻 ...
- 字符设备驱动程序——点亮、熄灭LED操作
2019独角兽企业重金招聘Python工程师标准>>> 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流的设 ...
- Linux驱动实践:你知道【字符设备驱动程序】的两种写法吗?
作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...
- 字符设备驱动程序框架
1, 设备号的内部表示形式 类型:dev_t 32=12(主设备号) + 20(次设备号) 相关宏:<linux/kdev_t.h> MAJOR(dev_t dev) MINOR(dev_ ...
- linux驱动开发字符设备,linux驱动开发(三) 字符设备驱动框架
还是老规矩先上代码 demo.c #include #include#include#include#include int demo_major = 250;int demo_minor = 0;i ...
- 字符设备驱动0:一个简单但完整的字符设备驱动程序
参考: linux设备驱动程序之简单字符设备驱动 [很详细,必看]http://www.cnblogs.com/geneil/archive/2011/12/03/2272869.html //在驱动 ...
- 第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进
第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进 //仅用flash上的根文件系统启动后,手工MOUNT NFS mount -t nfs -o nolock,vers=2 192.16 ...
- 字符设备驱动程序的使用
1.编译.安装驱动 linux系统中,驱动程序通常采用内核模块的程序结构来进行编码,因此,编译.安装一个驱动程序,其实质就是编译.安装一个内核模块. 将文件memdev.c makefile 放入虚拟 ...
- i.MX6ULL学习笔记--字符设备驱动程序
i.MX6ULL学习笔记--字符设备驱动程序 简介 1.驱动的配置过程 1.1设备号 1.2哈希表-chrdevs 1.3哈希表-obj_map->probes 1.4文件操作接口 1.5简单了 ...
- Linux字符设备驱动程序开发(1)-使用字符设备驱动
1.编译/安装驱动 在Linux系统中,驱动程序通常采用内核模块的程序结构来进行编码.因此,编译/安装一个驱动程序,其实质就是编译/安装一个内核模块.把下面的范例代码拷贝到Linux系统中: memd ...
最新文章
- hibernate连接池配置
- powerdesigner 同步mysql 报错_PowerDesigner技巧小结
- 使用wireshark分析tcp/ip报文之报文头
- python concat_python中merge、concat用法
- Qt文档阅读笔记-QScopedPointer解析及实例
- 成功解决“ValueError: Unknown metric function:sensitivity”
- 一些css3简单的练习
- 我打败了妈妈 - 张朔
- matlab2c使用c++实现matlab函数系列教程-flipud函数
- hive join 数据倾斜 真实案例
- 计算机组成原理知识点总结白中英,计算机组成原理知识点总结.pdf
- OFFICE专业增强版2016 32位安装VISIO出错记录
- VS2010中自制8*8字模提取
- 中央集成管理系统服务器,一种用于PCB生产设备的CIM集成中央管理系统
- 何夕 - 六道众生·何夕科幻自选集(2014年1月31日)
- APtos 简介及机制
- 透视矩阵的推导(最直观、最深入、最还原,看完请点赞。)
- win10删除Windows更新和预览的版本,关闭自动更新
- 搭建小说系统源码,如何实现读书的分页功能
- 分享腾讯官方二维码生成接口地址