【问题标题】:How does the virtual filesystem handle syscalls like read and write?虚拟文件系统如何处理读取和写入等系统调用?
【发布时间】:2021-07-17 01:33:33
【问题描述】:

(所有sn-ps代码均取自:https://docs.huihoo.com/doxygen/linux/kernel/3.7/dir_97b3d2b63ac216821c2d7a22ee0ab2b0.html

嗨!为了确定我的问题,我一直在研究 Linux fs 代码近一个月来进行研究,但我被困在这里。所以我正在查看include/linux/fs.h 中的这段代码(如果我没记错的话,几乎所有主要结构和指针的定义都被read_write.copen.c 等代码使用)并且我观察到这段代码sn-p:

struct file_operations {
 1519     struct module *owner;
 1520     loff_t (*llseek) (struct file *, loff_t, int);
 1521     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
 1522     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
 1523     ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
 1524     ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
 1525     int (*readdir) (struct file *, void *, filldir_t);
 1526     unsigned int (*poll) (struct file *, struct poll_table_struct *);
 1527     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
 1528     long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
 1529     int (*mmap) (struct file *, struct vm_area_struct *);
 1530     int (*open) (struct inode *, struct file *);
 1531     int (*flush) (struct file *, fl_owner_t id);
 1532     int (*release) (struct inode *, struct file *);
 1533     int (*fsync) (struct file *, loff_t, loff_t, int datasync);
 1534     int (*aio_fsync) (struct kiocb *, int datasync);
 1535     int (*fasync) (int, struct file *, int);
 1536     int (*lock) (struct file *, int, struct file_lock *);
 1537     ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 1538     unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
 1539     int (*check_flags)(int);
 1540     int (*flock) (struct file *, int, struct file_lock *);
 1541     ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
 1542     ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
 1543     int (*setlease)(struct file *, long, struct file_lock **);
 1544     long (*fallocate)(struct file *file, int mode, loff_t offset,
 1545               loff_t len);
 1546 };

在这里,您可以看到他们已经定义了这些非常具体的系统调用,这些系统调用已在各自的文件中声明。例如 read_write.c 的 read 和 write 系统调用定义分别为 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count)。现在出于研究目的,我基本上进入了这两个定义,并追查了每个函数调用(至少是那些在 Doxygen 文档中链接的)发生在它们中的每个函数以及这些函数调用中但无法回答的函数调用一个非常简单的问题。 这两个系统调用如何调用虚拟文件系统以进一步调用从文件系统读取实际数据块所需的驱动程序? (如果它是特定于文件系统的,那么请告诉我代码中将其交给 FS 驱动程序的位置)

附:我对打开的系统调用进行了同样的搜索,但能够找到他们调用namei.c 代码的一部分以专门执行该任务的位置:struct file *do_filp_open(int dfd, struct filename *pathname, const struct open_flags *op, int flags)。在这里,他们使用具有来自 inode 的相关信息的结构 nameidata 来打开文件。

【问题讨论】:

    标签: c linux-kernel filesystems vfs


    【解决方案1】:

    Linux 中的内核内文件系统

    在 Linux 中,内核内文件系统以模块化方式实现。例如,每个struct inode 都包含一个指向struct file_operations 的指针,即您在问题中复制的相同结构。该结构包含各种文件操作的函数指针。

    例如,成员ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); 是一个函数指针,指向一个函数,该函数接受struct file *char *size_tloff_t * 作为参数,并返回一个ssize_t

    将系统调用路由到底层文件系统

    当read系统调用发生时,内核VFS代码找到对应的inode,然后调用struct file_operations中指定的文件系统的read函数。这是读取系统调用的痕迹:

    1. read() syscall handler 被调用,
    2. 调用ksys_read()
    3. 调用vfs_read()

    这就是 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);
    else
        ret = -EINVAL;
    

    相关结构struct file 还包含一个指向struct file_operations 的指针。上面的 if 条件检查该文件是否有 read() 处理程序,如果存在则调用它。如果read() 处理程序不存在,它会检查read_iter 处理程序。如果都不存在,则返回-EINVAL

    示例:ext4

    在 ext4 中,struct file_operations 定义为 here。它在多个地方使用,但它与 inode here 相关联。 ext4 定义了一个read_iter 处理程序(即ext4_file_read_iter),但没有定义一个read 处理程序。因此,当在 ext4 文件上调用 read(2) 时,最终会调用 ext4_file_read_iter()

    此时,我们已经了解了文件系统特定的代码。可以从这里进一步探索 ext4 如何管理块。

    【讨论】:

    • 非常感谢@Sagar。这是一个很好的解释,正是我想要的。只是一个后续问题:vfs_read 代码是访问 inode 还是 ext4 文件处理它?我想知道是否有一个地方可以更改传递给 read 系统调用的 inode 值?
    • @Preet Derasari,inode 由文件系统实例化,而不是 VFS。但是,struct inode 的内容可以由 VFS 修改和访问。至于更改索引节点的字段,最好的办法是更改目标文件系统的 read() 处理程序。确定实现这一目标的正确方法可能很棘手。
    【解决方案2】:

    我建议使用 ftrace 来找出完整的代码库。它提供了内核中所有的函数调用跟踪。

    https://lwn.net/Articles/370423/

    【讨论】:

      猜你喜欢
      • 2014-12-31
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 2013-05-10
      相关资源
      最近更新 更多