---------------------------------------------------------------------
文件指针说明当前的文件偏移位置,即下一个操作(读或写等)将在该位置发生。由于几个文件可能同时访问同一个文件,因此文件指针必须存放在file对象而不是索引节点对象中。
文件对象包含在具体文件系统的超级块的几个链表中。每个超级快对象把文件对象链表的表头存放在s_files字段中。链表中分别指向前一个元素和后一个元素的指针都存放在文件对象的fu_list字段中。
文件对象的f_count字段是一个引用计数器:它记录使用文件对象的进程数(以CLONE_FILES标志创建的轻量级进程共享文件描述符表,因此他们可以使用相同的文件对象)。当内核本身要使用该文件对象时也要增加计数器的值。
每个文件系统都有自己的文件操作集合,执行诸如读写文件这样的操作。当内核将一个索引节点从磁盘装入内核时,就会把指向这些文件操作的指针存放在file_operations结构中,而该结构的地址存放在该索引节点的i_fop字段中。当进程打开文件时,VFS就用存放在索引节点中的这个地址初始化新文件对象的f_op字段,使得对文件操作的后续调用能够使用这些函数。如果需要,VFS随后也可以通过在f_op字段存放一个新值而修改文件操作的集合。file_operations定义如下:
---------------------------------------------------------------------
include/linux/fs.h
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 (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*aio_fsync) (struct kiocb *, 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 **);
};
---------------------------------------------------------------------
该结构的各成员,与同名的系统调用有着相同的语义,包括参数和返回值,这里也就不再做过多的解释了。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-76619-10.html
该亮剑了