在内核的fs/super.c中,我们可以看到,通过
static LIST_HEAD(super_blocks);
将所有的struct super_block实例都链接到了一起,这个变量并没有通过EXPORT_SYMBOL进行导出,那么,如果我想在不修改内核代码的情况下获取这些super block的实例的信息,该怎么做?
查看代码,发现通过如下方法可以做到:
- 通过函数struct file_system_type *get_fs_type(const char *name)获取struct file_system_type的实例
- 通过void iterate_supers_type(struct file_system_type *type,
void (*f)(struct super_block *, void *), void *arg)函数传入回调函数进行信息的获取
这么讲也不容易明白,直接上代码,基于linux4.4.198
struct file_system_type *get_fs_type(const char *name)
{
struct file_system_type *fs;
const char *dot = strchr(name, '.');
int len = dot ? dot - name : strlen(name);
fs = __get_fs_type(name, len);
if (!fs && (request_module("fs-%.*s", len, name) == 0))
fs = __get_fs_type(name, len);
if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
put_filesystem(fs);
fs = NULL;
}
return fs;
}
EXPORT_SYMBOL(get_fs_type);
void iterate_supers_type(struct file_system_type *type,
void (*f)(struct super_block *, void *), void *arg)
{
struct super_block *sb, *p = NULL;
spin_lock(&sb_lock);
hlist_for_each_entry(sb, &type->fs_supers, s_instances) {
sb->s_count++;
spin_unlock(&sb_lock);
down_read(&sb->s_umount);
if (sb->s_root && (sb->s_flags & MS_BORN))
f(sb, arg);
up_read(&sb->s_umount);
spin_lock(&sb_lock);
if (p)
__put_super(p);
p = sb;
}
if (p)
__put_super(p);
spin_unlock(&sb_lock);
}
EXPORT_SYMBOL(iterate_supers_type);