]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/splice.c
switch default_file_splice_read() to use of pipe-backed iov_iter
[mirror_ubuntu-bionic-kernel.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 put_page(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 .confirm = page_cache_pipe_buf_confirm,
140 .release = page_cache_pipe_buf_release,
141 .steal = page_cache_pipe_buf_steal,
142 .get = generic_pipe_buf_get,
143 };
144
145 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
146 struct pipe_buffer *buf)
147 {
148 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
149 return 1;
150
151 buf->flags |= PIPE_BUF_FLAG_LRU;
152 return generic_pipe_buf_steal(pipe, buf);
153 }
154
155 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
156 .can_merge = 0,
157 .confirm = generic_pipe_buf_confirm,
158 .release = page_cache_pipe_buf_release,
159 .steal = user_page_pipe_buf_steal,
160 .get = generic_pipe_buf_get,
161 };
162
163 static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
164 {
165 smp_mb();
166 if (waitqueue_active(&pipe->wait))
167 wake_up_interruptible(&pipe->wait);
168 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
169 }
170
171 /**
172 * splice_to_pipe - fill passed data into a pipe
173 * @pipe: pipe to fill
174 * @spd: data to fill
175 *
176 * Description:
177 * @spd contains a map of pages and len/offset tuples, along with
178 * the struct pipe_buf_operations associated with these pages. This
179 * function will link that data to the pipe.
180 *
181 */
182 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
183 struct splice_pipe_desc *spd)
184 {
185 unsigned int spd_pages = spd->nr_pages;
186 int ret = 0, page_nr = 0;
187
188 if (!spd_pages)
189 return 0;
190
191 if (unlikely(!pipe->readers)) {
192 send_sig(SIGPIPE, current, 0);
193 ret = -EPIPE;
194 goto out;
195 }
196
197 while (pipe->nrbufs < pipe->buffers) {
198 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
199 struct pipe_buffer *buf = pipe->bufs + newbuf;
200
201 buf->page = spd->pages[page_nr];
202 buf->offset = spd->partial[page_nr].offset;
203 buf->len = spd->partial[page_nr].len;
204 buf->private = spd->partial[page_nr].private;
205 buf->ops = spd->ops;
206
207 pipe->nrbufs++;
208 page_nr++;
209 ret += buf->len;
210
211 if (!--spd->nr_pages)
212 break;
213 }
214
215 if (!ret)
216 ret = -EAGAIN;
217
218 out:
219 while (page_nr < spd_pages)
220 spd->spd_release(spd, page_nr++);
221
222 return ret;
223 }
224 EXPORT_SYMBOL_GPL(splice_to_pipe);
225
226 ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
227 {
228 int ret;
229
230 if (unlikely(!pipe->readers)) {
231 send_sig(SIGPIPE, current, 0);
232 ret = -EPIPE;
233 } else if (pipe->nrbufs == pipe->buffers) {
234 ret = -EAGAIN;
235 } else {
236 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
237 pipe->bufs[newbuf] = *buf;
238 pipe->nrbufs++;
239 return buf->len;
240 }
241 buf->ops->release(pipe, buf);
242 buf->ops = NULL;
243 return ret;
244 }
245 EXPORT_SYMBOL(add_to_pipe);
246
247 void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
248 {
249 put_page(spd->pages[i]);
250 }
251
252 /*
253 * Check if we need to grow the arrays holding pages and partial page
254 * descriptions.
255 */
256 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
257 {
258 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
259
260 spd->nr_pages_max = buffers;
261 if (buffers <= PIPE_DEF_BUFFERS)
262 return 0;
263
264 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
265 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
266
267 if (spd->pages && spd->partial)
268 return 0;
269
270 kfree(spd->pages);
271 kfree(spd->partial);
272 return -ENOMEM;
273 }
274
275 void splice_shrink_spd(struct splice_pipe_desc *spd)
276 {
277 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
278 return;
279
280 kfree(spd->pages);
281 kfree(spd->partial);
282 }
283
284 /**
285 * generic_file_splice_read - splice data from file to a pipe
286 * @in: file to splice from
287 * @ppos: position in @in
288 * @pipe: pipe to splice to
289 * @len: number of bytes to splice
290 * @flags: splice modifier flags
291 *
292 * Description:
293 * Will read pages from given file and fill them into a pipe. Can be
294 * used as long as it has more or less sane ->read_iter().
295 *
296 */
297 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
298 struct pipe_inode_info *pipe, size_t len,
299 unsigned int flags)
300 {
301 struct iov_iter to;
302 struct kiocb kiocb;
303 loff_t isize;
304 int idx, ret;
305
306 isize = i_size_read(in->f_mapping->host);
307 if (unlikely(*ppos >= isize))
308 return 0;
309
310 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len);
311 idx = to.idx;
312 init_sync_kiocb(&kiocb, in);
313 kiocb.ki_pos = *ppos;
314 ret = in->f_op->read_iter(&kiocb, &to);
315 if (ret > 0) {
316 *ppos = kiocb.ki_pos;
317 file_accessed(in);
318 } else if (ret < 0) {
319 if (WARN_ON(to.idx != idx || to.iov_offset)) {
320 /*
321 * a bogus ->read_iter() has copied something and still
322 * returned an error instead of a short read.
323 */
324 to.idx = idx;
325 to.iov_offset = 0;
326 iov_iter_advance(&to, 0); /* to free what was emitted */
327 }
328 /*
329 * callers of ->splice_read() expect -EAGAIN on
330 * "can't put anything in there", rather than -EFAULT.
331 */
332 if (ret == -EFAULT)
333 ret = -EAGAIN;
334 }
335
336 return ret;
337 }
338 EXPORT_SYMBOL(generic_file_splice_read);
339
340 const struct pipe_buf_operations default_pipe_buf_ops = {
341 .can_merge = 0,
342 .confirm = generic_pipe_buf_confirm,
343 .release = generic_pipe_buf_release,
344 .steal = generic_pipe_buf_steal,
345 .get = generic_pipe_buf_get,
346 };
347
348 static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
349 struct pipe_buffer *buf)
350 {
351 return 1;
352 }
353
354 /* Pipe buffer operations for a socket and similar. */
355 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
356 .can_merge = 0,
357 .confirm = generic_pipe_buf_confirm,
358 .release = generic_pipe_buf_release,
359 .steal = generic_pipe_buf_nosteal,
360 .get = generic_pipe_buf_get,
361 };
362 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
363
364 static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
365 unsigned long vlen, loff_t offset)
366 {
367 mm_segment_t old_fs;
368 loff_t pos = offset;
369 ssize_t res;
370
371 old_fs = get_fs();
372 set_fs(get_ds());
373 /* The cast to a user pointer is valid due to the set_fs() */
374 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
375 set_fs(old_fs);
376
377 return res;
378 }
379
380 ssize_t kernel_write(struct file *file, const char *buf, size_t count,
381 loff_t pos)
382 {
383 mm_segment_t old_fs;
384 ssize_t res;
385
386 old_fs = get_fs();
387 set_fs(get_ds());
388 /* The cast to a user pointer is valid due to the set_fs() */
389 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
390 set_fs(old_fs);
391
392 return res;
393 }
394 EXPORT_SYMBOL(kernel_write);
395
396 static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
397 struct pipe_inode_info *pipe, size_t len,
398 unsigned int flags)
399 {
400 struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
401 struct iov_iter to;
402 struct page **pages;
403 unsigned int nr_pages;
404 size_t offset, dummy, copied = 0;
405 ssize_t res;
406 int i;
407
408 if (pipe->nrbufs == pipe->buffers)
409 return -EAGAIN;
410
411 /*
412 * Try to keep page boundaries matching to source pagecache ones -
413 * it probably won't be much help, but...
414 */
415 offset = *ppos & ~PAGE_MASK;
416
417 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len + offset);
418
419 res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &dummy);
420 if (res <= 0)
421 return -ENOMEM;
422
423 nr_pages = res / PAGE_SIZE;
424
425 vec = __vec;
426 if (nr_pages > PIPE_DEF_BUFFERS) {
427 vec = kmalloc(nr_pages * sizeof(struct kvec), GFP_KERNEL);
428 if (unlikely(!vec)) {
429 res = -ENOMEM;
430 goto out;
431 }
432 }
433
434 pipe->bufs[to.idx].offset = offset;
435 pipe->bufs[to.idx].len -= offset;
436
437 for (i = 0; i < nr_pages; i++) {
438 size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
439 vec[i].iov_base = page_address(pages[i]) + offset;
440 vec[i].iov_len = this_len;
441 len -= this_len;
442 offset = 0;
443 }
444
445 res = kernel_readv(in, vec, nr_pages, *ppos);
446 if (res > 0) {
447 copied = res;
448 *ppos += res;
449 }
450
451 if (vec != __vec)
452 kfree(vec);
453 out:
454 for (i = 0; i < nr_pages; i++)
455 put_page(pages[i]);
456 kvfree(pages);
457 iov_iter_advance(&to, copied); /* truncates and discards */
458 return res;
459 }
460
461 /*
462 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
463 * using sendpage(). Return the number of bytes sent.
464 */
465 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
466 struct pipe_buffer *buf, struct splice_desc *sd)
467 {
468 struct file *file = sd->u.file;
469 loff_t pos = sd->pos;
470 int more;
471
472 if (!likely(file->f_op->sendpage))
473 return -EINVAL;
474
475 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
476
477 if (sd->len < sd->total_len && pipe->nrbufs > 1)
478 more |= MSG_SENDPAGE_NOTLAST;
479
480 return file->f_op->sendpage(file, buf->page, buf->offset,
481 sd->len, &pos, more);
482 }
483
484 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
485 {
486 smp_mb();
487 if (waitqueue_active(&pipe->wait))
488 wake_up_interruptible(&pipe->wait);
489 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
490 }
491
492 /**
493 * splice_from_pipe_feed - feed available data from a pipe to a file
494 * @pipe: pipe to splice from
495 * @sd: information to @actor
496 * @actor: handler that splices the data
497 *
498 * Description:
499 * This function loops over the pipe and calls @actor to do the
500 * actual moving of a single struct pipe_buffer to the desired
501 * destination. It returns when there's no more buffers left in
502 * the pipe or if the requested number of bytes (@sd->total_len)
503 * have been copied. It returns a positive number (one) if the
504 * pipe needs to be filled with more data, zero if the required
505 * number of bytes have been copied and -errno on error.
506 *
507 * This, together with splice_from_pipe_{begin,end,next}, may be
508 * used to implement the functionality of __splice_from_pipe() when
509 * locking is required around copying the pipe buffers to the
510 * destination.
511 */
512 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
513 splice_actor *actor)
514 {
515 int ret;
516
517 while (pipe->nrbufs) {
518 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
519 const struct pipe_buf_operations *ops = buf->ops;
520
521 sd->len = buf->len;
522 if (sd->len > sd->total_len)
523 sd->len = sd->total_len;
524
525 ret = buf->ops->confirm(pipe, buf);
526 if (unlikely(ret)) {
527 if (ret == -ENODATA)
528 ret = 0;
529 return ret;
530 }
531
532 ret = actor(pipe, buf, sd);
533 if (ret <= 0)
534 return ret;
535
536 buf->offset += ret;
537 buf->len -= ret;
538
539 sd->num_spliced += ret;
540 sd->len -= ret;
541 sd->pos += ret;
542 sd->total_len -= ret;
543
544 if (!buf->len) {
545 buf->ops = NULL;
546 ops->release(pipe, buf);
547 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
548 pipe->nrbufs--;
549 if (pipe->files)
550 sd->need_wakeup = true;
551 }
552
553 if (!sd->total_len)
554 return 0;
555 }
556
557 return 1;
558 }
559
560 /**
561 * splice_from_pipe_next - wait for some data to splice from
562 * @pipe: pipe to splice from
563 * @sd: information about the splice operation
564 *
565 * Description:
566 * This function will wait for some data and return a positive
567 * value (one) if pipe buffers are available. It will return zero
568 * or -errno if no more data needs to be spliced.
569 */
570 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
571 {
572 /*
573 * Check for signal early to make process killable when there are
574 * always buffers available
575 */
576 if (signal_pending(current))
577 return -ERESTARTSYS;
578
579 while (!pipe->nrbufs) {
580 if (!pipe->writers)
581 return 0;
582
583 if (!pipe->waiting_writers && sd->num_spliced)
584 return 0;
585
586 if (sd->flags & SPLICE_F_NONBLOCK)
587 return -EAGAIN;
588
589 if (signal_pending(current))
590 return -ERESTARTSYS;
591
592 if (sd->need_wakeup) {
593 wakeup_pipe_writers(pipe);
594 sd->need_wakeup = false;
595 }
596
597 pipe_wait(pipe);
598 }
599
600 return 1;
601 }
602
603 /**
604 * splice_from_pipe_begin - start splicing from pipe
605 * @sd: information about the splice operation
606 *
607 * Description:
608 * This function should be called before a loop containing
609 * splice_from_pipe_next() and splice_from_pipe_feed() to
610 * initialize the necessary fields of @sd.
611 */
612 static void splice_from_pipe_begin(struct splice_desc *sd)
613 {
614 sd->num_spliced = 0;
615 sd->need_wakeup = false;
616 }
617
618 /**
619 * splice_from_pipe_end - finish splicing from pipe
620 * @pipe: pipe to splice from
621 * @sd: information about the splice operation
622 *
623 * Description:
624 * This function will wake up pipe writers if necessary. It should
625 * be called after a loop containing splice_from_pipe_next() and
626 * splice_from_pipe_feed().
627 */
628 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
629 {
630 if (sd->need_wakeup)
631 wakeup_pipe_writers(pipe);
632 }
633
634 /**
635 * __splice_from_pipe - splice data from a pipe to given actor
636 * @pipe: pipe to splice from
637 * @sd: information to @actor
638 * @actor: handler that splices the data
639 *
640 * Description:
641 * This function does little more than loop over the pipe and call
642 * @actor to do the actual moving of a single struct pipe_buffer to
643 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
644 * pipe_to_user.
645 *
646 */
647 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
648 splice_actor *actor)
649 {
650 int ret;
651
652 splice_from_pipe_begin(sd);
653 do {
654 cond_resched();
655 ret = splice_from_pipe_next(pipe, sd);
656 if (ret > 0)
657 ret = splice_from_pipe_feed(pipe, sd, actor);
658 } while (ret > 0);
659 splice_from_pipe_end(pipe, sd);
660
661 return sd->num_spliced ? sd->num_spliced : ret;
662 }
663 EXPORT_SYMBOL(__splice_from_pipe);
664
665 /**
666 * splice_from_pipe - splice data from a pipe to a file
667 * @pipe: pipe to splice from
668 * @out: file to splice to
669 * @ppos: position in @out
670 * @len: how many bytes to splice
671 * @flags: splice modifier flags
672 * @actor: handler that splices the data
673 *
674 * Description:
675 * See __splice_from_pipe. This function locks the pipe inode,
676 * otherwise it's identical to __splice_from_pipe().
677 *
678 */
679 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
680 loff_t *ppos, size_t len, unsigned int flags,
681 splice_actor *actor)
682 {
683 ssize_t ret;
684 struct splice_desc sd = {
685 .total_len = len,
686 .flags = flags,
687 .pos = *ppos,
688 .u.file = out,
689 };
690
691 pipe_lock(pipe);
692 ret = __splice_from_pipe(pipe, &sd, actor);
693 pipe_unlock(pipe);
694
695 return ret;
696 }
697
698 /**
699 * iter_file_splice_write - splice data from a pipe to a file
700 * @pipe: pipe info
701 * @out: file to write to
702 * @ppos: position in @out
703 * @len: number of bytes to splice
704 * @flags: splice modifier flags
705 *
706 * Description:
707 * Will either move or copy pages (determined by @flags options) from
708 * the given pipe inode to the given file.
709 * This one is ->write_iter-based.
710 *
711 */
712 ssize_t
713 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
714 loff_t *ppos, size_t len, unsigned int flags)
715 {
716 struct splice_desc sd = {
717 .total_len = len,
718 .flags = flags,
719 .pos = *ppos,
720 .u.file = out,
721 };
722 int nbufs = pipe->buffers;
723 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
724 GFP_KERNEL);
725 ssize_t ret;
726
727 if (unlikely(!array))
728 return -ENOMEM;
729
730 pipe_lock(pipe);
731
732 splice_from_pipe_begin(&sd);
733 while (sd.total_len) {
734 struct iov_iter from;
735 size_t left;
736 int n, idx;
737
738 ret = splice_from_pipe_next(pipe, &sd);
739 if (ret <= 0)
740 break;
741
742 if (unlikely(nbufs < pipe->buffers)) {
743 kfree(array);
744 nbufs = pipe->buffers;
745 array = kcalloc(nbufs, sizeof(struct bio_vec),
746 GFP_KERNEL);
747 if (!array) {
748 ret = -ENOMEM;
749 break;
750 }
751 }
752
753 /* build the vector */
754 left = sd.total_len;
755 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
756 struct pipe_buffer *buf = pipe->bufs + idx;
757 size_t this_len = buf->len;
758
759 if (this_len > left)
760 this_len = left;
761
762 if (idx == pipe->buffers - 1)
763 idx = -1;
764
765 ret = buf->ops->confirm(pipe, buf);
766 if (unlikely(ret)) {
767 if (ret == -ENODATA)
768 ret = 0;
769 goto done;
770 }
771
772 array[n].bv_page = buf->page;
773 array[n].bv_len = this_len;
774 array[n].bv_offset = buf->offset;
775 left -= this_len;
776 }
777
778 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
779 sd.total_len - left);
780 ret = vfs_iter_write(out, &from, &sd.pos);
781 if (ret <= 0)
782 break;
783
784 sd.num_spliced += ret;
785 sd.total_len -= ret;
786 *ppos = sd.pos;
787
788 /* dismiss the fully eaten buffers, adjust the partial one */
789 while (ret) {
790 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
791 if (ret >= buf->len) {
792 const struct pipe_buf_operations *ops = buf->ops;
793 ret -= buf->len;
794 buf->len = 0;
795 buf->ops = NULL;
796 ops->release(pipe, buf);
797 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
798 pipe->nrbufs--;
799 if (pipe->files)
800 sd.need_wakeup = true;
801 } else {
802 buf->offset += ret;
803 buf->len -= ret;
804 ret = 0;
805 }
806 }
807 }
808 done:
809 kfree(array);
810 splice_from_pipe_end(pipe, &sd);
811
812 pipe_unlock(pipe);
813
814 if (sd.num_spliced)
815 ret = sd.num_spliced;
816
817 return ret;
818 }
819
820 EXPORT_SYMBOL(iter_file_splice_write);
821
822 static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
823 struct splice_desc *sd)
824 {
825 int ret;
826 void *data;
827 loff_t tmp = sd->pos;
828
829 data = kmap(buf->page);
830 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
831 kunmap(buf->page);
832
833 return ret;
834 }
835
836 static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
837 struct file *out, loff_t *ppos,
838 size_t len, unsigned int flags)
839 {
840 ssize_t ret;
841
842 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
843 if (ret > 0)
844 *ppos += ret;
845
846 return ret;
847 }
848
849 /**
850 * generic_splice_sendpage - splice data from a pipe to a socket
851 * @pipe: pipe to splice from
852 * @out: socket to write to
853 * @ppos: position in @out
854 * @len: number of bytes to splice
855 * @flags: splice modifier flags
856 *
857 * Description:
858 * Will send @len bytes from the pipe to a network socket. No data copying
859 * is involved.
860 *
861 */
862 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
863 loff_t *ppos, size_t len, unsigned int flags)
864 {
865 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
866 }
867
868 EXPORT_SYMBOL(generic_splice_sendpage);
869
870 /*
871 * Attempt to initiate a splice from pipe to file.
872 */
873 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
874 loff_t *ppos, size_t len, unsigned int flags)
875 {
876 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
877 loff_t *, size_t, unsigned int);
878
879 if (out->f_op->splice_write)
880 splice_write = out->f_op->splice_write;
881 else
882 splice_write = default_file_splice_write;
883
884 return splice_write(pipe, out, ppos, len, flags);
885 }
886
887 /*
888 * Attempt to initiate a splice from a file to a pipe.
889 */
890 static long do_splice_to(struct file *in, loff_t *ppos,
891 struct pipe_inode_info *pipe, size_t len,
892 unsigned int flags)
893 {
894 ssize_t (*splice_read)(struct file *, loff_t *,
895 struct pipe_inode_info *, size_t, unsigned int);
896 int ret;
897
898 if (unlikely(!(in->f_mode & FMODE_READ)))
899 return -EBADF;
900
901 ret = rw_verify_area(READ, in, ppos, len);
902 if (unlikely(ret < 0))
903 return ret;
904
905 if (unlikely(len > MAX_RW_COUNT))
906 len = MAX_RW_COUNT;
907
908 if (in->f_op->splice_read)
909 splice_read = in->f_op->splice_read;
910 else
911 splice_read = default_file_splice_read;
912
913 return splice_read(in, ppos, pipe, len, flags);
914 }
915
916 /**
917 * splice_direct_to_actor - splices data directly between two non-pipes
918 * @in: file to splice from
919 * @sd: actor information on where to splice to
920 * @actor: handles the data splicing
921 *
922 * Description:
923 * This is a special case helper to splice directly between two
924 * points, without requiring an explicit pipe. Internally an allocated
925 * pipe is cached in the process, and reused during the lifetime of
926 * that process.
927 *
928 */
929 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
930 splice_direct_actor *actor)
931 {
932 struct pipe_inode_info *pipe;
933 long ret, bytes;
934 umode_t i_mode;
935 size_t len;
936 int i, flags, more;
937
938 /*
939 * We require the input being a regular file, as we don't want to
940 * randomly drop data for eg socket -> socket splicing. Use the
941 * piped splicing for that!
942 */
943 i_mode = file_inode(in)->i_mode;
944 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
945 return -EINVAL;
946
947 /*
948 * neither in nor out is a pipe, setup an internal pipe attached to
949 * 'out' and transfer the wanted data from 'in' to 'out' through that
950 */
951 pipe = current->splice_pipe;
952 if (unlikely(!pipe)) {
953 pipe = alloc_pipe_info();
954 if (!pipe)
955 return -ENOMEM;
956
957 /*
958 * We don't have an immediate reader, but we'll read the stuff
959 * out of the pipe right after the splice_to_pipe(). So set
960 * PIPE_READERS appropriately.
961 */
962 pipe->readers = 1;
963
964 current->splice_pipe = pipe;
965 }
966
967 /*
968 * Do the splice.
969 */
970 ret = 0;
971 bytes = 0;
972 len = sd->total_len;
973 flags = sd->flags;
974
975 /*
976 * Don't block on output, we have to drain the direct pipe.
977 */
978 sd->flags &= ~SPLICE_F_NONBLOCK;
979 more = sd->flags & SPLICE_F_MORE;
980
981 while (len) {
982 size_t read_len;
983 loff_t pos = sd->pos, prev_pos = pos;
984
985 ret = do_splice_to(in, &pos, pipe, len, flags);
986 if (unlikely(ret <= 0))
987 goto out_release;
988
989 read_len = ret;
990 sd->total_len = read_len;
991
992 /*
993 * If more data is pending, set SPLICE_F_MORE
994 * If this is the last data and SPLICE_F_MORE was not set
995 * initially, clears it.
996 */
997 if (read_len < len)
998 sd->flags |= SPLICE_F_MORE;
999 else if (!more)
1000 sd->flags &= ~SPLICE_F_MORE;
1001 /*
1002 * NOTE: nonblocking mode only applies to the input. We
1003 * must not do the output in nonblocking mode as then we
1004 * could get stuck data in the internal pipe:
1005 */
1006 ret = actor(pipe, sd);
1007 if (unlikely(ret <= 0)) {
1008 sd->pos = prev_pos;
1009 goto out_release;
1010 }
1011
1012 bytes += ret;
1013 len -= ret;
1014 sd->pos = pos;
1015
1016 if (ret < read_len) {
1017 sd->pos = prev_pos + ret;
1018 goto out_release;
1019 }
1020 }
1021
1022 done:
1023 pipe->nrbufs = pipe->curbuf = 0;
1024 file_accessed(in);
1025 return bytes;
1026
1027 out_release:
1028 /*
1029 * If we did an incomplete transfer we must release
1030 * the pipe buffers in question:
1031 */
1032 for (i = 0; i < pipe->buffers; i++) {
1033 struct pipe_buffer *buf = pipe->bufs + i;
1034
1035 if (buf->ops) {
1036 buf->ops->release(pipe, buf);
1037 buf->ops = NULL;
1038 }
1039 }
1040
1041 if (!bytes)
1042 bytes = ret;
1043
1044 goto done;
1045 }
1046 EXPORT_SYMBOL(splice_direct_to_actor);
1047
1048 static int direct_splice_actor(struct pipe_inode_info *pipe,
1049 struct splice_desc *sd)
1050 {
1051 struct file *file = sd->u.file;
1052
1053 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1054 sd->flags);
1055 }
1056
1057 /**
1058 * do_splice_direct - splices data directly between two files
1059 * @in: file to splice from
1060 * @ppos: input file offset
1061 * @out: file to splice to
1062 * @opos: output file offset
1063 * @len: number of bytes to splice
1064 * @flags: splice modifier flags
1065 *
1066 * Description:
1067 * For use by do_sendfile(). splice can easily emulate sendfile, but
1068 * doing it in the application would incur an extra system call
1069 * (splice in + splice out, as compared to just sendfile()). So this helper
1070 * can splice directly through a process-private pipe.
1071 *
1072 */
1073 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1074 loff_t *opos, size_t len, unsigned int flags)
1075 {
1076 struct splice_desc sd = {
1077 .len = len,
1078 .total_len = len,
1079 .flags = flags,
1080 .pos = *ppos,
1081 .u.file = out,
1082 .opos = opos,
1083 };
1084 long ret;
1085
1086 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1087 return -EBADF;
1088
1089 if (unlikely(out->f_flags & O_APPEND))
1090 return -EINVAL;
1091
1092 ret = rw_verify_area(WRITE, out, opos, len);
1093 if (unlikely(ret < 0))
1094 return ret;
1095
1096 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1097 if (ret > 0)
1098 *ppos = sd.pos;
1099
1100 return ret;
1101 }
1102 EXPORT_SYMBOL(do_splice_direct);
1103
1104 static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1105 {
1106 while (pipe->nrbufs == pipe->buffers) {
1107 if (flags & SPLICE_F_NONBLOCK)
1108 return -EAGAIN;
1109 if (signal_pending(current))
1110 return -ERESTARTSYS;
1111 pipe->waiting_writers++;
1112 pipe_wait(pipe);
1113 pipe->waiting_writers--;
1114 }
1115 return 0;
1116 }
1117
1118 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1119 struct pipe_inode_info *opipe,
1120 size_t len, unsigned int flags);
1121
1122 /*
1123 * Determine where to splice to/from.
1124 */
1125 static long do_splice(struct file *in, loff_t __user *off_in,
1126 struct file *out, loff_t __user *off_out,
1127 size_t len, unsigned int flags)
1128 {
1129 struct pipe_inode_info *ipipe;
1130 struct pipe_inode_info *opipe;
1131 loff_t offset;
1132 long ret;
1133
1134 ipipe = get_pipe_info(in);
1135 opipe = get_pipe_info(out);
1136
1137 if (ipipe && opipe) {
1138 if (off_in || off_out)
1139 return -ESPIPE;
1140
1141 if (!(in->f_mode & FMODE_READ))
1142 return -EBADF;
1143
1144 if (!(out->f_mode & FMODE_WRITE))
1145 return -EBADF;
1146
1147 /* Splicing to self would be fun, but... */
1148 if (ipipe == opipe)
1149 return -EINVAL;
1150
1151 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1152 }
1153
1154 if (ipipe) {
1155 if (off_in)
1156 return -ESPIPE;
1157 if (off_out) {
1158 if (!(out->f_mode & FMODE_PWRITE))
1159 return -EINVAL;
1160 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1161 return -EFAULT;
1162 } else {
1163 offset = out->f_pos;
1164 }
1165
1166 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1167 return -EBADF;
1168
1169 if (unlikely(out->f_flags & O_APPEND))
1170 return -EINVAL;
1171
1172 ret = rw_verify_area(WRITE, out, &offset, len);
1173 if (unlikely(ret < 0))
1174 return ret;
1175
1176 file_start_write(out);
1177 ret = do_splice_from(ipipe, out, &offset, len, flags);
1178 file_end_write(out);
1179
1180 if (!off_out)
1181 out->f_pos = offset;
1182 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1183 ret = -EFAULT;
1184
1185 return ret;
1186 }
1187
1188 if (opipe) {
1189 if (off_out)
1190 return -ESPIPE;
1191 if (off_in) {
1192 if (!(in->f_mode & FMODE_PREAD))
1193 return -EINVAL;
1194 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1195 return -EFAULT;
1196 } else {
1197 offset = in->f_pos;
1198 }
1199
1200 pipe_lock(opipe);
1201 ret = wait_for_space(opipe, flags);
1202 if (!ret)
1203 ret = do_splice_to(in, &offset, opipe, len, flags);
1204 pipe_unlock(opipe);
1205 if (ret > 0)
1206 wakeup_pipe_readers(opipe);
1207 if (!off_in)
1208 in->f_pos = offset;
1209 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1210 ret = -EFAULT;
1211
1212 return ret;
1213 }
1214
1215 return -EINVAL;
1216 }
1217
1218 static int iter_to_pipe(struct iov_iter *from,
1219 struct pipe_inode_info *pipe,
1220 unsigned flags)
1221 {
1222 struct pipe_buffer buf = {
1223 .ops = &user_page_pipe_buf_ops,
1224 .flags = flags
1225 };
1226 size_t total = 0;
1227 int ret = 0;
1228 bool failed = false;
1229
1230 while (iov_iter_count(from) && !failed) {
1231 struct page *pages[16];
1232 ssize_t copied;
1233 size_t start;
1234 int n;
1235
1236 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1237 if (copied <= 0) {
1238 ret = copied;
1239 break;
1240 }
1241
1242 for (n = 0; copied; n++, start = 0) {
1243 int size = min_t(int, copied, PAGE_SIZE - start);
1244 if (!failed) {
1245 buf.page = pages[n];
1246 buf.offset = start;
1247 buf.len = size;
1248 ret = add_to_pipe(pipe, &buf);
1249 if (unlikely(ret < 0)) {
1250 failed = true;
1251 } else {
1252 iov_iter_advance(from, ret);
1253 total += ret;
1254 }
1255 } else {
1256 put_page(pages[n]);
1257 }
1258 copied -= size;
1259 }
1260 }
1261 return total ? total : ret;
1262 }
1263
1264 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1265 struct splice_desc *sd)
1266 {
1267 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1268 return n == sd->len ? n : -EFAULT;
1269 }
1270
1271 /*
1272 * For lack of a better implementation, implement vmsplice() to userspace
1273 * as a simple copy of the pipes pages to the user iov.
1274 */
1275 static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
1276 unsigned long nr_segs, unsigned int flags)
1277 {
1278 struct pipe_inode_info *pipe;
1279 struct splice_desc sd;
1280 long ret;
1281 struct iovec iovstack[UIO_FASTIOV];
1282 struct iovec *iov = iovstack;
1283 struct iov_iter iter;
1284
1285 pipe = get_pipe_info(file);
1286 if (!pipe)
1287 return -EBADF;
1288
1289 ret = import_iovec(READ, uiov, nr_segs,
1290 ARRAY_SIZE(iovstack), &iov, &iter);
1291 if (ret < 0)
1292 return ret;
1293
1294 sd.total_len = iov_iter_count(&iter);
1295 sd.len = 0;
1296 sd.flags = flags;
1297 sd.u.data = &iter;
1298 sd.pos = 0;
1299
1300 if (sd.total_len) {
1301 pipe_lock(pipe);
1302 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1303 pipe_unlock(pipe);
1304 }
1305
1306 kfree(iov);
1307 return ret;
1308 }
1309
1310 /*
1311 * vmsplice splices a user address range into a pipe. It can be thought of
1312 * as splice-from-memory, where the regular splice is splice-from-file (or
1313 * to file). In both cases the output is a pipe, naturally.
1314 */
1315 static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
1316 unsigned long nr_segs, unsigned int flags)
1317 {
1318 struct pipe_inode_info *pipe;
1319 struct iovec iovstack[UIO_FASTIOV];
1320 struct iovec *iov = iovstack;
1321 struct iov_iter from;
1322 long ret;
1323 unsigned buf_flag = 0;
1324
1325 if (flags & SPLICE_F_GIFT)
1326 buf_flag = PIPE_BUF_FLAG_GIFT;
1327
1328 pipe = get_pipe_info(file);
1329 if (!pipe)
1330 return -EBADF;
1331
1332 ret = import_iovec(WRITE, uiov, nr_segs,
1333 ARRAY_SIZE(iovstack), &iov, &from);
1334 if (ret < 0)
1335 return ret;
1336
1337 pipe_lock(pipe);
1338 ret = wait_for_space(pipe, flags);
1339 if (!ret)
1340 ret = iter_to_pipe(&from, pipe, buf_flag);
1341 pipe_unlock(pipe);
1342 if (ret > 0)
1343 wakeup_pipe_readers(pipe);
1344 kfree(iov);
1345 return ret;
1346 }
1347
1348 /*
1349 * Note that vmsplice only really supports true splicing _from_ user memory
1350 * to a pipe, not the other way around. Splicing from user memory is a simple
1351 * operation that can be supported without any funky alignment restrictions
1352 * or nasty vm tricks. We simply map in the user memory and fill them into
1353 * a pipe. The reverse isn't quite as easy, though. There are two possible
1354 * solutions for that:
1355 *
1356 * - memcpy() the data internally, at which point we might as well just
1357 * do a regular read() on the buffer anyway.
1358 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1359 * has restriction limitations on both ends of the pipe).
1360 *
1361 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1362 *
1363 */
1364 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1365 unsigned long, nr_segs, unsigned int, flags)
1366 {
1367 struct fd f;
1368 long error;
1369
1370 if (unlikely(nr_segs > UIO_MAXIOV))
1371 return -EINVAL;
1372 else if (unlikely(!nr_segs))
1373 return 0;
1374
1375 error = -EBADF;
1376 f = fdget(fd);
1377 if (f.file) {
1378 if (f.file->f_mode & FMODE_WRITE)
1379 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1380 else if (f.file->f_mode & FMODE_READ)
1381 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1382
1383 fdput(f);
1384 }
1385
1386 return error;
1387 }
1388
1389 #ifdef CONFIG_COMPAT
1390 COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1391 unsigned int, nr_segs, unsigned int, flags)
1392 {
1393 unsigned i;
1394 struct iovec __user *iov;
1395 if (nr_segs > UIO_MAXIOV)
1396 return -EINVAL;
1397 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1398 for (i = 0; i < nr_segs; i++) {
1399 struct compat_iovec v;
1400 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1401 get_user(v.iov_len, &iov32[i].iov_len) ||
1402 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1403 put_user(v.iov_len, &iov[i].iov_len))
1404 return -EFAULT;
1405 }
1406 return sys_vmsplice(fd, iov, nr_segs, flags);
1407 }
1408 #endif
1409
1410 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1411 int, fd_out, loff_t __user *, off_out,
1412 size_t, len, unsigned int, flags)
1413 {
1414 struct fd in, out;
1415 long error;
1416
1417 if (unlikely(!len))
1418 return 0;
1419
1420 error = -EBADF;
1421 in = fdget(fd_in);
1422 if (in.file) {
1423 if (in.file->f_mode & FMODE_READ) {
1424 out = fdget(fd_out);
1425 if (out.file) {
1426 if (out.file->f_mode & FMODE_WRITE)
1427 error = do_splice(in.file, off_in,
1428 out.file, off_out,
1429 len, flags);
1430 fdput(out);
1431 }
1432 }
1433 fdput(in);
1434 }
1435 return error;
1436 }
1437
1438 /*
1439 * Make sure there's data to read. Wait for input if we can, otherwise
1440 * return an appropriate error.
1441 */
1442 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1443 {
1444 int ret;
1445
1446 /*
1447 * Check ->nrbufs without the inode lock first. This function
1448 * is speculative anyways, so missing one is ok.
1449 */
1450 if (pipe->nrbufs)
1451 return 0;
1452
1453 ret = 0;
1454 pipe_lock(pipe);
1455
1456 while (!pipe->nrbufs) {
1457 if (signal_pending(current)) {
1458 ret = -ERESTARTSYS;
1459 break;
1460 }
1461 if (!pipe->writers)
1462 break;
1463 if (!pipe->waiting_writers) {
1464 if (flags & SPLICE_F_NONBLOCK) {
1465 ret = -EAGAIN;
1466 break;
1467 }
1468 }
1469 pipe_wait(pipe);
1470 }
1471
1472 pipe_unlock(pipe);
1473 return ret;
1474 }
1475
1476 /*
1477 * Make sure there's writeable room. Wait for room if we can, otherwise
1478 * return an appropriate error.
1479 */
1480 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1481 {
1482 int ret;
1483
1484 /*
1485 * Check ->nrbufs without the inode lock first. This function
1486 * is speculative anyways, so missing one is ok.
1487 */
1488 if (pipe->nrbufs < pipe->buffers)
1489 return 0;
1490
1491 ret = 0;
1492 pipe_lock(pipe);
1493
1494 while (pipe->nrbufs >= pipe->buffers) {
1495 if (!pipe->readers) {
1496 send_sig(SIGPIPE, current, 0);
1497 ret = -EPIPE;
1498 break;
1499 }
1500 if (flags & SPLICE_F_NONBLOCK) {
1501 ret = -EAGAIN;
1502 break;
1503 }
1504 if (signal_pending(current)) {
1505 ret = -ERESTARTSYS;
1506 break;
1507 }
1508 pipe->waiting_writers++;
1509 pipe_wait(pipe);
1510 pipe->waiting_writers--;
1511 }
1512
1513 pipe_unlock(pipe);
1514 return ret;
1515 }
1516
1517 /*
1518 * Splice contents of ipipe to opipe.
1519 */
1520 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1521 struct pipe_inode_info *opipe,
1522 size_t len, unsigned int flags)
1523 {
1524 struct pipe_buffer *ibuf, *obuf;
1525 int ret = 0, nbuf;
1526 bool input_wakeup = false;
1527
1528
1529 retry:
1530 ret = ipipe_prep(ipipe, flags);
1531 if (ret)
1532 return ret;
1533
1534 ret = opipe_prep(opipe, flags);
1535 if (ret)
1536 return ret;
1537
1538 /*
1539 * Potential ABBA deadlock, work around it by ordering lock
1540 * grabbing by pipe info address. Otherwise two different processes
1541 * could deadlock (one doing tee from A -> B, the other from B -> A).
1542 */
1543 pipe_double_lock(ipipe, opipe);
1544
1545 do {
1546 if (!opipe->readers) {
1547 send_sig(SIGPIPE, current, 0);
1548 if (!ret)
1549 ret = -EPIPE;
1550 break;
1551 }
1552
1553 if (!ipipe->nrbufs && !ipipe->writers)
1554 break;
1555
1556 /*
1557 * Cannot make any progress, because either the input
1558 * pipe is empty or the output pipe is full.
1559 */
1560 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
1561 /* Already processed some buffers, break */
1562 if (ret)
1563 break;
1564
1565 if (flags & SPLICE_F_NONBLOCK) {
1566 ret = -EAGAIN;
1567 break;
1568 }
1569
1570 /*
1571 * We raced with another reader/writer and haven't
1572 * managed to process any buffers. A zero return
1573 * value means EOF, so retry instead.
1574 */
1575 pipe_unlock(ipipe);
1576 pipe_unlock(opipe);
1577 goto retry;
1578 }
1579
1580 ibuf = ipipe->bufs + ipipe->curbuf;
1581 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1582 obuf = opipe->bufs + nbuf;
1583
1584 if (len >= ibuf->len) {
1585 /*
1586 * Simply move the whole buffer from ipipe to opipe
1587 */
1588 *obuf = *ibuf;
1589 ibuf->ops = NULL;
1590 opipe->nrbufs++;
1591 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
1592 ipipe->nrbufs--;
1593 input_wakeup = true;
1594 } else {
1595 /*
1596 * Get a reference to this pipe buffer,
1597 * so we can copy the contents over.
1598 */
1599 ibuf->ops->get(ipipe, ibuf);
1600 *obuf = *ibuf;
1601
1602 /*
1603 * Don't inherit the gift flag, we need to
1604 * prevent multiple steals of this page.
1605 */
1606 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1607
1608 obuf->len = len;
1609 opipe->nrbufs++;
1610 ibuf->offset += obuf->len;
1611 ibuf->len -= obuf->len;
1612 }
1613 ret += obuf->len;
1614 len -= obuf->len;
1615 } while (len);
1616
1617 pipe_unlock(ipipe);
1618 pipe_unlock(opipe);
1619
1620 /*
1621 * If we put data in the output pipe, wakeup any potential readers.
1622 */
1623 if (ret > 0)
1624 wakeup_pipe_readers(opipe);
1625
1626 if (input_wakeup)
1627 wakeup_pipe_writers(ipipe);
1628
1629 return ret;
1630 }
1631
1632 /*
1633 * Link contents of ipipe to opipe.
1634 */
1635 static int link_pipe(struct pipe_inode_info *ipipe,
1636 struct pipe_inode_info *opipe,
1637 size_t len, unsigned int flags)
1638 {
1639 struct pipe_buffer *ibuf, *obuf;
1640 int ret = 0, i = 0, nbuf;
1641
1642 /*
1643 * Potential ABBA deadlock, work around it by ordering lock
1644 * grabbing by pipe info address. Otherwise two different processes
1645 * could deadlock (one doing tee from A -> B, the other from B -> A).
1646 */
1647 pipe_double_lock(ipipe, opipe);
1648
1649 do {
1650 if (!opipe->readers) {
1651 send_sig(SIGPIPE, current, 0);
1652 if (!ret)
1653 ret = -EPIPE;
1654 break;
1655 }
1656
1657 /*
1658 * If we have iterated all input buffers or ran out of
1659 * output room, break.
1660 */
1661 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
1662 break;
1663
1664 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1665 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
1666
1667 /*
1668 * Get a reference to this pipe buffer,
1669 * so we can copy the contents over.
1670 */
1671 ibuf->ops->get(ipipe, ibuf);
1672
1673 obuf = opipe->bufs + nbuf;
1674 *obuf = *ibuf;
1675
1676 /*
1677 * Don't inherit the gift flag, we need to
1678 * prevent multiple steals of this page.
1679 */
1680 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1681
1682 if (obuf->len > len)
1683 obuf->len = len;
1684
1685 opipe->nrbufs++;
1686 ret += obuf->len;
1687 len -= obuf->len;
1688 i++;
1689 } while (len);
1690
1691 /*
1692 * return EAGAIN if we have the potential of some data in the
1693 * future, otherwise just return 0
1694 */
1695 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1696 ret = -EAGAIN;
1697
1698 pipe_unlock(ipipe);
1699 pipe_unlock(opipe);
1700
1701 /*
1702 * If we put data in the output pipe, wakeup any potential readers.
1703 */
1704 if (ret > 0)
1705 wakeup_pipe_readers(opipe);
1706
1707 return ret;
1708 }
1709
1710 /*
1711 * This is a tee(1) implementation that works on pipes. It doesn't copy
1712 * any data, it simply references the 'in' pages on the 'out' pipe.
1713 * The 'flags' used are the SPLICE_F_* variants, currently the only
1714 * applicable one is SPLICE_F_NONBLOCK.
1715 */
1716 static long do_tee(struct file *in, struct file *out, size_t len,
1717 unsigned int flags)
1718 {
1719 struct pipe_inode_info *ipipe = get_pipe_info(in);
1720 struct pipe_inode_info *opipe = get_pipe_info(out);
1721 int ret = -EINVAL;
1722
1723 /*
1724 * Duplicate the contents of ipipe to opipe without actually
1725 * copying the data.
1726 */
1727 if (ipipe && opipe && ipipe != opipe) {
1728 /*
1729 * Keep going, unless we encounter an error. The ipipe/opipe
1730 * ordering doesn't really matter.
1731 */
1732 ret = ipipe_prep(ipipe, flags);
1733 if (!ret) {
1734 ret = opipe_prep(opipe, flags);
1735 if (!ret)
1736 ret = link_pipe(ipipe, opipe, len, flags);
1737 }
1738 }
1739
1740 return ret;
1741 }
1742
1743 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1744 {
1745 struct fd in;
1746 int error;
1747
1748 if (unlikely(!len))
1749 return 0;
1750
1751 error = -EBADF;
1752 in = fdget(fdin);
1753 if (in.file) {
1754 if (in.file->f_mode & FMODE_READ) {
1755 struct fd out = fdget(fdout);
1756 if (out.file) {
1757 if (out.file->f_mode & FMODE_WRITE)
1758 error = do_tee(in.file, out.file,
1759 len, flags);
1760 fdput(out);
1761 }
1762 }
1763 fdput(in);
1764 }
1765
1766 return error;
1767 }