Linux--文件结构体struct file
struct file *f_next,**f_pprev;
struct dentry *f_dentry;
struct file_operations *f_op;
mode_t f_mode;
loff_t f_pos;
unsigned int f_count,f_flags;
unsigned long f_reada,f_ramax,f_raend,f_ralen,f_rawin;
struct fown_struct f_owner;
unsigned long f_version;
void *private_data;
};
- 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;
- atomic_t f_count;
- unsigned int f_flags;
- mode_t f_mode;
- loff_t f_pos;
- struct fown_struct f_owner;
- unsigned int f_uid, f_gid;
- struct file_ra_state f_ra;
- unsigned long 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;
- };
文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的struct file。它由内核在打开文件时创建,并传递给在文件上进行操作的任何函数。在文件的所有实例都关闭后,内核释放这个数据结构。在内核创建和驱动源码中,struct file的指针通常被命名为file或filp。一下是对结构中的每个数据成员的解释:
一、
union {
struct list_head fu_list;
struct rcu_head rcuhead;
}f_u;
其中的struct list_head定义在 linux/include/linux/list.h中,原型为:
struct list_head {
struct list_head *next, *prev;
};
用于通用文件对象链表的指针。
struct rcu_head定义在linux/include/linux/rcupdate.h中,其原型为:
- /**
- * struct rcu_head - callback structure for use with RCU
- * @next: next update requests in a list
- * @func: actual update function to call after the grace period.
- */
- struct rcu_head {
- struct rcu_head *next;
- void (*func)(struct rcu_head *head);
- };
RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制,具体在这里有介绍:
http://www.ibm.com/developerworks/cn/linux/l-rcu/
二、
struct path f_path;
被定义在linux/include/linux/namei.h中,其原型为:
struct path {
struct vfsmount *mnt;
struct dentry *dentry;
};
在早些版本的内核中并没有此结构,而是直接将path的两个数据成员作为struct file的数据成员,
struct vfsmount *mnt的作用是指出该文件的已安装的文件系统,
struct dentry *dentry是与文件相关的目录项对象。
三、
const struct file_operations *f_op;
被定义在linux/include/linux/fs.h中,其中包含着与文件关联的操作,如:
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 *);
等。当打开一个文件时,内核就创建一个与该文件相关联的struct file结构,其中的*f_op就指向的是
具体对该文件进行操作的函数。例如用户调用系统调用read来读取该文件的内容时,那么系统调用read最终会陷入内核调用sys_read函数,而sys_read最终会调用于该文件关联的struct file结构中的f_op->read函数对文件内容进行读取。
四、
atomic_t f_count;
atomic_t被定义为:
typedef struct { volatile int counter; } atomic_t;
volatile修饰字段告诉gcc不要对该类型的数据做优化处理,对它的访问都是对内存的访问,而不是对寄存器的访问。
本质是int类型,之所以这样写是让编译器对基于该类型变量的操作进行严格的类型检查。此处f_count的作用是记录对文件对象的引用计数,也即当前有多少个进程在使用该文件。
五、
unsigned int f_flags;
当打开文件时指定的标志,对应系统调用open的int flags参数。驱动程序为了支持非阻塞型操作需要检查这个标志。
六、
mode_t f_mode;
对文件的读写模式,对应系统调用open的mod_t mode参数。如果驱动程序需要这个值,可以直接读取这个字段。
mod_t被定义为:
typedef unsigned int __kernel_mode_t;
typedef __kernel_mode_t mode_t;
七、
loff_t f_pos;
当前的文件指针位置,即文件的读写位置。
loff_t被定义为:
typedef long long __kernel_loff_t;
typedef __kernel_loff_t loff_t;
八、
- struct fown_struct f_owner;
- struct fown_struct在linux/include/linux/fs.h被定义,原型为:
- struct fown_struct {
- rwlock_t lock; /* protects pid, uid, euid fields */
- struct pid *pid; /* pid or -pgrp where SIGIO should be sent */
- enum pid_type pid_type; /* Kind of process group SIGIO should be sent to */
- uid_t uid, euid; /* uid/euid of process setting the owner */
- int signum; /* posix.1b rt signal to be delivered on IO */
- };
该结构的作用是通过信号进行I/O时间通知的数据。
九、
unsigned int f_uid, f_gid;
标识文件的所有者id,所有者所在组的id.
十、
struct file_ra_state f_ra;
struct file_ra_state结构被定义在/linux/include/linux/fs.h中,原型为:
- struct file_ra_state {
- pgoff_t start; /* where readahead started */
- unsigned long size; /* # of readahead pages */
- unsigned long async_size; /* do asynchronous readahead when
- there are only # of pages ahead */
- unsigned long ra_pages; /* Maximum readahead window */
- unsigned long mmap_hit; /* Cache hit stat for mmap accesses */
- unsigned long mmap_miss; /* Cache miss stat for mmap accesses */
- unsigned long prev_index; /* Cache last read() position */
- unsigned int prev_offset; /* Offset where last read() ended in a page */
- };
文件预读状态,文件预读算法使用的主要数据结构,当打开一个文件时,f_ra中出了perv_page(默认为-1)和ra_apges(对该文件允许的最大预读量)这两个字段外,其他的所有西端都置为0。
十一、
unsigned long f_version;
记录文件的版本号,每次使用后都自动递增。
十二、
#ifdef CONFIG_SECURITY
void *f_security;
#endif
此处我的理解是如果在编译内核时配置了安全措施,那么struct file结构中就会有void *f_security数据项,用来描述安全措施或者是记录与安全有关的信息。
十三、
void *private_data;
系统在调用驱动程序的open方法前将这个指针置为NULL。驱动程序可以将这个字段用于任意目的,也可以忽略这个字段。驱动程序可以用这个字段指向已分配的数据,但是一定要在内核释放file结构前的release方法中清除它。
十四、
#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 */
被用在fs/eventpoll.c来链接所有钩到这个文件上。其中f_ep_links是文件的事件轮询等待者链表的头,f_ep_lock是保护f_ep_links链表的自旋锁。
十五、struct address_space *f_mapping;
struct address_space被定义在/linux/include/linux/fs.h中,此处是指向文件地址空间的指针。
在驱动开发中,文件读/写模式mode、标志f_flags都是设备驱动关心的内容,而私有数据指针private_data在折本驱动中被广泛使用,大多被指向设备驱动自定义用于描述设备的结构体。
驱动程序中常用如下类似的代码来检测用户打开文件的读写方式:
if (file->f_mode & FMODE_WRITE) //用户要求可写
{
}
if (file->f_mode & FMODE_READ) //用户要求可读
{
}
下面的代码可用于判断以阻塞还是非阻塞方式打开设备文件:
if (file->f_flags & O_NONBLOCK) //非阻塞
pr_debug("open:non-blocking/n");
else //阻塞
pr_debug("open:blocking/n");
参考:
Linux--文件结构体struct file相关推荐
- linux 结构体 struct addrinfo 简介
目录 定义 相关方法 1. getaddrinfo(const char, const char, const struct addrinfo, struct addrinfo*) 2. freead ...
- linux创建文件结构体,Linux file 结构体和 inode 结构体,Go语言入门技术,Go语言基础...
在设备驱动程序中,一般需要关心两个结构体:file 和 inode. 1. file 结构体 file 结构体代表一个打开的文件,系统中每个打开的文件在内核空间都有一个关联的 struct file. ...
- Linux驱动程序中的file,inode,file_operations三大结构体
本文允许转载,但请标明出处:http://blog.csdn.net/u010944778/article/details/45077565 file_operations: 该结构是将系统调 ...
- linux sock结构体,struct socket结构体详解
在内核中为什么要有struct socket结构体呢? struct socket结构体的作用是什么? 下面这个图,我觉得可以回答以上两个问题. 由这个图可知,内核中的进程可以通过使用struct ...
- Linux下网络相关结构体 struct servent
Linux下网络相关结构体 struct servent 参考书籍:<UNIX环境高级编程> 参考链接: http://www.cnblogs.com/benxintuzi/p/45898 ...
- linux系统中struct timeval结构体、struct timezone结构体以及gettimeofday函数
格林尼治时间.协调世界时 间.世界时间.日光节约时间以及时区等介绍: 格林尼治时间(Greenwich Mean Time,GMT)是指位于英国伦敦郊区的皇家格林尼治天文台当地的标准时间,因为本初子午 ...
- linux 网络设备 if_net,struct net_device网络设备结构体详解
在linux中使用struct net_device结构体来描述每一个网络设备.同时这个用来刻画网络设备的struct net_device结构体包含的字段非常的多,以至于内核的开发者都觉得在现在的l ...
- linux 内核 struct file 获取文件名 全路径
获取文件名: struct file *filp; filp->f_path.dentry->d_iname 获取全路径: dentry_path_raw(filp->f_path. ...
- linux device结构体,struct device结构体
一.定义: linux/include/linux/device.h struct device { struct klist klist_children; struct klist_nod ...
最新文章
- 一道简单题目的优化过程——抽签问题
- Tensor Decomposition
- Unity Shader-描边效果
- OC第八节 内存管理高级
- 理解Promise (1)
- VS2013工具箱中使用WindowsMediaPlyer控件
- 如何从 SAP Fiori Elements List Report Table 点击事件响应函数里拿到表格某一行的信息
- spring boot注释_使用Spring Boot和注释支持配置Spring JMS应用程序
- 001. Ansible简介
- Githug第42关rebase_onto通关秘籍
- 【华为云技术分享】使用CloudIDE快速体验基于华为云ModelArts SDK的AI开发
- 联发科预计天玑系列5G智能手机处理器今年出货超4500万颗
- python encode和decode函数说明
- 基于vue的验证码组件
- php无法查询excel数据,laravel phpexcel无法读取excel中中文表头列数据
- mp2555sp文件服务器,理光mp2555sp驱动
- 微信小号来了,微信小号怎么申请(内附微信小号注册说明)
- VS2013附加包含目录,添加相对路径
- 关于VMBox重启无法打开虚拟机问题
- 多线程 join 钉钉考勤
热门文章
- Oil Deposit
- (转)[Android分享] Android中用Ant把ndk的so文件打包进apk
- 欺诈行为识别_使用R(编程)识别欺诈性的招聘广告
- leetcode 1208. 尽可能使字符串相等(滑动窗口)
- leetcode 1319. 连通网络的操作次数(并查集)
- leetcode1011. 在 D 天内送达包裹的能力(二分查找)
- leetcode129. 求根到叶子节点数字之和(dfs)
- 工具_HBuilder使用快捷方式
- 最大熵对应的概率分布
- 《The Pomodoro Technique》