在较新的Linux中,ext4中的哪个函数负责读取?

时间:2017-07-15 15:08:25

标签: linux filesystems system-calls vfs ext4

传统文件系统创建 struct file_operations 结构以实现VFS功能。例如,在ext4(Linux 4.0及之前版本)中, struct file_operations ext4_file_operations 使读指针指向new_sync_read。

Linux 4.0 /fs/ext4/file.c

const struct file_operations ext4_dax_file_operations = {
    .read       = new_sync_read,
    .read_iter  = generic_file_read_iter,
     ....
}

但是,在Linux 4.1及更高版本中,读指针没有这样的赋值,但是添加了splice_read指针。

Linux 4.1 /fs/ext4/file.c

const struct file_operations ext4_file_operations = {   
    .read_iter  = generic_file_read_iter,
    .splice_read    = generic_file_splice_read,
    ...
}

但" /include/linux/fs.h"中定义的 struct file_operations ;仍然有读指针。那么,ext4中的哪个函数现在负责传统的读函数?

2 个答案:

答案 0 :(得分:1)

我已经通过编写一个新的文件系统进行了测试,发现如果我们初始化两个指针,那么如果我使用 cat 命令,则会调用.read。如果我使用 cat 命令而未初始化.read但初始化.read_iter则会调用.read_iter

答案 1 :(得分:1)

我知道这个问题已经很老了,但是我实际上是在寻找相同的东西并找到了答案。

在Linux 5.8的vfs_read()函数中,

if (file->f_op->read)
    ret = file->f_op->read(file, buf, count, pos);
else if (file->f_op->read_iter)
    ret = new_sync_read(file, buf, count, pos);

这些行查找.read是否由file的文件操作(f_op)定义。 如果不是,则.read_iter中的new_sync_read()调用将代替读操作。

相关问题