3. 超级块深度解析:超级块结构体、超级块操作函数表、挂载描述符、文件系统类型注册
好,咱们今天来啃一块硬骨头——超级块。
说实话,我当年刚接触VFS时,觉得超级块就是个“元数据的元数据”,没啥大不了的。直到有一次我在调试一个嵌入式文件系统的挂载问题,折腾了两天才发现是超级块里的一个标志位没设对。嗯,从那以后我再也不敢小看它了。
超级块,说白了就是文件系统的“户口本”。它记录了整个文件系统的全局信息,比如块大小、inode总数、空闲块数、挂载状态等等。每个已挂载的文件系统,在内存里都有一个对应的超级块对象。
3.1 超级块结构体:struct super_block
在Linux内核里,超级块用 struct super_block 表示。这个结构体定义在 <linux/fs.h> 中,字段非常多。我挑几个最核心的给你讲讲。
struct super_block {
struct list_head s_list; // 全局超级块链表
dev_t s_dev; // 块设备标识
unsigned long s_blocksize; // 块大小(字节)
unsigned char s_blocksize_bits; // 块大小的对数
loff_t s_maxbytes; // 最大文件大小
struct file_system_type *s_type; // 所属文件系统类型
struct super_operations *s_op; // 超级块操作函数表
struct dentry *s_root; // 根目录的dentry
struct list_head s_inodes; // 所有inode链表
struct list_head s_dirty; // 脏inode链表
unsigned long s_flags; // 挂载标志
// ... 还有很多
};
你看,这个结构体里藏着不少关键信息。我个人习惯重点关注这几个字段:
- s_list:所有超级块通过这个链表串起来。内核遍历已挂载文件系统时,就是顺着这个链表走的。
- s_blocksize:块大小。我遇到过有人把块大小设成512字节,结果读写性能惨不忍睹。后来改成4096,速度直接翻倍。
- s_op:超级块操作函数表。这个咱们下一节细讲。
- s_root:根目录的dentry。挂载点就是通过它找到的。
- s_flags:挂载标志。比如
MS_RDONLY表示只读挂载,MS_NOATIME表示不更新访问时间。
核心要点:超级块是文件系统的“大脑”。它不存具体文件数据,但存了“怎么找到这些数据”的信息。
3.2 超级块操作函数表:struct super_operations
超级块操作函数表,就是 struct super_operations。它定义了一组回调函数,VFS通过这些函数来操作具体的文件系统。
struct super_operations {
struct inode *(*alloc_inode)(struct super_block *sb);
void (*destroy_inode)(struct inode *);
void (*dirty_inode)(struct inode *, int flags);
int (*write_inode)(struct inode *, struct writeback_control *wbc);
void (*evict_inode)(struct inode *);
int (*sync_fs)(struct super_block *sb, int wait);
int (*statfs)(struct dentry *, struct kstatfs *);
int (*remount_fs)(struct super_block *, int *, char *);
void (*put_super)(struct super_block *);
// ... 还有不少
};
这里我挑几个常用的讲讲:
- alloc_inode:分配一个inode。每个文件系统都有自己的inode结构,比如ext4的
ext4_inode_info。这个函数就是用来分配这些结构体的。 - write_inode:把inode写回磁盘。我调试过一个bug,就是write_inode没实现,结果文件属性改了但重启后全丢了。
- sync_fs:同步文件系统。执行
sync命令时,内核会调用所有已挂载文件系统的sync_fs。 - statfs:获取文件系统统计信息。比如
df命令就是通过它拿到磁盘使用情况的。 - put_super:释放超级块。卸载文件系统时调用。
避坑指南:我曾经在写一个自定义文件系统时,忘了实现 sync_fs。结果每次系统崩溃,数据都回滚到上次sync的状态。后来我加了个定时器,每5秒自动sync一次,问题才解决。
3.3 挂载描述符:struct mount
挂载描述符,就是 struct mount。它描述了一个挂载点的信息。你想想看,一个文件系统可以被挂载到多个目录,每个挂载点都需要一个独立的描述符。
struct mount {
struct hlist_node mnt_hash; // 挂载哈希表
struct mount *mnt_parent; // 父挂载点
struct dentry *mnt_mountpoint; // 挂载点的dentry
struct vfsmount mnt; // 旧的vfsmount结构
struct super_block *mnt_sb; // 指向超级块
struct list_head mnt_mounts; // 子挂载点链表
struct list_head mnt_child; // 兄弟挂载点链表
unsigned long mnt_flags; // 挂载标志
// ...
};
这里有个关键点:mnt_sb 指向超级块,而 mnt_mountpoint 指向挂载点的dentry。通过这两个指针,VFS就能把文件系统挂载到目录树上。
举个例子:你把U盘挂载到 /mnt/usb,那么:
mnt_mountpoint指向/mnt/usb的dentrymnt_sb指向U盘文件系统的超级块- 访问
/mnt/usb时,VFS会通过挂载描述符跳转到U盘的根目录
注意:挂载描述符是VFS实现“挂载点”功能的核心。没有它,你就没法在同一个目录下挂载多个文件系统。
3.4 文件系统类型注册:register_filesystem
文件系统类型,用 struct file_system_type 表示。每个文件系统(比如ext4、ntfs、tmpfs)在内核里都有一个对应的类型对象。
struct file_system_type {
const char *name; // 文件系统名称
int fs_flags; // 标志位
struct dentry *(*mount)(struct file_system_type *, int, const char *, void *);
void (*kill_sb)(struct super_block *);
struct module *owner; // 所属模块
struct file_system_type *next; // 链表下一个
struct hlist_head fs_supers; // 该类型的所有超级块
// ...
};
文件系统要能被内核识别,必须先注册。注册函数是 register_filesystem,它把 file_system_type 加入到一个全局链表中。
int register_filesystem(struct file_system_type *fs)
{
// 检查是否已注册
// 加入全局链表
// 返回0表示成功
}
我举个例子,ext4文件系统的注册代码大概长这样:
static struct file_system_type ext4_fs_type = {
.owner = THIS_MODULE,
.name = "ext4",
.mount = ext4_mount,
.kill_sb = ext4_kill_sb,
.fs_flags = FS_REQUIRES_DEV,
};
static int __init init_ext4_fs(void)
{
return register_filesystem(&ext4_fs_type);
}
你看,注册时指定了 mount 和 kill_sb 两个关键函数。当用户执行 mount -t ext4 /dev/sda1 /mnt 时,内核会调用 ext4_mount 来创建超级块和根dentry。
警告:我曾经见过有人把 register_filesystem 放在模块初始化函数的最后,结果前面的代码出错了,模块加载失败,文件系统没注册上。建议把注册放在初始化函数的最前面,确保尽早注册。
3.5 知识体系总览
为了让你更直观地理解这些概念之间的关系,我画了一张图:
这张图展示了四个核心概念之间的关系:文件系统类型注册后,可以创建超级块;超级块通过操作函数表提供具体功能;挂载描述符则把超级块和目录树连接起来。
3.6 小结
超级块这块内容,说白了就是VFS的“底盘”。你理解了它,后面讲inode、dentry、文件操作时就会轻松很多。
我个人建议你动手写一个简单的文件系统模块,注册到内核里试试。哪怕只是打印几行日志,也能帮你把今天讲的内容串起来。我曾经就是这么干的,效果比光看书好十倍。
小技巧:调试超级块相关代码时,可以用 mount 命令查看挂载信息,用 cat /proc/fs/*/info 查看超级块状态。这些工具能帮你快速定位问题。