【问题标题】:In the newer Linux, which function in ext4 is responsible for read?在较新的 Linux 中,ext4 中的哪个函数负责读取?
【发布时间】:2017-12-20 12:57:44
【问题描述】:

传统的文件系统创建一个 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及以后的版本中,read指针没有这样的赋值,而是增加了一个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 中的哪个函数负责常规的读取函数呢?

【问题讨论】:

  • 我现在认为在新版本中常规读取是由read_iter直接实现的,而在旧版本中它是由read_iter间接实现的。如果是这样,那么新版本中 VFS 的 read pinter 的作用是什么?

标签: linux filesystems system-calls vfs ext4


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    在 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() 将改为处理读取操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2018-03-06
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      相关资源
      最近更新 更多