Linux文件系统学习摘要--4
声明:
本文只作为参考,我也是初学者,请辩证的看待文章内容,欢迎各位在评论区更正. 补充经典博文链接。
本文主要记录多进程和文件系统相关的内容
struct task_struct{
…………
/* Filesystem information: */
struct fs_struct *fs;
/* Open file information: */
struct files_struct *files;
…………
}
struct fs_struct {
int users;
spinlock_t lock;
seqcount_t seq;
int umask;
int in_exec;
struct path root, pwd;
} __randomize_layout;
/*
* Open file table structure
*/
struct files_struct {
/*
* read mostly part
*/
atomic_t count;
bool resize_in_progress;
wait_queue_head_t resize_wait;
struct fdtable __rcu *fdt;
struct fdtable fdtab;
/*
* written part on a separate cache line in SMP
*/
spinlock_t file_lock ____cacheline_aligned_in_smp;
unsigned int next_fd;
unsigned long close_on_exec_init[1];
unsigned long open_fds_init[1];
unsigned long full_fds_bits_init[1];
struct file __rcu * fd_array[NR_OPEN_DEFAULT];
};
主要是files_struct 这个结构,其中最后一个fd_array[NR_OPEN_DEFAULT] 就是根据fd找到file的指针,指向的是一个对于全部进程共享的file_table,file是一个对所有进程共享的结构,但是每个进程的fd其实都会有一个file结构,为什么这样设计?后面说。
file结构如下:
struct file {
union {
struct llist_node fu_llist;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
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;
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;
errseq_t f_wb_err;
}
file结构里面有个f_inode指针,指向真正的文件inode,当然前提是已经获取到了真正的inode,
每个file里面都有f_pos,通过这个来保存进程里面的fd读写文件的position,为什么要把这个结构放在全局的结构里面?那我们分析下什么不?为什么不放到全局结构里面,那么就是只能每个进程独享,也就是放到进程结构里面,那么每个fd都要保存一个,跟放到全局里面其实耗的空间是一样的,但是如果调用了fork呢?fork了子进程也要拷贝一份,这样空间就是2倍了,如果保存在全局里面,那么子进程还是指向全局file指针即可,也就是说放在全局可以节省空间,也能起到应有的作用了。file结构里面还有一个f_path,f_path里面有dentry,dentry里面包含了所有已经读取了的inode信息,在没有查到的情况下是通过dentry的operation函数获取?dentry有d_inode,就是inode了。