1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/bvec.h>
21#include <linux/fs.h>
22#include <linux/file.h>
23#include <linux/pagemap.h>
24#include <linux/splice.h>
25#include <linux/memcontrol.h>
26#include <linux/mm_inline.h>
27#include <linux/swap.h>
28#include <linux/writeback.h>
29#include <linux/export.h>
30#include <linux/syscalls.h>
31#include <linux/uio.h>
32#include <linux/security.h>
33#include <linux/gfp.h>
34#include <linux/socket.h>
35#include <linux/compat.h>
36#include <linux/sched/signal.h>
37
38#include "internal.h"
39
40
41
42
43
44
45
46static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
47 struct pipe_buffer *buf)
48{
49 struct page *page = buf->page;
50 struct address_space *mapping;
51
52 lock_page(page);
53
54 mapping = page_mapping(page);
55 if (mapping) {
56 WARN_ON(!PageUptodate(page));
57
58
59
60
61
62
63
64
65
66 wait_on_page_writeback(page);
67
68 if (page_has_private(page) &&
69 !try_to_release_page(page, GFP_KERNEL))
70 goto out_unlock;
71
72
73
74
75
76 if (remove_mapping(mapping, page)) {
77 buf->flags |= PIPE_BUF_FLAG_LRU;
78 return 0;
79 }
80 }
81
82
83
84
85
86out_unlock:
87 unlock_page(page);
88 return 1;
89}
90
91static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
92 struct pipe_buffer *buf)
93{
94 put_page(buf->page);
95 buf->flags &= ~PIPE_BUF_FLAG_LRU;
96}
97
98
99
100
101
102static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
103 struct pipe_buffer *buf)
104{
105 struct page *page = buf->page;
106 int err;
107
108 if (!PageUptodate(page)) {
109 lock_page(page);
110
111
112
113
114
115 if (!page->mapping) {
116 err = -ENODATA;
117 goto error;
118 }
119
120
121
122
123 if (!PageUptodate(page)) {
124 err = -EIO;
125 goto error;
126 }
127
128
129
130
131 unlock_page(page);
132 }
133
134 return 0;
135error:
136 unlock_page(page);
137 return err;
138}
139
140const struct pipe_buf_operations page_cache_pipe_buf_ops = {
141 .can_merge = 0,
142 .confirm = page_cache_pipe_buf_confirm,
143 .release = page_cache_pipe_buf_release,
144 .steal = page_cache_pipe_buf_steal,
145 .get = generic_pipe_buf_get,
146};
147
148static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
149 struct pipe_buffer *buf)
150{
151 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
152 return 1;
153
154 buf->flags |= PIPE_BUF_FLAG_LRU;
155 return generic_pipe_buf_steal(pipe, buf);
156}
157
158static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159 .can_merge = 0,
160 .confirm = generic_pipe_buf_confirm,
161 .release = page_cache_pipe_buf_release,
162 .steal = user_page_pipe_buf_steal,
163 .get = generic_pipe_buf_get,
164};
165
166static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
167{
168 smp_mb();
169 if (waitqueue_active(&pipe->wait))
170 wake_up_interruptible(&pipe->wait);
171 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
172}
173
174
175
176
177
178
179
180
181
182
183
184
185ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
186 struct splice_pipe_desc *spd)
187{
188 unsigned int spd_pages = spd->nr_pages;
189 int ret = 0, page_nr = 0;
190
191 if (!spd_pages)
192 return 0;
193
194 if (unlikely(!pipe->readers)) {
195 send_sig(SIGPIPE, current, 0);
196 ret = -EPIPE;
197 goto out;
198 }
199
200 while (pipe->nrbufs < pipe->buffers) {
201 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
202 struct pipe_buffer *buf = pipe->bufs + newbuf;
203
204 buf->page = spd->pages[page_nr];
205 buf->offset = spd->partial[page_nr].offset;
206 buf->len = spd->partial[page_nr].len;
207 buf->private = spd->partial[page_nr].private;
208 buf->ops = spd->ops;
209 buf->flags = 0;
210
211 pipe->nrbufs++;
212 page_nr++;
213 ret += buf->len;
214
215 if (!--spd->nr_pages)
216 break;
217 }
218
219 if (!ret)
220 ret = -EAGAIN;
221
222out:
223 while (page_nr < spd_pages)
224 spd->spd_release(spd, page_nr++);
225
226 return ret;
227}
228EXPORT_SYMBOL_GPL(splice_to_pipe);
229
230ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
231{
232 int ret;
233
234 if (unlikely(!pipe->readers)) {
235 send_sig(SIGPIPE, current, 0);
236 ret = -EPIPE;
237 } else if (pipe->nrbufs == pipe->buffers) {
238 ret = -EAGAIN;
239 } else {
240 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
241 pipe->bufs[newbuf] = *buf;
242 pipe->nrbufs++;
243 return buf->len;
244 }
245 pipe_buf_release(pipe, buf);
246 return ret;
247}
248EXPORT_SYMBOL(add_to_pipe);
249
250
251
252
253
254int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
255{
256 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
257
258 spd->nr_pages_max = buffers;
259 if (buffers <= PIPE_DEF_BUFFERS)
260 return 0;
261
262 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
263 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
264
265 if (spd->pages && spd->partial)
266 return 0;
267
268 kfree(spd->pages);
269 kfree(spd->partial);
270 return -ENOMEM;
271}
272
273void splice_shrink_spd(struct splice_pipe_desc *spd)
274{
275 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
276 return;
277
278 kfree(spd->pages);
279 kfree(spd->partial);
280}
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
296 struct pipe_inode_info *pipe, size_t len,
297 unsigned int flags)
298{
299 struct iov_iter to;
300 struct kiocb kiocb;
301 int idx, ret;
302
303 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len);
304 idx = to.idx;
305 init_sync_kiocb(&kiocb, in);
306 kiocb.ki_pos = *ppos;
307 ret = call_read_iter(in, &kiocb, &to);
308 if (ret > 0) {
309 *ppos = kiocb.ki_pos;
310 file_accessed(in);
311 } else if (ret < 0) {
312 to.idx = idx;
313 to.iov_offset = 0;
314 iov_iter_advance(&to, 0);
315
316
317
318
319 if (ret == -EFAULT)
320 ret = -EAGAIN;
321 }
322
323 return ret;
324}
325EXPORT_SYMBOL(generic_file_splice_read);
326
327const struct pipe_buf_operations default_pipe_buf_ops = {
328 .can_merge = 0,
329 .confirm = generic_pipe_buf_confirm,
330 .release = generic_pipe_buf_release,
331 .steal = generic_pipe_buf_steal,
332 .get = generic_pipe_buf_get,
333};
334
335static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
336 struct pipe_buffer *buf)
337{
338 return 1;
339}
340
341
342const struct pipe_buf_operations nosteal_pipe_buf_ops = {
343 .can_merge = 0,
344 .confirm = generic_pipe_buf_confirm,
345 .release = generic_pipe_buf_release,
346 .steal = generic_pipe_buf_nosteal,
347 .get = generic_pipe_buf_get,
348};
349EXPORT_SYMBOL(nosteal_pipe_buf_ops);
350
351static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
352 unsigned long vlen, loff_t offset)
353{
354 mm_segment_t old_fs;
355 loff_t pos = offset;
356 ssize_t res;
357
358 old_fs = get_fs();
359 set_fs(get_ds());
360
361 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
362 set_fs(old_fs);
363
364 return res;
365}
366
367static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
368 struct pipe_inode_info *pipe, size_t len,
369 unsigned int flags)
370{
371 struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
372 struct iov_iter to;
373 struct page **pages;
374 unsigned int nr_pages;
375 size_t offset, base, copied = 0;
376 ssize_t res;
377 int i;
378
379 if (pipe->nrbufs == pipe->buffers)
380 return -EAGAIN;
381
382
383
384
385
386 offset = *ppos & ~PAGE_MASK;
387
388 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len + offset);
389
390 res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
391 if (res <= 0)
392 return -ENOMEM;
393
394 nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
395
396 vec = __vec;
397 if (nr_pages > PIPE_DEF_BUFFERS) {
398 vec = kmalloc(nr_pages * sizeof(struct kvec), GFP_KERNEL);
399 if (unlikely(!vec)) {
400 res = -ENOMEM;
401 goto out;
402 }
403 }
404
405 pipe->bufs[to.idx].offset = offset;
406 pipe->bufs[to.idx].len -= offset;
407
408 for (i = 0; i < nr_pages; i++) {
409 size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
410 vec[i].iov_base = page_address(pages[i]) + offset;
411 vec[i].iov_len = this_len;
412 len -= this_len;
413 offset = 0;
414 }
415
416 res = kernel_readv(in, vec, nr_pages, *ppos);
417 if (res > 0) {
418 copied = res;
419 *ppos += res;
420 }
421
422 if (vec != __vec)
423 kfree(vec);
424out:
425 for (i = 0; i < nr_pages; i++)
426 put_page(pages[i]);
427 kvfree(pages);
428 iov_iter_advance(&to, copied);
429 return res;
430}
431
432
433
434
435
436static int pipe_to_sendpage(struct pipe_inode_info *pipe,
437 struct pipe_buffer *buf, struct splice_desc *sd)
438{
439 struct file *file = sd->u.file;
440 loff_t pos = sd->pos;
441 int more;
442
443 if (!likely(file->f_op->sendpage))
444 return -EINVAL;
445
446 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
447
448 if (sd->len < sd->total_len && pipe->nrbufs > 1)
449 more |= MSG_SENDPAGE_NOTLAST;
450
451 return file->f_op->sendpage(file, buf->page, buf->offset,
452 sd->len, &pos, more);
453}
454
455static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
456{
457 smp_mb();
458 if (waitqueue_active(&pipe->wait))
459 wake_up_interruptible(&pipe->wait);
460 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
461}
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
484 splice_actor *actor)
485{
486 int ret;
487
488 while (pipe->nrbufs) {
489 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
490
491 sd->len = buf->len;
492 if (sd->len > sd->total_len)
493 sd->len = sd->total_len;
494
495 ret = pipe_buf_confirm(pipe, buf);
496 if (unlikely(ret)) {
497 if (ret == -ENODATA)
498 ret = 0;
499 return ret;
500 }
501
502 ret = actor(pipe, buf, sd);
503 if (ret <= 0)
504 return ret;
505
506 buf->offset += ret;
507 buf->len -= ret;
508
509 sd->num_spliced += ret;
510 sd->len -= ret;
511 sd->pos += ret;
512 sd->total_len -= ret;
513
514 if (!buf->len) {
515 pipe_buf_release(pipe, buf);
516 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
517 pipe->nrbufs--;
518 if (pipe->files)
519 sd->need_wakeup = true;
520 }
521
522 if (!sd->total_len)
523 return 0;
524 }
525
526 return 1;
527}
528
529
530
531
532
533
534
535
536
537
538
539static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
540{
541
542
543
544
545 if (signal_pending(current))
546 return -ERESTARTSYS;
547
548 while (!pipe->nrbufs) {
549 if (!pipe->writers)
550 return 0;
551
552 if (!pipe->waiting_writers && sd->num_spliced)
553 return 0;
554
555 if (sd->flags & SPLICE_F_NONBLOCK)
556 return -EAGAIN;
557
558 if (signal_pending(current))
559 return -ERESTARTSYS;
560
561 if (sd->need_wakeup) {
562 wakeup_pipe_writers(pipe);
563 sd->need_wakeup = false;
564 }
565
566 pipe_wait(pipe);
567 }
568
569 return 1;
570}
571
572
573
574
575
576
577
578
579
580
581static void splice_from_pipe_begin(struct splice_desc *sd)
582{
583 sd->num_spliced = 0;
584 sd->need_wakeup = false;
585}
586
587
588
589
590
591
592
593
594
595
596
597static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
598{
599 if (sd->need_wakeup)
600 wakeup_pipe_writers(pipe);
601}
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
617 splice_actor *actor)
618{
619 int ret;
620
621 splice_from_pipe_begin(sd);
622 do {
623 cond_resched();
624 ret = splice_from_pipe_next(pipe, sd);
625 if (ret > 0)
626 ret = splice_from_pipe_feed(pipe, sd, actor);
627 } while (ret > 0);
628 splice_from_pipe_end(pipe, sd);
629
630 return sd->num_spliced ? sd->num_spliced : ret;
631}
632EXPORT_SYMBOL(__splice_from_pipe);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
649 loff_t *ppos, size_t len, unsigned int flags,
650 splice_actor *actor)
651{
652 ssize_t ret;
653 struct splice_desc sd = {
654 .total_len = len,
655 .flags = flags,
656 .pos = *ppos,
657 .u.file = out,
658 };
659
660 pipe_lock(pipe);
661 ret = __splice_from_pipe(pipe, &sd, actor);
662 pipe_unlock(pipe);
663
664 return ret;
665}
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681ssize_t
682iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
683 loff_t *ppos, size_t len, unsigned int flags)
684{
685 struct splice_desc sd = {
686 .total_len = len,
687 .flags = flags,
688 .pos = *ppos,
689 .u.file = out,
690 };
691 int nbufs = pipe->buffers;
692 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
693 GFP_KERNEL);
694 ssize_t ret;
695
696 if (unlikely(!array))
697 return -ENOMEM;
698
699 pipe_lock(pipe);
700
701 splice_from_pipe_begin(&sd);
702 while (sd.total_len) {
703 struct iov_iter from;
704 size_t left;
705 int n, idx;
706
707 ret = splice_from_pipe_next(pipe, &sd);
708 if (ret <= 0)
709 break;
710
711 if (unlikely(nbufs < pipe->buffers)) {
712 kfree(array);
713 nbufs = pipe->buffers;
714 array = kcalloc(nbufs, sizeof(struct bio_vec),
715 GFP_KERNEL);
716 if (!array) {
717 ret = -ENOMEM;
718 break;
719 }
720 }
721
722
723 left = sd.total_len;
724 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
725 struct pipe_buffer *buf = pipe->bufs + idx;
726 size_t this_len = buf->len;
727
728 if (this_len > left)
729 this_len = left;
730
731 if (idx == pipe->buffers - 1)
732 idx = -1;
733
734 ret = pipe_buf_confirm(pipe, buf);
735 if (unlikely(ret)) {
736 if (ret == -ENODATA)
737 ret = 0;
738 goto done;
739 }
740
741 array[n].bv_page = buf->page;
742 array[n].bv_len = this_len;
743 array[n].bv_offset = buf->offset;
744 left -= this_len;
745 }
746
747 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
748 sd.total_len - left);
749 ret = vfs_iter_write(out, &from, &sd.pos, 0);
750 if (ret <= 0)
751 break;
752
753 sd.num_spliced += ret;
754 sd.total_len -= ret;
755 *ppos = sd.pos;
756
757
758 while (ret) {
759 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
760 if (ret >= buf->len) {
761 ret -= buf->len;
762 buf->len = 0;
763 pipe_buf_release(pipe, buf);
764 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
765 pipe->nrbufs--;
766 if (pipe->files)
767 sd.need_wakeup = true;
768 } else {
769 buf->offset += ret;
770 buf->len -= ret;
771 ret = 0;
772 }
773 }
774 }
775done:
776 kfree(array);
777 splice_from_pipe_end(pipe, &sd);
778
779 pipe_unlock(pipe);
780
781 if (sd.num_spliced)
782 ret = sd.num_spliced;
783
784 return ret;
785}
786
787EXPORT_SYMBOL(iter_file_splice_write);
788
789static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
790 struct splice_desc *sd)
791{
792 int ret;
793 void *data;
794 loff_t tmp = sd->pos;
795
796 data = kmap(buf->page);
797 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
798 kunmap(buf->page);
799
800 return ret;
801}
802
803static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
804 struct file *out, loff_t *ppos,
805 size_t len, unsigned int flags)
806{
807 ssize_t ret;
808
809 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
810 if (ret > 0)
811 *ppos += ret;
812
813 return ret;
814}
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
830 loff_t *ppos, size_t len, unsigned int flags)
831{
832 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
833}
834
835EXPORT_SYMBOL(generic_splice_sendpage);
836
837
838
839
840static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
841 loff_t *ppos, size_t len, unsigned int flags)
842{
843 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
844 loff_t *, size_t, unsigned int);
845
846 if (out->f_op->splice_write)
847 splice_write = out->f_op->splice_write;
848 else
849 splice_write = default_file_splice_write;
850
851 return splice_write(pipe, out, ppos, len, flags);
852}
853
854
855
856
857static long do_splice_to(struct file *in, loff_t *ppos,
858 struct pipe_inode_info *pipe, size_t len,
859 unsigned int flags)
860{
861 ssize_t (*splice_read)(struct file *, loff_t *,
862 struct pipe_inode_info *, size_t, unsigned int);
863 int ret;
864
865 if (unlikely(!(in->f_mode & FMODE_READ)))
866 return -EBADF;
867
868 ret = rw_verify_area(READ, in, ppos, len);
869 if (unlikely(ret < 0))
870 return ret;
871
872 if (unlikely(len > MAX_RW_COUNT))
873 len = MAX_RW_COUNT;
874
875 if (in->f_op->splice_read)
876 splice_read = in->f_op->splice_read;
877 else
878 splice_read = default_file_splice_read;
879
880 return splice_read(in, ppos, pipe, len, flags);
881}
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
897 splice_direct_actor *actor)
898{
899 struct pipe_inode_info *pipe;
900 long ret, bytes;
901 umode_t i_mode;
902 size_t len;
903 int i, flags, more;
904
905
906
907
908
909
910 i_mode = file_inode(in)->i_mode;
911 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
912 return -EINVAL;
913
914
915
916
917
918 pipe = current->splice_pipe;
919 if (unlikely(!pipe)) {
920 pipe = alloc_pipe_info();
921 if (!pipe)
922 return -ENOMEM;
923
924
925
926
927
928
929 pipe->readers = 1;
930
931 current->splice_pipe = pipe;
932 }
933
934
935
936
937 ret = 0;
938 bytes = 0;
939 len = sd->total_len;
940 flags = sd->flags;
941
942
943
944
945 sd->flags &= ~SPLICE_F_NONBLOCK;
946 more = sd->flags & SPLICE_F_MORE;
947
948 while (len) {
949 size_t read_len;
950 loff_t pos = sd->pos, prev_pos = pos;
951
952 ret = do_splice_to(in, &pos, pipe, len, flags);
953 if (unlikely(ret <= 0))
954 goto out_release;
955
956 read_len = ret;
957 sd->total_len = read_len;
958
959
960
961
962
963
964 if (read_len < len)
965 sd->flags |= SPLICE_F_MORE;
966 else if (!more)
967 sd->flags &= ~SPLICE_F_MORE;
968
969
970
971
972
973 ret = actor(pipe, sd);
974 if (unlikely(ret <= 0)) {
975 sd->pos = prev_pos;
976 goto out_release;
977 }
978
979 bytes += ret;
980 len -= ret;
981 sd->pos = pos;
982
983 if (ret < read_len) {
984 sd->pos = prev_pos + ret;
985 goto out_release;
986 }
987 }
988
989done:
990 pipe->nrbufs = pipe->curbuf = 0;
991 file_accessed(in);
992 return bytes;
993
994out_release:
995
996
997
998
999 for (i = 0; i < pipe->buffers; i++) {
1000 struct pipe_buffer *buf = pipe->bufs + i;
1001
1002 if (buf->ops)
1003 pipe_buf_release(pipe, buf);
1004 }
1005
1006 if (!bytes)
1007 bytes = ret;
1008
1009 goto done;
1010}
1011EXPORT_SYMBOL(splice_direct_to_actor);
1012
1013static int direct_splice_actor(struct pipe_inode_info *pipe,
1014 struct splice_desc *sd)
1015{
1016 struct file *file = sd->u.file;
1017
1018 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1019 sd->flags);
1020}
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1039 loff_t *opos, size_t len, unsigned int flags)
1040{
1041 struct splice_desc sd = {
1042 .len = len,
1043 .total_len = len,
1044 .flags = flags,
1045 .pos = *ppos,
1046 .u.file = out,
1047 .opos = opos,
1048 };
1049 long ret;
1050
1051 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1052 return -EBADF;
1053
1054 if (unlikely(out->f_flags & O_APPEND))
1055 return -EINVAL;
1056
1057 ret = rw_verify_area(WRITE, out, opos, len);
1058 if (unlikely(ret < 0))
1059 return ret;
1060
1061 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1062 if (ret > 0)
1063 *ppos = sd.pos;
1064
1065 return ret;
1066}
1067EXPORT_SYMBOL(do_splice_direct);
1068
1069static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1070{
1071 for (;;) {
1072 if (unlikely(!pipe->readers)) {
1073 send_sig(SIGPIPE, current, 0);
1074 return -EPIPE;
1075 }
1076 if (pipe->nrbufs != pipe->buffers)
1077 return 0;
1078 if (flags & SPLICE_F_NONBLOCK)
1079 return -EAGAIN;
1080 if (signal_pending(current))
1081 return -ERESTARTSYS;
1082 pipe->waiting_writers++;
1083 pipe_wait(pipe);
1084 pipe->waiting_writers--;
1085 }
1086}
1087
1088static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1089 struct pipe_inode_info *opipe,
1090 size_t len, unsigned int flags);
1091
1092
1093
1094
1095static long do_splice(struct file *in, loff_t __user *off_in,
1096 struct file *out, loff_t __user *off_out,
1097 size_t len, unsigned int flags)
1098{
1099 struct pipe_inode_info *ipipe;
1100 struct pipe_inode_info *opipe;
1101 loff_t offset;
1102 long ret;
1103
1104 ipipe = get_pipe_info(in);
1105 opipe = get_pipe_info(out);
1106
1107 if (ipipe && opipe) {
1108 if (off_in || off_out)
1109 return -ESPIPE;
1110
1111 if (!(in->f_mode & FMODE_READ))
1112 return -EBADF;
1113
1114 if (!(out->f_mode & FMODE_WRITE))
1115 return -EBADF;
1116
1117
1118 if (ipipe == opipe)
1119 return -EINVAL;
1120
1121 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1122 }
1123
1124 if (ipipe) {
1125 if (off_in)
1126 return -ESPIPE;
1127 if (off_out) {
1128 if (!(out->f_mode & FMODE_PWRITE))
1129 return -EINVAL;
1130 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1131 return -EFAULT;
1132 } else {
1133 offset = out->f_pos;
1134 }
1135
1136 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1137 return -EBADF;
1138
1139 if (unlikely(out->f_flags & O_APPEND))
1140 return -EINVAL;
1141
1142 ret = rw_verify_area(WRITE, out, &offset, len);
1143 if (unlikely(ret < 0))
1144 return ret;
1145
1146 file_start_write(out);
1147 ret = do_splice_from(ipipe, out, &offset, len, flags);
1148 file_end_write(out);
1149
1150 if (!off_out)
1151 out->f_pos = offset;
1152 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1153 ret = -EFAULT;
1154
1155 return ret;
1156 }
1157
1158 if (opipe) {
1159 if (off_out)
1160 return -ESPIPE;
1161 if (off_in) {
1162 if (!(in->f_mode & FMODE_PREAD))
1163 return -EINVAL;
1164 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1165 return -EFAULT;
1166 } else {
1167 offset = in->f_pos;
1168 }
1169
1170 pipe_lock(opipe);
1171 ret = wait_for_space(opipe, flags);
1172 if (!ret)
1173 ret = do_splice_to(in, &offset, opipe, len, flags);
1174 pipe_unlock(opipe);
1175 if (ret > 0)
1176 wakeup_pipe_readers(opipe);
1177 if (!off_in)
1178 in->f_pos = offset;
1179 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1180 ret = -EFAULT;
1181
1182 return ret;
1183 }
1184
1185 return -EINVAL;
1186}
1187
1188static int iter_to_pipe(struct iov_iter *from,
1189 struct pipe_inode_info *pipe,
1190 unsigned flags)
1191{
1192 struct pipe_buffer buf = {
1193 .ops = &user_page_pipe_buf_ops,
1194 .flags = flags
1195 };
1196 size_t total = 0;
1197 int ret = 0;
1198 bool failed = false;
1199
1200 while (iov_iter_count(from) && !failed) {
1201 struct page *pages[16];
1202 ssize_t copied;
1203 size_t start;
1204 int n;
1205
1206 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1207 if (copied <= 0) {
1208 ret = copied;
1209 break;
1210 }
1211
1212 for (n = 0; copied; n++, start = 0) {
1213 int size = min_t(int, copied, PAGE_SIZE - start);
1214 if (!failed) {
1215 buf.page = pages[n];
1216 buf.offset = start;
1217 buf.len = size;
1218 ret = add_to_pipe(pipe, &buf);
1219 if (unlikely(ret < 0)) {
1220 failed = true;
1221 } else {
1222 iov_iter_advance(from, ret);
1223 total += ret;
1224 }
1225 } else {
1226 put_page(pages[n]);
1227 }
1228 copied -= size;
1229 }
1230 }
1231 return total ? total : ret;
1232}
1233
1234static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1235 struct splice_desc *sd)
1236{
1237 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1238 return n == sd->len ? n : -EFAULT;
1239}
1240
1241
1242
1243
1244
1245static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
1246 unsigned long nr_segs, unsigned int flags)
1247{
1248 struct pipe_inode_info *pipe;
1249 struct splice_desc sd;
1250 long ret;
1251 struct iovec iovstack[UIO_FASTIOV];
1252 struct iovec *iov = iovstack;
1253 struct iov_iter iter;
1254
1255 pipe = get_pipe_info(file);
1256 if (!pipe)
1257 return -EBADF;
1258
1259 ret = import_iovec(READ, uiov, nr_segs,
1260 ARRAY_SIZE(iovstack), &iov, &iter);
1261 if (ret < 0)
1262 return ret;
1263
1264 sd.total_len = iov_iter_count(&iter);
1265 sd.len = 0;
1266 sd.flags = flags;
1267 sd.u.data = &iter;
1268 sd.pos = 0;
1269
1270 if (sd.total_len) {
1271 pipe_lock(pipe);
1272 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1273 pipe_unlock(pipe);
1274 }
1275
1276 kfree(iov);
1277 return ret;
1278}
1279
1280
1281
1282
1283
1284
1285static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
1286 unsigned long nr_segs, unsigned int flags)
1287{
1288 struct pipe_inode_info *pipe;
1289 struct iovec iovstack[UIO_FASTIOV];
1290 struct iovec *iov = iovstack;
1291 struct iov_iter from;
1292 long ret;
1293 unsigned buf_flag = 0;
1294
1295 if (flags & SPLICE_F_GIFT)
1296 buf_flag = PIPE_BUF_FLAG_GIFT;
1297
1298 pipe = get_pipe_info(file);
1299 if (!pipe)
1300 return -EBADF;
1301
1302 ret = import_iovec(WRITE, uiov, nr_segs,
1303 ARRAY_SIZE(iovstack), &iov, &from);
1304 if (ret < 0)
1305 return ret;
1306
1307 pipe_lock(pipe);
1308 ret = wait_for_space(pipe, flags);
1309 if (!ret)
1310 ret = iter_to_pipe(&from, pipe, buf_flag);
1311 pipe_unlock(pipe);
1312 if (ret > 0)
1313 wakeup_pipe_readers(pipe);
1314 kfree(iov);
1315 return ret;
1316}
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1335 unsigned long, nr_segs, unsigned int, flags)
1336{
1337 struct fd f;
1338 long error;
1339
1340 if (unlikely(flags & ~SPLICE_F_ALL))
1341 return -EINVAL;
1342 if (unlikely(nr_segs > UIO_MAXIOV))
1343 return -EINVAL;
1344 else if (unlikely(!nr_segs))
1345 return 0;
1346
1347 error = -EBADF;
1348 f = fdget(fd);
1349 if (f.file) {
1350 if (f.file->f_mode & FMODE_WRITE)
1351 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1352 else if (f.file->f_mode & FMODE_READ)
1353 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1354
1355 fdput(f);
1356 }
1357
1358 return error;
1359}
1360
1361#ifdef CONFIG_COMPAT
1362COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1363 unsigned int, nr_segs, unsigned int, flags)
1364{
1365 unsigned i;
1366 struct iovec __user *iov;
1367 if (nr_segs > UIO_MAXIOV)
1368 return -EINVAL;
1369 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1370 for (i = 0; i < nr_segs; i++) {
1371 struct compat_iovec v;
1372 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1373 get_user(v.iov_len, &iov32[i].iov_len) ||
1374 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1375 put_user(v.iov_len, &iov[i].iov_len))
1376 return -EFAULT;
1377 }
1378 return sys_vmsplice(fd, iov, nr_segs, flags);
1379}
1380#endif
1381
1382SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1383 int, fd_out, loff_t __user *, off_out,
1384 size_t, len, unsigned int, flags)
1385{
1386 struct fd in, out;
1387 long error;
1388
1389 if (unlikely(!len))
1390 return 0;
1391
1392 if (unlikely(flags & ~SPLICE_F_ALL))
1393 return -EINVAL;
1394
1395 error = -EBADF;
1396 in = fdget(fd_in);
1397 if (in.file) {
1398 if (in.file->f_mode & FMODE_READ) {
1399 out = fdget(fd_out);
1400 if (out.file) {
1401 if (out.file->f_mode & FMODE_WRITE)
1402 error = do_splice(in.file, off_in,
1403 out.file, off_out,
1404 len, flags);
1405 fdput(out);
1406 }
1407 }
1408 fdput(in);
1409 }
1410 return error;
1411}
1412
1413
1414
1415
1416
1417static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1418{
1419 int ret;
1420
1421
1422
1423
1424
1425 if (pipe->nrbufs)
1426 return 0;
1427
1428 ret = 0;
1429 pipe_lock(pipe);
1430
1431 while (!pipe->nrbufs) {
1432 if (signal_pending(current)) {
1433 ret = -ERESTARTSYS;
1434 break;
1435 }
1436 if (!pipe->writers)
1437 break;
1438 if (!pipe->waiting_writers) {
1439 if (flags & SPLICE_F_NONBLOCK) {
1440 ret = -EAGAIN;
1441 break;
1442 }
1443 }
1444 pipe_wait(pipe);
1445 }
1446
1447 pipe_unlock(pipe);
1448 return ret;
1449}
1450
1451
1452
1453
1454
1455static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1456{
1457 int ret;
1458
1459
1460
1461
1462
1463 if (pipe->nrbufs < pipe->buffers)
1464 return 0;
1465
1466 ret = 0;
1467 pipe_lock(pipe);
1468
1469 while (pipe->nrbufs >= pipe->buffers) {
1470 if (!pipe->readers) {
1471 send_sig(SIGPIPE, current, 0);
1472 ret = -EPIPE;
1473 break;
1474 }
1475 if (flags & SPLICE_F_NONBLOCK) {
1476 ret = -EAGAIN;
1477 break;
1478 }
1479 if (signal_pending(current)) {
1480 ret = -ERESTARTSYS;
1481 break;
1482 }
1483 pipe->waiting_writers++;
1484 pipe_wait(pipe);
1485 pipe->waiting_writers--;
1486 }
1487
1488 pipe_unlock(pipe);
1489 return ret;
1490}
1491
1492
1493
1494
1495static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1496 struct pipe_inode_info *opipe,
1497 size_t len, unsigned int flags)
1498{
1499 struct pipe_buffer *ibuf, *obuf;
1500 int ret = 0, nbuf;
1501 bool input_wakeup = false;
1502
1503
1504retry:
1505 ret = ipipe_prep(ipipe, flags);
1506 if (ret)
1507 return ret;
1508
1509 ret = opipe_prep(opipe, flags);
1510 if (ret)
1511 return ret;
1512
1513
1514
1515
1516
1517
1518 pipe_double_lock(ipipe, opipe);
1519
1520 do {
1521 if (!opipe->readers) {
1522 send_sig(SIGPIPE, current, 0);
1523 if (!ret)
1524 ret = -EPIPE;
1525 break;
1526 }
1527
1528 if (!ipipe->nrbufs && !ipipe->writers)
1529 break;
1530
1531
1532
1533
1534
1535 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
1536
1537 if (ret)
1538 break;
1539
1540 if (flags & SPLICE_F_NONBLOCK) {
1541 ret = -EAGAIN;
1542 break;
1543 }
1544
1545
1546
1547
1548
1549
1550 pipe_unlock(ipipe);
1551 pipe_unlock(opipe);
1552 goto retry;
1553 }
1554
1555 ibuf = ipipe->bufs + ipipe->curbuf;
1556 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1557 obuf = opipe->bufs + nbuf;
1558
1559 if (len >= ibuf->len) {
1560
1561
1562
1563 *obuf = *ibuf;
1564 ibuf->ops = NULL;
1565 opipe->nrbufs++;
1566 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
1567 ipipe->nrbufs--;
1568 input_wakeup = true;
1569 } else {
1570
1571
1572
1573
1574 pipe_buf_get(ipipe, ibuf);
1575 *obuf = *ibuf;
1576
1577
1578
1579
1580
1581 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1582
1583 obuf->len = len;
1584 opipe->nrbufs++;
1585 ibuf->offset += obuf->len;
1586 ibuf->len -= obuf->len;
1587 }
1588 ret += obuf->len;
1589 len -= obuf->len;
1590 } while (len);
1591
1592 pipe_unlock(ipipe);
1593 pipe_unlock(opipe);
1594
1595
1596
1597
1598 if (ret > 0)
1599 wakeup_pipe_readers(opipe);
1600
1601 if (input_wakeup)
1602 wakeup_pipe_writers(ipipe);
1603
1604 return ret;
1605}
1606
1607
1608
1609
1610static int link_pipe(struct pipe_inode_info *ipipe,
1611 struct pipe_inode_info *opipe,
1612 size_t len, unsigned int flags)
1613{
1614 struct pipe_buffer *ibuf, *obuf;
1615 int ret = 0, i = 0, nbuf;
1616
1617
1618
1619
1620
1621
1622 pipe_double_lock(ipipe, opipe);
1623
1624 do {
1625 if (!opipe->readers) {
1626 send_sig(SIGPIPE, current, 0);
1627 if (!ret)
1628 ret = -EPIPE;
1629 break;
1630 }
1631
1632
1633
1634
1635
1636 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1637 break;
1638
1639 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1640 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1641
1642
1643
1644
1645
1646 pipe_buf_get(ipipe, ibuf);
1647
1648 obuf = opipe->bufs + nbuf;
1649 *obuf = *ibuf;
1650
1651
1652
1653
1654
1655 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1656
1657 if (obuf->len > len)
1658 obuf->len = len;
1659
1660 opipe->nrbufs++;
1661 ret += obuf->len;
1662 len -= obuf->len;
1663 i++;
1664 } while (len);
1665
1666
1667
1668
1669
1670 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1671 ret = -EAGAIN;
1672
1673 pipe_unlock(ipipe);
1674 pipe_unlock(opipe);
1675
1676
1677
1678
1679 if (ret > 0)
1680 wakeup_pipe_readers(opipe);
1681
1682 return ret;
1683}
1684
1685
1686
1687
1688
1689
1690
1691static long do_tee(struct file *in, struct file *out, size_t len,
1692 unsigned int flags)
1693{
1694 struct pipe_inode_info *ipipe = get_pipe_info(in);
1695 struct pipe_inode_info *opipe = get_pipe_info(out);
1696 int ret = -EINVAL;
1697
1698
1699
1700
1701
1702 if (ipipe && opipe && ipipe != opipe) {
1703
1704
1705
1706
1707 ret = ipipe_prep(ipipe, flags);
1708 if (!ret) {
1709 ret = opipe_prep(opipe, flags);
1710 if (!ret)
1711 ret = link_pipe(ipipe, opipe, len, flags);
1712 }
1713 }
1714
1715 return ret;
1716}
1717
1718SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1719{
1720 struct fd in;
1721 int error;
1722
1723 if (unlikely(flags & ~SPLICE_F_ALL))
1724 return -EINVAL;
1725
1726 if (unlikely(!len))
1727 return 0;
1728
1729 error = -EBADF;
1730 in = fdget(fdin);
1731 if (in.file) {
1732 if (in.file->f_mode & FMODE_READ) {
1733 struct fd out = fdget(fdout);
1734 if (out.file) {
1735 if (out.file->f_mode & FMODE_WRITE)
1736 error = do_tee(in.file, out.file,
1737 len, flags);
1738 fdput(out);
1739 }
1740 }
1741 fdput(in);
1742 }
1743
1744 return error;
1745}
1746