码迷,mamicode.com
首页 > 其他好文 > 详细

open调用过程

时间:2017-07-16 12:41:14      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:ddr   文件   .com   lin   cache   linu   填充   调用   eid   

1. 首先传到vfs的do_sys_open,在open.c中。

      long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)

此时,我们只知道open传递过来的一些参数,比如filename,open调用是这样的

     int open(const char *pathname, int flags, mode_t mode);

 因此,只有dfd是新加的,我们先不管他的作用。

do_sys_open主要做以下事情

(1) int fd = build_open_flags(flags, mode, &op);//将open传递过来的flags和mode进行整理,赋值给op

   fd = get_unused_fd_flags(flags);//分配一个未使用的fd

    这个fd就是返回给用户open函数的fd。

(2) tmp = getname(filename); 将文件名做整理,并填充给tmp结构//struct filename *tmp;

(3) struct file *f = do_filp_open(dfd, tmp, &op); 这个就是实际的打开函数,填充struct file

(4) fd_install(fd, f); 将fd与 f进行关联。

 

2. 上面第(3)步是核心内容。首先我们在这里看struct file结构,/include/linux/fs.h

里面有三个比较重要的域。

	struct inode		*f_inode;	/* cached value */
	const struct file_operations	*f_op;
	struct address_space	*f_mapping;

 我们看do_filp_open如何对他们进行填充。

(1)首先创建struct nameidata nd; set_nameidata(&nd, dfd, pathname);  struct nameidata 含有域struct path root; struct inode *inode; struct inode *link_inode;等。set_nameidata主要使用参数 pathname 。

struct task_struct首先使用局部指针指向当前进程的nameidata, struct nameidata *old = current->nameidata; 也即是说,每个进程结构包含一个nameidata

然后只是给nd的total_link_count和pathname赋值。inode并没有管。total_link_count据说是用来防止循环死链的。

(2) filp = path_openat(&nd, op, flags | LOOKUP_RCU);

首先是file = get_empty_filp(); 为struct file 分配内存。

 

(3)主要的打开操作在 do_last 中。

 

link_path_walk 用于一级一级解析文件路径,每一级都调用do_last。 参考:http://alanwu.blog.51cto.com/3652632/1120652

 

open调用过程

标签:ddr   文件   .com   lin   cache   linu   填充   调用   eid   

原文地址:http://www.cnblogs.com/bettersky/p/7189765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!