Merge branch 'for_paulus' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc
[pandora-kernel.git] / fs / splice.c
index 5d3eda6..0559e75 100644 (file)
@@ -50,7 +50,8 @@ static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
        struct page *page = buf->page;
        struct address_space *mapping = page_mapping(page);
 
-       WARN_ON(!PageLocked(page));
+       lock_page(page);
+
        WARN_ON(!PageUptodate(page));
 
        /*
@@ -65,8 +66,10 @@ static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
        if (PagePrivate(page))
                try_to_release_page(page, mapping_gfp_mask(mapping));
 
-       if (!remove_mapping(mapping, page))
+       if (!remove_mapping(mapping, page)) {
+               unlock_page(page);
                return 1;
+       }
 
        buf->flags |= PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU;
        return 0;
@@ -125,12 +128,19 @@ static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
        kunmap(buf->page);
 }
 
+static void page_cache_pipe_buf_get(struct pipe_inode_info *info,
+                                   struct pipe_buffer *buf)
+{
+       page_cache_get(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,
+       .get = page_cache_pipe_buf_get,
 };
 
 /*
@@ -138,8 +148,8 @@ static struct pipe_buf_operations page_cache_pipe_buf_ops = {
  * 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 nr_pages, unsigned long len,
+                           unsigned int offset, unsigned int flags)
 {
        int ret, do_wakeup, i;
 
@@ -236,14 +246,16 @@ __generic_file_splice_read(struct file *in, loff_t *ppos,
                           unsigned int flags)
 {
        struct address_space *mapping = in->f_mapping;
-       unsigned int offset, nr_pages;
+       unsigned int loff, offset, nr_pages;
        struct page *pages[PIPE_BUFFERS];
        struct page *page;
-       pgoff_t index;
+       pgoff_t index, end_index;
+       loff_t isize;
+       size_t bytes;
        int i, error;
 
        index = *ppos >> PAGE_CACHE_SHIFT;
-       offset = *ppos & ~PAGE_CACHE_MASK;
+       loff = offset = *ppos & ~PAGE_CACHE_MASK;
        nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
 
        if (nr_pages > PIPE_BUFFERS)
@@ -261,21 +273,23 @@ __generic_file_splice_read(struct file *in, loff_t *ppos,
         * Now fill in the holes:
         */
        error = 0;
+       bytes = 0;
        for (i = 0; i < nr_pages; i++, index++) {
+               unsigned int this_len;
+
+               if (!len)
+                       break;
+
+               /*
+                * this_len is the max we'll use from this page
+                */
+               this_len = min(len, PAGE_CACHE_SIZE - loff);
 find_page:
                /*
                 * lookup the page for this index
                 */
                page = find_get_page(mapping, index);
                if (!page) {
-                       /*
-                        * If in nonblock mode then dont block on
-                        * readpage (we've kicked readahead so there
-                        * will be asynchronous progress):
-                        */
-                       if (flags & SPLICE_F_NONBLOCK)
-                               break;
-
                        /*
                         * page didn't exist, allocate one
                         */
@@ -297,6 +311,13 @@ find_page:
                 * If the page isn't uptodate, we may need to start io on it
                 */
                if (!PageUptodate(page)) {
+                       /*
+                        * If in nonblock mode then dont block on waiting
+                        * for an in-flight io page
+                        */
+                       if (flags & SPLICE_F_NONBLOCK)
+                               break;
+
                        lock_page(page);
 
                        /*
@@ -329,13 +350,43 @@ readpage:
                                        goto find_page;
                                break;
                        }
+
+                       /*
+                        * i_size must be checked after ->readpage().
+                        */
+                       isize = i_size_read(mapping->host);
+                       end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
+                       if (unlikely(!isize || index > end_index)) {
+                               page_cache_release(page);
+                               break;
+                       }
+
+                       /*
+                        * if this is the last page, see if we need to shrink
+                        * the length and stop
+                        */
+                       if (end_index == index) {
+                               loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
+                               if (bytes + loff > isize) {
+                                       page_cache_release(page);
+                                       break;
+                               }
+                               /*
+                                * force quit after adding this page
+                                */
+                               nr_pages = i;
+                               this_len = min(this_len, loff);
+                       }
                }
 fill_it:
                pages[i] = page;
+               bytes += this_len;
+               len -= this_len;
+               loff = 0;
        }
 
        if (i)
-               return move_to_pipe(pipe, pages, i, offset, len, flags);
+               return move_to_pipe(pipe, pages, i, bytes, offset, flags);
 
        return error;
 }
@@ -362,17 +413,20 @@ ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
        while (len) {
                ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
 
-               if (ret <= 0)
+               if (ret < 0)
                        break;
+               else if (!ret) {
+                       if (spliced)
+                               break;
+                       if (flags & SPLICE_F_NONBLOCK) {
+                               ret = -EAGAIN;
+                               break;
+                       }
+               }
 
                *ppos += ret;
                len -= ret;
                spliced += ret;
-
-               if (!(flags & SPLICE_F_NONBLOCK))
-                       continue;
-               ret = -EAGAIN;
-               break;
        }
 
        if (spliced)
@@ -467,14 +521,12 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
        if (sd->flags & SPLICE_F_MOVE) {
                /*
                 * If steal succeeds, buf->page is now pruned from the vm
-                * side (LRU and page cache) and we can reuse it.
+                * side (LRU and page cache) and we can reuse it. The page
+                * will also be looked on successful return.
                 */
                if (buf->ops->steal(info, buf))
                        goto find_page;
 
-               /*
-                * this will also set the page locked
-                */
                page = buf->page;
                if (add_to_page_cache(page, mapping, index, gfp_mask))
                        goto find_page;
@@ -483,15 +535,27 @@ static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
                        lru_cache_add(page);
        } else {
 find_page:
-               ret = -ENOMEM;
-               page = find_or_create_page(mapping, index, gfp_mask);
-               if (!page)
-                       goto out_nomem;
+               page = find_lock_page(mapping, index);
+               if (!page) {
+                       ret = -ENOMEM;
+                       page = page_cache_alloc_cold(mapping);
+                       if (unlikely(!page))
+                               goto out_nomem;
+
+                       /*
+                        * This will also lock the page
+                        */
+                       ret = add_to_page_cache_lru(page, mapping, index,
+                                                   gfp_mask);
+                       if (unlikely(ret))
+                               goto out;
+               }
 
                /*
-                * If the page is uptodate, it is also locked. If it isn't
-                * uptodate, we can mark it uptodate if we are filling the
-                * full page. Otherwise we need to read it in first...
+                * We get here with the page locked. If the page is also
+                * uptodate, we don't need to do more. If it isn't, we
+                * may need to bring it in if we are not going to overwrite
+                * the full page.
                 */
                if (!PageUptodate(page)) {
                        if (sd->len < PAGE_CACHE_SIZE) {
@@ -513,10 +577,8 @@ find_page:
                                        ret = -EIO;
                                        goto out;
                                }
-                       } else {
-                               WARN_ON(!PageLocked(page));
+                       } else
                                SetPageUptodate(page);
-                       }
                }
        }
 
@@ -545,10 +607,10 @@ find_page:
        mark_page_accessed(page);
        balance_dirty_pages_ratelimited(mapping);
 out:
-       if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
+       if (!(buf->flags & PIPE_BUF_FLAG_STOLEN))
                page_cache_release(page);
-               unlock_page(page);
-       }
+
+       unlock_page(page);
 out_nomem:
        buf->ops->unmap(info, buf);
        return ret;
@@ -680,22 +742,26 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
        ssize_t ret;
 
        ret = move_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
-
-       /*
-        * If file or inode is SYNC and we actually wrote some data, sync it.
-        */
-       if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(mapping->host))
-           && ret > 0) {
+       if (ret > 0) {
                struct inode *inode = mapping->host;
-               int err;
 
-               mutex_lock(&inode->i_mutex);
-               err = generic_osync_inode(mapping->host, mapping,
-                                         OSYNC_METADATA|OSYNC_DATA);
-               mutex_unlock(&inode->i_mutex);
+               *ppos += ret;
+
+               /*
+                * If file or inode is SYNC and we actually wrote some data,
+                * sync it.
+                */
+               if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
+                       int err;
 
-               if (err)
-                       ret = err;
+                       mutex_lock(&inode->i_mutex);
+                       err = generic_osync_inode(inode, mapping,
+                                                 OSYNC_METADATA|OSYNC_DATA);
+                       mutex_unlock(&inode->i_mutex);
+
+                       if (err)
+                               ret = err;
+               }
        }
 
        return ret;
@@ -897,6 +963,7 @@ static long do_splice(struct file *in, loff_t __user *off_in,
 {
        struct pipe_inode_info *pipe;
        loff_t offset, *off;
+       long ret;
 
        pipe = in->f_dentry->d_inode->i_pipe;
        if (pipe) {
@@ -911,7 +978,12 @@ static long do_splice(struct file *in, loff_t __user *off_in,
                } else
                        off = &out->f_pos;
 
-               return do_splice_from(pipe, out, off, len, flags);
+               ret = do_splice_from(pipe, out, off, len, flags);
+
+               if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
+                       ret = -EFAULT;
+
+               return ret;
        }
 
        pipe = out->f_dentry->d_inode->i_pipe;
@@ -927,7 +999,12 @@ static long do_splice(struct file *in, loff_t __user *off_in,
                } else
                        off = &in->f_pos;
 
-               return do_splice_to(in, off, pipe, len, flags);
+               ret = do_splice_to(in, off, pipe, len, flags);
+
+               if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
+                       ret = -EFAULT;
+
+               return ret;
        }
 
        return -EINVAL;
@@ -963,3 +1040,192 @@ asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
 
        return error;
 }
+
+/*
+ * Link contents of ipipe to opipe.
+ */
+static int link_pipe(struct pipe_inode_info *ipipe,
+                    struct pipe_inode_info *opipe,
+                    size_t len, unsigned int flags)
+{
+       struct pipe_buffer *ibuf, *obuf;
+       int ret, do_wakeup, i, ipipe_first;
+
+       ret = do_wakeup = ipipe_first = 0;
+
+       /*
+        * Potential ABBA deadlock, work around it by ordering lock
+        * grabbing by inode address. Otherwise two different processes
+        * could deadlock (one doing tee from A -> B, the other from B -> A).
+        */
+       if (ipipe->inode < opipe->inode) {
+               ipipe_first = 1;
+               mutex_lock(&ipipe->inode->i_mutex);
+               mutex_lock(&opipe->inode->i_mutex);
+       } else {
+               mutex_lock(&opipe->inode->i_mutex);
+               mutex_lock(&ipipe->inode->i_mutex);
+       }
+
+       for (i = 0;; i++) {
+               if (!opipe->readers) {
+                       send_sig(SIGPIPE, current, 0);
+                       if (!ret)
+                               ret = -EPIPE;
+                       break;
+               }
+               if (ipipe->nrbufs - i) {
+                       ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
+
+                       /*
+                        * If we have room, fill this buffer
+                        */
+                       if (opipe->nrbufs < PIPE_BUFFERS) {
+                               int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
+
+                               /*
+                                * Get a reference to this pipe buffer,
+                                * so we can copy the contents over.
+                                */
+                               ibuf->ops->get(ipipe, ibuf);
+
+                               obuf = opipe->bufs + nbuf;
+                               *obuf = *ibuf;
+
+                               if (obuf->len > len)
+                                       obuf->len = len;
+
+                               opipe->nrbufs++;
+                               do_wakeup = 1;
+                               ret += obuf->len;
+                               len -= obuf->len;
+
+                               if (!len)
+                                       break;
+                               if (opipe->nrbufs < PIPE_BUFFERS)
+                                       continue;
+                       }
+
+                       /*
+                        * We have input available, but no output room.
+                        * If we already copied data, return that. If we
+                        * need to drop the opipe lock, it must be ordered
+                        * last to avoid deadlocks.
+                        */
+                       if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
+                               if (!ret)
+                                       ret = -EAGAIN;
+                               break;
+                       }
+                       if (signal_pending(current)) {
+                               if (!ret)
+                                       ret = -ERESTARTSYS;
+                               break;
+                       }
+                       if (do_wakeup) {
+                               smp_mb();
+                               if (waitqueue_active(&opipe->wait))
+                                       wake_up_interruptible(&opipe->wait);
+                               kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
+                               do_wakeup = 0;
+                       }
+
+                       opipe->waiting_writers++;
+                       pipe_wait(opipe);
+                       opipe->waiting_writers--;
+                       continue;
+               }
+
+               /*
+                * No input buffers, do the usual checks for available
+                * writers and blocking and wait if necessary
+                */
+               if (!ipipe->writers)
+                       break;
+               if (!ipipe->waiting_writers) {
+                       if (ret)
+                               break;
+               }
+               /*
+                * pipe_wait() drops the ipipe mutex. To avoid deadlocks
+                * with another process, we can only safely do that if
+                * the ipipe lock is ordered last.
+                */
+               if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
+                       if (!ret)
+                               ret = -EAGAIN;
+                       break;
+               }
+               if (signal_pending(current)) {
+                       if (!ret)
+                               ret = -ERESTARTSYS;
+                       break;
+               }
+
+               if (waitqueue_active(&ipipe->wait))
+                       wake_up_interruptible_sync(&ipipe->wait);
+               kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
+
+               pipe_wait(ipipe);
+       }
+
+       mutex_unlock(&ipipe->inode->i_mutex);
+       mutex_unlock(&opipe->inode->i_mutex);
+
+       if (do_wakeup) {
+               smp_mb();
+               if (waitqueue_active(&opipe->wait))
+                       wake_up_interruptible(&opipe->wait);
+               kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
+       }
+
+       return ret;
+}
+
+/*
+ * This is a tee(1) implementation that works on pipes. It doesn't copy
+ * any data, it simply references the 'in' pages on the 'out' pipe.
+ * The 'flags' used are the SPLICE_F_* variants, currently the only
+ * applicable one is SPLICE_F_NONBLOCK.
+ */
+static long do_tee(struct file *in, struct file *out, size_t len,
+                  unsigned int flags)
+{
+       struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
+       struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
+
+       /*
+        * Link ipipe to the two output pipes, consuming as we go along.
+        */
+       if (ipipe && opipe)
+               return link_pipe(ipipe, opipe, len, flags);
+
+       return -EINVAL;
+}
+
+asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
+{
+       struct file *in;
+       int error, fput_in;
+
+       if (unlikely(!len))
+               return 0;
+
+       error = -EBADF;
+       in = fget_light(fdin, &fput_in);
+       if (in) {
+               if (in->f_mode & FMODE_READ) {
+                       int fput_out;
+                       struct file *out = fget_light(fdout, &fput_out);
+
+                       if (out) {
+                               if (out->f_mode & FMODE_WRITE)
+                                       error = do_tee(in, out, len, flags);
+                               fput_light(out, fput_out);
+                       }
+               }
+               fput_light(in, fput_in);
+       }
+
+       return error;
+}