接上篇vfs四大对象(上)——基于linux4.18
4. 文件对象
文件对象描述进程怎样与一个打开的文件进行交互。文件对象是在文件被打开时创建的。注意:文件对象在磁盘上没有对应的映像,因此file结构体中没有dirty字段表示文件对象是否被修改。
4.1 文件对象数据结构
fs.h
struct file {
union {
struct llist_node fu_llist; /* 用于通用文件对象链表的指针 */
struct rcu_head fu_rcuhead; /* 回收文件对象时,由RCU描述符使用 */
} f_u;
struct path f_path; // 包含挂载点和dentry
struct inode *f_inode; /* cached value */
const struct file_operations *f_op; /* 指向文件操作表的指针 */
/*
* Protects f_ep_links, f_flags.
* Must not be taken from IRQ context.
*/
spinlock_t f_lock;
enum rw_hint f_write_hint;
atomic_long_t f_count; /* 文件对象的引用计数器 */
unsigned int f_flags; /* 打开文件时所指定的标志,包括O_RDONLY、O_WRONLY、O_RDWR、O_APPEND、O_CREAT等。 */
fmode_t f_mode; /* 进程的访问模式 */
struct mutex f_pos_lock;
loff_t f_pos; /* 当前的文件位置(文件指针) */
struct fown_struct f_owner; // 文件的拥有者
const struct cred *f_cred;
struct file_ra_state f_ra; // 文件的预读状态
u64 f_version; /* 版本号,每次使用后自动递增 */
#ifdef CONFIG_SECURITY
void *f_security; /* 指向文件对象的安全结构的指针 */
#endif
/* needed for tty driver, and maybe others */
void *private_data; /* 指向特定文件系统或设备驱动程序所需要的数据的指针 */
#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links; /* 文件的事件轮询等待者链表的头 */
struct list_head f_tfile_llink;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping; /* 指向文件对应的address_space对象的指针,用于管理文件的页缓存 */
errseq_t f_wb_err;
}
4.2 关键字段
fu_list:
每个超级块对象把文件对象链表的头存放在 s_files 字段中,因此,属于不同文件系统的文件对象包含在不同的链表中。fu_list 字段链接链表中的相邻元素。
f_pos:
文件指针存放的是文件中当前的位置,由于几个进程可能同时访问同一文件,因此指针必须存放在文件对象而不是索引节点对象中。
f_count:
记录使用文件对象的进程数(以 CLONE_FILES标志创建的轻量级进程共享打开文件表,他们可以使用相同的文件对象)。
f_flags:
通过该字段可以解析出打开文件时指定的标志。
4.3 文件操作
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int); // 移动文件指针时调用的函数,用于将文件指针移动到指定位置。
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
int (*iterate) (struct file *, struct dir_context *);
int (*iterate_shared) (struct file *, struct dir_context *);
__poll_t (*poll) (struct file *, struct poll_table_struct *); // 检查是否在一个文件上有操作发生,如果没有则睡眠,直到该文件上有操作发生。
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); //用于执行非阻塞设备控制操作。
long (*compat_ioctl) (struct file *, unsigned int, unsigned long); // 用于执行兼容性设备控制操作, 64位的内核使用该方法执行32位的系统调用 ioctl()
int (*mmap) (struct file *, struct vm_area_struct *); // 将文件映射到内存中时调用的函数,用于将文件映射到内存中以便更快地访问。
unsigned long mmap_supported_flags;
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync); // 将文件所缓存的全部数据写入磁盘。
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); // 用于获取未映射的内存区域
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **, void **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
unsigned (*mmap_capabilities)(struct file *);
#endif
ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
loff_t, size_t, unsigned int);
int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t,
u64);
ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *,
u64);
}
4.4 文件操作集的初始化
当内核将一个索引节点从磁盘装入内存时,就会把指向这些文件操作的指针放在file_operations 结构体中,该结构体地址放在该索引节点对象的 i_fop 字段中。当进程打开这个文件时,VFS就用存放在索引节点中的这个地址初始化新文件对象的 f_op 字段,使得后续操作可以调用这些函数。
long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
struct file *f = do_filp_open(dfd, tmp, &op);
filp = path_openat(&nd, op, flags | LOOKUP_RCU);
error = do_last(nd, file, op, &opened)
error = vfs_open(&nd->path, file, current_cred());
do_dentry_open(file, d_backing_inode(dentry), NULL, cred);
f->f_op = fops_get(inode->i_fop);
4.5 进程与文件对象
(摘自深入《理解linux内核》)
如图所示是一个简单的示例,说明进程怎样与文件进行交互。三个不同的进程已经打开同一个文件,其中两个进程使用同一个硬链接。在这种情况下,其中的每个进程都使用自己的文件对象,但只需要两个目录项对象,每个硬链接对应一个目录项对象。这两个目录项对象指向同一个索引节点对象,该索引节点对象标识超级块对象,以及随后的普通磁盘文件。