传统的文件系统创建一个结构file_operations结构来实现VFS函数。例如,在 ext4(Linux 4.0 及更早版本(中,结构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"中定义的结构file_operations仍然具有读取指针。那么,现在 ext4 中的哪个函数负责传统的读取函数呢?
我知道这个问题已经很老了,但我实际上正在寻找同样的东西并找到了答案。
在 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
(定义。 否则,new_sync_read()
中的.read_iter
调用将改为处理读取操作。
我已经通过编写一个新的文件系统进行了测试,发现如果我们初始化两个指针,那么如果我使用cat命令.read
就会调用它。如果我使用cat命令而不初始化.read
而是初始化.read_iter
则调用.read_iter
。