ce6ffe94ba26ffba6169b9d4c07c2fe4a02c4690
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / splice.c
1 /*
2 * "splice": joining two ropes together by interweaving their strands.
3 *
4 * This is the "extended pipe" functionality, where a pipe is used as
5 * an arbitrary in-memory buffer. Think of a pipe as a small kernel
6 * buffer that you can use to transfer data from one end to the other.
7 *
8 * The traditional unix read/write is extended with a "splice()" operation
9 * that transfers data buffers to or from a pipe buffer.
10 *
11 * Named by Larry McVoy, original implementation from Linus, extended by
12 * Jens to support splicing to files, network, direct splicing, etc and
13 * fixing lots of bugs.
14 *
15 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
16 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
17 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
18 *
19 */
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/pagemap.h>
23 #include <linux/splice.h>
24 #include <linux/memcontrol.h>
25 #include <linux/mm_inline.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/export.h>
29 #include <linux/syscalls.h>
30 #include <linux/uio.h>
31 #include <linux/security.h>
32 #include <linux/gfp.h>
33 #include <linux/socket.h>
34 #include <linux/compat.h>
35 #include "internal.h"
36
37 /*
38 * Attempt to steal a page from a pipe buffer. This should perhaps go into
39 * a vm helper function, it's already simplified quite a bit by the
40 * addition of remove_mapping(). If success is returned, the caller may
41 * attempt to reuse this page for another destination.
42 */
43 static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
44 struct pipe_buffer *buf)
45 {
46 struct page *page = buf->page;
47 struct address_space *mapping;
48
49 lock_page(page);
50
51 mapping = page_mapping(page);
52 if (mapping) {
53 WARN_ON(!PageUptodate(page));
54
55 /*
56 * At least for ext2 with nobh option, we need to wait on
57 * writeback completing on this page, since we'll remove it
58 * from the pagecache. Otherwise truncate wont wait on the
59 * page, allowing the disk blocks to be reused by someone else
60 * before we actually wrote our data to them. fs corruption
61 * ensues.
62 */
63 wait_on_page_writeback(page);
64
65 if (page_has_private(page) &&
66 !try_to_release_page(page, GFP_KERNEL))
67 goto out_unlock;
68
69 /*
70 * If we succeeded in removing the mapping, set LRU flag
71 * and return good.
72 */
73 if (remove_mapping(mapping, page)) {
74 buf->flags |= PIPE_BUF_FLAG_LRU;
75 return 0;
76 }
77 }
78
79 /*
80 * Raced with truncate or failed to remove page from current
81 * address space, unlock and return failure.
82 */
83 out_unlock:
84 unlock_page(page);
85 return 1;
86 }
87
88 static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
89 struct pipe_buffer *buf)
90 {
91 page_cache_release(buf->page);
92 buf->flags &= ~PIPE_BUF_FLAG_LRU;
93 }
94
95 /*
96 * Check whether the contents of buf is OK to access. Since the content
97 * is a page cache page, IO may be in flight.
98 */
99 static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
100 struct pipe_buffer *buf)
101 {
102 struct page *page = buf->page;
103 int err;
104
105 if (!PageUptodate(page)) {
106 lock_page(page);
107
108 /*
109 * Page got truncated/unhashed. This will cause a 0-byte
110 * splice, if this is the first page.
111 */
112 if (!page->mapping) {
113 err = -ENODATA;
114 goto error;
115 }
116
117 /*
118 * Uh oh, read-error from disk.
119 */
120 if (!PageUptodate(page)) {
121 err = -EIO;
122 goto error;
123 }
124
125 /*
126 * Page is ok afterall, we are done.
127 */
128 unlock_page(page);
129 }
130
131 return 0;
132 error:
133 unlock_page(page);
134 return err;
135 }
136
137 const struct pipe_buf_operations page_cache_pipe_buf_ops = {
138 .can_merge = 0,
139 .map = generic_pipe_buf_map,
140 .unmap = generic_pipe_buf_unmap,
141 .confirm = page_cache_pipe_buf_confirm,
142 .release = page_cache_pipe_buf_release,
143 .steal = page_cache_pipe_buf_steal,
144 .get = generic_pipe_buf_get,
145 };
146
147 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
148 struct pipe_buffer *buf)
149 {
150 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
151 return 1;
152
153 buf->flags |= PIPE_BUF_FLAG_LRU;
154 return generic_pipe_buf_steal(pipe, buf);
155 }
156
157 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
158 .can_merge = 0,
159 .map = generic_pipe_buf_map,
160 .unmap = generic_pipe_buf_unmap,
161 .confirm = generic_pipe_buf_confirm,
162 .release = page_cache_pipe_buf_release,
163 .steal = user_page_pipe_buf_steal,
164 .get = generic_pipe_buf_get,
165 };
166
167 static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
168 {
169 smp_mb();
170 if (waitqueue_active(&pipe->wait))
171 wake_up_interruptible(&pipe->wait);
172 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
173 }
174
175 /**
176 * splice_to_pipe - fill passed data into a pipe
177 * @pipe: pipe to fill
178 * @spd: data to fill
179 *
180 * Description:
181 * @spd contains a map of pages and len/offset tuples, along with
182 * the struct pipe_buf_operations associated with these pages. This
183 * function will link that data to the pipe.
184 *
185 */
186 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
187 struct splice_pipe_desc *spd)
188 {
189 unsigned int spd_pages = spd->nr_pages;
190 int ret, do_wakeup, page_nr;
191
192 if (!spd_pages)
193 return 0;
194
195 ret = 0;
196 do_wakeup = 0;
197 page_nr = 0;
198
199 pipe_lock(pipe);
200
201 for (;;) {
202 if (!pipe->readers) {
203 send_sig(SIGPIPE, current, 0);
204 if (!ret)
205 ret = -EPIPE;
206 break;
207 }
208
209 if (pipe->nrbufs < pipe->buffers) {
210 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
211 struct pipe_buffer *buf = pipe->bufs + newbuf;
212
213 buf->page = spd->pages[page_nr];
214 buf->offset = spd->partial[page_nr].offset;
215 buf->len = spd->partial[page_nr].len;
216 buf->private = spd->partial[page_nr].private;
217 buf->ops = spd->ops;
218 buf->flags = 0;
219 if (spd->flags & SPLICE_F_GIFT)
220 buf->flags |= PIPE_BUF_FLAG_GIFT;
221
222 pipe->nrbufs++;
223 page_nr++;
224 ret += buf->len;
225
226 if (pipe->files)
227 do_wakeup = 1;
228
229 if (!--spd->nr_pages)
230 break;
231 if (pipe->nrbufs < pipe->buffers)
232 continue;
233
234 break;
235 }
236
237 if (spd->flags & SPLICE_F_NONBLOCK) {
238 if (!ret)
239 ret = -EAGAIN;
240 break;
241 }
242
243 if (signal_pending(current)) {
244 if (!ret)
245 ret = -ERESTARTSYS;
246 break;
247 }
248
249 if (do_wakeup) {
250 smp_mb();
251 if (waitqueue_active(&pipe->wait))
252 wake_up_interruptible_sync(&pipe->wait);
253 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
254 do_wakeup = 0;
255 }
256
257 pipe->waiting_writers++;
258 pipe_wait(pipe);
259 pipe->waiting_writers--;
260 }
261
262 pipe_unlock(pipe);
263
264 if (do_wakeup)
265 wakeup_pipe_readers(pipe);
266
267 while (page_nr < spd_pages)
268 spd->spd_release(spd, page_nr++);
269
270 return ret;
271 }
272
273 void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
274 {
275 page_cache_release(spd->pages[i]);
276 }
277
278 /*
279 * Check if we need to grow the arrays holding pages and partial page
280 * descriptions.
281 */
282 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
283 {
284 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
285
286 spd->nr_pages_max = buffers;
287 if (buffers <= PIPE_DEF_BUFFERS)
288 return 0;
289
290 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
291 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
292
293 if (spd->pages && spd->partial)
294 return 0;
295
296 kfree(spd->pages);
297 kfree(spd->partial);
298 return -ENOMEM;
299 }
300
301 void splice_shrink_spd(struct splice_pipe_desc *spd)
302 {
303 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
304 return;
305
306 kfree(spd->pages);
307 kfree(spd->partial);
308 }
309
310 static int
311 __generic_file_splice_read(struct file *in, loff_t *ppos,
312 struct pipe_inode_info *pipe, size_t len,
313 unsigned int flags)
314 {
315 struct address_space *mapping = in->f_mapping;
316 unsigned int loff, nr_pages, req_pages;
317 struct page *pages[PIPE_DEF_BUFFERS];
318 struct partial_page partial[PIPE_DEF_BUFFERS];
319 struct page *page;
320 pgoff_t index, end_index;
321 loff_t isize;
322 int error, page_nr;
323 struct splice_pipe_desc spd = {
324 .pages = pages,
325 .partial = partial,
326 .nr_pages_max = PIPE_DEF_BUFFERS,
327 .flags = flags,
328 .ops = &page_cache_pipe_buf_ops,
329 .spd_release = spd_release_page,
330 };
331
332 if (splice_grow_spd(pipe, &spd))
333 return -ENOMEM;
334
335 index = *ppos >> PAGE_CACHE_SHIFT;
336 loff = *ppos & ~PAGE_CACHE_MASK;
337 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
338 nr_pages = min(req_pages, spd.nr_pages_max);
339
340 /*
341 * Lookup the (hopefully) full range of pages we need.
342 */
343 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
344 index += spd.nr_pages;
345
346 /*
347 * If find_get_pages_contig() returned fewer pages than we needed,
348 * readahead/allocate the rest and fill in the holes.
349 */
350 if (spd.nr_pages < nr_pages)
351 page_cache_sync_readahead(mapping, &in->f_ra, in,
352 index, req_pages - spd.nr_pages);
353
354 error = 0;
355 while (spd.nr_pages < nr_pages) {
356 /*
357 * Page could be there, find_get_pages_contig() breaks on
358 * the first hole.
359 */
360 page = find_get_page(mapping, index);
361 if (!page) {
362 /*
363 * page didn't exist, allocate one.
364 */
365 page = page_cache_alloc_cold(mapping);
366 if (!page)
367 break;
368
369 error = add_to_page_cache_lru(page, mapping, index,
370 GFP_KERNEL);
371 if (unlikely(error)) {
372 page_cache_release(page);
373 if (error == -EEXIST)
374 continue;
375 break;
376 }
377 /*
378 * add_to_page_cache() locks the page, unlock it
379 * to avoid convoluting the logic below even more.
380 */
381 unlock_page(page);
382 }
383
384 spd.pages[spd.nr_pages++] = page;
385 index++;
386 }
387
388 /*
389 * Now loop over the map and see if we need to start IO on any
390 * pages, fill in the partial map, etc.
391 */
392 index = *ppos >> PAGE_CACHE_SHIFT;
393 nr_pages = spd.nr_pages;
394 spd.nr_pages = 0;
395 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
396 unsigned int this_len;
397
398 if (!len)
399 break;
400
401 /*
402 * this_len is the max we'll use from this page
403 */
404 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
405 page = spd.pages[page_nr];
406
407 if (PageReadahead(page))
408 page_cache_async_readahead(mapping, &in->f_ra, in,
409 page, index, req_pages - page_nr);
410
411 /*
412 * If the page isn't uptodate, we may need to start io on it
413 */
414 if (!PageUptodate(page)) {
415 lock_page(page);
416
417 /*
418 * Page was truncated, or invalidated by the
419 * filesystem. Redo the find/create, but this time the
420 * page is kept locked, so there's no chance of another
421 * race with truncate/invalidate.
422 */
423 if (!page->mapping) {
424 unlock_page(page);
425 page = find_or_create_page(mapping, index,
426 mapping_gfp_mask(mapping));
427
428 if (!page) {
429 error = -ENOMEM;
430 break;
431 }
432 page_cache_release(spd.pages[page_nr]);
433 spd.pages[page_nr] = page;
434 }
435 /*
436 * page was already under io and is now done, great
437 */
438 if (PageUptodate(page)) {
439 unlock_page(page);
440 goto fill_it;
441 }
442
443 /*
444 * need to read in the page
445 */
446 error = mapping->a_ops->readpage(in, page);
447 if (unlikely(error)) {
448 /*
449 * We really should re-lookup the page here,
450 * but it complicates things a lot. Instead
451 * lets just do what we already stored, and
452 * we'll get it the next time we are called.
453 */
454 if (error == AOP_TRUNCATED_PAGE)
455 error = 0;
456
457 break;
458 }
459 }
460 fill_it:
461 /*
462 * i_size must be checked after PageUptodate.
463 */
464 isize = i_size_read(mapping->host);
465 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
466 if (unlikely(!isize || index > end_index))
467 break;
468
469 /*
470 * if this is the last page, see if we need to shrink
471 * the length and stop
472 */
473 if (end_index == index) {
474 unsigned int plen;
475
476 /*
477 * max good bytes in this page
478 */
479 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
480 if (plen <= loff)
481 break;
482
483 /*
484 * force quit after adding this page
485 */
486 this_len = min(this_len, plen - loff);
487 len = this_len;
488 }
489
490 spd.partial[page_nr].offset = loff;
491 spd.partial[page_nr].len = this_len;
492 len -= this_len;
493 loff = 0;
494 spd.nr_pages++;
495 index++;
496 }
497
498 /*
499 * Release any pages at the end, if we quit early. 'page_nr' is how far
500 * we got, 'nr_pages' is how many pages are in the map.
501 */
502 while (page_nr < nr_pages)
503 page_cache_release(spd.pages[page_nr++]);
504 in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
505
506 if (spd.nr_pages)
507 error = splice_to_pipe(pipe, &spd);
508
509 splice_shrink_spd(&spd);
510 return error;
511 }
512
513 /**
514 * generic_file_splice_read - splice data from file to a pipe
515 * @in: file to splice from
516 * @ppos: position in @in
517 * @pipe: pipe to splice to
518 * @len: number of bytes to splice
519 * @flags: splice modifier flags
520 *
521 * Description:
522 * Will read pages from given file and fill them into a pipe. Can be
523 * used as long as the address_space operations for the source implements
524 * a readpage() hook.
525 *
526 */
527 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
528 struct pipe_inode_info *pipe, size_t len,
529 unsigned int flags)
530 {
531 loff_t isize, left;
532 int ret;
533
534 isize = i_size_read(in->f_mapping->host);
535 if (unlikely(*ppos >= isize))
536 return 0;
537
538 left = isize - *ppos;
539 if (unlikely(left < len))
540 len = left;
541
542 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
543 if (ret > 0) {
544 *ppos += ret;
545 file_accessed(in);
546 }
547
548 return ret;
549 }
550 EXPORT_SYMBOL(generic_file_splice_read);
551
552 static const struct pipe_buf_operations default_pipe_buf_ops = {
553 .can_merge = 0,
554 .map = generic_pipe_buf_map,
555 .unmap = generic_pipe_buf_unmap,
556 .confirm = generic_pipe_buf_confirm,
557 .release = generic_pipe_buf_release,
558 .steal = generic_pipe_buf_steal,
559 .get = generic_pipe_buf_get,
560 };
561
562 static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
563 struct pipe_buffer *buf)
564 {
565 return 1;
566 }
567
568 /* Pipe buffer operations for a socket and similar. */
569 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
570 .can_merge = 0,
571 .map = generic_pipe_buf_map,
572 .unmap = generic_pipe_buf_unmap,
573 .confirm = generic_pipe_buf_confirm,
574 .release = generic_pipe_buf_release,
575 .steal = generic_pipe_buf_nosteal,
576 .get = generic_pipe_buf_get,
577 };
578 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
579
580 static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
581 unsigned long vlen, loff_t offset)
582 {
583 mm_segment_t old_fs;
584 loff_t pos = offset;
585 ssize_t res;
586
587 old_fs = get_fs();
588 set_fs(get_ds());
589 /* The cast to a user pointer is valid due to the set_fs() */
590 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos);
591 set_fs(old_fs);
592
593 return res;
594 }
595
596 ssize_t kernel_write(struct file *file, const char *buf, size_t count,
597 loff_t pos)
598 {
599 mm_segment_t old_fs;
600 ssize_t res;
601
602 old_fs = get_fs();
603 set_fs(get_ds());
604 /* The cast to a user pointer is valid due to the set_fs() */
605 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
606 set_fs(old_fs);
607
608 return res;
609 }
610 EXPORT_SYMBOL(kernel_write);
611
612 ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
613 struct pipe_inode_info *pipe, size_t len,
614 unsigned int flags)
615 {
616 unsigned int nr_pages;
617 unsigned int nr_freed;
618 size_t offset;
619 struct page *pages[PIPE_DEF_BUFFERS];
620 struct partial_page partial[PIPE_DEF_BUFFERS];
621 struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
622 ssize_t res;
623 size_t this_len;
624 int error;
625 int i;
626 struct splice_pipe_desc spd = {
627 .pages = pages,
628 .partial = partial,
629 .nr_pages_max = PIPE_DEF_BUFFERS,
630 .flags = flags,
631 .ops = &default_pipe_buf_ops,
632 .spd_release = spd_release_page,
633 };
634
635 if (splice_grow_spd(pipe, &spd))
636 return -ENOMEM;
637
638 res = -ENOMEM;
639 vec = __vec;
640 if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
641 vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
642 if (!vec)
643 goto shrink_ret;
644 }
645
646 offset = *ppos & ~PAGE_CACHE_MASK;
647 nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
648
649 for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
650 struct page *page;
651
652 page = alloc_page(GFP_USER);
653 error = -ENOMEM;
654 if (!page)
655 goto err;
656
657 this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset);
658 vec[i].iov_base = (void __user *) page_address(page);
659 vec[i].iov_len = this_len;
660 spd.pages[i] = page;
661 spd.nr_pages++;
662 len -= this_len;
663 offset = 0;
664 }
665
666 res = kernel_readv(in, vec, spd.nr_pages, *ppos);
667 if (res < 0) {
668 error = res;
669 goto err;
670 }
671
672 error = 0;
673 if (!res)
674 goto err;
675
676 nr_freed = 0;
677 for (i = 0; i < spd.nr_pages; i++) {
678 this_len = min_t(size_t, vec[i].iov_len, res);
679 spd.partial[i].offset = 0;
680 spd.partial[i].len = this_len;
681 if (!this_len) {
682 __free_page(spd.pages[i]);
683 spd.pages[i] = NULL;
684 nr_freed++;
685 }
686 res -= this_len;
687 }
688 spd.nr_pages -= nr_freed;
689
690 res = splice_to_pipe(pipe, &spd);
691 if (res > 0)
692 *ppos += res;
693
694 shrink_ret:
695 if (vec != __vec)
696 kfree(vec);
697 splice_shrink_spd(&spd);
698 return res;
699
700 err:
701 for (i = 0; i < spd.nr_pages; i++)
702 __free_page(spd.pages[i]);
703
704 res = error;
705 goto shrink_ret;
706 }
707 EXPORT_SYMBOL(default_file_splice_read);
708
709 /*
710 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
711 * using sendpage(). Return the number of bytes sent.
712 */
713 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
714 struct pipe_buffer *buf, struct splice_desc *sd)
715 {
716 struct file *file = sd->u.file;
717 loff_t pos = sd->pos;
718 int more;
719
720 if (!likely(file->f_op && file->f_op->sendpage))
721 return -EINVAL;
722
723 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
724
725 if (sd->len < sd->total_len && pipe->nrbufs > 1)
726 more |= MSG_SENDPAGE_NOTLAST;
727
728 return file->f_op->sendpage(file, buf->page, buf->offset,
729 sd->len, &pos, more);
730 }
731
732 /*
733 * This is a little more tricky than the file -> pipe splicing. There are
734 * basically three cases:
735 *
736 * - Destination page already exists in the address space and there
737 * are users of it. For that case we have no other option that
738 * copying the data. Tough luck.
739 * - Destination page already exists in the address space, but there
740 * are no users of it. Make sure it's uptodate, then drop it. Fall
741 * through to last case.
742 * - Destination page does not exist, we can add the pipe page to
743 * the page cache and avoid the copy.
744 *
745 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
746 * sd->flags), we attempt to migrate pages from the pipe to the output
747 * file address space page cache. This is possible if no one else has
748 * the pipe page referenced outside of the pipe and page cache. If
749 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
750 * a new page in the output file page cache and fill/dirty that.
751 */
752 int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
753 struct splice_desc *sd)
754 {
755 struct file *file = sd->u.file;
756 struct address_space *mapping = file->f_mapping;
757 unsigned int offset, this_len;
758 struct page *page;
759 void *fsdata;
760 int ret;
761
762 offset = sd->pos & ~PAGE_CACHE_MASK;
763
764 this_len = sd->len;
765 if (this_len + offset > PAGE_CACHE_SIZE)
766 this_len = PAGE_CACHE_SIZE - offset;
767
768 ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
769 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
770 if (unlikely(ret))
771 goto out;
772
773 if (buf->page != page) {
774 char *src = buf->ops->map(pipe, buf, 1);
775 char *dst = kmap_atomic(page);
776
777 memcpy(dst + offset, src + buf->offset, this_len);
778 flush_dcache_page(page);
779 kunmap_atomic(dst);
780 buf->ops->unmap(pipe, buf, src);
781 }
782 ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
783 page, fsdata);
784 out:
785 return ret;
786 }
787 EXPORT_SYMBOL(pipe_to_file);
788
789 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
790 {
791 smp_mb();
792 if (waitqueue_active(&pipe->wait))
793 wake_up_interruptible(&pipe->wait);
794 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
795 }
796
797 /**
798 * splice_from_pipe_feed - feed available data from a pipe to a file
799 * @pipe: pipe to splice from
800 * @sd: information to @actor
801 * @actor: handler that splices the data
802 *
803 * Description:
804 * This function loops over the pipe and calls @actor to do the
805 * actual moving of a single struct pipe_buffer to the desired
806 * destination. It returns when there's no more buffers left in
807 * the pipe or if the requested number of bytes (@sd->total_len)
808 * have been copied. It returns a positive number (one) if the
809 * pipe needs to be filled with more data, zero if the required
810 * number of bytes have been copied and -errno on error.
811 *
812 * This, together with splice_from_pipe_{begin,end,next}, may be
813 * used to implement the functionality of __splice_from_pipe() when
814 * locking is required around copying the pipe buffers to the
815 * destination.
816 */
817 int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
818 splice_actor *actor)
819 {
820 int ret;
821
822 while (pipe->nrbufs) {
823 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
824 const struct pipe_buf_operations *ops = buf->ops;
825
826 sd->len = buf->len;
827 if (sd->len > sd->total_len)
828 sd->len = sd->total_len;
829
830 ret = buf->ops->confirm(pipe, buf);
831 if (unlikely(ret)) {
832 if (ret == -ENODATA)
833 ret = 0;
834 return ret;
835 }
836
837 ret = actor(pipe, buf, sd);
838 if (ret <= 0)
839 return ret;
840
841 buf->offset += ret;
842 buf->len -= ret;
843
844 sd->num_spliced += ret;
845 sd->len -= ret;
846 sd->pos += ret;
847 sd->total_len -= ret;
848
849 if (!buf->len) {
850 buf->ops = NULL;
851 ops->release(pipe, buf);
852 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
853 pipe->nrbufs--;
854 if (pipe->files)
855 sd->need_wakeup = true;
856 }
857
858 if (!sd->total_len)
859 return 0;
860 }
861
862 return 1;
863 }
864 EXPORT_SYMBOL(splice_from_pipe_feed);
865
866 /**
867 * splice_from_pipe_next - wait for some data to splice from
868 * @pipe: pipe to splice from
869 * @sd: information about the splice operation
870 *
871 * Description:
872 * This function will wait for some data and return a positive
873 * value (one) if pipe buffers are available. It will return zero
874 * or -errno if no more data needs to be spliced.
875 */
876 int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
877 {
878 while (!pipe->nrbufs) {
879 if (!pipe->writers)
880 return 0;
881
882 if (!pipe->waiting_writers && sd->num_spliced)
883 return 0;
884
885 if (sd->flags & SPLICE_F_NONBLOCK)
886 return -EAGAIN;
887
888 if (signal_pending(current))
889 return -ERESTARTSYS;
890
891 if (sd->need_wakeup) {
892 wakeup_pipe_writers(pipe);
893 sd->need_wakeup = false;
894 }
895
896 pipe_wait(pipe);
897 }
898
899 return 1;
900 }
901 EXPORT_SYMBOL(splice_from_pipe_next);
902
903 /**
904 * splice_from_pipe_begin - start splicing from pipe
905 * @sd: information about the splice operation
906 *
907 * Description:
908 * This function should be called before a loop containing
909 * splice_from_pipe_next() and splice_from_pipe_feed() to
910 * initialize the necessary fields of @sd.
911 */
912 void splice_from_pipe_begin(struct splice_desc *sd)
913 {
914 sd->num_spliced = 0;
915 sd->need_wakeup = false;
916 }
917 EXPORT_SYMBOL(splice_from_pipe_begin);
918
919 /**
920 * splice_from_pipe_end - finish splicing from pipe
921 * @pipe: pipe to splice from
922 * @sd: information about the splice operation
923 *
924 * Description:
925 * This function will wake up pipe writers if necessary. It should
926 * be called after a loop containing splice_from_pipe_next() and
927 * splice_from_pipe_feed().
928 */
929 void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
930 {
931 if (sd->need_wakeup)
932 wakeup_pipe_writers(pipe);
933 }
934 EXPORT_SYMBOL(splice_from_pipe_end);
935
936 /**
937 * __splice_from_pipe - splice data from a pipe to given actor
938 * @pipe: pipe to splice from
939 * @sd: information to @actor
940 * @actor: handler that splices the data
941 *
942 * Description:
943 * This function does little more than loop over the pipe and call
944 * @actor to do the actual moving of a single struct pipe_buffer to
945 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
946 * pipe_to_user.
947 *
948 */
949 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
950 splice_actor *actor)
951 {
952 int ret;
953
954 splice_from_pipe_begin(sd);
955 do {
956 cond_resched();
957 ret = splice_from_pipe_next(pipe, sd);
958 if (ret > 0)
959 ret = splice_from_pipe_feed(pipe, sd, actor);
960 } while (ret > 0);
961 splice_from_pipe_end(pipe, sd);
962
963 return sd->num_spliced ? sd->num_spliced : ret;
964 }
965 EXPORT_SYMBOL(__splice_from_pipe);
966
967 /**
968 * splice_from_pipe - splice data from a pipe to a file
969 * @pipe: pipe to splice from
970 * @out: file to splice to
971 * @ppos: position in @out
972 * @len: how many bytes to splice
973 * @flags: splice modifier flags
974 * @actor: handler that splices the data
975 *
976 * Description:
977 * See __splice_from_pipe. This function locks the pipe inode,
978 * otherwise it's identical to __splice_from_pipe().
979 *
980 */
981 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
982 loff_t *ppos, size_t len, unsigned int flags,
983 splice_actor *actor)
984 {
985 ssize_t ret;
986 struct splice_desc sd = {
987 .total_len = len,
988 .flags = flags,
989 .pos = *ppos,
990 .u.file = out,
991 };
992
993 pipe_lock(pipe);
994 ret = __splice_from_pipe(pipe, &sd, actor);
995 pipe_unlock(pipe);
996
997 return ret;
998 }
999
1000 /**
1001 * generic_file_splice_write - splice data from a pipe to a file
1002 * @pipe: pipe info
1003 * @out: file to write to
1004 * @ppos: position in @out
1005 * @len: number of bytes to splice
1006 * @flags: splice modifier flags
1007 *
1008 * Description:
1009 * Will either move or copy pages (determined by @flags options) from
1010 * the given pipe inode to the given file.
1011 *
1012 */
1013 ssize_t
1014 generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
1015 loff_t *ppos, size_t len, unsigned int flags)
1016 {
1017 struct address_space *mapping = out->f_mapping;
1018 struct inode *inode = mapping->host;
1019 struct splice_desc sd = {
1020 .flags = flags,
1021 .u.file = out,
1022 };
1023 ssize_t ret;
1024
1025 ret = generic_write_checks(out, ppos, &len, S_ISBLK(inode->i_mode));
1026 if (ret)
1027 return ret;
1028 sd.total_len = len;
1029 sd.pos = *ppos;
1030
1031 pipe_lock(pipe);
1032
1033 splice_from_pipe_begin(&sd);
1034 do {
1035 ret = splice_from_pipe_next(pipe, &sd);
1036 if (ret <= 0)
1037 break;
1038
1039 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1040 ret = file_remove_suid(out);
1041 if (!ret) {
1042 ret = file_update_time(out);
1043 if (!ret)
1044 ret = splice_from_pipe_feed(pipe, &sd,
1045 pipe_to_file);
1046 }
1047 mutex_unlock(&inode->i_mutex);
1048 } while (ret > 0);
1049 splice_from_pipe_end(pipe, &sd);
1050
1051 pipe_unlock(pipe);
1052
1053 if (sd.num_spliced)
1054 ret = sd.num_spliced;
1055
1056 if (ret > 0) {
1057 int err;
1058
1059 err = generic_write_sync(out, *ppos, ret);
1060 if (err)
1061 ret = err;
1062 else
1063 *ppos += ret;
1064 balance_dirty_pages_ratelimited(mapping);
1065 }
1066
1067 return ret;
1068 }
1069
1070 EXPORT_SYMBOL(generic_file_splice_write);
1071
1072 static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1073 struct splice_desc *sd)
1074 {
1075 int ret;
1076 void *data;
1077 loff_t tmp = sd->pos;
1078
1079 data = buf->ops->map(pipe, buf, 0);
1080 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
1081 buf->ops->unmap(pipe, buf, data);
1082
1083 return ret;
1084 }
1085
1086 static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
1087 struct file *out, loff_t *ppos,
1088 size_t len, unsigned int flags)
1089 {
1090 ssize_t ret;
1091
1092 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1093 if (ret > 0)
1094 *ppos += ret;
1095
1096 return ret;
1097 }
1098
1099 /**
1100 * generic_splice_sendpage - splice data from a pipe to a socket
1101 * @pipe: pipe to splice from
1102 * @out: socket to write to
1103 * @ppos: position in @out
1104 * @len: number of bytes to splice
1105 * @flags: splice modifier flags
1106 *
1107 * Description:
1108 * Will send @len bytes from the pipe to a network socket. No data copying
1109 * is involved.
1110 *
1111 */
1112 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
1113 loff_t *ppos, size_t len, unsigned int flags)
1114 {
1115 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
1116 }
1117
1118 EXPORT_SYMBOL(generic_splice_sendpage);
1119
1120 /*
1121 * Attempt to initiate a splice from pipe to file.
1122 */
1123 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
1124 loff_t *ppos, size_t len, unsigned int flags)
1125 {
1126 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
1127 loff_t *, size_t, unsigned int);
1128 int ret;
1129
1130 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1131 return -EBADF;
1132
1133 if (unlikely(out->f_flags & O_APPEND))
1134 return -EINVAL;
1135
1136 ret = rw_verify_area(WRITE, out, ppos, len);
1137 if (unlikely(ret < 0))
1138 return ret;
1139
1140 if (out->f_op && out->f_op->splice_write)
1141 splice_write = out->f_op->splice_write;
1142 else
1143 splice_write = default_file_splice_write;
1144
1145 file_start_write(out);
1146 ret = splice_write(pipe, out, ppos, len, flags);
1147 file_end_write(out);
1148 return ret;
1149 }
1150
1151 /*
1152 * Attempt to initiate a splice from a file to a pipe.
1153 */
1154 static long do_splice_to(struct file *in, loff_t *ppos,
1155 struct pipe_inode_info *pipe, size_t len,
1156 unsigned int flags)
1157 {
1158 ssize_t (*splice_read)(struct file *, loff_t *,
1159 struct pipe_inode_info *, size_t, unsigned int);
1160 int ret;
1161
1162 if (unlikely(!(in->f_mode & FMODE_READ)))
1163 return -EBADF;
1164
1165 ret = rw_verify_area(READ, in, ppos, len);
1166 if (unlikely(ret < 0))
1167 return ret;
1168
1169 if (in->f_op && in->f_op->splice_read)
1170 splice_read = in->f_op->splice_read;
1171 else
1172 splice_read = default_file_splice_read;
1173
1174 return splice_read(in, ppos, pipe, len, flags);
1175 }
1176
1177 /**
1178 * splice_direct_to_actor - splices data directly between two non-pipes
1179 * @in: file to splice from
1180 * @sd: actor information on where to splice to
1181 * @actor: handles the data splicing
1182 *
1183 * Description:
1184 * This is a special case helper to splice directly between two
1185 * points, without requiring an explicit pipe. Internally an allocated
1186 * pipe is cached in the process, and reused during the lifetime of
1187 * that process.
1188 *
1189 */
1190 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1191 splice_direct_actor *actor)
1192 {
1193 struct pipe_inode_info *pipe;
1194 long ret, bytes;
1195 umode_t i_mode;
1196 size_t len;
1197 int i, flags, more;
1198
1199 /*
1200 * We require the input being a regular file, as we don't want to
1201 * randomly drop data for eg socket -> socket splicing. Use the
1202 * piped splicing for that!
1203 */
1204 i_mode = file_inode(in)->i_mode;
1205 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1206 return -EINVAL;
1207
1208 /*
1209 * neither in nor out is a pipe, setup an internal pipe attached to
1210 * 'out' and transfer the wanted data from 'in' to 'out' through that
1211 */
1212 pipe = current->splice_pipe;
1213 if (unlikely(!pipe)) {
1214 pipe = alloc_pipe_info();
1215 if (!pipe)
1216 return -ENOMEM;
1217
1218 /*
1219 * We don't have an immediate reader, but we'll read the stuff
1220 * out of the pipe right after the splice_to_pipe(). So set
1221 * PIPE_READERS appropriately.
1222 */
1223 pipe->readers = 1;
1224
1225 current->splice_pipe = pipe;
1226 }
1227
1228 /*
1229 * Do the splice.
1230 */
1231 ret = 0;
1232 bytes = 0;
1233 len = sd->total_len;
1234 flags = sd->flags;
1235
1236 /*
1237 * Don't block on output, we have to drain the direct pipe.
1238 */
1239 sd->flags &= ~SPLICE_F_NONBLOCK;
1240 more = sd->flags & SPLICE_F_MORE;
1241
1242 while (len) {
1243 size_t read_len;
1244 loff_t pos = sd->pos, prev_pos = pos;
1245
1246 ret = do_splice_to(in, &pos, pipe, len, flags);
1247 if (unlikely(ret <= 0))
1248 goto out_release;
1249
1250 read_len = ret;
1251 sd->total_len = read_len;
1252
1253 /*
1254 * If more data is pending, set SPLICE_F_MORE
1255 * If this is the last data and SPLICE_F_MORE was not set
1256 * initially, clears it.
1257 */
1258 if (read_len < len)
1259 sd->flags |= SPLICE_F_MORE;
1260 else if (!more)
1261 sd->flags &= ~SPLICE_F_MORE;
1262 /*
1263 * NOTE: nonblocking mode only applies to the input. We
1264 * must not do the output in nonblocking mode as then we
1265 * could get stuck data in the internal pipe:
1266 */
1267 ret = actor(pipe, sd);
1268 if (unlikely(ret <= 0)) {
1269 sd->pos = prev_pos;
1270 goto out_release;
1271 }
1272
1273 bytes += ret;
1274 len -= ret;
1275 sd->pos = pos;
1276
1277 if (ret < read_len) {
1278 sd->pos = prev_pos + ret;
1279 goto out_release;
1280 }
1281 }
1282
1283 done:
1284 pipe->nrbufs = pipe->curbuf = 0;
1285 file_accessed(in);
1286 return bytes;
1287
1288 out_release:
1289 /*
1290 * If we did an incomplete transfer we must release
1291 * the pipe buffers in question:
1292 */
1293 for (i = 0; i < pipe->buffers; i++) {
1294 struct pipe_buffer *buf = pipe->bufs + i;
1295
1296 if (buf->ops) {
1297 buf->ops->release(pipe, buf);
1298 buf->ops = NULL;
1299 }
1300 }
1301
1302 if (!bytes)
1303 bytes = ret;
1304
1305 goto done;
1306 }
1307 EXPORT_SYMBOL(splice_direct_to_actor);
1308
1309 static int direct_splice_actor(struct pipe_inode_info *pipe,
1310 struct splice_desc *sd)
1311 {
1312 struct file *file = sd->u.file;
1313
1314 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1315 sd->flags);
1316 }
1317
1318 /**
1319 * do_splice_direct - splices data directly between two files
1320 * @in: file to splice from
1321 * @ppos: input file offset
1322 * @out: file to splice to
1323 * @opos: output file offset
1324 * @len: number of bytes to splice
1325 * @flags: splice modifier flags
1326 *
1327 * Description:
1328 * For use by do_sendfile(). splice can easily emulate sendfile, but
1329 * doing it in the application would incur an extra system call
1330 * (splice in + splice out, as compared to just sendfile()). So this helper
1331 * can splice directly through a process-private pipe.
1332 *
1333 */
1334 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1335 loff_t *opos, size_t len, unsigned int flags)
1336 {
1337 struct splice_desc sd = {
1338 .len = len,
1339 .total_len = len,
1340 .flags = flags,
1341 .pos = *ppos,
1342 .u.file = out,
1343 .opos = opos,
1344 };
1345 long ret;
1346
1347 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1348 if (ret > 0)
1349 *ppos = sd.pos;
1350
1351 return ret;
1352 }
1353
1354 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1355 struct pipe_inode_info *opipe,
1356 size_t len, unsigned int flags);
1357
1358 /*
1359 * Determine where to splice to/from.
1360 */
1361 static long do_splice(struct file *in, loff_t __user *off_in,
1362 struct file *out, loff_t __user *off_out,
1363 size_t len, unsigned int flags)
1364 {
1365 struct pipe_inode_info *ipipe;
1366 struct pipe_inode_info *opipe;
1367 loff_t offset;
1368 long ret;
1369
1370 ipipe = get_pipe_info(in);
1371 opipe = get_pipe_info(out);
1372
1373 if (ipipe && opipe) {
1374 if (off_in || off_out)
1375 return -ESPIPE;
1376
1377 if (!(in->f_mode & FMODE_READ))
1378 return -EBADF;
1379
1380 if (!(out->f_mode & FMODE_WRITE))
1381 return -EBADF;
1382
1383 /* Splicing to self would be fun, but... */
1384 if (ipipe == opipe)
1385 return -EINVAL;
1386
1387 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1388 }
1389
1390 if (ipipe) {
1391 if (off_in)
1392 return -ESPIPE;
1393 if (off_out) {
1394 if (!(out->f_mode & FMODE_PWRITE))
1395 return -EINVAL;
1396 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1397 return -EFAULT;
1398 } else {
1399 offset = out->f_pos;
1400 }
1401
1402 ret = do_splice_from(ipipe, out, &offset, len, flags);
1403
1404 if (!off_out)
1405 out->f_pos = offset;
1406 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1407 ret = -EFAULT;
1408
1409 return ret;
1410 }
1411
1412 if (opipe) {
1413 if (off_out)
1414 return -ESPIPE;
1415 if (off_in) {
1416 if (!(in->f_mode & FMODE_PREAD))
1417 return -EINVAL;
1418 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1419 return -EFAULT;
1420 } else {
1421 offset = in->f_pos;
1422 }
1423
1424 ret = do_splice_to(in, &offset, opipe, len, flags);
1425
1426 if (!off_in)
1427 in->f_pos = offset;
1428 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1429 ret = -EFAULT;
1430
1431 return ret;
1432 }
1433
1434 return -EINVAL;
1435 }
1436
1437 /*
1438 * Map an iov into an array of pages and offset/length tupples. With the
1439 * partial_page structure, we can map several non-contiguous ranges into
1440 * our ones pages[] map instead of splitting that operation into pieces.
1441 * Could easily be exported as a generic helper for other users, in which
1442 * case one would probably want to add a 'max_nr_pages' parameter as well.
1443 */
1444 static int get_iovec_page_array(const struct iovec __user *iov,
1445 unsigned int nr_vecs, struct page **pages,
1446 struct partial_page *partial, bool aligned,
1447 unsigned int pipe_buffers)
1448 {
1449 int buffers = 0, error = 0;
1450
1451 while (nr_vecs) {
1452 unsigned long off, npages;
1453 struct iovec entry;
1454 void __user *base;
1455 size_t len;
1456 int i;
1457
1458 error = -EFAULT;
1459 if (copy_from_user(&entry, iov, sizeof(entry)))
1460 break;
1461
1462 base = entry.iov_base;
1463 len = entry.iov_len;
1464
1465 /*
1466 * Sanity check this iovec. 0 read succeeds.
1467 */
1468 error = 0;
1469 if (unlikely(!len))
1470 break;
1471 error = -EFAULT;
1472 if (!access_ok(VERIFY_READ, base, len))
1473 break;
1474
1475 /*
1476 * Get this base offset and number of pages, then map
1477 * in the user pages.
1478 */
1479 off = (unsigned long) base & ~PAGE_MASK;
1480
1481 /*
1482 * If asked for alignment, the offset must be zero and the
1483 * length a multiple of the PAGE_SIZE.
1484 */
1485 error = -EINVAL;
1486 if (aligned && (off || len & ~PAGE_MASK))
1487 break;
1488
1489 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1490 if (npages > pipe_buffers - buffers)
1491 npages = pipe_buffers - buffers;
1492
1493 error = get_user_pages_fast((unsigned long)base, npages,
1494 0, &pages[buffers]);
1495
1496 if (unlikely(error <= 0))
1497 break;
1498
1499 /*
1500 * Fill this contiguous range into the partial page map.
1501 */
1502 for (i = 0; i < error; i++) {
1503 const int plen = min_t(size_t, len, PAGE_SIZE - off);
1504
1505 partial[buffers].offset = off;
1506 partial[buffers].len = plen;
1507
1508 off = 0;
1509 len -= plen;
1510 buffers++;
1511 }
1512
1513 /*
1514 * We didn't complete this iov, stop here since it probably
1515 * means we have to move some of this into a pipe to
1516 * be able to continue.
1517 */
1518 if (len)
1519 break;
1520
1521 /*
1522 * Don't continue if we mapped fewer pages than we asked for,
1523 * or if we mapped the max number of pages that we have
1524 * room for.
1525 */
1526 if (error < npages || buffers == pipe_buffers)
1527 break;
1528
1529 nr_vecs--;
1530 iov++;
1531 }
1532
1533 if (buffers)
1534 return buffers;
1535
1536 return error;
1537 }
1538
1539 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1540 struct splice_desc *sd)
1541 {
1542 char *src;
1543 int ret;
1544
1545 /*
1546 * See if we can use the atomic maps, by prefaulting in the
1547 * pages and doing an atomic copy
1548 */
1549 if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
1550 src = buf->ops->map(pipe, buf, 1);
1551 ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
1552 sd->len);
1553 buf->ops->unmap(pipe, buf, src);
1554 if (!ret) {
1555 ret = sd->len;
1556 goto out;
1557 }
1558 }
1559
1560 /*
1561 * No dice, use slow non-atomic map and copy
1562 */
1563 src = buf->ops->map(pipe, buf, 0);
1564
1565 ret = sd->len;
1566 if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
1567 ret = -EFAULT;
1568
1569 buf->ops->unmap(pipe, buf, src);
1570 out:
1571 if (ret > 0)
1572 sd->u.userptr += ret;
1573 return ret;
1574 }
1575
1576 /*
1577 * For lack of a better implementation, implement vmsplice() to userspace
1578 * as a simple copy of the pipes pages to the user iov.
1579 */
1580 static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
1581 unsigned long nr_segs, unsigned int flags)
1582 {
1583 struct pipe_inode_info *pipe;
1584 struct splice_desc sd;
1585 ssize_t size;
1586 int error;
1587 long ret;
1588
1589 pipe = get_pipe_info(file);
1590 if (!pipe)
1591 return -EBADF;
1592
1593 pipe_lock(pipe);
1594
1595 error = ret = 0;
1596 while (nr_segs) {
1597 void __user *base;
1598 size_t len;
1599
1600 /*
1601 * Get user address base and length for this iovec.
1602 */
1603 error = get_user(base, &iov->iov_base);
1604 if (unlikely(error))
1605 break;
1606 error = get_user(len, &iov->iov_len);
1607 if (unlikely(error))
1608 break;
1609
1610 /*
1611 * Sanity check this iovec. 0 read succeeds.
1612 */
1613 if (unlikely(!len))
1614 break;
1615 if (unlikely(!base)) {
1616 error = -EFAULT;
1617 break;
1618 }
1619
1620 if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
1621 error = -EFAULT;
1622 break;
1623 }
1624
1625 sd.len = 0;
1626 sd.total_len = len;
1627 sd.flags = flags;
1628 sd.u.userptr = base;
1629 sd.pos = 0;
1630
1631 size = __splice_from_pipe(pipe, &sd, pipe_to_user);
1632 if (size < 0) {
1633 if (!ret)
1634 ret = size;
1635
1636 break;
1637 }
1638
1639 ret += size;
1640
1641 if (size < len)
1642 break;
1643
1644 nr_segs--;
1645 iov++;
1646 }
1647
1648 pipe_unlock(pipe);
1649
1650 if (!ret)
1651 ret = error;
1652
1653 return ret;
1654 }
1655
1656 /*
1657 * vmsplice splices a user address range into a pipe. It can be thought of
1658 * as splice-from-memory, where the regular splice is splice-from-file (or
1659 * to file). In both cases the output is a pipe, naturally.
1660 */
1661 static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
1662 unsigned long nr_segs, unsigned int flags)
1663 {
1664 struct pipe_inode_info *pipe;
1665 struct page *pages[PIPE_DEF_BUFFERS];
1666 struct partial_page partial[PIPE_DEF_BUFFERS];
1667 struct splice_pipe_desc spd = {
1668 .pages = pages,
1669 .partial = partial,
1670 .nr_pages_max = PIPE_DEF_BUFFERS,
1671 .flags = flags,
1672 .ops = &user_page_pipe_buf_ops,
1673 .spd_release = spd_release_page,
1674 };
1675 long ret;
1676
1677 pipe = get_pipe_info(file);
1678 if (!pipe)
1679 return -EBADF;
1680
1681 if (splice_grow_spd(pipe, &spd))
1682 return -ENOMEM;
1683
1684 spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages,
1685 spd.partial, false,
1686 spd.nr_pages_max);
1687 if (spd.nr_pages <= 0)
1688 ret = spd.nr_pages;
1689 else
1690 ret = splice_to_pipe(pipe, &spd);
1691
1692 splice_shrink_spd(&spd);
1693 return ret;
1694 }
1695
1696 /*
1697 * Note that vmsplice only really supports true splicing _from_ user memory
1698 * to a pipe, not the other way around. Splicing from user memory is a simple
1699 * operation that can be supported without any funky alignment restrictions
1700 * or nasty vm tricks. We simply map in the user memory and fill them into
1701 * a pipe. The reverse isn't quite as easy, though. There are two possible
1702 * solutions for that:
1703 *
1704 * - memcpy() the data internally, at which point we might as well just
1705 * do a regular read() on the buffer anyway.
1706 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1707 * has restriction limitations on both ends of the pipe).
1708 *
1709 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1710 *
1711 */
1712 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1713 unsigned long, nr_segs, unsigned int, flags)
1714 {
1715 struct fd f;
1716 long error;
1717
1718 if (unlikely(nr_segs > UIO_MAXIOV))
1719 return -EINVAL;
1720 else if (unlikely(!nr_segs))
1721 return 0;
1722
1723 error = -EBADF;
1724 f = fdget(fd);
1725 if (f.file) {
1726 if (f.file->f_mode & FMODE_WRITE)
1727 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1728 else if (f.file->f_mode & FMODE_READ)
1729 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1730
1731 fdput(f);
1732 }
1733
1734 return error;
1735 }
1736
1737 #ifdef CONFIG_COMPAT
1738 COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1739 unsigned int, nr_segs, unsigned int, flags)
1740 {
1741 unsigned i;
1742 struct iovec __user *iov;
1743 if (nr_segs > UIO_MAXIOV)
1744 return -EINVAL;
1745 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1746 for (i = 0; i < nr_segs; i++) {
1747 struct compat_iovec v;
1748 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1749 get_user(v.iov_len, &iov32[i].iov_len) ||
1750 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1751 put_user(v.iov_len, &iov[i].iov_len))
1752 return -EFAULT;
1753 }
1754 return sys_vmsplice(fd, iov, nr_segs, flags);
1755 }
1756 #endif
1757
1758 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1759 int, fd_out, loff_t __user *, off_out,
1760 size_t, len, unsigned int, flags)
1761 {
1762 struct fd in, out;
1763 long error;
1764
1765 if (unlikely(!len))
1766 return 0;
1767
1768 error = -EBADF;
1769 in = fdget(fd_in);
1770 if (in.file) {
1771 if (in.file->f_mode & FMODE_READ) {
1772 out = fdget(fd_out);
1773 if (out.file) {
1774 if (out.file->f_mode & FMODE_WRITE)
1775 error = do_splice(in.file, off_in,
1776 out.file, off_out,
1777 len, flags);
1778 fdput(out);
1779 }
1780 }
1781 fdput(in);
1782 }
1783 return error;
1784 }
1785
1786 /*
1787 * Make sure there's data to read. Wait for input if we can, otherwise
1788 * return an appropriate error.
1789 */
1790 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1791 {
1792 int ret;
1793
1794 /*
1795 * Check ->nrbufs without the inode lock first. This function
1796 * is speculative anyways, so missing one is ok.
1797 */
1798 if (pipe->nrbufs)
1799 return 0;
1800
1801 ret = 0;
1802 pipe_lock(pipe);
1803
1804 while (!pipe->nrbufs) {
1805 if (signal_pending(current)) {
1806 ret = -ERESTARTSYS;
1807 break;
1808 }
1809 if (!pipe->writers)
1810 break;
1811 if (!pipe->waiting_writers) {
1812 if (flags & SPLICE_F_NONBLOCK) {
1813 ret = -EAGAIN;
1814 break;
1815 }
1816 }
1817 pipe_wait(pipe);
1818 }
1819
1820 pipe_unlock(pipe);
1821 return ret;
1822 }
1823
1824 /*
1825 * Make sure there's writeable room. Wait for room if we can, otherwise
1826 * return an appropriate error.
1827 */
1828 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1829 {
1830 int ret;
1831
1832 /*
1833 * Check ->nrbufs without the inode lock first. This function
1834 * is speculative anyways, so missing one is ok.
1835 */
1836 if (pipe->nrbufs < pipe->buffers)
1837 return 0;
1838
1839 ret = 0;
1840 pipe_lock(pipe);
1841
1842 while (pipe->nrbufs >= pipe->buffers) {
1843 if (!pipe->readers) {
1844 send_sig(SIGPIPE, current, 0);
1845 ret = -EPIPE;
1846 break;
1847 }
1848 if (flags & SPLICE_F_NONBLOCK) {
1849 ret = -EAGAIN;
1850 break;
1851 }
1852 if (signal_pending(current)) {
1853 ret = -ERESTARTSYS;
1854 break;
1855 }
1856 pipe->waiting_writers++;
1857 pipe_wait(pipe);
1858 pipe->waiting_writers--;
1859 }
1860
1861 pipe_unlock(pipe);
1862 return ret;
1863 }
1864
1865 /*
1866 * Splice contents of ipipe to opipe.
1867 */
1868 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1869 struct pipe_inode_info *opipe,
1870 size_t len, unsigned int flags)
1871 {
1872 struct pipe_buffer *ibuf, *obuf;
1873 int ret = 0, nbuf;
1874 bool input_wakeup = false;
1875
1876
1877 retry:
1878 ret = ipipe_prep(ipipe, flags);
1879 if (ret)
1880 return ret;
1881
1882 ret = opipe_prep(opipe, flags);
1883 if (ret)
1884 return ret;
1885
1886 /*
1887 * Potential ABBA deadlock, work around it by ordering lock
1888 * grabbing by pipe info address. Otherwise two different processes
1889 * could deadlock (one doing tee from A -> B, the other from B -> A).
1890 */
1891 pipe_double_lock(ipipe, opipe);
1892
1893 do {
1894 if (!opipe->readers) {
1895 send_sig(SIGPIPE, current, 0);
1896 if (!ret)
1897 ret = -EPIPE;
1898 break;
1899 }
1900
1901 if (!ipipe->nrbufs && !ipipe->writers)
1902 break;
1903
1904 /*
1905 * Cannot make any progress, because either the input
1906 * pipe is empty or the output pipe is full.
1907 */
1908 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
1909 /* Already processed some buffers, break */
1910 if (ret)
1911 break;
1912
1913 if (flags & SPLICE_F_NONBLOCK) {
1914 ret = -EAGAIN;
1915 break;
1916 }
1917
1918 /*
1919 * We raced with another reader/writer and haven't
1920 * managed to process any buffers. A zero return
1921 * value means EOF, so retry instead.
1922 */
1923 pipe_unlock(ipipe);
1924 pipe_unlock(opipe);
1925 goto retry;
1926 }
1927
1928 ibuf = ipipe->bufs + ipipe->curbuf;
1929 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1930 obuf = opipe->bufs + nbuf;
1931
1932 if (len >= ibuf->len) {
1933 /*
1934 * Simply move the whole buffer from ipipe to opipe
1935 */
1936 *obuf = *ibuf;
1937 ibuf->ops = NULL;
1938 opipe->nrbufs++;
1939 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
1940 ipipe->nrbufs--;
1941 input_wakeup = true;
1942 } else {
1943 /*
1944 * Get a reference to this pipe buffer,
1945 * so we can copy the contents over.
1946 */
1947 ibuf->ops->get(ipipe, ibuf);
1948 *obuf = *ibuf;
1949
1950 /*
1951 * Don't inherit the gift flag, we need to
1952 * prevent multiple steals of this page.
1953 */
1954 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1955
1956 obuf->len = len;
1957 opipe->nrbufs++;
1958 ibuf->offset += obuf->len;
1959 ibuf->len -= obuf->len;
1960 }
1961 ret += obuf->len;
1962 len -= obuf->len;
1963 } while (len);
1964
1965 pipe_unlock(ipipe);
1966 pipe_unlock(opipe);
1967
1968 /*
1969 * If we put data in the output pipe, wakeup any potential readers.
1970 */
1971 if (ret > 0)
1972 wakeup_pipe_readers(opipe);
1973
1974 if (input_wakeup)
1975 wakeup_pipe_writers(ipipe);
1976
1977 return ret;
1978 }
1979
1980 /*
1981 * Link contents of ipipe to opipe.
1982 */
1983 static int link_pipe(struct pipe_inode_info *ipipe,
1984 struct pipe_inode_info *opipe,
1985 size_t len, unsigned int flags)
1986 {
1987 struct pipe_buffer *ibuf, *obuf;
1988 int ret = 0, i = 0, nbuf;
1989
1990 /*
1991 * Potential ABBA deadlock, work around it by ordering lock
1992 * grabbing by pipe info address. Otherwise two different processes
1993 * could deadlock (one doing tee from A -> B, the other from B -> A).
1994 */
1995 pipe_double_lock(ipipe, opipe);
1996
1997 do {
1998 if (!opipe->readers) {
1999 send_sig(SIGPIPE, current, 0);
2000 if (!ret)
2001 ret = -EPIPE;
2002 break;
2003 }
2004
2005 /*
2006 * If we have iterated all input buffers or ran out of
2007 * output room, break.
2008 */
2009 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
2010 break;
2011
2012 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
2013 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
2014
2015 /*
2016 * Get a reference to this pipe buffer,
2017 * so we can copy the contents over.
2018 */
2019 ibuf->ops->get(ipipe, ibuf);
2020
2021 obuf = opipe->bufs + nbuf;
2022 *obuf = *ibuf;
2023
2024 /*
2025 * Don't inherit the gift flag, we need to
2026 * prevent multiple steals of this page.
2027 */
2028 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2029
2030 if (obuf->len > len)
2031 obuf->len = len;
2032
2033 opipe->nrbufs++;
2034 ret += obuf->len;
2035 len -= obuf->len;
2036 i++;
2037 } while (len);
2038
2039 /*
2040 * return EAGAIN if we have the potential of some data in the
2041 * future, otherwise just return 0
2042 */
2043 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
2044 ret = -EAGAIN;
2045
2046 pipe_unlock(ipipe);
2047 pipe_unlock(opipe);
2048
2049 /*
2050 * If we put data in the output pipe, wakeup any potential readers.
2051 */
2052 if (ret > 0)
2053 wakeup_pipe_readers(opipe);
2054
2055 return ret;
2056 }
2057
2058 /*
2059 * This is a tee(1) implementation that works on pipes. It doesn't copy
2060 * any data, it simply references the 'in' pages on the 'out' pipe.
2061 * The 'flags' used are the SPLICE_F_* variants, currently the only
2062 * applicable one is SPLICE_F_NONBLOCK.
2063 */
2064 static long do_tee(struct file *in, struct file *out, size_t len,
2065 unsigned int flags)
2066 {
2067 struct pipe_inode_info *ipipe = get_pipe_info(in);
2068 struct pipe_inode_info *opipe = get_pipe_info(out);
2069 int ret = -EINVAL;
2070
2071 /*
2072 * Duplicate the contents of ipipe to opipe without actually
2073 * copying the data.
2074 */
2075 if (ipipe && opipe && ipipe != opipe) {
2076 /*
2077 * Keep going, unless we encounter an error. The ipipe/opipe
2078 * ordering doesn't really matter.
2079 */
2080 ret = ipipe_prep(ipipe, flags);
2081 if (!ret) {
2082 ret = opipe_prep(opipe, flags);
2083 if (!ret)
2084 ret = link_pipe(ipipe, opipe, len, flags);
2085 }
2086 }
2087
2088 return ret;
2089 }
2090
2091 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
2092 {
2093 struct fd in;
2094 int error;
2095
2096 if (unlikely(!len))
2097 return 0;
2098
2099 error = -EBADF;
2100 in = fdget(fdin);
2101 if (in.file) {
2102 if (in.file->f_mode & FMODE_READ) {
2103 struct fd out = fdget(fdout);
2104 if (out.file) {
2105 if (out.file->f_mode & FMODE_WRITE)
2106 error = do_tee(in.file, out.file,
2107 len, flags);
2108 fdput(out);
2109 }
2110 }
2111 fdput(in);
2112 }
2113
2114 return error;
2115 }