【文件管理】file,dentry,inode,super

本节研究file,dentry,inode,super_block相关基本知识;

file,dentry,inode基本关系图

基本知识点

(1)一个OS最重要的部件是什么呢?那就是进程管理和文件系统;有些嵌入式系统可能有进程管理,但是没有文件系统;而另一些操作系统由文件系统,但是没有进程管理;起初Linux使用的是mnix文件系统,但是mnix文件系统有很多缺陷,最后形成了现在的EXt2文件系统,但是为了支持其他文件系统,linux抽象出了一个虚拟文件系统界面(VFS);这个抽象的界面,以系统调用形式提供于用户程序,如read(),write()等等;每一个文件系统都有自己的file_operations(这是VFS与具体文件系统的主体连线),里面的结构成分基本都是函数指针;

(2)task_struct中关于文件的相关结构如下;

/* filesystem information */ //文件系统信息struct fs_struct *fs;/* open file information *///打开文件信息struct files_struct *files;

(3)fs_struct是关于文件系统的信息,它与具体的打开文件无关(它有可能是设备文件),path中root(本进程的根目录),pwd(当前进程所在的目录);path中dengtry指向代表目录项的dentry数据结构,里面记录着文件的各项属性,如文件名,访问数据权限等;这两个path不一定都在同一个文件系统中,安装点起着很大的作用,path中mnt分别指向不同的安装点;

//文件系统信息struct fs_struct {int users;spinlock_t lock;seqcount_t seq;int umask;//标准的编码,用于设置新文件的权限,其值可以通过umask来读取和设置int in_exec;struct path root, pwd;//包括root目录和当前工作目录,分别包括dentry和vfsmount};struct path {struct vfsmount *mnt;//有关文件系统的信息struct dentry *dentry;//提供了文件名和inode之间的关联};

(4)files_struct是关于已打开文件的信息的集合;每打开一个文件后,进程通过一个打开文件号fd来访问这个文件;

//打开文件表struct files_struct { /* * read mostly part */atomic_t count;struct fdtable __rcu *fdt;//一个指针struct fdtable fdtab;//内嵌 /* * written part on a separate cache line in SMP */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 __rcu * fd_array[NR_OPEN_DEFAULT];//每一个数组项是一个指针,指};//只是一个简单的整数struct embedded_fd_set {unsigned long fds_bits[1];};struct fdtable {unsigned int max_fds;//指定了进程可以处理的文件对象和文件描述符的最大数目,可以增加struct file __rcu **fd;/* current fd array *///指针数组,每一个数组项指向一个file结构的实例,管理一个文件的打开信息fd_set *close_on_exec;fd_set *open_fds;//指向位图的指针,管理着当前所有打开文件的描述符;如果对应的bit位置位就表示正在使用struct rcu_head rcu;//也是一个指向位图的指针,保存了所有在exec系统调用时将要关闭的文件描述符的信息struct fdtable *next;};

(5)每一个file都有一个file_operations,同时还有f_dentry(一个进程可多次打开同一个文件,建立一个读写上下文);每种文件系统都有个file_operations,它既不属于某个特定的文件,更不属于某个特定的上下文;每一个file除了dnetry(在path中)外,还有一个inode(记录着文件在存储介质的位置与分布等信息);同时dentry也有一个inode指针;

(6)VFS与具体的文件系统之间的界面除了file_operations数据结构;它还有一个与目录项相联系的dentry_operations(d_delete是指向具体文件系统的删除文件操作的入口函数,d_release则用于关闭文件的操作,d_compare是用来文件名比对的);以及与索引节点相联系的inode_operations数据结构;

struct file {/* * fu_list becomes invalid after file_free is called and queued via * fu_rcuhead for RCU freeing */union {struct list_headfu_list;//链入到超级块的s_list中struct rcu_head fu_rcuhead;} f_u;struct pathf_path;//封装了两部分的信息#define f_dentryf_path.dentry#define f_vfsmntf_path.mntconst struct file_operations*f_op;//指定了文件操作的各个参数spinlock_tf_lock; /* f_ep_links, f_flags, no IRQ */#ifdef CONFIG_SMPintf_sb_list_cpu;#endifatomic_long_tf_count;unsigned int f_flags;fmode_tf_mode;//打开文件的模式参数loff_tf_pos;//字节偏移struct fown_structf_owner;//指定了处理该文件的进程有关的信息const struct cred*f_cred;struct file_ra_statef_ra;//预读u64f_version;//供文件系统使用,以检查一个file实例是否仍然与相关的inode兼容#ifdef CONFIG_SECURITYvoid*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_headf_ep_links;#endif /* #ifdef CONFIG_EPOLL */struct address_space*f_mapping;//指向属于文件相关的inode实例的地址映射空间#ifdef CONFIG_DEBUG_WRITECOUNTunsigned long f_mnt_write_state;#endif};

//目录项struct dentry {/* RCU lookup touched fields */unsigned int d_flags;/* protected by d_lock *///几个标志,DCACHE_DISCONNECTEDseqcount_t d_seq;/* per dentry seqlock */struct hlist_bl_node d_hash;/* lookup hash list */struct dentry *d_parent;/* parent directory *///指向父目录的dentrystruct qstr d_name;//指定了文件的名称struct inode *d_inode;/* Where the name belongs to – NULL is 指向相关的inode实例的指针* negative */unsigned char d_iname[DNAME_INLINE_LEN];/* small names *///名称较少的时候/* Ref lookup also touches following */unsigned int d_count;/* protected by d_lock */spinlock_t d_lock;/* per dentry lock */const struct dentry_operations *d_op;//提供对dentry对象的各种操作struct super_block *d_sb;/* The root of the dentry tree *///指向dentry所属的超级块unsigned long d_time;/* used by d_revalidate */void *d_fsdata;/* fs-specific data */struct list_head d_lru;/* LRU list *///链入到d_count为0的链表中,一项链表越靠近越后,表示越老/* * d_child and d_rcu can share memory */union {struct list_head d_child;/* child of parent list *///作为链入父目录链表的d_child struct rcu_head d_rcu;} d_u;struct list_head d_subdirs;/* our children *///给定目录下的所有文件和子目录相关联的dnetry的链表struct list_head d_alias;/* inode alias list *///用来链接表示相同文件的各个dentry,inode中的i_dentry用作表头};可是旅行的彼时那刻我的心情一直是好的吗?

【文件管理】file,dentry,inode,super

相关文章:

你感兴趣的文章:

标签云: