linux内核源码分析之虚拟文件系统VFS
Posted 为了维护世界和平_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux内核源码分析之虚拟文件系统VFS相关的知识,希望对你有一定的参考价值。
系统调用
read()->ksys_read()
ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
struct fd f = fdget_pos(fd);//根据fd号从task的文件描述符中获取file指针
ssize_t ret = -EBADF;
if (f.file)
loff_t pos, *ppos = file_ppos(f.file);//找到文件当前读写位置
if (ppos)
pos = *ppos;
ppos = &pos;
ret = vfs_read(f.file, buf, count, ppos);//执行file->f_op->read/->read_iter
if (ret >= 0 && ppos)
f.file->f_pos = pos;//更新读取位置
fdput_pos(f);//
return ret;
SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
return ksys_read(fd, buf, count);
vfs_read()->__vfs_read()
ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
loff_t *pos)
if (file->f_op->read)
return file->f_op->read(file, buf, count, pos);
else if (file->f_op->read_iter)
return new_sync_read(file, buf, count, pos);
else
return -EINVAL;
重要!!!结构体之间的关联
- vfs_read会调用file->f_op->read_iter函数。
- file->f_op是在文件打开时,在do_dentry_open中赋值为inode->i__fop
- inode->i_fop是在初始化inode时,在ext4_iget中赋值为&ext4_file_operations。
const struct file_operations ext2_file_operations =
.llseek = generic_file_llseek,
.read_iter = ext2_file_read_iter,
.write_iter = ext2_file_write_iter,
.unlocked_ioctl = ext2_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = ext2_compat_ioctl,
#endif
.mmap = ext2_file_mmap,
.open = dquot_file_open,
.release = ext2_release_file,
.fsync = ext2_fsync,
.get_unmapped_area = thp_get_unmapped_area,
.splice_read = generic_file_splice_read,
.splice_write = iter_file_splice_write,
;
void ext2_set_file_ops(struct inode *inode)
inode->i_op = &ext2_file_inode_operations;
inode->i_fop = &ext2_file_operations;
if (IS_DAX(inode))
inode->i_mapping->a_ops = &ext2_dax_aops;
else if (test_opt(inode->i_sb, NOBH))
inode->i_mapping->a_ops = &ext2_nobh_aops;
else
inode->i_mapping->a_ops = &ext2_aops;
参考链接
VFS(一) 虚拟文件系统概述 - 知乎 (zhihu.com)
VFS(二) 读文件的过程中发生了什么 - 知乎 (zhihu.com)
Linux内核源码/内存调优/文件系统/进程管理/设备驱动/网络协议栈-学习视频教程-腾讯课堂
以上是关于linux内核源码分析之虚拟文件系统VFS的主要内容,如果未能解决你的问题,请参考以下文章