Linux VFS 之 open系统调用(kernel 3.4)

本文详细介绍了Linux 3.4.67版本中open系统调用的工作流程,从mount到do_filp_open,再到fd_install。通过kernel代码分析,揭示了open命令如何在fs/namei.c和fs/open.c中实现,以及如何建立进程的current->files对象与file文件对象之间的关联。
摘要由CSDN通过智能技术生成

linux version: 3.4.67

kernel 代码

fs/open.c

fs/namei.c

open命令调用关系

mount- > /fs/open.c/SYSCALL_DEFINE3(open, ....); -> do_sys_open->get_unused_fd_flags->

->do_filp_open->fd_install

根据调用关系对各函数注解

1、do_sys_open

long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
{
struct open_flags op;
int lookup = build_open_flags(flags, mode, &op);
char *tmp = getname(filename);
int fd = PTR_ERR(tmp);
if (!IS_ERR(tmp)) {
fd = get_unused_fd_flags(flags);   //获取一个未使用的文件描述符,参考fs/file.c文件alloc_fd() ,后续仔细研究file.c文件。
if (fd >= 0) {
struct file *f = do_filp_open(dfd, tmp, &op, lookup);  //直接调用path_openat打开文件,返回file文件对象。
if (IS_ERR(f)) {
put_unused_fd(fd);
fd = PTR_ERR(f);
} else {
fsnotify_open(f);
fd_install(fd, f);  //file文件对象与当前进程current关联。fd作为file对象的索引,这样file与fd也一一对应。
}
}
putname(tmp);
}
return fd;  //返回文件描述符
}


2、path_openat
static struct file *path_openat(int dfd, const char *pathname,
struct nameidata *nd, const struct open_flags *op, int flags)
{
struct file *base =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值