aboutsummaryrefslogtreecommitdiffstats
path: root/fs/splice.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/splice.c')
-rw-r--r--fs/splice.c195
1 files changed, 103 insertions, 92 deletions
diff --git a/fs/splice.c b/fs/splice.c
index e246954ea48c..f5cb9ba84510 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -32,6 +32,7 @@
32#include <linux/gfp.h> 32#include <linux/gfp.h>
33#include <linux/socket.h> 33#include <linux/socket.h>
34#include <linux/compat.h> 34#include <linux/compat.h>
35#include <linux/aio.h>
35#include "internal.h" 36#include "internal.h"
36 37
37/* 38/*
@@ -717,63 +718,6 @@ static int pipe_to_sendpage(struct pipe_inode_info *pipe,
717 sd->len, &pos, more); 718 sd->len, &pos, more);
718} 719}
719 720
720/*
721 * This is a little more tricky than the file -> pipe splicing. There are
722 * basically three cases:
723 *
724 * - Destination page already exists in the address space and there
725 * are users of it. For that case we have no other option that
726 * copying the data. Tough luck.
727 * - Destination page already exists in the address space, but there
728 * are no users of it. Make sure it's uptodate, then drop it. Fall
729 * through to last case.
730 * - Destination page does not exist, we can add the pipe page to
731 * the page cache and avoid the copy.
732 *
733 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
734 * sd->flags), we attempt to migrate pages from the pipe to the output
735 * file address space page cache. This is possible if no one else has
736 * the pipe page referenced outside of the pipe and page cache. If
737 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
738 * a new page in the output file page cache and fill/dirty that.
739 */
740int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
741 struct splice_desc *sd)
742{
743 struct file *file = sd->u.file;
744 struct address_space *mapping = file->f_mapping;
745 unsigned int offset, this_len;
746 struct page *page;
747 void *fsdata;
748 int ret;
749
750 offset = sd->pos & ~PAGE_CACHE_MASK;
751
752 this_len = sd->len;
753 if (this_len + offset > PAGE_CACHE_SIZE)
754 this_len = PAGE_CACHE_SIZE - offset;
755
756 ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
757 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
758 if (unlikely(ret))
759 goto out;
760
761 if (buf->page != page) {
762 char *src = kmap_atomic(buf->page);
763 char *dst = kmap_atomic(page);
764
765 memcpy(dst + offset, src + buf->offset, this_len);
766 flush_dcache_page(page);
767 kunmap_atomic(dst);
768 kunmap_atomic(src);
769 }
770 ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
771 page, fsdata);
772out:
773 return ret;
774}
775EXPORT_SYMBOL(pipe_to_file);
776
777static void wakeup_pipe_writers(struct pipe_inode_info *pipe) 721static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
778{ 722{
779 smp_mb(); 723 smp_mb();
@@ -802,7 +746,7 @@ static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
802 * locking is required around copying the pipe buffers to the 746 * locking is required around copying the pipe buffers to the
803 * destination. 747 * destination.
804 */ 748 */
805int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, 749static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
806 splice_actor *actor) 750 splice_actor *actor)
807{ 751{
808 int ret; 752 int ret;
@@ -849,7 +793,6 @@ int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
849 793
850 return 1; 794 return 1;
851} 795}
852EXPORT_SYMBOL(splice_from_pipe_feed);
853 796
854/** 797/**
855 * splice_from_pipe_next - wait for some data to splice from 798 * splice_from_pipe_next - wait for some data to splice from
@@ -861,7 +804,7 @@ EXPORT_SYMBOL(splice_from_pipe_feed);
861 * value (one) if pipe buffers are available. It will return zero 804 * value (one) if pipe buffers are available. It will return zero
862 * or -errno if no more data needs to be spliced. 805 * or -errno if no more data needs to be spliced.
863 */ 806 */
864int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) 807static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
865{ 808{
866 while (!pipe->nrbufs) { 809 while (!pipe->nrbufs) {
867 if (!pipe->writers) 810 if (!pipe->writers)
@@ -886,7 +829,6 @@ int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
886 829
887 return 1; 830 return 1;
888} 831}
889EXPORT_SYMBOL(splice_from_pipe_next);
890 832
891/** 833/**
892 * splice_from_pipe_begin - start splicing from pipe 834 * splice_from_pipe_begin - start splicing from pipe
@@ -897,12 +839,11 @@ EXPORT_SYMBOL(splice_from_pipe_next);
897 * splice_from_pipe_next() and splice_from_pipe_feed() to 839 * splice_from_pipe_next() and splice_from_pipe_feed() to
898 * initialize the necessary fields of @sd. 840 * initialize the necessary fields of @sd.
899 */ 841 */
900void splice_from_pipe_begin(struct splice_desc *sd) 842static void splice_from_pipe_begin(struct splice_desc *sd)
901{ 843{
902 sd->num_spliced = 0; 844 sd->num_spliced = 0;
903 sd->need_wakeup = false; 845 sd->need_wakeup = false;
904} 846}
905EXPORT_SYMBOL(splice_from_pipe_begin);
906 847
907/** 848/**
908 * splice_from_pipe_end - finish splicing from pipe 849 * splice_from_pipe_end - finish splicing from pipe
@@ -914,12 +855,11 @@ EXPORT_SYMBOL(splice_from_pipe_begin);
914 * be called after a loop containing splice_from_pipe_next() and 855 * be called after a loop containing splice_from_pipe_next() and
915 * splice_from_pipe_feed(). 856 * splice_from_pipe_feed().
916 */ 857 */
917void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) 858static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
918{ 859{
919 if (sd->need_wakeup) 860 if (sd->need_wakeup)
920 wakeup_pipe_writers(pipe); 861 wakeup_pipe_writers(pipe);
921} 862}
922EXPORT_SYMBOL(splice_from_pipe_end);
923 863
924/** 864/**
925 * __splice_from_pipe - splice data from a pipe to given actor 865 * __splice_from_pipe - splice data from a pipe to given actor
@@ -985,7 +925,7 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
985} 925}
986 926
987/** 927/**
988 * generic_file_splice_write - splice data from a pipe to a file 928 * iter_file_splice_write - splice data from a pipe to a file
989 * @pipe: pipe info 929 * @pipe: pipe info
990 * @out: file to write to 930 * @out: file to write to
991 * @ppos: position in @out 931 * @ppos: position in @out
@@ -995,40 +935,122 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
995 * Description: 935 * Description:
996 * Will either move or copy pages (determined by @flags options) from 936 * Will either move or copy pages (determined by @flags options) from
997 * the given pipe inode to the given file. 937 * the given pipe inode to the given file.
938 * This one is ->write_iter-based.
998 * 939 *
999 */ 940 */
1000ssize_t 941ssize_t
1001generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, 942iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
1002 loff_t *ppos, size_t len, unsigned int flags) 943 loff_t *ppos, size_t len, unsigned int flags)
1003{ 944{
1004 struct address_space *mapping = out->f_mapping;
1005 struct inode *inode = mapping->host;
1006 struct splice_desc sd = { 945 struct splice_desc sd = {
1007 .total_len = len, 946 .total_len = len,
1008 .flags = flags, 947 .flags = flags,
1009 .pos = *ppos, 948 .pos = *ppos,
1010 .u.file = out, 949 .u.file = out,
1011 }; 950 };
951 int nbufs = pipe->buffers;
952 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
953 GFP_KERNEL);
1012 ssize_t ret; 954 ssize_t ret;
1013 955
956 if (unlikely(!array))
957 return -ENOMEM;
958
1014 pipe_lock(pipe); 959 pipe_lock(pipe);
1015 960
1016 splice_from_pipe_begin(&sd); 961 splice_from_pipe_begin(&sd);
1017 do { 962 while (sd.total_len) {
963 struct iov_iter from;
964 struct kiocb kiocb;
965 size_t left;
966 int n, idx;
967
1018 ret = splice_from_pipe_next(pipe, &sd); 968 ret = splice_from_pipe_next(pipe, &sd);
1019 if (ret <= 0) 969 if (ret <= 0)
1020 break; 970 break;
1021 971
1022 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 972 if (unlikely(nbufs < pipe->buffers)) {
1023 ret = file_remove_suid(out); 973 kfree(array);
1024 if (!ret) { 974 nbufs = pipe->buffers;
1025 ret = file_update_time(out); 975 array = kcalloc(nbufs, sizeof(struct bio_vec),
1026 if (!ret) 976 GFP_KERNEL);
1027 ret = splice_from_pipe_feed(pipe, &sd, 977 if (!array) {
1028 pipe_to_file); 978 ret = -ENOMEM;
979 break;
980 }
1029 } 981 }
1030 mutex_unlock(&inode->i_mutex); 982
1031 } while (ret > 0); 983 /* build the vector */
984 left = sd.total_len;
985 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
986 struct pipe_buffer *buf = pipe->bufs + idx;
987 size_t this_len = buf->len;
988
989 if (this_len > left)
990 this_len = left;
991
992 if (idx == pipe->buffers - 1)
993 idx = -1;
994
995 ret = buf->ops->confirm(pipe, buf);
996 if (unlikely(ret)) {
997 if (ret == -ENODATA)
998 ret = 0;
999 goto done;
1000 }
1001
1002 array[n].bv_page = buf->page;
1003 array[n].bv_len = this_len;
1004 array[n].bv_offset = buf->offset;
1005 left -= this_len;
1006 }
1007
1008 /* ... iov_iter */
1009 from.type = ITER_BVEC | WRITE;
1010 from.bvec = array;
1011 from.nr_segs = n;
1012 from.count = sd.total_len - left;
1013 from.iov_offset = 0;
1014
1015 /* ... and iocb */
1016 init_sync_kiocb(&kiocb, out);
1017 kiocb.ki_pos = sd.pos;
1018 kiocb.ki_nbytes = sd.total_len - left;
1019
1020 /* now, send it */
1021 ret = out->f_op->write_iter(&kiocb, &from);
1022 if (-EIOCBQUEUED == ret)
1023 ret = wait_on_sync_kiocb(&kiocb);
1024
1025 if (ret <= 0)
1026 break;
1027
1028 sd.num_spliced += ret;
1029 sd.total_len -= ret;
1030 *ppos = sd.pos = kiocb.ki_pos;
1031
1032 /* dismiss the fully eaten buffers, adjust the partial one */
1033 while (ret) {
1034 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
1035 if (ret >= buf->len) {
1036 const struct pipe_buf_operations *ops = buf->ops;
1037 ret -= buf->len;
1038 buf->len = 0;
1039 buf->ops = NULL;
1040 ops->release(pipe, buf);
1041 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1042 pipe->nrbufs--;
1043 if (pipe->files)
1044 sd.need_wakeup = true;
1045 } else {
1046 buf->offset += ret;
1047 buf->len -= ret;
1048 ret = 0;
1049 }
1050 }
1051 }
1052done:
1053 kfree(array);
1032 splice_from_pipe_end(pipe, &sd); 1054 splice_from_pipe_end(pipe, &sd);
1033 1055
1034 pipe_unlock(pipe); 1056 pipe_unlock(pipe);
@@ -1036,21 +1058,10 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
1036 if (sd.num_spliced) 1058 if (sd.num_spliced)
1037 ret = sd.num_spliced; 1059 ret = sd.num_spliced;
1038 1060
1039 if (ret > 0) {
1040 int err;
1041
1042 err = generic_write_sync(out, *ppos, ret);
1043 if (err)
1044 ret = err;
1045 else
1046 *ppos += ret;
1047 balance_dirty_pages_ratelimited(mapping);
1048 }
1049
1050 return ret; 1061 return ret;
1051} 1062}
1052 1063
1053EXPORT_SYMBOL(generic_file_splice_write); 1064EXPORT_SYMBOL(iter_file_splice_write);
1054 1065
1055static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 1066static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1056 struct splice_desc *sd) 1067 struct splice_desc *sd)
@@ -1549,7 +1560,7 @@ static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
1549 goto out; 1560 goto out;
1550 1561
1551 count = ret; 1562 count = ret;
1552 iov_iter_init(&iter, iov, nr_segs, count, 0); 1563 iov_iter_init(&iter, READ, iov, nr_segs, count);
1553 1564
1554 sd.len = 0; 1565 sd.len = 0;
1555 sd.total_len = count; 1566 sd.total_len = count;