看来, struct file
包含一个struct inode *
,但都传递给VFS函数。 为什么不简单地传递struct file *
?
例如int (*open) (struct inode *, struct file *);
简短的回答: 由于历史原因 。
他们开始从Linux 2.1中的file_operations
参数中删除struct inode*
,即看一下2.1.60 commit:
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)()
之前就是这样做的,所以它是基本的特性。