繁体   English   中英

虚拟文件系统如何处理像读写这样的系统调用?

[英]How does the virtual filesystem handle syscalls like read and write?

(所有代码片段均来自: https : //docs.huihoo.com/doxygen/linux/kernel/3.7/dir_97b3d2b63ac216821c2d7a22ee0ab2b0.html

你好! 为了确定我的问题,我一直在研究 Linux fs 代码近一个月来进行研究,但我被困在这里。 所以,我在看这个代码include/linux/fs.h (其中,如果我没看错的已经像代码中使用的几乎所有主要结构和指针的定义read_write.copen.c ),我看到这个代码片断:

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 };

如您所见,他们定义了这些非常具体的系统调用,这些系统调用已在各自的文件中声明。 例如SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)将读写系统调用定义为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 驱动程序的位置)

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

Linux 中的内核文件系统

在 Linux 中,内核文件系统以模块化方式实现。 例如,每个struct inode包含一个指向struct file_operations的指针,该指针与您在问题中复制的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 函数。 这是 read 系统调用的跟踪:

  1. 调用read()系统调用处理程序
  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 在此处定义。 它在多个地方使用,但在此处与一个 inode 相关联。 ext4 定义了一个read_iter处理程序(即ext4_file_read_iter ),但不是一个read处理程序。 因此,当对 ext4 文件调用read(2)时,最终会调用ext4_file_read_iter()

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM