系统调用-open
入口
open.c/sys_open()
函数签名
asmlinkage long sys_open(const char __user * filename, int flags, int mode)
调用
-
tmp = getname(filename) 将文件字符串从用户态转换到内核态
-
open.c/get_unused_fd() 获取fd,能够获取最小的fd
-
open.c/filp_open(filename, flags, mode) 获取struct file 文件
-
namei.c/open_namei(pathname, int flag, mode, struct nameidata *nd) 查找文件dentry,并判断权限
- 如果非O_CREAT,则查找文件namei.c/path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd),此处查找目标文件
- 如果为O_CREAT,则查找父文件namei.c/path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd)
- namei.c/may_open(nd, acc_mode, flag) 通过inode的权限属性判断权限
- 判断权限
- 如果存在O_TRUNCATE标识,则做截断
-
open.c/dentry_open(dentry, mnt, flags) 通过dentry和mnt创建struct file
- file_tables.c/get_empty_filp() 获取一个空闲的file,判断是否达到最大文件数,并且有足够内存
- 初始化file,包括flag,mode,page cache, 从nd中获取dentry和mnt,初始化ops,f_op操作
-
-
open.c/ fd_install(unsigned int fd, struct file * file) 安装fd,这里指将file放置到current->files->fd[fd]中
- 获取current->files
- 设置到files上,fd作为索引
->files
- 设置到files上,fd作为索引