[PATCH] splice: rename remaining info variables to pipe
[safe/jmp/linux-2.6] / fs / splice.c
index 0a69164..8fa9217 100644 (file)
@@ -51,7 +51,7 @@ struct splice_pipe_desc {
  * 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,
+static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
                                     struct pipe_buffer *buf)
 {
        struct page *page = buf->page;
@@ -82,15 +82,14 @@ static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
        return 0;
 }
 
-static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
+static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
                                        struct pipe_buffer *buf)
 {
        page_cache_release(buf->page);
-       buf->page = NULL;
        buf->flags &= ~PIPE_BUF_FLAG_LRU;
 }
 
-static int page_cache_pipe_buf_pin(struct pipe_inode_info *info,
+static int page_cache_pipe_buf_pin(struct pipe_inode_info *pipe,
                                   struct pipe_buffer *buf)
 {
        struct page *page = buf->page;
@@ -141,7 +140,11 @@ static struct pipe_buf_operations page_cache_pipe_buf_ops = {
 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
                                    struct pipe_buffer *buf)
 {
-       return 1;
+       if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
+               return 1;
+
+       buf->flags |= PIPE_BUF_FLAG_LRU;
+       return generic_pipe_buf_steal(pipe, buf);
 }
 
 static struct pipe_buf_operations user_page_pipe_buf_ops = {
@@ -186,6 +189,9 @@ static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
                        buf->offset = spd->partial[page_nr].offset;
                        buf->len = spd->partial[page_nr].len;
                        buf->ops = spd->ops;
+                       if (spd->flags & SPLICE_F_GIFT)
+                               buf->flags |= PIPE_BUF_FLAG_GIFT;
+
                        pipe->nrbufs++;
                        page_nr++;
                        ret += buf->len;
@@ -494,14 +500,14 @@ EXPORT_SYMBOL(generic_file_splice_read);
  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
  * using sendpage(). Return the number of bytes sent.
  */
-static int pipe_to_sendpage(struct pipe_inode_info *info,
+static int pipe_to_sendpage(struct pipe_inode_info *pipe,
                            struct pipe_buffer *buf, struct splice_desc *sd)
 {
        struct file *file = sd->file;
        loff_t pos = sd->pos;
        int ret, more;
 
-       ret = buf->ops->pin(info, buf);
+       ret = buf->ops->pin(pipe, buf);
        if (!ret) {
                more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
 
@@ -532,7 +538,7 @@ static int pipe_to_sendpage(struct pipe_inode_info *info,
  * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
  * a new page in the output file page cache and fill/dirty that.
  */
-static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
+static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
                        struct splice_desc *sd)
 {
        struct file *file = sd->file;
@@ -546,7 +552,7 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
        /*
         * make sure the data in this buffer is uptodate
         */
-       ret = buf->ops->pin(info, buf);
+       ret = buf->ops->pin(pipe, buf);
        if (unlikely(ret))
                return ret;
 
@@ -563,11 +569,11 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
         */
        if ((sd->flags & SPLICE_F_MOVE) && this_len == PAGE_CACHE_SIZE) {
                /*
-                * If steal succeeds, buf->page is now pruned from the vm
-                * side (LRU and page cache) and we can reuse it. The page
-                * will also be looked on successful return.
+                * If steal succeeds, buf->page is now pruned from the
+                * pagecache and we can reuse it. The page will also be
+                * locked on successful return.
                 */
-               if (buf->ops->steal(info, buf))
+               if (buf->ops->steal(pipe, buf))
                        goto find_page;
 
                page = buf->page;
@@ -630,23 +636,36 @@ find_page:
        }
 
        ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
-       if (ret == AOP_TRUNCATED_PAGE) {
+       if (unlikely(ret)) {
+               loff_t isize = i_size_read(mapping->host);
+
+               if (ret != AOP_TRUNCATED_PAGE)
+                       unlock_page(page);
                page_cache_release(page);
-               goto find_page;
-       } else if (ret)
+               if (ret == AOP_TRUNCATED_PAGE)
+                       goto find_page;
+
+               /*
+                * prepare_write() may have instantiated a few blocks
+                * outside i_size.  Trim these off again.
+                */
+               if (sd->pos + this_len > isize)
+                       vmtruncate(mapping->host, isize);
+
                goto out;
+       }
 
        if (buf->page != page) {
                /*
                 * Careful, ->map() uses KM_USER0!
                 */
-               char *src = buf->ops->map(info, buf);
+               char *src = buf->ops->map(pipe, buf, 1);
                char *dst = kmap_atomic(page, KM_USER1);
 
                memcpy(dst + offset, src + buf->offset, this_len);
                flush_dcache_page(page);
                kunmap_atomic(dst, KM_USER1);
-               buf->ops->unmap(info, buf);
+               buf->ops->unmap(pipe, buf, src);
        }
 
        ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
@@ -1073,7 +1092,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
  */
 static int get_iovec_page_array(const struct iovec __user *iov,
                                unsigned int nr_vecs, struct page **pages,
-                               struct partial_page *partial)
+                               struct partial_page *partial, int aligned)
 {
        int buffers = 0, error = 0;
 
@@ -1113,6 +1132,15 @@ static int get_iovec_page_array(const struct iovec __user *iov,
                 * in the user pages.
                 */
                off = (unsigned long) base & ~PAGE_MASK;
+
+               /*
+                * If asked for alignment, the offset must be zero and the
+                * length a multiple of the PAGE_SIZE.
+                */
+               error = -EINVAL;
+               if (aligned && (off || len & ~PAGE_MASK))
+                       break;
+
                npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
                if (npages > PIPE_BUFFERS - buffers)
                        npages = PIPE_BUFFERS - buffers;
@@ -1128,7 +1156,7 @@ static int get_iovec_page_array(const struct iovec __user *iov,
                 * Fill this contiguous range into the partial page map.
                 */
                for (i = 0; i < error; i++) {
-                       const int plen = min_t(size_t, len, PAGE_SIZE) - off;
+                       const int plen = min_t(size_t, len, PAGE_SIZE - off);
 
                        partial[buffers].offset = off;
                        partial[buffers].len = plen;
@@ -1206,7 +1234,8 @@ static long do_vmsplice(struct file *file, const struct iovec __user *iov,
        else if (unlikely(!nr_segs))
                return 0;
 
-       spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial);
+       spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
+                                           flags & SPLICE_F_GIFT);
        if (spd.nr_pages <= 0)
                return spd.nr_pages;
 
@@ -1314,6 +1343,12 @@ static int link_pipe(struct pipe_inode_info *ipipe,
                                obuf = opipe->bufs + nbuf;
                                *obuf = *ibuf;
 
+                               /*
+                                * Don't inherit the gift flag, we need to
+                                * prevent multiple steals of this page.
+                                */
+                               obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
+
                                if (obuf->len > len)
                                        obuf->len = len;