Skip to content

Commit

Permalink
splice: abstract out actor data
Browse files Browse the repository at this point in the history
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: Jens Axboe <jens.axboe@oracle.com>
  • Loading branch information
Jens Axboe committed Jul 10, 2007
1 parent 71780f5 commit c66ab6f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
11 changes: 8 additions & 3 deletions fs/ocfs2/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1636,9 +1636,14 @@ static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
int ret, err;
struct address_space *mapping = out->f_mapping;
struct inode *inode = mapping->host;

ret = __splice_from_pipe(pipe, out, ppos, len, flags,
ocfs2_splice_write_actor);
struct splice_desc sd = {
.total_len = len,
.flags = flags,
.pos = *ppos,
.file = out,
};

ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
if (ret > 0) {
*ppos += ret;

Expand Down
99 changes: 70 additions & 29 deletions fs/splice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
* to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
*/
ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
struct file *out, loff_t *ppos, size_t len,
unsigned int flags, splice_actor *actor)
ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
splice_actor *actor)
{
int ret, do_wakeup, err;
struct splice_desc sd;

ret = 0;
do_wakeup = 0;

sd.total_len = len;
sd.flags = flags;
sd.file = out;
sd.pos = *ppos;

for (;;) {
if (pipe->nrbufs) {
struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
const struct pipe_buf_operations *ops = buf->ops;

sd.len = buf->len;
if (sd.len > sd.total_len)
sd.len = sd.total_len;
sd->len = buf->len;
if (sd->len > sd->total_len)
sd->len = sd->total_len;

err = actor(pipe, buf, &sd);
err = actor(pipe, buf, sd);
if (err <= 0) {
if (!ret && err != -ENODATA)
ret = err;
Expand All @@ -704,10 +697,10 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
buf->offset += err;
buf->len -= err;

sd.len -= err;
sd.pos += err;
sd.total_len -= err;
if (sd.len)
sd->len -= err;
sd->pos += err;
sd->total_len -= err;
if (sd->len)
continue;

if (!buf->len) {
Expand All @@ -719,7 +712,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
do_wakeup = 1;
}

if (!sd.total_len)
if (!sd->total_len)
break;
}

Expand All @@ -732,7 +725,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
break;
}

if (flags & SPLICE_F_NONBLOCK) {
if (sd->flags & SPLICE_F_NONBLOCK) {
if (!ret)
ret = -EAGAIN;
break;
Expand Down Expand Up @@ -772,6 +765,12 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
{
ssize_t ret;
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
Expand All @@ -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.
*/
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);

return ret;
Expand All @@ -804,14 +803,20 @@ generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
{
struct address_space *mapping = out->f_mapping;
struct inode *inode = mapping->host;
struct splice_desc sd = {
.total_len = len,
.flags = flags,
.pos = *ppos,
.file = out,
};
ssize_t ret;
int err;

err = remove_suid(out->f_path.dentry);
if (unlikely(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) {
unsigned long nr_pages;

Expand Down Expand Up @@ -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);
}

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;
long ret, bytes;
loff_t out_off;
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
Expand Down Expand Up @@ -999,7 +1007,13 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
*/
ret = 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) {
size_t read_len, max_read_len;
Expand All @@ -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));

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))
goto out_release;

read_len = ret;
sd->total_len = read_len;

/*
* NOTE: nonblocking mode only applies to the input. We
* must not do the output in nonblocking mode as then we
* could get stuck data in the internal pipe:
*/
ret = do_splice_from(pipe, out, &out_off, read_len,
flags & ~SPLICE_F_NONBLOCK);
ret = actor(pipe, sd);
if (unlikely(ret < 0))
goto out_release;

Expand Down Expand Up @@ -1066,6 +1080,33 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
return bytes;

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;
}

/*
Expand Down
10 changes: 6 additions & 4 deletions include/linux/pipe_fs_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ struct splice_desc {

typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
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 *,
loff_t *, size_t, unsigned int,
splice_actor *);

extern ssize_t __splice_from_pipe(struct pipe_inode_info *, struct file *,
loff_t *, size_t, unsigned int,
splice_actor *);
extern ssize_t __splice_from_pipe(struct pipe_inode_info *,
struct splice_desc *, splice_actor *);
extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *,
splice_direct_actor *);

#endif

0 comments on commit c66ab6f

Please sign in to comment.