linux version: 3.4.67
kernel 代码
fs/open.cfs/namei.c
open命令调用关系
mount- > /fs/open.c/SYSCALL_DEFINE3(open, ....); -> do_sys_open->get_unused_fd_flags->
->do_filp_open->fd_install
根据调用关系对各函数注解
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; //返回文件描述符
}
struct nameidata *nd, const struct open_flags *op, int flags)
{
struct file *base =