Understanding loff_t *offp for file_operations

前端 未结 2 1491
误落风尘
误落风尘 2020-12-13 06:52

I\'m designing a device driver that simply reads and writes to a character buffer. My question is however regarding the two functions in the file_operations str

2条回答
  •  清歌不尽
    2020-12-13 07:34

    Torek's answer is excellent. Just adding a bit extra detail/context... From an earlier Linux kernel (2.6.28), here is an example of offset in use in a system call... it copies the offset from user space to a temporary variable before getting into the kernel driver-invocation mechanism, and then copies it back out to the user file. This is how the offset the driver sees is decoupled from the user view of it, and facilitates the situations where offset is NULL in the system call, so no SEGVIO occurs.

    SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
    {
        loff_t pos;
        ssize_t ret;
    
        if (offset) {
            if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
                return -EFAULT;
            ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
            if (unlikely(put_user(pos, offset)))
                return -EFAULT;
            return ret;
        }
    
        return do_sendfile(out_fd, in_fd, NULL, count, 0);
    }
    

提交回复
热议问题