为什么VFS函数同时获取结构inode和文件?

时间:2015-03-13 12:46:45

标签: linux linux-kernel linux-device-driver

struct file似乎包含struct inode *,但两者都传递给VFS函数。为什么不单独通过struct file *

例如int (*open) (struct inode *, struct file *);

1 个答案:

答案 0 :(得分:2)

简短回答:因历史原因

他们开始从Linux 2.1中的struct inode*参数中删除file_operations - 即看看2.1.60提交:

http://repo.or.cz/w/davej-history.git/blobdiff/1018aab0cbe3c20a69685bfa5d217d3535067686..be6cc637f086b24887a11bd4bcc7445220c9b0ff:/include/linux/fs.h

@@ -533,9 +534,9 @@ struct super_block {
typedef int (*filldir_t)(void *, const char *, int, off_t, ino_t);

struct file_operations {
-       long long (*llseek) (struct file *, long long, int);
-       long (*read) (struct inode *, struct file *, char *, unsigned long);
-       long (*write) (struct inode *, struct file *, const char *, unsigned long);
+       loff_t (*llseek) (struct file *, loff_t, int);
+       ssize_t (*read) (struct file *, char *, size_t, loff_t *);
+       ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
        int (*readdir) (struct file *, void *, filldir_t);
        unsigned int (*poll) (struct file *, poll_table *);
        int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);

我不知道为什么他们没有为(*open)()做到这一点 - 可能是因为在我们打开文件时,struct file*的初始化没有完成。

在现代内核中do_dentry_open()在调用(*open)()之前执行此操作,因此它是基本功能。