Linux VFS 之 write/read系统调用(kernel 3.4)

linux version: 3.4.67


kernel 代码

fs/read_write.c


write系统调用函数注解

SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
size_t, count)
{
struct file *file;
ssize_t ret = -EBADF;
int fput_needed;
file = fget_light(fd, &fput_needed);  //通过fd从current进程文件对象table里找出file对象
if (file) {
loff_t pos = file_pos_read(file);     //获取文件位置
ret = vfs_write(file, buf, count, &pos);     //调用vfs_write,这里会调用特定文件系统的file_operations->write做写动作。
file_pos_write(file, pos);                   //更改文件位置
fput_light(file, fput_needed);                   // 释放file对象
}
return ret;
}


ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
ssize_t ret;
struct task_struct *tsk = current;
struct kstatfs stat;
//static long long store = 0;
unsigned char num = 0;
struct mount *mount_data;
char *file_list[10] = {"ccci_fsd", "ccci2_fsd", "eemcs_fsd", NULL};
mount_data = real_mount(file->f_path.mnt);


if (!(file->f_mode & FMODE_WRITE))  
return -EBADF;
if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
return -EINVAL;
if (unlikely(!access_ok(VERIFY_READ, buf, count)))
return -EFAULT;

ret = rw_verify_area(WRITE, file, pos, count);
if (ret >= 0) {
count = ret;
if (file->f_op->write)
ret = file->f_op->write(file, buf, count, pos);    //调用真实文件系统的file_operations->write接口,做写入工作。

                        //注:从这可以看出vfs没有对写入文件内容进程缓存
else
ret = do_sync_write(file, buf, count, pos);       //调用aio_write,和write有什么区别? 后面再研究。

if (ret > 0) {
fsnotify_modify(file);                   //fs nodify 
add_wchar(current, ret);
}
inc_syscw(current);
}

return ret;
}


fs/read_write.c提供的内核常用方法

vfs_llseek

vfs_read

vfs_write


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值