mount过程分析之四(vfs_kern_mount->mount_fs->xfs_fs_mount)

本文详细解析了Linux内核中mount过程,从sys_mount开始,经过do_mount、do_new_mount、vfs_kern_mount,直至mount_fs和特定文件系统的mount回调函数。重点介绍了xfs文件系统的mount流程,包括xfs_super.c中的init_xfs_fs模块初始化,以及xfs_fs_mount函数如何调用mount_bdev()。通过一系列博客链接提供了深入学习资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sys_mount - > do_mount -> do_new_mount -> vfs_kern_mount

vfs_kern_mount的作用就是准备好一个完整的mount结构。
vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
{
        struct mount *mnt;
        struct dentry *root;

        if (!type)
                return ERR_PTR(-ENODEV);

        // alloc一个新的struct mount结构,并初始化里面一部分(如链表指针、mnt_devname等成员内容)
        mnt = alloc_vfsmnt(name);
        if (!mnt)
                return ERR_PTR(-ENOMEM);

        if (flags & MS_KERNMOUNT)
                mnt->mnt.mnt_flags = MNT_INTERNAL;

        // 调用具体文件系统的mount回调函数type->mount,继续挂载操作
        root = mount_fs(type, flags, name, data);
        if (IS_ERR(root)) {
                mnt_free_id(mnt);
                free_vfsmnt(mnt);
                return ERR_CAST(root);
        }

        // 完成mnt结构的最后赋值,并返回vfsmount结构
        mnt->mnt.mnt_root = root;
        mnt-
Linux内核支持多种不同的文件系统类型,包括ext4、NTFS、FAT、XFS等等。这些文件系统类型的实现代码可以在内核源码中找到,并对其进行逐行介绍。 以下是一个示范的文件系统实现,名为"myfs": 1. 首先定义了一些预处理指令,包括头文件和一些常量定义: ``` #include <linux/module.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/buffer_head.h> #define MYFS_MAGIC_NUMBER 0x13131313 #define MYFS_DEFAULT_BLOCK_SIZE 4096 #define MYFS_FILENAME_MAX_LEN 256 ``` 2. 然后定义了一个结构体,用于存储文件系统的元数据信息: ``` struct myfs_sb_info { __u32 magic_number; __u32 block_size; __u64 inode_count; __u64 block_count; __u64 free_blocks; __u64 free_inodes; struct mutex lock; }; ``` 其中,magic_number是一个用于标识该文件系统类型的数字;block_size是文件系统使用的块大小;inode_count和block_count分别表示文件系统中的inode和block数量;free_blocks和free_inodes表示可用的block和inode数量;lock是用于保护元数据结构的互斥锁。 3. 接下来定义了一个inode结构体,用于表示文件或目录的属性信息: ``` struct myfs_inode_info { __u32 mode; uid_t uid; gid_t gid; __u64 size; __u64 atime; __u64 mtime; __u64 ctime; __u32 block_count; __u32 blocks[MYFS_DEFAULT_BLOCK_SIZE / sizeof(__u32)]; struct inode vfs_inode; }; ``` 其中,mode表示文件或目录的访问权限;uid和gid表示文件或目录的所有者和所属组;size表示文件大小;atime、mtime和ctime表示文件或目录的访问、修改和创建时间;block_count表示该文件或目录使用的block数量;blocks数组存储了该文件或目录所使用的所有block的编号;vfs_inode是用于与VFS交互的inode结构体。 4. 接下来定义了一些用于读取和写入磁盘的函数: ``` static int myfs_read_block(struct super_block *sb, void *buf, __u64 block_no); static int myfs_write_block(struct super_block *sb, void *buf, __u64 block_no); ``` 这些函数使用了内核提供的缓冲区头结构体(buffer_head)来读写磁盘块。 5. 定义了用于初始化文件系统的函数: ``` static int myfs_fill_super(struct super_block *sb, void *data, int silent); ``` 该函数用于读取文件系统的元数据信息,并填充超级块结构体(super_block)。 6. 接下来定义了一些用于VFS操作的函数: ``` static struct inode *myfs_inode_lookup(struct inode *parent_inode, struct dentry *child_dentry, unsigned int flags); static int myfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl); static int myfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); static int myfs_rmdir(struct inode *dir, struct dentry *dentry); static int myfs_unlink(struct inode *dir, struct dentry *dentry); static int myfs_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry); static ssize_t myfs_file_read(struct file *filp, char *buf, size_t count, loff_t *pos); static ssize_t myfs_file_write(struct file *filp, const char *buf, size_t count, loff_t *pos); static int myfs_mmap(struct file *filp, struct vm_area_struct *vma); ``` 这些函数实现了VFS的各种操作,例如查找inode、创建和删除文件或目录、读写文件、内存映射等。 7. 最后定义了用于注册文件系统的函数: ``` static struct file_system_type myfs_fs_type = { .owner = THIS_MODULE, .name = "myfs", .mount = myfs_mount, .kill_sb = myfs_kill_sb, }; static int __init myfs_init(void) { int ret = register_filesystem(&myfs_fs_type); if (ret) { printk(KERN_ERR "myfs: Failed to register filesystem (error %d)\n", ret); return ret; } printk(KERN_INFO "myfs: Filesystem registered successfully\n"); return 0; } static void __exit myfs_exit(void) { int ret = unregister_filesystem(&myfs_fs_type); if (ret) { printk(KERN_ERR "myfs: Failed to unregister filesystem (error %d)\n", ret); } printk(KERN_INFO "myfs: Filesystem unregistered successfully\n"); } module_init(myfs_init); module_exit(myfs_exit); ``` 这些函数定义了文件系统类型,注册和注销文件系统。 以上就是一个简单的示范文件系统的实现。在实际的文件系统实现中,还需要处理更多的细节和异常情况,例如文件系统的格式化、坏块处理、权限检查、错误恢复等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值