Skip to content

Commit

Permalink
[PATCH] splice: fix offset problems
Browse files Browse the repository at this point in the history
Make the move_from_pipe() actors return number of bytes processed, then
move_from_pipe() can decide more cleverly when to move on to the next
buffer.

This fixes problems with pipe offset and differing file offset.

Signed-off-by: Jens Axboe <axboe@suse.de>
  • Loading branch information
Jens Axboe authored and Jens Axboe committed Apr 26, 2006
1 parent ba5f5d9 commit 016b661
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions fs/splice.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,13 @@ EXPORT_SYMBOL(generic_file_splice_read);

/*
* Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
* using sendpage().
* using sendpage(). Return the number of bytes sent.
*/
static int pipe_to_sendpage(struct pipe_inode_info *info,
struct pipe_buffer *buf, struct splice_desc *sd)
{
struct file *file = sd->file;
loff_t pos = sd->pos;
unsigned int offset;
ssize_t ret;
void *ptr;
int more;
Expand All @@ -461,16 +460,13 @@ static int pipe_to_sendpage(struct pipe_inode_info *info,
if (IS_ERR(ptr))
return PTR_ERR(ptr);

offset = pos & ~PAGE_CACHE_MASK;
more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;

ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,more);
ret = file->f_op->sendpage(file, buf->page, buf->offset, sd->len,
&pos, more);

buf->ops->unmap(info, buf);
if (ret == sd->len)
return 0;

return -EIO;
return ret;
}

/*
Expand Down Expand Up @@ -499,7 +495,7 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
struct file *file = sd->file;
struct address_space *mapping = file->f_mapping;
gfp_t gfp_mask = mapping_gfp_mask(mapping);
unsigned int offset;
unsigned int offset, this_len;
struct page *page;
pgoff_t index;
char *src;
Expand All @@ -515,6 +511,10 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
index = sd->pos >> PAGE_CACHE_SHIFT;
offset = sd->pos & ~PAGE_CACHE_MASK;

this_len = sd->len;
if (this_len + offset > PAGE_CACHE_SIZE)
this_len = PAGE_CACHE_SIZE - offset;

/*
* Reuse buf page, if SPLICE_F_MOVE is set.
*/
Expand Down Expand Up @@ -558,7 +558,7 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
* the full page.
*/
if (!PageUptodate(page)) {
if (sd->len < PAGE_CACHE_SIZE) {
if (this_len < PAGE_CACHE_SIZE) {
ret = mapping->a_ops->readpage(file, page);
if (unlikely(ret))
goto out;
Expand All @@ -582,7 +582,7 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
}
}

ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
if (ret == AOP_TRUNCATED_PAGE) {
page_cache_release(page);
goto find_page;
Expand All @@ -592,18 +592,22 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
char *dst = kmap_atomic(page, KM_USER0);

memcpy(dst + offset, src + buf->offset, sd->len);
memcpy(dst + offset, src + buf->offset, this_len);
flush_dcache_page(page);
kunmap_atomic(dst, KM_USER0);
}

ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
if (ret == AOP_TRUNCATED_PAGE) {
page_cache_release(page);
goto find_page;
} else if (ret)
goto out;

/*
* Return the number of bytes written.
*/
ret = this_len;
mark_page_accessed(page);
balance_dirty_pages_ratelimited(mapping);
out:
Expand Down Expand Up @@ -652,16 +656,22 @@ static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
sd.len = sd.total_len;

err = actor(pipe, buf, &sd);
if (err) {
if (err <= 0) {
if (!ret && err != -ENODATA)
ret = err;

break;
}

ret += sd.len;
buf->offset += sd.len;
buf->len -= sd.len;
ret += err;
buf->offset += err;
buf->len -= err;

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

if (!buf->len) {
buf->ops = NULL;
Expand All @@ -672,8 +682,6 @@ static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
do_wakeup = 1;
}

sd.pos += sd.len;
sd.total_len -= sd.len;
if (!sd.total_len)
break;
}
Expand Down

0 comments on commit 016b661

Please sign in to comment.