1.每个文件进程控制块中都有一份文件描述符表(可以把它看成是一个数组,里面的元素是指向file结构体指针类型),这个数组的下标就是文件描述符。
2.file结构体中有打开文件的权限,当前读写位置,引用计数等属性。每个file结构体都指向一个file_operations结构体,这个结构体的成员都是函数指针,指向实现各种文件操作的内核函数。
3.每个file结构体都有一个指向dentry结构体的指针。为了减少读盘次数,内核缓存了目录的树状结构,称为dentry cache,其中每个节点是一个dentry结构体
4.每个dentry结构体都有一个指针指向inode结构体。
5.inode结构体中保存着从磁盘分区的inode读上来信息,例如所有者、文件大小、文件类型和权限位等。
6.每个inode结构体都有一个指向inode_operations结构体的指针。
7.inode_operations是一组函数指针指向一些完成文件目录操作的内核函数。属于同一文件系统的各inode结构体可以指向同一个inode_operations结构体。
8.inode结构体有一个指向super_block结构体的指针。
9.super_block结构体保存着从磁盘分区的超级块读上来的信息。
名称 | 代码实现 |
struct file file结构体
| 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; };
|
struct dentry dentry结构体
| struct dentry { atomic_t d_count; 目录项对象使用计数器,可以有未使用态,使用态和负状态 unsigned int d_flags; 目录项标志 struct inode * d_inode; 与文件名关联的索引节点 struct dentry * d_parent; 父目录的目录项对象 struct list_head d_hash; 散列表表项的指针 struct list_head d_lru; 未使用链表的指针 struct list_head d_child; 父目录中目录项对象的链表的指针 struct list_head d_subdirs; 对目录而言,表示子目录目录项对象的链表 struct list_head d_alias; 相关索引节点(别名)的链表 int d_mounted; 对于安装点而言,表示被安装文件系统根项 struct qstr d_name; 文件名 unsigned long d_time; /* used by d_revalidate */ struct dentry_operations *d_op; 目录项方法 struct super_block * d_sb; 文件的超级块对象 vunsigned long d_vfs_flags; void * d_fsdata; 与文件系统相关的数据 unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名 }; |
struct files_struct 文件描述符表 | 对于每个进程,包含一个files_struct结构,用来记录文件描述符的使用情况,定义在include/linux/file.h中 struct files_struct { atomic_t count; 使用该表的进程数 struct fdtable *fdt; struct fdtable fdtab; spinlock_t file_lock ____cacheline_aligned_in_smp; int next_fd; 数值最小的最近关闭文件的文件描述符,下一个可用的文件描述符 struct embedded_fd_set close_on_exec_init; 执行exec时需要关闭的文件描述符初值集合 struct embedded_fd_set open_fds_init; 文件描述符的屏蔽字初值集合 struct file * fd_array[NR_OPEN_DEFAULT]; 默认打开的fd队列 }; struct fdtable { unsigned int max_fds; struct file ** fd; 指向打开的文件描述符列表的指针,开始的时候指向fd_array, 当超过max_fds时,重新分配地址 fd_set *close_on_exec; 执行exec需要关闭的文件描述符位图(fork,exec即不被子进程继承的文件 描述符) fd_set *open_fds; 打开的文件描述符位图 struct rcu_head rcu; struct fdtable *next; }; 1.4 struct fs_struct struct fs_struct { atomic_t count; 计数器 rwlock_t lock; 读写锁 int umask; struct dentry * root, * pwd, * altroot;根目录("/"),当前目录以及替换根目录 struct vfsmount * rootmnt, * pwdmnt, * altrootmnt; };
|
struct inode | struct inode { struct hlist_node i_hash; 哈希表 struct list_head i_list; 索引节点链表 struct list_head i_dentry; 目录项链表 unsigned long i_ino; 节点号 atomic_t i_count; 引用记数 umode_t i_mode; 访问权限控制 unsigned int i_nlink; 硬链接数 uid_t i_uid; 使用者id gid_t i_gid; 使用者id组 kdev_t i_rdev; 实设备标识符 loff_t i_size; 以字节为单位的文件大小 struct timespec i_atime; 最后访问时间 struct timespec i_mtime; 最后修改(modify)时间 struct timespec i_ctime; 最后改变(change)时间 unsigned int i_blkbits; 以位为单位的块大小 unsigned long i_blksize; 以字节为单位的块大小 unsigned long i_version; 版本号 unsigned long i_blocks; 文件的块数 unsigned short i_bytes; 使用的字节数 spinlock_t i_lock; 自旋锁 struct rw_semaphore i_alloc_sem; 索引节点信号量 struct inode_operations *i_op; 索引节点操作表 struct file_operations *i_fop; 默认的索引节点操作 struct super_block *i_sb; 相关的超级块 struct file_lock *i_flock; 文件锁链表 struct address_space *i_mapping; 相关的地址映射 struct address_space i_data; 设备地址映射 struct dquot *i_dquot[MAXQUOTAS];节点的磁盘限额 struct list_head i_devices; 块设备链表 struct pipe_inode_info *i_pipe; 管道信息 struct block_device *i_bdev; 块设备驱动 unsigned long i_dnotify_mask;目录通知掩码 struct dnotify_struct *i_dnotify; 目录通知 unsigned long i_state; 状态标志 unsigned long dirtied_when;首次修改时间 unsigned int i_flags; 文件系统标志 unsigned char i_sock; 套接字 atomic_t i_writecount; 写者记数 void *i_security; 安全模块 __u32 i_generation; 索引节点版本号 union { void *generic_ip;文件特殊信息 } u; }; |