简体   繁体   English

Linux内核vfs_write函数混乱

[英]Linux Kernel vfs_write function confusion

I was looking at the old Linux kernel code (3.10.1), particularly the IO path. 我正在查看旧的Linux内核代码(3.10.1),尤其是IO路径。

So when the IO enters the VFS layer, the function vfs_write() is called. 因此,当IO进入VFS层时,将调用函数vfs_write()

Here I can see a call to file->f_op->write() , which is a blocking call as the man page of the system call write() says. 在这里,我可以看到对file->f_op->write()调用,这是一个阻塞调用,正如系统调用write()的手册页所述。

The other option in the code is when file->f_op->write pointer is not defined, in that case vfs_write() calls do_sync_write() . 代码中的另一个选项是file->f_op->write指针时,在这种情况下, vfs_write()调用do_sync_write()

do_sync_write() goes ahead and calls filp->f_op->aio_write() , which is an async call as the man page of aio_write() explains. do_sync_write()继续并调用filp->f_op->aio_write() ,这是一个异步调用,如aio_write()的手册页所述。

Now, my question is, why was the function do_sync_write() named "sync", when it clearly goes on to call an async IO function? 现在,我的问题是,当函数do_sync_write()显然可以继续调用异步IO函数时,为什么将其命名为“ sync”?

I might be missing something probably, or there was a blunder made here back in those times? 也许我可能错过了一些东西,或者那时候在这里犯了一个大错?

Function definitions for reference, 功能定义供参考,

ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
    ssize_t ret;

    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;
        file_start_write(file);
        if (file->f_op->write)
            ret = file->f_op->write(file, buf, count, pos);
        else
            ret = do_sync_write(file, buf, count, pos);
        if (ret > 0) {
            fsnotify_modify(file);
            add_wchar(current, ret);
        }
        inc_syscw(current);
        file_end_write(file);
    }

    return ret;
}



ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
{
    struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
    struct kiocb kiocb;
    ssize_t ret;

    init_sync_kiocb(&kiocb, filp);
    kiocb.ki_pos = *ppos;
    kiocb.ki_left = len;
    kiocb.ki_nbytes = len;

    ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
    if (-EIOCBQUEUED == ret)
        ret = wait_on_sync_kiocb(&kiocb);
    *ppos = kiocb.ki_pos;
    return ret;
}

why was the function do_sync_write() named "sync", when it clearly goes on to call an async IO function? 为什么函数do_sync_write()名为“ sync”,当它显然可以继续调用异步IO函数时呢?

It calls async function and then waits for its completion with 它调用异步函数,然后等待其完成

ret = wait_on_sync_kiocb(&kiocb);

So from the view of the caller of do_sync_write function, the whole function behavior is synced. 因此,从do_sync_write函数的调用者的do_sync_write ,整个函数的行为都是同步的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM