/*
* "splice": joining two ropes together by interweaving their strands.
*
* This is the "extended pipe" functionality, where a pipe is used as
* an arbitrary in-memory buffer. Think of a pipe as a small kernel
* buffer that you can use to transfer data from one end to the other.
*
* The traditional unix read/write is extended with a "splice()" operation
* that transfers data buffers to or from a pipe buffer.
*
* Named by Larry McVoy, original implementation from Linus, extended by
* Jens to support splicing to files and fixing the initial implementation
* bugs.
*
* Copyright (C) 2005 Jens Axboe <axboe@suse.de>
* Copyright (C) 2005 Linus Torvalds <torvalds@osdl.org>
*
*/
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/pagemap.h>
#include <linux/pipe_fs_i.h>
#include <linux/mm_inline.h>
#include <linux/swap.h>
#include <linux/writeback.h>
#include <linux/buffer_head.h>
#include <linux/module.h>
#include <linux/syscalls.h>
/*
* Passed to the actors
*/
struct splice_desc {
unsigned int len, total_len; /* current and remaining length */
unsigned int flags; /* splice flags */
struct file *file; /* file to read/write */
loff_t pos; /* file position */
};
/*
* Attempt to steal a page from a pipe buffer. This should perhaps go into
* a vm helper function, it's already simplified quite a bit by the
* addition of remove_mapping(). If success is returned, the caller may
* attempt to reuse this page for another destination.
*/
static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
struct pipe_buffer *buf)
{
struct page *page = buf->page;
struct address_space *mapping = page_mapping(page);
WARN_ON(!PageLocked(page));
WARN_ON(!PageUptodate(page));
/*
* At least for ext2 with nobh option, we need to wait on writeback
* completing on this page, since we'll remove it from the pagecache.
* Otherwise truncate wont wait on the page, allowing the disk
* blocks to be reused by someone else before we actually wrote our
* data to them. fs corruption ensues.
*/
wait_on_page_writeback(page);
if (PagePrivate(page))
try_to_release_page(page, mapping_gfp_mask(mapping));
if (!remove_mapping(mapping, page))
return 1;
buf->flags |= PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU;
return 0;
}
static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
struct pipe_buffer *buf)
{
page_cache_release(buf->page);
buf->page = NULL;
buf->flags &= ~(PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU);
}
static void *page_cache_pipe_buf_map(struct file *file,
struct pipe_inode_info *info,
struct pipe_buffer *buf)
{
struct page *page = buf->page;
int err;
if (!PageUptodate(page)) {
lock_page(page);
/*
* Page got truncated/unhashed. This will cause a 0-byte
* splice, if this is the first page
*/
if (!page->mapping) {
err = -ENODATA;
goto error;
}
/*
* uh oh, read-error from disk
*/
if (!PageUptodate(page)) {
err = -EIO;
goto error;
}
/*
* page is ok afterall, fall through to mapping
*/
unlock_page(page);
}
return kmap(page);
error:
unlock_page(page);
return ERR_PTR(err);
}
static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
struct pipe_buffer *buf)
{
kunmap(buf->page);
}
static struct pipe_buf_operations page_cache_pipe_buf_ops = {
.can_merge = 0,
.map = page_cache_pipe_buf_map,
.unmap = page_cache_pipe_buf_unmap,
.release = page_cache_pipe_buf_release,
.steal = page_cache_pipe_buf_steal,
};
/*
* Pipe output worker. This sets up our pipe format with the page cache
* pipe buffer operations. Otherwise very similar to the regular pipe_writev().
*/
static ssize_t move_to_pipe(struct pipe_inode_info *pipe, struct page **pages,
int nr_pages, unsigned long offset,
unsigned long len, unsigned int flags)
{
int ret, do_wakeup, i;
ret = 0;
do_wakeup = 0;
i = 0;
if (pipe->inode)
mutex_lock(&pipe->inode->i_mutex);
for (;;) {
if (!pipe->readers) {
send_sig(SIGPIPE, current, 0);
if (!ret)
ret = -EPIPE;
break;
}
if (pipe->nrbufs < PIPE_BUFFERS) {
int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
struct pipe_buffer *buf = pipe->bufs + newbuf;
struct page *page = pages[i++];
unsigned long this_len;
this_len = PAGE_CACHE_SIZE - offset;
if (this_len > len)
this_len = len;
buf->page = page;
buf->offset = offset;
buf->len = this_len;
buf->ops = &page_cache_pipe_buf_ops;
pipe->nrbufs++;
if (pipe->inode)
do_wakeup = 1;
ret += this_len;
len -= this_len;
offset