mirror of https://gitee.com/openkylin/linux.git
kernfs: implement ->read_iter
Switch kernfs to implement the read_iter method instead of plain old read to prepare to supporting splice and sendfile again. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20210120204631.274206-2-hch@lst.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
895bee2708
commit
4eaad21a6a
|
@ -14,6 +14,7 @@
|
||||||
#include <linux/pagemap.h>
|
#include <linux/pagemap.h>
|
||||||
#include <linux/sched/mm.h>
|
#include <linux/sched/mm.h>
|
||||||
#include <linux/fsnotify.h>
|
#include <linux/fsnotify.h>
|
||||||
|
#include <linux/uio.h>
|
||||||
|
|
||||||
#include "kernfs-internal.h"
|
#include "kernfs-internal.h"
|
||||||
|
|
||||||
|
@ -180,11 +181,10 @@ static const struct seq_operations kernfs_seq_ops = {
|
||||||
* it difficult to use seq_file. Implement simplistic custom buffering for
|
* it difficult to use seq_file. Implement simplistic custom buffering for
|
||||||
* bin files.
|
* bin files.
|
||||||
*/
|
*/
|
||||||
static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
|
static ssize_t kernfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
|
||||||
char __user *user_buf, size_t count,
|
|
||||||
loff_t *ppos)
|
|
||||||
{
|
{
|
||||||
ssize_t len = min_t(size_t, count, PAGE_SIZE);
|
struct kernfs_open_file *of = kernfs_of(iocb->ki_filp);
|
||||||
|
ssize_t len = min_t(size_t, iov_iter_count(iter), PAGE_SIZE);
|
||||||
const struct kernfs_ops *ops;
|
const struct kernfs_ops *ops;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
|
@ -210,7 +210,7 @@ static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
|
||||||
of->event = atomic_read(&of->kn->attr.open->event);
|
of->event = atomic_read(&of->kn->attr.open->event);
|
||||||
ops = kernfs_ops(of->kn);
|
ops = kernfs_ops(of->kn);
|
||||||
if (ops->read)
|
if (ops->read)
|
||||||
len = ops->read(of, buf, len, *ppos);
|
len = ops->read(of, buf, len, iocb->ki_pos);
|
||||||
else
|
else
|
||||||
len = -EINVAL;
|
len = -EINVAL;
|
||||||
|
|
||||||
|
@ -220,12 +220,12 @@ static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
|
||||||
if (len < 0)
|
if (len < 0)
|
||||||
goto out_free;
|
goto out_free;
|
||||||
|
|
||||||
if (copy_to_user(user_buf, buf, len)) {
|
if (copy_to_iter(buf, len, iter) != len) {
|
||||||
len = -EFAULT;
|
len = -EFAULT;
|
||||||
goto out_free;
|
goto out_free;
|
||||||
}
|
}
|
||||||
|
|
||||||
*ppos += len;
|
iocb->ki_pos += len;
|
||||||
|
|
||||||
out_free:
|
out_free:
|
||||||
if (buf == of->prealloc_buf)
|
if (buf == of->prealloc_buf)
|
||||||
|
@ -235,22 +235,11 @@ static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static ssize_t kernfs_fop_read_iter(struct kiocb *iocb, struct iov_iter *iter)
|
||||||
* kernfs_fop_read - kernfs vfs read callback
|
|
||||||
* @file: file pointer
|
|
||||||
* @user_buf: data to write
|
|
||||||
* @count: number of bytes
|
|
||||||
* @ppos: starting offset
|
|
||||||
*/
|
|
||||||
static ssize_t kernfs_fop_read(struct file *file, char __user *user_buf,
|
|
||||||
size_t count, loff_t *ppos)
|
|
||||||
{
|
{
|
||||||
struct kernfs_open_file *of = kernfs_of(file);
|
if (kernfs_of(iocb->ki_filp)->kn->flags & KERNFS_HAS_SEQ_SHOW)
|
||||||
|
return seq_read_iter(iocb, iter);
|
||||||
if (of->kn->flags & KERNFS_HAS_SEQ_SHOW)
|
return kernfs_file_read_iter(iocb, iter);
|
||||||
return seq_read(file, user_buf, count, ppos);
|
|
||||||
else
|
|
||||||
return kernfs_file_direct_read(of, user_buf, count, ppos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -960,7 +949,7 @@ void kernfs_notify(struct kernfs_node *kn)
|
||||||
EXPORT_SYMBOL_GPL(kernfs_notify);
|
EXPORT_SYMBOL_GPL(kernfs_notify);
|
||||||
|
|
||||||
const struct file_operations kernfs_file_fops = {
|
const struct file_operations kernfs_file_fops = {
|
||||||
.read = kernfs_fop_read,
|
.read_iter = kernfs_fop_read_iter,
|
||||||
.write = kernfs_fop_write,
|
.write = kernfs_fop_write,
|
||||||
.llseek = generic_file_llseek,
|
.llseek = generic_file_llseek,
|
||||||
.mmap = kernfs_fop_mmap,
|
.mmap = kernfs_fop_mmap,
|
||||||
|
|
Loading…
Reference in New Issue