33
33
#include <linux/fsnotify.h>
34
34
#include <linux/security.h>
35
35
#include <linux/gfp.h>
36
+ #include <linux/net.h>
36
37
#include <linux/socket.h>
37
38
#include <linux/sched/signal.h>
38
39
@@ -448,30 +449,6 @@ const struct pipe_buf_operations nosteal_pipe_buf_ops = {
448
449
};
449
450
EXPORT_SYMBOL (nosteal_pipe_buf_ops );
450
451
451
- /*
452
- * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
453
- * using sendpage(). Return the number of bytes sent.
454
- */
455
- static int pipe_to_sendpage (struct pipe_inode_info * pipe ,
456
- struct pipe_buffer * buf , struct splice_desc * sd )
457
- {
458
- struct file * file = sd -> u .file ;
459
- loff_t pos = sd -> pos ;
460
- int more ;
461
-
462
- if (!likely (file -> f_op -> sendpage ))
463
- return - EINVAL ;
464
-
465
- more = (sd -> flags & SPLICE_F_MORE ) ? MSG_MORE : 0 ;
466
-
467
- if (sd -> len < sd -> total_len &&
468
- pipe_occupancy (pipe -> head , pipe -> tail ) > 1 )
469
- more |= MSG_SENDPAGE_NOTLAST ;
470
-
471
- return file -> f_op -> sendpage (file , buf -> page , buf -> offset ,
472
- sd -> len , & pos , more );
473
- }
474
-
475
452
static void wakeup_pipe_writers (struct pipe_inode_info * pipe )
476
453
{
477
454
smp_mb ();
@@ -652,7 +629,7 @@ static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_des
652
629
* Description:
653
630
* This function does little more than loop over the pipe and call
654
631
* @actor to do the actual moving of a single struct pipe_buffer to
655
- * the desired destination. See pipe_to_file, pipe_to_sendpage , or
632
+ * the desired destination. See pipe_to_file, pipe_to_sendmsg , or
656
633
* pipe_to_user.
657
634
*
658
635
*/
@@ -833,8 +810,9 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
833
810
834
811
EXPORT_SYMBOL (iter_file_splice_write );
835
812
813
+ #ifdef CONFIG_NET
836
814
/**
837
- * generic_splice_sendpage - splice data from a pipe to a socket
815
+ * splice_to_socket - splice data from a pipe to a socket
838
816
* @pipe: pipe to splice from
839
817
* @out: socket to write to
840
818
* @ppos: position in @out
@@ -846,13 +824,131 @@ EXPORT_SYMBOL(iter_file_splice_write);
846
824
* is involved.
847
825
*
848
826
*/
849
- ssize_t generic_splice_sendpage (struct pipe_inode_info * pipe , struct file * out ,
850
- loff_t * ppos , size_t len , unsigned int flags )
827
+ ssize_t splice_to_socket (struct pipe_inode_info * pipe , struct file * out ,
828
+ loff_t * ppos , size_t len , unsigned int flags )
851
829
{
852
- return splice_from_pipe (pipe , out , ppos , len , flags , pipe_to_sendpage );
853
- }
830
+ struct socket * sock = sock_from_file (out );
831
+ struct bio_vec bvec [16 ];
832
+ struct msghdr msg = {};
833
+ ssize_t ret = 0 ;
834
+ size_t spliced = 0 ;
835
+ bool need_wakeup = false;
836
+
837
+ pipe_lock (pipe );
838
+
839
+ while (len > 0 ) {
840
+ unsigned int head , tail , mask , bc = 0 ;
841
+ size_t remain = len ;
842
+
843
+ /*
844
+ * Check for signal early to make process killable when there
845
+ * are always buffers available
846
+ */
847
+ ret = - ERESTARTSYS ;
848
+ if (signal_pending (current ))
849
+ break ;
854
850
855
- EXPORT_SYMBOL (generic_splice_sendpage );
851
+ while (pipe_empty (pipe -> head , pipe -> tail )) {
852
+ ret = 0 ;
853
+ if (!pipe -> writers )
854
+ goto out ;
855
+
856
+ if (spliced )
857
+ goto out ;
858
+
859
+ ret = - EAGAIN ;
860
+ if (flags & SPLICE_F_NONBLOCK )
861
+ goto out ;
862
+
863
+ ret = - ERESTARTSYS ;
864
+ if (signal_pending (current ))
865
+ goto out ;
866
+
867
+ if (need_wakeup ) {
868
+ wakeup_pipe_writers (pipe );
869
+ need_wakeup = false;
870
+ }
871
+
872
+ pipe_wait_readable (pipe );
873
+ }
874
+
875
+ head = pipe -> head ;
876
+ tail = pipe -> tail ;
877
+ mask = pipe -> ring_size - 1 ;
878
+
879
+ while (!pipe_empty (head , tail )) {
880
+ struct pipe_buffer * buf = & pipe -> bufs [tail & mask ];
881
+ size_t seg ;
882
+
883
+ if (!buf -> len ) {
884
+ tail ++ ;
885
+ continue ;
886
+ }
887
+
888
+ seg = min_t (size_t , remain , buf -> len );
889
+ seg = min_t (size_t , seg , PAGE_SIZE );
890
+
891
+ ret = pipe_buf_confirm (pipe , buf );
892
+ if (unlikely (ret )) {
893
+ if (ret == - ENODATA )
894
+ ret = 0 ;
895
+ break ;
896
+ }
897
+
898
+ bvec_set_page (& bvec [bc ++ ], buf -> page , seg , buf -> offset );
899
+ remain -= seg ;
900
+ if (seg >= buf -> len )
901
+ tail ++ ;
902
+ if (bc >= ARRAY_SIZE (bvec ))
903
+ break ;
904
+ }
905
+
906
+ if (!bc )
907
+ break ;
908
+
909
+ msg .msg_flags = MSG_SPLICE_PAGES ;
910
+ if (flags & SPLICE_F_MORE )
911
+ msg .msg_flags |= MSG_MORE ;
912
+ if (remain && pipe_occupancy (pipe -> head , tail ) > 0 )
913
+ msg .msg_flags |= MSG_MORE ;
914
+
915
+ iov_iter_bvec (& msg .msg_iter , ITER_SOURCE , bvec , bc ,
916
+ len - remain );
917
+ ret = sock_sendmsg (sock , & msg );
918
+ if (ret <= 0 )
919
+ break ;
920
+
921
+ spliced += ret ;
922
+ len -= ret ;
923
+ tail = pipe -> tail ;
924
+ while (ret > 0 ) {
925
+ struct pipe_buffer * buf = & pipe -> bufs [tail & mask ];
926
+ size_t seg = min_t (size_t , ret , buf -> len );
927
+
928
+ buf -> offset += seg ;
929
+ buf -> len -= seg ;
930
+ ret -= seg ;
931
+
932
+ if (!buf -> len ) {
933
+ pipe_buf_release (pipe , buf );
934
+ tail ++ ;
935
+ }
936
+ }
937
+
938
+ if (tail != pipe -> tail ) {
939
+ pipe -> tail = tail ;
940
+ if (pipe -> files )
941
+ need_wakeup = true;
942
+ }
943
+ }
944
+
945
+ out :
946
+ pipe_unlock (pipe );
947
+ if (need_wakeup )
948
+ wakeup_pipe_writers (pipe );
949
+ return spliced ?: ret ;
950
+ }
951
+ #endif
856
952
857
953
static int warn_unsupported (struct file * file , const char * op )
858
954
{
0 commit comments