在上面两节中,主要介绍了VFS模块相关的结构体以及文件系统类型的注册、文件及目录对应的结构体变量dentry与inode操作、文件系统挂载等内容。本小节主要介绍VFS相关结构体与进程之间的关系。我们知道在应用层打开文件时,均是在进程环境下执行文件操作,且可以在多个进程中打开相同的文件,因此VFS与进程之间必然存在着联系,本小节主要介绍这些内容。
进程相关的结构体说明
在linux内核中,进程相关的结构体为进程描述符(struct task_struct),本小节主要说明进程描述符与文件系统相关的变量,对其他变量不做扩展分析。如下所示,进程描述符中涉及文件系统的有当前目录对应的文件系统相关的结构体struct fs_struct,以及文件描述符struct files_struct。
struct task_struct {
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
void *stack;
pid_t pid;/*进程id*/
pid_t tgid;/*线程组id*/
#ifdef CONFIG_CC_STACKPROTECTOR
/* Canary value for the -fstack-protector gcc feature */
unsigned long stack_canary;
#endif
struct list_head thread_group;
struct completion *vfork_done; /* for vfork() */
int __user *set_child_tid; /* CLONE_CHILD_SETTID */
int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
cputime_t utime, stime, utimescaled, stimescaled;
cputime_t gtime;
/* filesystem information */
struct fs_struct *fs;/*启动该进程的用户文件系统相关*/
/* open file information */
struct files_struct *files;
/* namespaces */
struct nsproxy *nsproxy;
};
struct fs_struct说明
该结构体主要包括该进程执行时,对应用户所在的根目录项,以及当前目录项(使用struct path变量表示)。
struct fs_struct {
int users;/*共用该变量的进程数*/
spinlock_t lock;/*自旋锁*/
seqcount_t seq;
int umask;/*当创建文件时,默认设置的位掩码,根据该位掩码,通过系统调用创建文件或目录时,未指定权限时,使用默认权限*/
int in_exec;
struct path root, pwd;/*分别表示该进程对应的根目录对应的根目录项以及当前目录对应的目录项以及文件系统对应的mount变量*/
};
而struct path中包括s