struct file
似乎包含struct inode *
,但两者都传递给VFS函数。为什么不单独通过struct file *
?
例如int (*open) (struct inode *, struct file *);
答案 0 :(得分:2)
简短回答:因历史原因。
他们开始从Linux 2.1中的struct inode*
参数中删除file_operations
- 即看看2.1.60提交:
@@ -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)()
之前执行此操作,因此它是基本功能。