open调用过程
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 path;
struct qstr last;
struct path root;
struct inode *inode; /* path.dentry.d_inode */
unsigned int flags;
unsigned seq;
int last_type;
unsigned depth;
char *saved_names[MAX_NESTED_LINKS + 1]; /* Intent data */
union {
struct open_intent open;
} intent;
};
set_nameidata主要使用参数 pathname 。
static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
{
struct nameidata *old = current->nameidata;
p->stack = p->internal;
p->dfd = dfd;
p->name = name;
p->total_link_count = old ? old->total_link_count : 0;
p->saved = old;
current->nameidata = p;
}
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 分配内存。
path_init为nd的path和inode赋值,为path->dentry赋值. static int link_path_walk 为nd->last 赋值
(3)主要的打开操作在 do_last 中。
link_path_walk 用于一级一级解析文件路径,每一级都调用do_last。 参考:http://alanwu.blog.51cto.com/3652632/1120652
3. 现在我们来分析 link_path_walk
函数原型:static int link_path_walk(const char *name, struct nameidata *nd)
调用方式:link_path_walk(s, nd) ,调用时,s通过 s=path_init(nd, flags)进行了赋值。
(1)首先,while (*name=='/') name++;假设name是/home/user/xxx,这条语句将name定位到h这个地方。疑惑:这里为什么要用while?这不是会导致路径名可以有多个斜杠?
如果while后name为null,则直接返回0. 这表明路径是一个目录。
(2)
4. 最终,do_last 调用 lookup_open, 调用atmoic_open,调用dir->i_op->atomic_open
dir是struct inode类型的,定义在/include/linux/fs.h,其中的 struct inode_operations i_op结构含有 atomic_open指针。
open调用过程的更多相关文章
- FormatMessage与GetLastError配合使用,排查windows api调用过程中的错误
前一段时间在学习windows api调用过程中,遇到过一些调用错误或者程序没能显示预期的结果,或者直接出现vc运行时错误. 这对新手来说是司空见惯的事,因为不太熟悉难免会出错,出错的信息如果能显示很 ...
- pro*c调用过程
数据库内有无参数过程名为procedure. pro*c调用过程 EXEC SQL EXECUTE BEGIN procedure; END; END-EXEC; 需要在cfg配置文件 ...
- Hadoop中客户端和服务器端的方法调用过程
1.Java动态代理实例 Java 动态代理一个简单的demo:(用以对比Hadoop中的动态代理) Hello接口: public interface Hello { void sayHello(S ...
- Hbase的WAL在RegionServer基本调用过程
版权声明:本文由熊训德原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/221 来源:腾云阁 https://www.qclo ...
- Servlet视频学习笔记 57-58 (servlet入门和调用过程)
网易云课堂<30天轻松掌握JavaWeb视频>servlet部分 课时57 servlet开发入门 servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术.S ...
- 最原始的COM组件调用过程(不使用注册表信息)
最原始的COM组件调用过程(不使用注册表信息) 最近因为项目的关系开始研究COM组件了,以前都认为COM过时了,所以也没怎么接触. 现在好好补补课了. 一般调用COM都是通过注册表找到它的位置, 然后 ...
- oracle顺序控制语句goto、null和分页过程中输入输出存储、java程序的调用过程
顺序控制语句1 goto建议不要使用 declare i number:=; begin loop dbms_output.put_line(i); then goto end_loop; end i ...
- .net ADF 中 Ajax 的调用过程.
图示是 .net ADF Ajax调用过程的简略过程: 1,2)当页面初始化之后, 浏览器一旦触发回调事件, 脚本函数负责处理回调信息, 并调用 ASP.NET 2.0/3.5 中的 WebForm_ ...
- alsa声卡分析alsa-utils调用过程
如何分析tinyplay 播放音频和tinymix的过程?需要相应的工具来支持追查: 一.利用strace工具分析tinyplay和tinymix: strace -o tinyplay.log ti ...
- springMVC源码分析--HandlerInterceptor拦截器调用过程(二)
在上一篇博客springMVC源码分析--HandlerInterceptor拦截器(一)中我们介绍了HandlerInterceptor拦截器相关的内容,了解到了HandlerInterceptor ...
随机推荐
- bash变量类型详解
本地变量:作用于当前shell,对当前shell之外的其他shell进程和当前shell子进程均无效. 本地变量赋值为 name='value' value可以是字符串或者是变量,引用变量使用${na ...
- cmd指令
d: 进入D盘: cd job 进入d盘名为job的文件夹:cd显示当前路径: md test创建名为test的文件夹: rd test删除名为test的文件夹: cd.>test.json创 ...
- P3367 并查集【模板】 洛谷
https://www.luogu.org/problem/show?pid=3367#sub 题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整 ...
- SQL2000数据库密码被替换,重置密码提示未能找到存储过程sp_password解决方案
利用windows身份验证进入查询分析器后在master数据库下运行如下脚本: create procedure sp_password @old sysname = NULL, -- the old ...
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...
- [转]Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
原文地址:http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html 在VS 2012 中编译 ...
- QVector的内存分配策略
我们都知道 STL std::vector 作为动态数组在所分配的内存被填满时.假设继续加入数据,std::vector 会另外申请一个大小当前容量两倍的区域(假设 n > size 则申请 n ...
- reorder-list——链表、快慢指针、逆转链表、链表合并
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 8. Smarty3:模版中的内置函数
smarty3中对内置函数的修改比較大,加入了很多新的功能:变量声明.表达式,流程控制,函数.数组等.可是建议不要在模版中去使用过于复杂的逻辑,而是要尽量将一些程序设计逻辑写到PHP中,并在模版中採用 ...
- cin,和几个get函数的用法
1.cin.get(字符变量名):用来接收字符 ch = cin.get(); cin.get(ch); 以上两者均可以 2.cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格 ...