文件对象

文件对象是进程已经打开文件描述符的内存中的表示,单个文件可能有多个表示打开文件描述符的file结构;

 struct file {
union {
struct llist_node fu_llist; /* 文件对象链表 */
struct rcu_head fu_rcuhead; /* 释放之后的RCU链表 */
} f_u;
/* 目录项 */
struct path f_path;
/* inode */
struct inode *f_inode; /* cached value */
/* 文件操作 */
const struct file_operations *f_op; /*
* Protects f_ep_links, f_flags.
* Must not be taken from IRQ context.
*/
/* 锁 */
spinlock_t f_lock;
/* 引用计数 */
atomic_long_t f_count;
/* 打开文件时所指定的标志 */
unsigned int f_flags;
/* 文件访问模式 */
fmode_t f_mode;
struct mutex f_pos_lock;
/* 文件当前偏移量 */
loff_t f_pos;
/* 拥有者通过信号进行异步IO数据传送 */
struct fown_struct f_owner;
/* 文件的信任状 */
const struct cred *f_cred;
/* 预读状态 */
struct file_ra_state f_ra;
/* 版本号 */
u64 f_version;
#ifdef CONFIG_SECURITY
/* 安全模块 */
void *f_security;
#endif
/* needed for tty driver, and maybe others */
/* tty设备驱动的钩子 */
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;
struct list_head f_tfile_llink;
#endif /* #ifdef CONFIG_EPOLL */
/* 页缓存映射 */
struct address_space *f_mapping;
} __attribute__((aligned())); /* lest something weird decides that 2 is OK */
文件操作

file_operations提供了文件操作函数,这些函数与系统调用进行关联;

 /* 文件操作 */
struct file_operations {
/* 拥有该模块的指针,通常被初始化为THIS_MODULE */
struct module *owner;
/*
用来修改文件的当前读写位置,并将新位置作为返回值,由系统调用lseek调用
参数2是一个"长偏移量";
参数3是SEEK_SET,SEEK_CUR,SEEK_END中的一个:
SEEK_SET-参数2设置为新的读写位置
SEEK_CUR-当前读写位置后增加参数2个偏移量
SEEK_END-读写位置指向文件尾后再增加参数2个偏移量
*/
loff_t (*llseek) (struct file *, loff_t, int);
/*
从给定文件的offset偏移出读取count字节数据到buf中,
同时更新文件指针,由系统调用read调用
*/
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
/*
从给定buf中读取count字节数据,写入给定文件offset偏移处,
同时更新文件指针,由系统调用write调用
*/
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
/* 同步异步读写 */
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *); int (*iterate) (struct file *, struct dir_context *);
int (*iterate_shared) (struct file *, struct dir_context *);
/* 睡眠等待给定文件活动,由系统调用poll调用 */
unsigned int (*poll) (struct file *, struct poll_table_struct *);
/*
用来给设备发送名称参数对,当文件是一个被打开的设备节点时,
可以通过它进行设置,由系统调用iotcl调用
*/
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
/*
是ioctl函数的可移植变种,被32位应用程序用在64位系统上,
新的驱动程序应该设计自己的ioctl命令,以便所有的驱动程序都是可移植的,
从而使得compat_ioctl和unlocked_ioctl指向同一个函数
*/
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
/* 将给定的文件映射到指定地址空间行,由系统调用mmap调用 */
int (*mmap) (struct file *, struct vm_area_struct *);
/* 创建一个新的文件对象,并将它和响应的索引节点对象关联起来,由系统调用open调用 */
int (*open) (struct inode *, struct file *);
/* 当已打开文件的引用计数减少时,该函数被VFS调用,它的作用根据具体文件系统而定 */
int (*flush) (struct file *, fl_owner_t id);
/*
当文件最后一个引用被注销时(如当最后一个共享文件描述符进程调用了close()或者退出),
该函数被VFS调用,它的作用根据具体文件系统而定 */
int (*release) (struct inode *, struct file *);
/* 将给定文件的所有被缓存数据写回磁盘,由系统调用fsync调用 */
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
/* 打开或者关闭异步IO的通告信号 */
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);
/* 允许模块检查传递给fcntl(F_SETFL...)调用的标志 */
int (*check_flags)(int);
/* 实现flock()系统调用,该调用提供忠告锁 */
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 **, void **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
unsigned (*mmap_capabilities)(struct file *);
#endif
ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
loff_t, size_t, unsigned int);
int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t,
u64);
ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *,
u64);
};
inode对象

内核用inode结构在内部表示文件,因此它和file结构不同,后者表示打开的文件描述符。对单个文件,可能会有多个表示打开文件描述符的file结构,但它们都指向单个inode结构;

inode结构中包含了大量有关文件的信息。作为常规,只有下面两个字段对编写驱动程序代码有用

dev_t i_rdev:

对表示设备文件的inode结构,该字段包含了真正的设备编号;

struct cdev *i_cdev:

表示字符设备的内核的内部结构,当inode指向一个字符设备文件时,该字段包含了指向struct cdev结构的指针;

从inode中获取主设备号和次设备号,使用下面函数,不要直接操作i_rdev:

 static inline unsigned iminor(const struct inode *inode)
{
return MINOR(inode->i_rdev);
} static inline unsigned imajor(const struct inode *inode)
{
return MAJOR(inode->i_rdev);
}

Linux设备驱动程序 之 重要数据结构的更多相关文章

  1. linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)

    原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html 驱动常用头文件介绍 #include <linux/***.h> 是 ...

  2. 【转】linux设备驱动程序之简单字符设备驱动

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/03/2272869.html 一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用 ...

  3. 【转】linux设备驱动程序中的阻塞机制

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275272.html 阻塞与非阻塞是设备访问的两种方式.在写阻塞与非阻塞的驱动程序时,经 ...

  4. Linux设备驱动程序学习----2.内核模块与应用程序的对比

    内核模块与应用程序的对比 更多内容请参考Linux设备驱动程序学习----目录 1. 内核模块与应用程序的对比 内核模块和应用程序之间的不同之处: 大多数中小规模的应用程序是从头到尾执行单个任务,而模 ...

  5. Linux设备驱动程序学习----3.模块的编译和装载

    模块的编译和装载 更多内容请参考Linux设备驱动程序学习----目录 1. 设置测试系统 第1步,要先从kernel.org的镜像网站上获取一个主线内核,并安装到自己的系统中,因为学习驱动程序的编写 ...

  6. Linux设备驱动程序 第三版 读书笔记(一)

    Linux设备驱动程序 第三版 读书笔记(一) Bob Zhang 2017.08.25 编写基本的Hello World模块 #include <linux/init.h> #inclu ...

  7. Linux设备驱动程序学习之分配内存

    内核为设备驱动提供了一个统一的内存管理接口,所以模块无需涉及分段和分页等问题. 我已经在第一个scull模块中使用了 kmalloc 和 kfree 来分配和释放内存空间. kmalloc 函数内幕 ...

  8. 教你写Linux设备驱动程序:一个简短的教程

    教你写Linux设备驱动程序:一个简短的教程 http://blog.chinaunix.net/uid-20799298-id-99675.html

  9. linux设备驱动程序_hello word 模块编译各种问题集锦

    在看楼经典书籍<linux设备驱动程序>后,第一个程序就是编写一个hello word 模块. 原以为非常easy,真正弄起来,发现问题不少啊.前两天编过一次,因为没有记录,今天看的时候又 ...

随机推荐

  1. Boost,Eigen,Flann—C++标准库预备役

    Boost,Eigen,Flann—C++标准库预备役 第一预备役:Boost      Boost库是为C++语言标准库提供扩展的一些C++程序库的总称. Boost库由Boost社区组织开发.维护 ...

  2. java实现判定新旧版本号

    废话不多说,直接上代码 /** * 判断是否为最新版本方法 将版本号根据.切分为int数组 比较 * * @param localVersion 本地版本号 * @param onlineVersio ...

  3. NPOI 将excel转换为datatable或者将datatable转换为excel

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. LED点阵显示

    /*********************************************************** 8*8LED点阵---显示数字实验 实现现象:下载程序后点阵上显示数字0 注意 ...

  5. ubuntu安装交叉编译器

    # sudo apt-get install gcc-arm-linux-gnueabi # sudo apt-get install g++-arm-linux-gnueabi 官方下载 https ...

  6. pymysql_mysql密码重置方法,连接局域网数据库的解决办法

    https://blog.csdn.net/qq_37176126/article/details/72824106   pymysql模块的操作 https://blog.csdn.net/skh2 ...

  7. vue-element-admin后台的安装

    # 克隆项目 git clone https://github.com/PanJiaChen/vue-element-admin.git # 进入项目目录 cd vue-element-admin # ...

  8. rdb和aof二种持久化方式对比(Redis)

    我们已经知道对于一个企业级的redis架构来说,持久化是不可减少的 企业级redis集群架构:海量数据.高并发.高可用 持久化主要是做灾难恢复,数据恢复,也可以归类到高可用的一个环节里面去 比如你re ...

  9. ubuntu系统---切换Py2.X与Py3.X版本

    ubuntu系统---切换Python2.X与Python3.X版本 Python3.X将成为以后的趋势,Python2.X当前用的稍多的版本,但现在不再更新了.因此,小主电脑里也安装了好两个版本的p ...

  10. Matlab---三维视图的自动旋转

    Matlab---三维视图的自动旋转 这里给出一种格式说明: % figure % plot3(...); % xlabel('X轴'); % ylabel('Y轴'); % zlabel('Z轴') ...