Commit c66ab6fa authored by Jens Axboe's avatar Jens Axboe

splice: abstract out actor data

For direct splicing (or private splicing), the output may not be a file.
So abstract out the handling into a specified actor function and put
the data in the splice_desc structure earlier, so we can build on top
of that.

This is the first step in better splice handling for drivers, and also
for implementing vmsplice _to_ user memory.
Signed-off-by: default avatarJens Axboe <jens.axboe@oracle.com>
parent 71780f59
...@@ -1636,9 +1636,14 @@ static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe, ...@@ -1636,9 +1636,14 @@ static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
int ret, err; int ret, err;
struct address_space *mapping = out->f_mapping; struct address_space *mapping = out->f_mapping;
struct inode *inode = mapping->host; struct inode *inode = mapping->host;
struct splice_desc sd = {
ret = __splice_from_pipe(pipe, out, ppos, len, flags, .total_len = len,
ocfs2_splice_write_actor); .flags = flags,
.pos = *ppos,
.file = out,
};
ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
if (ret > 0) { if (ret > 0) {
*ppos += ret; *ppos += ret;
......
...@@ -668,31 +668,24 @@ static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, ...@@ -668,31 +668,24 @@ static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
* key here is the 'actor' worker passed in that actually moves the data * key here is the 'actor' worker passed in that actually moves the data
* to the wanted destination. See pipe_to_file/pipe_to_sendpage above. * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
*/ */
ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
struct file *out, loff_t *ppos, size_t len, splice_actor *actor)
unsigned int flags, splice_actor *actor)
{ {
int ret, do_wakeup, err; int ret, do_wakeup, err;
struct splice_desc sd;
ret = 0; ret = 0;
do_wakeup = 0; do_wakeup = 0;
sd.total_len = len;
sd.flags = flags;
sd.file = out;
sd.pos = *ppos;
for (;;) { for (;;) {
if (pipe->nrbufs) { if (pipe->nrbufs) {
struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
const struct pipe_buf_operations *ops = buf->ops; const struct pipe_buf_operations *ops = buf->ops;
sd.len = buf->len; sd->len = buf->len;
if (sd.len > sd.total_len) if (sd->len > sd->total_len)
sd.len = sd.total_len; sd->len = sd->total_len;
err = actor(pipe, buf, &sd); err = actor(pipe, buf, sd);
if (err <= 0) { if (err <= 0) {
if (!ret && err != -ENODATA) if (!ret && err != -ENODATA)
ret = err; ret = err;
...@@ -704,10 +697,10 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, ...@@ -704,10 +697,10 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
buf->offset += err; buf->offset += err;
buf->len -= err; buf->len -= err;
sd.len -= err; sd->len -= err;
sd.pos += err; sd->pos += err;
sd.total_len -= err; sd->total_len -= err;
if (sd.len) if (sd->len)
continue; continue;
if (!buf->len) { if (!buf->len) {
...@@ -719,7 +712,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, ...@@ -719,7 +712,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
do_wakeup = 1; do_wakeup = 1;
} }
if (!sd.total_len) if (!sd->total_len)
break; break;
} }
...@@ -732,7 +725,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, ...@@ -732,7 +725,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
break; break;
} }
if (flags & SPLICE_F_NONBLOCK) { if (sd->flags & SPLICE_F_NONBLOCK) {
if (!ret) if (!ret)
ret = -EAGAIN; ret = -EAGAIN;
break; break;
...@@ -772,6 +765,12 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, ...@@ -772,6 +765,12 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
{ {
ssize_t ret; ssize_t ret;
struct inode *inode = out->f_mapping->host; struct inode *inode = out->f_mapping->host;
struct splice_desc sd = {
.total_len = len,
.flags = flags,
.pos = *ppos,
.file = out,
};
/* /*
* The actor worker might be calling ->prepare_write and * The actor worker might be calling ->prepare_write and
...@@ -780,7 +779,7 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, ...@@ -780,7 +779,7 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
* pipe->inode, we have to order lock acquiry here. * pipe->inode, we have to order lock acquiry here.
*/ */
inode_double_lock(inode, pipe->inode); inode_double_lock(inode, pipe->inode);
ret = __splice_from_pipe(pipe, out, ppos, len, flags, actor); ret = __splice_from_pipe(pipe, &sd, actor);
inode_double_unlock(inode, pipe->inode); inode_double_unlock(inode, pipe->inode);
return ret; return ret;
...@@ -804,6 +803,12 @@ generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out, ...@@ -804,6 +803,12 @@ generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
{ {
struct address_space *mapping = out->f_mapping; struct address_space *mapping = out->f_mapping;
struct inode *inode = mapping->host; struct inode *inode = mapping->host;
struct splice_desc sd = {
.total_len = len,
.flags = flags,
.pos = *ppos,
.file = out,
};
ssize_t ret; ssize_t ret;
int err; int err;
...@@ -811,7 +816,7 @@ generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out, ...@@ -811,7 +816,7 @@ generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
if (unlikely(err)) if (unlikely(err))
return err; return err;
ret = __splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file); ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
if (ret > 0) { if (ret > 0) {
unsigned long nr_pages; unsigned long nr_pages;
...@@ -956,14 +961,17 @@ static long do_splice_to(struct file *in, loff_t *ppos, ...@@ -956,14 +961,17 @@ static long do_splice_to(struct file *in, loff_t *ppos,
return in->f_op->splice_read(in, ppos, pipe, len, flags); return in->f_op->splice_read(in, ppos, pipe, len, flags);
} }
long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, /*
size_t len, unsigned int flags) * Splices from an input file to an actor, using a 'direct' pipe.
*/
ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
splice_direct_actor *actor)
{ {
struct pipe_inode_info *pipe; struct pipe_inode_info *pipe;
long ret, bytes; long ret, bytes;
loff_t out_off;
umode_t i_mode; umode_t i_mode;
int i; size_t len;
int i, flags;
/* /*
* We require the input being a regular file, as we don't want to * We require the input being a regular file, as we don't want to
...@@ -999,7 +1007,13 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, ...@@ -999,7 +1007,13 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
*/ */
ret = 0; ret = 0;
bytes = 0; bytes = 0;
out_off = 0; len = sd->total_len;
flags = sd->flags;
/*
* Don't block on output, we have to drain the direct pipe.
*/
sd->flags &= ~SPLICE_F_NONBLOCK;
while (len) { while (len) {
size_t read_len, max_read_len; size_t read_len, max_read_len;
...@@ -1009,19 +1023,19 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, ...@@ -1009,19 +1023,19 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
*/ */
max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE)); max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
ret = do_splice_to(in, ppos, pipe, max_read_len, flags); ret = do_splice_to(in, &sd->pos, pipe, max_read_len, flags);
if (unlikely(ret < 0)) if (unlikely(ret < 0))
goto out_release; goto out_release;
read_len = ret; read_len = ret;
sd->total_len = read_len;
/* /*
* NOTE: nonblocking mode only applies to the input. We * NOTE: nonblocking mode only applies to the input. We
* must not do the output in nonblocking mode as then we * must not do the output in nonblocking mode as then we
* could get stuck data in the internal pipe: * could get stuck data in the internal pipe:
*/ */
ret = do_splice_from(pipe, out, &out_off, read_len, ret = actor(pipe, sd);
flags & ~SPLICE_F_NONBLOCK);
if (unlikely(ret < 0)) if (unlikely(ret < 0))
goto out_release; goto out_release;
...@@ -1066,6 +1080,33 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, ...@@ -1066,6 +1080,33 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
return bytes; return bytes;
return ret; return ret;
}
EXPORT_SYMBOL(splice_direct_to_actor);
static int direct_splice_actor(struct pipe_inode_info *pipe,
struct splice_desc *sd)
{
struct file *file = sd->file;
return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
}
long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
size_t len, unsigned int flags)
{
struct splice_desc sd = {
.len = len,
.total_len = len,
.flags = flags,
.pos = *ppos,
.file = out,
};
size_t ret;
ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
*ppos = sd.pos;
return ret;
} }
/* /*
......
...@@ -94,13 +94,15 @@ struct splice_desc { ...@@ -94,13 +94,15 @@ struct splice_desc {
typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *, typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
struct splice_desc *); struct splice_desc *);
typedef int (splice_direct_actor)(struct pipe_inode_info *,
struct splice_desc *);
extern ssize_t splice_from_pipe(struct pipe_inode_info *, struct file *, extern ssize_t splice_from_pipe(struct pipe_inode_info *, struct file *,
loff_t *, size_t, unsigned int, loff_t *, size_t, unsigned int,
splice_actor *); splice_actor *);
extern ssize_t __splice_from_pipe(struct pipe_inode_info *,
extern ssize_t __splice_from_pipe(struct pipe_inode_info *, struct file *, struct splice_desc *, splice_actor *);
loff_t *, size_t, unsigned int, extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
splice_actor *); splice_direct_actor *);
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment