转自:http://blog.csdn.net/sealyao/archive/2009/10/02/4626875.aspx
Linux2.6X内核中文件相关结构体总结一、 常见文件相关结构体 以2.6.22为例,其他的2.6.X版本可能调整,但是变化不大。 1.1 struct file 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; }; 1.2 struct dentry dentry的中文名称是目录项,是Linux文件系统中某个索引节点(inode)的链接。这个索引节点可以是文件,也可以是目录。inode(可理解为ext2 inode)对应于物理磁盘上的具体对象,dentry是一个内存实体,其中的d_inode成员指向对应的inode。也就是说,一个inode可以在运行的时候链接多个dentry,而d_count记录了这个链接的数量。 struct dentry { }; 1.3 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; }; 1.5 struct inode 索引节点对象由inode结构体表示,定义文件在linux/fs.h中。 struct inode { 下图为一个进程中各主要的文件相关结构的关系图: 下图为多个进程打开同一文件的情况: 二、实例2.1 由相对路径得到绝对路径 内核的结构体不提供文件的绝对路径成员,由相对路径得到绝对路径的关键在得到当前的工作路径。 当前的工作路径可以由current->fs的pwd和pwdmnt成员得到,基本过程: 1、由pwd->d_name.Name得到当前路径名,由pwd->d_parent得到父目录dentry结构, 一直向上检索,直到父目录为"/",此时得到的是文件在本文件系统下的路径。 2、如果current->fs->pwdmnt->mnt_mountpoint的路径不是"/",说明文件所在的文件系统不是挂载在根目录下,需要得到挂载点。mnt_mountpoint也是一个dentry变量,方法同1。 3、最后把文件系统挂载点和文件在文件系统中的路径拼接起来即可。 2.2 由文件描述符得到文件的绝对路径 由当前进程的文件描述符得到文件的绝对路径: #if LINUX_VERSION_CODE >= 0x020616 由current->files->fdt->fd[fd]->f_path.dentry->d_name.Name 和current->files->fdt->fd[fd]->f_path.dentry->d_parent 得到在所在文件系统下的路径 由current->files->fdt->fd[fd]->f_path.mnt->mnt_mountpoint->d_name.Name 和current->files->fdt->fd[fd]->f_path.mnt->mnt_mountpoint->d_parent 得到文件系统挂载点在系统中挂载点的路径 #elif LINUX_VERSION_CODE >= 0x020610 由current->files->fdt->fd[fd]->f_dentry->d_name.Name 和current->files->fdt->fd[fd]->f_dentry->d_parent 得到在所在文件系统下的路径 由current->files->fdt->fd[fd]->f_vfsmnt->mnt_mountpoint->d_name.Name 和current->files->fdt->fd[fd]->f_vfsmnt->mnt_mountpoint->d_parent 得到文件系统挂载点在系统中挂载点的路径 #else 由current->files->fd[fd]->f_dentry->d_name.Name 和current->files->fd[fd]->f_dentry->d_parent 得到在所在文件系统下的路径 由current->files->fd[fd]->f_vfsmnt->mnt_mountpoint->d_name.Name 和current->files->fd[fd]->f_vfsmnt->mnt_mountpoint->d_parent 得到文件系统挂载点在系统中挂载点的路径 #endif 2.3 得到当前进程的绝对路径 #if LINUX_VERSION_CODE >= 0x020616 由current->mm->mmap->vm_file->f_path.dentry->d_name.Name 和current->mm->mmap->vm_file->f_path.dentry->d_parent 得到在所在文件系统下的路径 由current->mm->mmap->vm_file->f_path.mnt->mnt_mountpoint->d_name.Name 和current->mm->mmap->vm_file->f_path.mnt->mnt_mountpoint->d_parent 得到文件系统挂载点在系统中挂载点的路径 #else 由current->mm->mmap->vm_file->f_dentry->d_name.Name 和current->mm->mmap->vm_file->f_dentry->d_parent 得到在所在文件系统下的路径 由current->mm->mmap->vm_file->f_vfsmnt->mnt_mountpoint->d_name.Name 和current->mm->mmap->vm_file->f_vfsmnt->mnt_mountpoint->d_parent 得到文件系统挂载点在系统中挂载点的路径 #endif |
|
来自: szhlwang > 《LINUX 文件系统》