前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >浅谈文件系统中的核心数据结构

浅谈文件系统中的核心数据结构

作者头像
用户4700054
发布2022-08-17 12:38:22
8810
发布2022-08-17 12:38:22
举报
文章被收录于专栏:存储内核技术交流

宏观上文件系统在kernel的形态

  • 文件系统运作流程按照:vfs->磁盘缓存->实际磁盘文件系统->通用块设备层->io调度层->块设备驱动层->磁盘。具体流程的详细展现如下如
如何理解文件系统中的数据结构?
  • linux中文件系统还有几种核心数据结构分别是super_blockinodedentryfile.super_block是磁盘文件系统(xfs/ext4)的内存呈现,inode是linux中文件唯一呈现,也是文件本身,存储了文件的元数据。dentry是文件本身的代表,存储了文件的名称和inode.file是文件被打开的状态,每个进程执行文件的系统调用都会实例化file。每一数据结构都会有一些列的函数表定义和私有数据。这个是为了实现不同文件系统而采用的工厂设计模式,这些私有数据是vfs和实际磁盘文件系统交互的核心数据结构。每个实际的磁盘文件系统(ext4/xfs)文件系统针对super_blockinodedentryfile.super_block都会有自己的操作实现,kernel只需要把定义通用接口,这些接口的具体实现都是有实际磁盘文件系统注册后把这些接口操作给实例化,这就达到了vfs和实际文件系统的交互。
struct super_block简单描述
代码语言:javascript
复制
// struct super_block 省略和一些字段,比较核心的字段描述了下
struct super_block {
	// 把suoer_block链接到s_list链表 
	struct list_head	s_list;		
	// s_blocksize的位数
	unsigned char		s_blocksize_bits;
	// 文件系统中块大小
	unsigned long		s_blocksize;
	// 支持最大文件大小
	loff_t			s_maxbytes;	
	// 文件系统类型
	struct file_system_type	*s_type;
	// super_block的操作函数
	const struct super_operations	*s_op;
	// dentry函数操作定义
	const struct dentry_operations *s_d_op;
	// dentry的指针,指向根节点的root
	struct dentry		*s_root;
	// 挂载该文件系统的挂载点组成的链表
	struct list_head	s_mounts;	
	
	// 同一种文件系统组成的链表
	struct hlist_node	s_instances;


	// 实际文件系统的私有数据
	void			*s_fs_info;	

} __randomize_layout;
struct file_system_type简单描述
代码语言:javascript
复制
// 每个文件系统在实话时候都会先注册这个文件系统,fs_system_type就是用来描述这个文件系统的
struct file_system_type {
	// 文件系统名称,比如ext4、xfs
	const char *name;
	// 实际文件提供的mount函数用来初始化super_block
	struct dentry *(*mount) (struct file_system_type *, int,
		       const char *, void *);
	// 释放文件系统的哈数
	void (*kill_sb) (struct super_block *);
	// 内核模块描述
	struct module *owner;
	
	// file_system_type链表
	struct file_system_type * next;
	
	// 同一种文件系统super_block组成的链表
	struct hlist_head fs_supers;
};

// 比如ext4的file_system_type的类型
static struct file_system_type ext4_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ext4",
	.mount		= ext4_mount,
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};
struct inode简单描述
代码语言:javascript
复制
// 文件系统中inode描述,其中说明了核心字段的函数
struct inode {
	// 文件类型
	umode_t			i_mode;
	
	// uid/gid是描述文件的属主
	kuid_t			i_uid;
	kgid_t			i_gid;


	// inode的操作函数,这个是由具体文件系统决定
	const struct inode_operations	*i_op;
	// inode属于的超级快
	struct super_block	*i_sb;
	// page cache涉及到缓存管理
	struct address_space	*i_mapping;
	
	// inode的编号,单个文件系统内这个是唯一的
	unsigned long		i_ino;
	// inode所指向的文件大小
	loff_t			i_size;
	// 文件的acess/mofidy/change时间
	struct timespec64	i_atime;
	struct timespec64	i_mtime;
	struct timespec64	i_ctime;
	
	// inode链接到哈希链表中
	struct hlist_node	i_hash;
	// inode链接到super_block上
	struct list_head	i_sb_list;
	union {
		// 进程打开文件时候的操作函数,这个是与文件类的系统调用对接

		const struct file_operations	*i_fop;	
		void (*free_inode)(struct inode *);
	};

	// inode的私有数据,一般存储实际文件系统的私有数据
	void			*i_private; /* fs or device private pointer */
} __randomize_layout;
  • inode在i_mapping是用缓存,其具体的关系如下
struct dentry简单描述
代码语言:javascript
复制
// 文件系统的目录树是采用组织dentry来呈现
struct dentry {

	// 指向父目录的dentry
	struct dentry *d_parent;
	
	// 保存了文件名字和哈希值
	struct qstr d_name;
	
	// 该目录项指向的inode
	struct inode *d_inode;
	
	// 当目录项名称比较短的时候保存在这里
	unsigned char d_iname[DNAME_INLINE_LEN];	/* small names */

	// 定义dentry的操作函数,每个文件系统都针对d_op进行初始化
	const struct dentry_operations *d_op;
	
	// dentry私有数据
	void *d_fsdata;			/* fs-specific data */

	union {
		struct list_head d_lru;		/* LRU list */
		wait_queue_head_t *d_wait;	/* in-lookup ones only */
	};
	// 当前dentry所有父目录项的链表
	struct list_head d_child;	/* child of parent list */
	
	//当前目录项下所有子目录项的链表
	struct list_head d_subdirs;	/* our children */

} __randomize_layout;
struct file简单描述
代码语言:javascript
复制
// 每当进程打开一个文件都会实例化一个struct file,这里面包含了标准的posix语义的操作
struct file {
	// 文件路径
	struct path		f_path;
	// 文件指向的inode
	struct inode		*f_inode;
	// 定义struct file的操作函数
	const struct file_operations	*f_op;
	// 文件的引用计数器
	atomic_long_t		f_count;
	// 文件打开的flags
	unsigned int 		f_flags;
	// 文件的mode
	fmode_t			f_mode;
	// 文件的当前位置
	loff_t			f_pos;

	// struct file的私有数据
	void			*private_data;

	// 文件的page cache相关的address_space
	struct address_space	*f_mapping;

} __randomize_layout
  • 进程打开一个文件的呈现如下面
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-12-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 存储内核技术交流 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 宏观上文件系统在kernel的形态
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档