open系统调用在内核中的流程分析

来源:互联网 发布:西南期货软件 编辑:程序博客网 时间:2024/05/17 08:13
int open(const char *pathname, int flags, mode_t mode);   --系统调用
                        ||
                        \/
long sys_open(const char __user *filename, int flags, int mode)   -- fs/open.c
/*对应内核中的open接口函数*/
                        ||
                        \/
long do_sys_open(int dfd, const char __user *filename, int flags, int mode) --fs/open.c
/*用户空间的filename被拷贝到内核空间,获取当前可用的文件描述符*/
                        ||
                        \/
static struct file *do_filp_open(int dfd, const char *filename, int flags, int mode) --fs/open.c 
                        ||
                        \/
int open_namei(int dfd, const char *pathname, int flag,
   int mode, struct nameidata *nd)
/*获取该文件对应的nameidata结构.该函数执行完毕,接着调用下面函数。这两个函数是顺序被do_filp_open调用*/
                        ||
                        \/
struct file *nameidata_to_filp(struct nameidata *nd, int flags) --fs/open.c
/*将nameidata 结构转换为打开的struct file结构*/
                        ||
                        \/
static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
    int flags, struct file *f,
    int (*open)(struct inode *, struct file *)) --fs/open.c
                        ||
                        \/
f->f_op = fops_get(inode->i_fop);   --fs/open.c
/*这里将系统调用中需要对应打开文件对应到内核中的file_operations结构体获取到,然后根据其函数
指针就可以找到该结构体中对该种文件操作的所有方法。scull对应的结构体是在scull_init的时候向内
核注册的。
                        ||
                        \/
open = f->f_op->open; 
open(inode, f); --fs/open.c
/*以上两行代码分别完成了open系统调用时执行实际文件对应内核的open方法,即scull_open
原创粉丝点击