EXT4文件系统学习(13)VFS之VFS超级块super_block

VFS超级块

VFS超级块是根据具体文件系统的超级块建立起来的内存结构:

struct super_block {
	struct list_head	s_list;		/* Keep this first */
	dev_t			s_dev;		/* search index; _not_ kdev_t */
	unsigned char		s_blocksize_bits;
	unsigned long		s_blocksize;
	loff_t			s_maxbytes;	/* Max file size */
	struct file_system_type	*s_type;指向对应的文件系统对象
	const struct super_operations	*s_op;指向具体文件系统超级块操作函数
	const struct dquot_operations	*dq_op;
	const struct quotactl_ops	*s_qcop;
	const struct export_operations *s_export_op;
	unsigned long		s_flags;
	unsigned long		s_magic;
	struct dentry		*s_root;
	struct rw_semaphore	s_umount;
	int			s_count;
	atomic_t		s_active;
#ifdef CONFIG_SECURITY
	void                    *s_security;
#endif
	const struct xattr_handler **s_xattr;

	struct list_head	s_inodes;	/* all inodes */
	struct hlist_bl_head	s_anon;		/* anonymous dentries for (nfs) exporting */
	struct list_head	s_mounts;	/* list of mounts; _not_ for fs use */
	struct block_device	*s_bdev;
	struct backing_dev_info *s_bdi;
	struct mtd_info		*s_mtd;
	struct hlist_node	s_instances;
	unsigned int		s_quota_types;	/* Bitmask of supported quota types */
	struct quota_info	s_dquot;	/* Diskquota specific options */

	struct sb_writers	s_writers;

	char s_id[32];				/* Informational name */
	u8 s_uuid[16];				/* UUID */

	void 			*s_fs_info;	/* Filesystem private info */指向具体文件系统的超级块内存对象,就是ext4_sb_info
	unsigned int		s_max_links;
	fmode_t			s_mode;

	/* Granularity of c/m/atime in ns.
	   Cannot be worse than a second */
	u32		   s_time_gran;

	/*
	 * The next field is for VFS *only*. No filesystems have any business
	 * even looking at it. You had been warned.
	 */
	struct mutex s_vfs_rename_mutex;	/* Kludge */

	/*
	 * Filesystem subtype.  If non-empty the filesystem type field
	 * in /proc/mounts will be "type.subtype"
	 */
	char *s_subtype;

	/*
	 * Saved mount options for lazy filesystems using
	 * generic_show_options()
	 */
	char __rcu *s_options;
	const struct dentry_operations *s_d_op; /* default d_op for dentries */

	/*
	 * Saved pool identifier for cleancache (-1 means none)
	 */
	int cleancache_poolid;

	struct shrinker s_shrink;	/* per-sb shrinker handle */

	/* Number of inodes with nlink == 0 but still referenced */
	atomic_long_t s_remove_count;

	/* Being remounted read-only */
	int s_readonly_remount;

	/* AIO completions deferred from interrupt context */
	struct workqueue_struct *s_dio_done_wq;
	struct hlist_head s_pins;

	/*
	 * Keep the lru lists last in the structure so they always sit on their
	 * own individual cachelines.
	 */
	struct list_lru		s_dentry_lru ____cacheline_aligned_in_smp;
	struct list_lru		s_inode_lru ____cacheline_aligned_in_smp;
	struct rcu_head		rcu;

	/*
	 * Indicates how deep in a filesystem stack this SB is
	 */
	int s_stack_depth;
};

当内核需要挂载(mount)一个块设备时,可以从分区表中信息得知这个块设备的文件系统类型,从文章EXT4文件系统学习(八)磁盘结构可以看出分区信息中的文件系统类型,也可以从分区的superblock信息中看出文件系统类型。

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,
};
MODULE_ALIAS_FS("ext4");

然后从file_system_type文件系统对象链表中找到对应的文件系统驱动程序的文件系统对象,调用里面的mount()函数获取具体的文件系统超级块信息。然后根据这些信息初始化VFS超级块,结构中的s_fs_info就指向具体文件系统的超级块内存对象,也就是ext4_sb_info。

由于各个文件系统的超级块不同,所以对操作超级块的方法也不同。为此内核定义了一个super_operations结构,定义如下:

struct super_operations {
   	struct inode *(*alloc_inode)(struct super_block *sb);分配一个inode结构
	void (*destroy_inode)(struct inode *);释放一个inode结构

   	void (*dirty_inode) (struct inode *, int flags);
	int (*write_inode) (struct inode *, struct writeback_control *wbc);
	int (*drop_inode) (struct inode *);
	void (*evict_inode) (struct inode *);
	void (*put_super) (struct super_block *);
	int (*sync_fs)(struct super_block *sb, int wait);
	int (*freeze_super) (struct super_block *);
	int (*freeze_fs) (struct super_block *);
	int (*thaw_super) (struct super_block *);
	int (*unfreeze_fs) (struct super_block *);
	int (*statfs) (struct dentry *, struct kstatfs *);
	int (*remount_fs) (struct super_block *, int *, char *);
	void (*umount_begin) (struct super_block *);

	int (*show_options)(struct seq_file *, struct dentry *);
	int (*show_devname)(struct seq_file *, struct dentry *);
	int (*show_path)(struct seq_file *, struct dentry *);
	int (*show_stats)(struct seq_file *, struct dentry *);
#ifdef CONFIG_QUOTA
	ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
	ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
	struct dquot **(*get_dquots)(struct inode *);
#endif
	int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
	long (*nr_cached_objects)(struct super_block *,
				  struct shrink_control *);
	long (*free_cached_objects)(struct super_block *,
				    struct shrink_control *);
};

可以看出super_operations结构中的函数指针都是在操作下层文件系统,不同的文件系统super_operations也是不同的。

当内核挂载块设备时,会根据分区表读出文件系统类型信息,然后找到驱动中对应的已经注册过的文件系统对象,并调用它的mount函数设置s_op指针。

ext4文件系统的mount函数是ext4_mount,里面调用了ext4_fill_super函数会把磁盘上数据读出,装载磁盘和内存超级块以及VFS超级块。(装载磁盘和内存超级块可参考11节里面介绍ext4_fill_super函数)

static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
		       const char *dev_name, void *data)
{
	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
}

这里比较重要的是设置s_op指针:

static int ext4_fill_super(struct super_block *sb, void *data, int silent)
{
	sb->s_op = &ext4_sops;

这样就建立起了抽象的VFS超级块对象与具体ext4超级块对象的联系。

操作具体文件系统的操作函数ext4_sops如下:

static const struct super_operations ext4_sops = {
	.alloc_inode	= ext4_alloc_inode,
	.destroy_inode	= ext4_destroy_inode,
	.write_inode	= ext4_write_inode,
	.dirty_inode	= ext4_dirty_inode,
	.drop_inode	= ext4_drop_inode,
	.evict_inode	= ext4_evict_inode,
	.put_super	= ext4_put_super,
	.sync_fs	= ext4_sync_fs,
	.freeze_fs	= ext4_freeze,
	.unfreeze_fs	= ext4_unfreeze,
	.statfs		= ext4_statfs,
	.remount_fs	= ext4_remount,
	.show_options	= ext4_show_options,
#ifdef CONFIG_QUOTA
	.quota_read	= ext4_quota_read,
	.quota_write	= ext4_quota_write,
	.get_dquots	= ext4_get_dquots,
#endif
	.bdev_try_to_free_page = bdev_try_to_free_page,
};

ext4_fill_super函数最后会请求读取根目录的inode,调用

#define EXT4_ROOT_INO		 2	/* Root inode */
root = ext4_iget(sb, EXT4_ROOT_INO);

继续分析iget函数,先去inode哈希链表缓存里面查找,没有的话就分配一个,分配不带指定inode号,所以这里必须在在表里面查找成功,但是根目录的inode号什么时候加载到内存inode表里面的?

struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
{
	struct inode *inode;
	inode = iget_locked(sb, ino);

挂载文件系统根目录时,根目录的inode号肯定不在哈希链表中,所以需要新分配一个, 分配后再去链表中查找inode为2的号, 没有找到的话就把根目录号赋值给新分配的inode,且标志设置为I_NEW

struct inode *iget_locked(struct super_block *sb, unsigned long ino)
{
	struct inode *inode;

	spin_lock(&inode_hash_lock);
	inode = find_inode_fast(sb, head, ino);
	spin_unlock(&inode_hash_lock);
	if (inode) {
		wait_on_inode(inode);
		return inode;
	}

	inode = alloc_inode(sb);
	if (inode) {
		struct inode *old;

		spin_lock(&inode_hash_lock);
		/* We released the lock, so.. */
		old = find_inode_fast(sb, head, ino);
		if (!old) {
			inode->i_ino = ino;
			spin_lock(&inode->i_lock);
			inode->i_state = I_NEW;
			hlist_add_head(&inode->i_hash, head);
			spin_unlock(&inode->i_lock);
			inode_sb_list_add(inode);
			spin_unlock(&inode_hash_lock);

			/* Return the locked inode with I_NEW set, the
			 * caller is responsible for filling in the contents
			 */
			return inode;
		}

iget_locked把根目录的inode返回后,VFS inode就已经分配好了;这时候通过宏EXT4_I转换得到EXT4 内存inode结构。

struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
{
	struct ext4_inode_info *ei;
	struct inode *inode;

	inode = iget_locked(sb, ino);
	ei = EXT4_I(inode);

获取到inode号信息后就可以读取磁盘上面逻辑的inode数据,读取方法:

根据inode号获取出属于哪个块组,然后根据inode在块组内的偏移计算出块inode在哪个块内,最后把块数据读出到buffer_head中,然后再根据块内偏移获取得到磁盘inode数据:

struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
{
	struct ext4_iloc iloc;
	struct ext4_inode *raw_inode;磁盘inode
	struct ext4_inode_info *ei;内存inode
	struct inode *inode;VFS inode

	inode = iget_locked(sb, ino);
	ei = EXT4_I(inode);

        __ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc, int in_mem)
            iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
            gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
            inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
            inode_offset = ((inode->i_ino - 1) %
			EXT4_INODES_PER_GROUP(sb));
            block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
            iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);

            bh = sb_getblk(sb, block);
            iloc->bh = bh;

	raw_inode = ext4_raw_inode(&iloc);

后面根据逻辑raw_inode设置内存inode和VFS inode:

	ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
	ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
	ei->i_flags = le32_to_cpu(raw_inode->i_flags);
	inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
	ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);

设置i_op和i_fop指针,这些个函数指针都是操作下层具体文件系统。

	if (S_ISREG(inode->i_mode)) {
		inode->i_op = &ext4_file_inode_operations;
		inode->i_fop = &ext4_file_operations;
		ext4_set_aops(inode);
	} else if (S_ISDIR(inode->i_mode)) {
		inode->i_op = &ext4_dir_inode_operations;
		inode->i_fop = &ext4_dir_operations;
	} else if (S_ISLNK(inode->i_mode)) {
		if (ext4_inode_is_fast_symlink(inode) &&
		    !ext4_encrypted_inode(inode)) {
			inode->i_op = &ext4_fast_symlink_inode_operations;
			nd_terminate_link(ei->i_data, inode->i_size,
				sizeof(ei->i_data) - 1);
		} else {
			inode->i_op = &ext4_symlink_inode_operations;
			ext4_set_aops(inode);
		}
	} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
	      S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
		inode->i_op = &ext4_special_inode_operations;
		if (raw_inode->i_block[0])
			init_special_inode(inode, inode->i_mode,
			   old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
		else
			init_special_inode(inode, inode->i_mode,
			   new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
	} else if (ino == EXT4_BOOT_LOADER_INO) {
		make_bad_inode(inode);
	} else {
		ret = -EIO;
		EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
		goto bad_inode;
	}

VFS超级块介绍完毕,下一篇介绍VFS inode。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Ext4文件系统是一种支持日志操作的Linux文件系统。它支持分层目录结构,以及支持更大文件和分区的大小。要挂载Ext4文件系统,首先需要格式化分区,然后将其挂载到VFS(虚拟文件系统)。格式化分区可以使用以下命令:sudo mkfs -t ext4 /dev/<disk-name>。接下来,可以使用以下命令挂载到VFS:sudo mount -t ext4 /dev/<disk-name> /mnt/<mount-point>。这些命令将Ext4文件系统挂载到VFS,以便可以访问分区内的文件和文件夹。 ### 回答2: ext4文件系统是Linux中最常用的文件系统之一。当我们要将ext4文件系统挂载到VFS(Virtual File System,虚拟文件系统)时,需要经过以下步骤: 1. 首先,需要通过系统调用(syscall)打开ext4文件系统的设备。在打开设备后,系统会为设备创建一个file对象,用于描述该设备。可以在Linux内核的文件`fs/ext4/super.c`中找到相关源代码。 2. 接下来,需要通过调用`mount_bdev()`函数来进行文件系统的挂载。该函数的作用是将设备与文件系统关联起来,创建超级对象,并将其添加到全局的超级链表中。详细代码可以在`fs/ext4/super.c`文件中的`ext4_fill_super()`函数中找到。 3. 在`ext4_fill_super()`函数中,还需要解析设备上的ext4文件系统超级信息,并进行一系列的初始化操作,如加载描述符表、inode表等。相关代码可以在`fs/ext4/super.c`文件中的`ext4_parse_sb()`函数中找到。 4. 通过调用`get_sb_bdev()`函数,将ext4超级对象与设备对象(在第一步中创建的file对象)关联起来。该函数会调用`ext4_fill_super()`函数,从而完成挂载过程。相关代码可以在`fs/ext4/super.c`文件中找到。 5. 挂载完成后,VFS会通过将ext4文件系统挂载点与超级对象关联起来,并维护一系列的内存数据结构,如VFSsuper_block结构等。这些数据结构用于管理和操作ext4文件系统的各种操作。 总结起来,ext4文件系统的挂载过程主要涉及打开设备、创建超级对象、解析超级信息以及与VFS的关联操作。在源码的层面上,主要通过调用相关的函数完成这些操作,涉及的源代码位于`fs/ext4/super.c`文件中。 ### 回答3: ext4是一种常用的文件系统,在Linux内核中有自己的实现。VFS(Virtual File System)是Linux内核提供的抽象层,用于统一不同文件系统的接口。 源码角度解释ext4文件系统如何挂载到VFS需要了解以下主要步骤: 1. 初始化ext4文件系统:在Linux内核中,ext4的初始化由文件系统对象super_block的填充和注册函数init_ext4_fs()完成。这些函数会设置文件系统的相关参数,并创建并初始化超级super_block)对象。 2. 注册ext4文件系统:通过调用register_filesystem()函数,将ext4文件系统类型注册到VFS中。此时,Linux内核就能够识别ext4文件系统。 3. 挂载ext4文件系统:在内核中,挂载操作由mount()函数实现。当用户使用mount命令挂载ext4文件系统时,该命令会调用mount()函数完成挂载过程。在函数中,首先会检查是否具有挂载权限,并调用find_filesystem()函数查找ext4文件系统类型。如果找到,则会调用ext4_mount()函数对具体的ext4文件系统进行挂载。 4. ext4文件系统挂载过程:在ext4_mount()函数中,首先会调用ext4_fill_super()函数填充文件系统超级信息。然后,会调用read_super()函数读取磁盘上的超级,并对文件系统进行一些初始化操作。最后,将文件系统的根目录(root)设置为当前进程的工作目录,并返回挂载成功的信息。 通过以上步骤,ext4文件系统就成功挂载到VFS中了。在挂载完成后,用户可以通过与其他文件系统相同的接口进行文件和目录的访问操作。 总结一下,ext4文件系统的挂载到VFS主要包括初始化ext4文件系统、注册ext4文件系统、挂载ext4文件系统这三个主要步骤。这些步骤通过相应的函数调用和参数设置来实现,在源码层面上保证了ext4文件系统VFS的兼容性和良好的交互。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值