Linux 文件系统模块的注册

用cramfs.ko做的实验,断点放在init_cramfs_fs(void)函数的rv = register_filesystem(&cramfs_fs_type);行,执行register_filesystem函数:

register_filesystem (fs=0xda7142e0) at fs/filesystems.c:74

显示cramfs的file_system_type结构提如下:

{name = 0xda714268 "cramfs", fs_flags = 1,
  get_sb = 0xda713150 <cramfs_get_sb>,
  kill_sb = 0xc0205bb0 <kill_block_super>, owner = 0xda714320, next = 0x0,
  fs_supers = {next = 0x0, prev = 0x0}, s_lock_key = {<No data fields>},
  s_umount_key = {<No data fields>}, i_lock_key = {<No data fields>},
  i_mutex_key = {<No data fields>}, i_mutex_dir_key = {<No data fields>},
  i_alloc_sem_key = {<No data fields>}}

先提一下,实际注册文件系统的时候是将file_system_type放在以file_systems为表头的一个链表中。

先看下file_systems的内容:

(gdb) p *file_systems
$8 = {name = 0xc06cf3f4 "sysfs", fs_flags = 0,
  get_sb = 0xc0259190 <sysfs_get_sb>, kill_sb = 0xc0205c00 <kill_anon_super>,
  owner = 0x0, next = 0xc0751460, fs_supers = {next = 0xd78044a0,
    prev = 0xd78044a0}, s_lock_key = {<No data fields>},
  s_umount_key = {<No data fields>}, i_lock_key = {<No data fields>},
  i_mutex_key = {<No data fields>}, i_mutex_dir_key = {<No data fields>},
  i_alloc_sem_key = {<No data fields>}}

根据next = 0xc0751460   p *(struct file_system_type *)0xc0751460

{name = 0xc06c0033 "rootfs", fs_flags = 0,
  get_sb = 0xc02b7240 <rootfs_get_sb>,
  kill_sb = 0xc0205c50 <kill_litter_super>, owner = 0x0, next = 0xc074fc00,
  fs_supers = {next = 0xd78046a0, prev = 0xd78046a0},
  s_lock_key = {<No data fields>}, s_umount_key = {<No data fields>},
  i_lock_key = {<No data fields>}, i_mutex_key = {<No data fields>},
  i_mutex_dir_key = {<No data fields>}, i_alloc_sem_key = {<No data fields>}}

rootfs的注册还在sysfs之后。

register_filesystem函数具体做了什么?主要是两点:

1、INIT_LIST_HEAD(&fs->fs_supers);

    初始化file_system_type结构体中的fs_supers双向链表,后来会看到它会与该种文件系统类型的super_block结构体的s_instances关联。

2、find_filesystem(fs->name, strlen(fs->name));

    在find_filesystem函数中就是根据name和名字长度遍历链表进行查找的,从而将新的文件系统加到这个链表中。

附上一个图:

Linux 文件系统模块的注册

 


文件系统模块的注册应该就这么简单,注册完成后,就可以mount并使用该文件系统了。

相关推荐