file 结构体和 inode 结构体
(1)struct file 结构体定义在 include/linux/fs.h 中定义。文件结构体代表一个打开的文件,
系统中的每个打开的文件在内核空间都有一个关联的 struct file。它由内核在打开文件时创
建,并传递给在文件上进行操作的任何函数。在文件的所有实例都关闭后,内核释放这个
数据结构。在内核创建和驱动源码中,struct file 的指针通常被命名为 file 或 filp。如下所示:
struct file {
union {
struct list_head fu_list; 文件对象链表指针 linux/include/linux/list.h
struct rcu_head fu_rcuhead; RCU(Read-Copy Update)是 Linux 2.6 内核中新的锁机制
} f_u;
struct path f_path; 包含 dentry 和 mnt 两个成员,用于确定文件路径
#define f_dentry f_path.dentry f_path 的成员之一,当前文件的 dentry 结构
#define f_vfsmnt f_path.mnt 表示当前文件所在文件系统的挂载根目录
const struct file_operations *f_op; 与该文件相关联的操作函数
atomic_t f_count; 文件的引用计数(有多少进程打开该文件)
unsigned int f_flags; 对应于 open 时指定的 flag
mode_t f_mode; 读写模式:open 的 mod_t mode 参数
off_t f_pos; 该文件在当前进程中的文件偏移量
struct fown_struct f_owner; 该结构的作用是通过信号进行 I/O 时间通知的数据。
unsigned int f_uid, f_gid; 文件所有者 id,所有者组 id
struct file_ra_state f_ra; 在 linux/include/linux/fs.h 中定义,文件预读相关
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;
};
(2)struct dentry
dentry 的中文名称是目录项,是 Linux 文件系统中某个索引节点(inode)的链接。这个索引节
点可以是文件,也可以是目录。inode(可理解为 ext2 inode)对应于物理磁盘上的具体对
象,dentry 是一个内存实体,其中的 d_inode 成员指向对应的 inode。也就是说,一个 inode
可以在运行的时候链接多个 dentry,而 d_count 记录了这个链接的数量。
struct dentry {
atomic_t d_count; 目录项对象使用计数器,可以有未使用态,使用态和负状态
unsigned int d_flags; 目录项标志
struct inode * d_inode; 与文件名关联的索引节点
struct dentry * d_parent; 父目录的目录项对象
评论1