move some stuff from common.c to neighbor.c
[cor.git] / fs / splice.c
blobfa1f3773c8cd2f875b6de26c9a09b7f07ccdb362
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * "splice": joining two ropes together by interweaving their strands.
5 * This is the "extended pipe" functionality, where a pipe is used as
6 * an arbitrary in-memory buffer. Think of a pipe as a small kernel
7 * buffer that you can use to transfer data from one end to the other.
9 * The traditional unix read/write is extended with a "splice()" operation
10 * that transfers data buffers to or from a pipe buffer.
12 * Named by Larry McVoy, original implementation from Linus, extended by
13 * Jens to support splicing to files, network, direct splicing, etc and
14 * fixing lots of bugs.
16 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
17 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
21 #include <linux/bvec.h>
22 #include <linux/fs.h>
23 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/splice.h>
26 #include <linux/memcontrol.h>
27 #include <linux/mm_inline.h>
28 #include <linux/swap.h>
29 #include <linux/writeback.h>
30 #include <linux/export.h>
31 #include <linux/syscalls.h>
32 #include <linux/uio.h>
33 #include <linux/security.h>
34 #include <linux/gfp.h>
35 #include <linux/socket.h>
36 #include <linux/compat.h>
37 #include <linux/sched/signal.h>
39 #include "internal.h"
42 * Attempt to steal a page from a pipe buffer. This should perhaps go into
43 * a vm helper function, it's already simplified quite a bit by the
44 * addition of remove_mapping(). If success is returned, the caller may
45 * attempt to reuse this page for another destination.
47 static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
48 struct pipe_buffer *buf)
50 struct page *page = buf->page;
51 struct address_space *mapping;
53 lock_page(page);
55 mapping = page_mapping(page);
56 if (mapping) {
57 WARN_ON(!PageUptodate(page));
60 * At least for ext2 with nobh option, we need to wait on
61 * writeback completing on this page, since we'll remove it
62 * from the pagecache. Otherwise truncate wont wait on the
63 * page, allowing the disk blocks to be reused by someone else
64 * before we actually wrote our data to them. fs corruption
65 * ensues.
67 wait_on_page_writeback(page);
69 if (page_has_private(page) &&
70 !try_to_release_page(page, GFP_KERNEL))
71 goto out_unlock;
74 * If we succeeded in removing the mapping, set LRU flag
75 * and return good.
77 if (remove_mapping(mapping, page)) {
78 buf->flags |= PIPE_BUF_FLAG_LRU;
79 return 0;
84 * Raced with truncate or failed to remove page from current
85 * address space, unlock and return failure.
87 out_unlock:
88 unlock_page(page);
89 return 1;
92 static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
93 struct pipe_buffer *buf)
95 put_page(buf->page);
96 buf->flags &= ~PIPE_BUF_FLAG_LRU;
100 * Check whether the contents of buf is OK to access. Since the content
101 * is a page cache page, IO may be in flight.
103 static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
104 struct pipe_buffer *buf)
106 struct page *page = buf->page;
107 int err;
109 if (!PageUptodate(page)) {
110 lock_page(page);
113 * Page got truncated/unhashed. This will cause a 0-byte
114 * splice, if this is the first page.
116 if (!page->mapping) {
117 err = -ENODATA;
118 goto error;
122 * Uh oh, read-error from disk.
124 if (!PageUptodate(page)) {
125 err = -EIO;
126 goto error;
130 * Page is ok afterall, we are done.
132 unlock_page(page);
135 return 0;
136 error:
137 unlock_page(page);
138 return err;
141 const struct pipe_buf_operations page_cache_pipe_buf_ops = {
142 .confirm = page_cache_pipe_buf_confirm,
143 .release = page_cache_pipe_buf_release,
144 .steal = page_cache_pipe_buf_steal,
145 .get = generic_pipe_buf_get,
148 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
149 struct pipe_buffer *buf)
151 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
152 return 1;
154 buf->flags |= PIPE_BUF_FLAG_LRU;
155 return generic_pipe_buf_steal(pipe, buf);
158 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159 .confirm = generic_pipe_buf_confirm,
160 .release = page_cache_pipe_buf_release,
161 .steal = user_page_pipe_buf_steal,
162 .get = generic_pipe_buf_get,
165 static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
167 smp_mb();
168 if (waitqueue_active(&pipe->wait))
169 wake_up_interruptible(&pipe->wait);
170 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
174 * splice_to_pipe - fill passed data into a pipe
175 * @pipe: pipe to fill
176 * @spd: data to fill
178 * Description:
179 * @spd contains a map of pages and len/offset tuples, along with
180 * the struct pipe_buf_operations associated with these pages. This
181 * function will link that data to the pipe.
184 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
185 struct splice_pipe_desc *spd)
187 unsigned int spd_pages = spd->nr_pages;
188 unsigned int tail = pipe->tail;
189 unsigned int head = pipe->head;
190 unsigned int mask = pipe->ring_size - 1;
191 int ret = 0, page_nr = 0;
193 if (!spd_pages)
194 return 0;
196 if (unlikely(!pipe->readers)) {
197 send_sig(SIGPIPE, current, 0);
198 ret = -EPIPE;
199 goto out;
202 while (!pipe_full(head, tail, pipe->max_usage)) {
203 struct pipe_buffer *buf = &pipe->bufs[head & mask];
205 buf->page = spd->pages[page_nr];
206 buf->offset = spd->partial[page_nr].offset;
207 buf->len = spd->partial[page_nr].len;
208 buf->private = spd->partial[page_nr].private;
209 buf->ops = spd->ops;
210 buf->flags = 0;
212 head++;
213 pipe->head = head;
214 page_nr++;
215 ret += buf->len;
217 if (!--spd->nr_pages)
218 break;
221 if (!ret)
222 ret = -EAGAIN;
224 out:
225 while (page_nr < spd_pages)
226 spd->spd_release(spd, page_nr++);
228 return ret;
230 EXPORT_SYMBOL_GPL(splice_to_pipe);
232 ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
234 unsigned int head = pipe->head;
235 unsigned int tail = pipe->tail;
236 unsigned int mask = pipe->ring_size - 1;
237 int ret;
239 if (unlikely(!pipe->readers)) {
240 send_sig(SIGPIPE, current, 0);
241 ret = -EPIPE;
242 } else if (pipe_full(head, tail, pipe->max_usage)) {
243 ret = -EAGAIN;
244 } else {
245 pipe->bufs[head & mask] = *buf;
246 pipe->head = head + 1;
247 return buf->len;
249 pipe_buf_release(pipe, buf);
250 return ret;
252 EXPORT_SYMBOL(add_to_pipe);
255 * Check if we need to grow the arrays holding pages and partial page
256 * descriptions.
258 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
260 unsigned int max_usage = READ_ONCE(pipe->max_usage);
262 spd->nr_pages_max = max_usage;
263 if (max_usage <= PIPE_DEF_BUFFERS)
264 return 0;
266 spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
267 spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
268 GFP_KERNEL);
270 if (spd->pages && spd->partial)
271 return 0;
273 kfree(spd->pages);
274 kfree(spd->partial);
275 return -ENOMEM;
278 void splice_shrink_spd(struct splice_pipe_desc *spd)
280 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
281 return;
283 kfree(spd->pages);
284 kfree(spd->partial);
288 * generic_file_splice_read - splice data from file to a pipe
289 * @in: file to splice from
290 * @ppos: position in @in
291 * @pipe: pipe to splice to
292 * @len: number of bytes to splice
293 * @flags: splice modifier flags
295 * Description:
296 * Will read pages from given file and fill them into a pipe. Can be
297 * used as long as it has more or less sane ->read_iter().
300 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
301 struct pipe_inode_info *pipe, size_t len,
302 unsigned int flags)
304 struct iov_iter to;
305 struct kiocb kiocb;
306 unsigned int i_head;
307 int ret;
309 iov_iter_pipe(&to, READ, pipe, len);
310 i_head = to.head;
311 init_sync_kiocb(&kiocb, in);
312 kiocb.ki_pos = *ppos;
313 ret = call_read_iter(in, &kiocb, &to);
314 if (ret > 0) {
315 *ppos = kiocb.ki_pos;
316 file_accessed(in);
317 } else if (ret < 0) {
318 to.head = i_head;
319 to.iov_offset = 0;
320 iov_iter_advance(&to, 0); /* to free what was emitted */
322 * callers of ->splice_read() expect -EAGAIN on
323 * "can't put anything in there", rather than -EFAULT.
325 if (ret == -EFAULT)
326 ret = -EAGAIN;
329 return ret;
331 EXPORT_SYMBOL(generic_file_splice_read);
333 const struct pipe_buf_operations default_pipe_buf_ops = {
334 .confirm = generic_pipe_buf_confirm,
335 .release = generic_pipe_buf_release,
336 .steal = generic_pipe_buf_steal,
337 .get = generic_pipe_buf_get,
340 int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
341 struct pipe_buffer *buf)
343 return 1;
346 /* Pipe buffer operations for a socket and similar. */
347 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
348 .confirm = generic_pipe_buf_confirm,
349 .release = generic_pipe_buf_release,
350 .steal = generic_pipe_buf_nosteal,
351 .get = generic_pipe_buf_get,
353 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
355 static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
356 unsigned long vlen, loff_t offset)
358 mm_segment_t old_fs;
359 loff_t pos = offset;
360 ssize_t res;
362 old_fs = get_fs();
363 set_fs(KERNEL_DS);
364 /* The cast to a user pointer is valid due to the set_fs() */
365 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
366 set_fs(old_fs);
368 return res;
371 static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
372 struct pipe_inode_info *pipe, size_t len,
373 unsigned int flags)
375 struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
376 struct iov_iter to;
377 struct page **pages;
378 unsigned int nr_pages;
379 unsigned int mask;
380 size_t offset, base, copied = 0;
381 ssize_t res;
382 int i;
384 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
385 return -EAGAIN;
388 * Try to keep page boundaries matching to source pagecache ones -
389 * it probably won't be much help, but...
391 offset = *ppos & ~PAGE_MASK;
393 iov_iter_pipe(&to, READ, pipe, len + offset);
395 res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
396 if (res <= 0)
397 return -ENOMEM;
399 nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
401 vec = __vec;
402 if (nr_pages > PIPE_DEF_BUFFERS) {
403 vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
404 if (unlikely(!vec)) {
405 res = -ENOMEM;
406 goto out;
410 mask = pipe->ring_size - 1;
411 pipe->bufs[to.head & mask].offset = offset;
412 pipe->bufs[to.head & mask].len -= offset;
414 for (i = 0; i < nr_pages; i++) {
415 size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
416 vec[i].iov_base = page_address(pages[i]) + offset;
417 vec[i].iov_len = this_len;
418 len -= this_len;
419 offset = 0;
422 res = kernel_readv(in, vec, nr_pages, *ppos);
423 if (res > 0) {
424 copied = res;
425 *ppos += res;
428 if (vec != __vec)
429 kfree(vec);
430 out:
431 for (i = 0; i < nr_pages; i++)
432 put_page(pages[i]);
433 kvfree(pages);
434 iov_iter_advance(&to, copied); /* truncates and discards */
435 return res;
439 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
440 * using sendpage(). Return the number of bytes sent.
442 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
443 struct pipe_buffer *buf, struct splice_desc *sd)
445 struct file *file = sd->u.file;
446 loff_t pos = sd->pos;
447 int more;
449 if (!likely(file->f_op->sendpage))
450 return -EINVAL;
452 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
454 if (sd->len < sd->total_len &&
455 pipe_occupancy(pipe->head, pipe->tail) > 1)
456 more |= MSG_SENDPAGE_NOTLAST;
458 return file->f_op->sendpage(file, buf->page, buf->offset,
459 sd->len, &pos, more);
462 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
464 smp_mb();
465 if (waitqueue_active(&pipe->wait))
466 wake_up_interruptible(&pipe->wait);
467 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
471 * splice_from_pipe_feed - feed available data from a pipe to a file
472 * @pipe: pipe to splice from
473 * @sd: information to @actor
474 * @actor: handler that splices the data
476 * Description:
477 * This function loops over the pipe and calls @actor to do the
478 * actual moving of a single struct pipe_buffer to the desired
479 * destination. It returns when there's no more buffers left in
480 * the pipe or if the requested number of bytes (@sd->total_len)
481 * have been copied. It returns a positive number (one) if the
482 * pipe needs to be filled with more data, zero if the required
483 * number of bytes have been copied and -errno on error.
485 * This, together with splice_from_pipe_{begin,end,next}, may be
486 * used to implement the functionality of __splice_from_pipe() when
487 * locking is required around copying the pipe buffers to the
488 * destination.
490 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
491 splice_actor *actor)
493 unsigned int head = pipe->head;
494 unsigned int tail = pipe->tail;
495 unsigned int mask = pipe->ring_size - 1;
496 int ret;
498 while (!pipe_empty(head, tail)) {
499 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
501 sd->len = buf->len;
502 if (sd->len > sd->total_len)
503 sd->len = sd->total_len;
505 ret = pipe_buf_confirm(pipe, buf);
506 if (unlikely(ret)) {
507 if (ret == -ENODATA)
508 ret = 0;
509 return ret;
512 ret = actor(pipe, buf, sd);
513 if (ret <= 0)
514 return ret;
516 buf->offset += ret;
517 buf->len -= ret;
519 sd->num_spliced += ret;
520 sd->len -= ret;
521 sd->pos += ret;
522 sd->total_len -= ret;
524 if (!buf->len) {
525 pipe_buf_release(pipe, buf);
526 tail++;
527 pipe->tail = tail;
528 if (pipe->files)
529 sd->need_wakeup = true;
532 if (!sd->total_len)
533 return 0;
536 return 1;
540 * splice_from_pipe_next - wait for some data to splice from
541 * @pipe: pipe to splice from
542 * @sd: information about the splice operation
544 * Description:
545 * This function will wait for some data and return a positive
546 * value (one) if pipe buffers are available. It will return zero
547 * or -errno if no more data needs to be spliced.
549 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
552 * Check for signal early to make process killable when there are
553 * always buffers available
555 if (signal_pending(current))
556 return -ERESTARTSYS;
558 while (pipe_empty(pipe->head, pipe->tail)) {
559 if (!pipe->writers)
560 return 0;
562 if (!pipe->waiting_writers && sd->num_spliced)
563 return 0;
565 if (sd->flags & SPLICE_F_NONBLOCK)
566 return -EAGAIN;
568 if (signal_pending(current))
569 return -ERESTARTSYS;
571 if (sd->need_wakeup) {
572 wakeup_pipe_writers(pipe);
573 sd->need_wakeup = false;
576 pipe_wait(pipe);
579 return 1;
583 * splice_from_pipe_begin - start splicing from pipe
584 * @sd: information about the splice operation
586 * Description:
587 * This function should be called before a loop containing
588 * splice_from_pipe_next() and splice_from_pipe_feed() to
589 * initialize the necessary fields of @sd.
591 static void splice_from_pipe_begin(struct splice_desc *sd)
593 sd->num_spliced = 0;
594 sd->need_wakeup = false;
598 * splice_from_pipe_end - finish splicing from pipe
599 * @pipe: pipe to splice from
600 * @sd: information about the splice operation
602 * Description:
603 * This function will wake up pipe writers if necessary. It should
604 * be called after a loop containing splice_from_pipe_next() and
605 * splice_from_pipe_feed().
607 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
609 if (sd->need_wakeup)
610 wakeup_pipe_writers(pipe);
614 * __splice_from_pipe - splice data from a pipe to given actor
615 * @pipe: pipe to splice from
616 * @sd: information to @actor
617 * @actor: handler that splices the data
619 * Description:
620 * This function does little more than loop over the pipe and call
621 * @actor to do the actual moving of a single struct pipe_buffer to
622 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
623 * pipe_to_user.
626 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
627 splice_actor *actor)
629 int ret;
631 splice_from_pipe_begin(sd);
632 do {
633 cond_resched();
634 ret = splice_from_pipe_next(pipe, sd);
635 if (ret > 0)
636 ret = splice_from_pipe_feed(pipe, sd, actor);
637 } while (ret > 0);
638 splice_from_pipe_end(pipe, sd);
640 return sd->num_spliced ? sd->num_spliced : ret;
642 EXPORT_SYMBOL(__splice_from_pipe);
645 * splice_from_pipe - splice data from a pipe to a file
646 * @pipe: pipe to splice from
647 * @out: file to splice to
648 * @ppos: position in @out
649 * @len: how many bytes to splice
650 * @flags: splice modifier flags
651 * @actor: handler that splices the data
653 * Description:
654 * See __splice_from_pipe. This function locks the pipe inode,
655 * otherwise it's identical to __splice_from_pipe().
658 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
659 loff_t *ppos, size_t len, unsigned int flags,
660 splice_actor *actor)
662 ssize_t ret;
663 struct splice_desc sd = {
664 .total_len = len,
665 .flags = flags,
666 .pos = *ppos,
667 .u.file = out,
670 pipe_lock(pipe);
671 ret = __splice_from_pipe(pipe, &sd, actor);
672 pipe_unlock(pipe);
674 return ret;
678 * iter_file_splice_write - splice data from a pipe to a file
679 * @pipe: pipe info
680 * @out: file to write to
681 * @ppos: position in @out
682 * @len: number of bytes to splice
683 * @flags: splice modifier flags
685 * Description:
686 * Will either move or copy pages (determined by @flags options) from
687 * the given pipe inode to the given file.
688 * This one is ->write_iter-based.
691 ssize_t
692 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
693 loff_t *ppos, size_t len, unsigned int flags)
695 struct splice_desc sd = {
696 .total_len = len,
697 .flags = flags,
698 .pos = *ppos,
699 .u.file = out,
701 int nbufs = pipe->max_usage;
702 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
703 GFP_KERNEL);
704 ssize_t ret;
706 if (unlikely(!array))
707 return -ENOMEM;
709 pipe_lock(pipe);
711 splice_from_pipe_begin(&sd);
712 while (sd.total_len) {
713 struct iov_iter from;
714 unsigned int head, tail, mask;
715 size_t left;
716 int n;
718 ret = splice_from_pipe_next(pipe, &sd);
719 if (ret <= 0)
720 break;
722 if (unlikely(nbufs < pipe->max_usage)) {
723 kfree(array);
724 nbufs = pipe->max_usage;
725 array = kcalloc(nbufs, sizeof(struct bio_vec),
726 GFP_KERNEL);
727 if (!array) {
728 ret = -ENOMEM;
729 break;
733 head = pipe->head;
734 tail = pipe->tail;
735 mask = pipe->ring_size - 1;
737 /* build the vector */
738 left = sd.total_len;
739 for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
740 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
741 size_t this_len = buf->len;
743 if (this_len > left)
744 this_len = left;
746 ret = pipe_buf_confirm(pipe, buf);
747 if (unlikely(ret)) {
748 if (ret == -ENODATA)
749 ret = 0;
750 goto done;
753 array[n].bv_page = buf->page;
754 array[n].bv_len = this_len;
755 array[n].bv_offset = buf->offset;
756 left -= this_len;
759 iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
760 ret = vfs_iter_write(out, &from, &sd.pos, 0);
761 if (ret <= 0)
762 break;
764 sd.num_spliced += ret;
765 sd.total_len -= ret;
766 *ppos = sd.pos;
768 /* dismiss the fully eaten buffers, adjust the partial one */
769 tail = pipe->tail;
770 while (ret) {
771 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
772 if (ret >= buf->len) {
773 ret -= buf->len;
774 buf->len = 0;
775 pipe_buf_release(pipe, buf);
776 tail++;
777 pipe->tail = tail;
778 if (pipe->files)
779 sd.need_wakeup = true;
780 } else {
781 buf->offset += ret;
782 buf->len -= ret;
783 ret = 0;
787 done:
788 kfree(array);
789 splice_from_pipe_end(pipe, &sd);
791 pipe_unlock(pipe);
793 if (sd.num_spliced)
794 ret = sd.num_spliced;
796 return ret;
799 EXPORT_SYMBOL(iter_file_splice_write);
801 static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
802 struct splice_desc *sd)
804 int ret;
805 void *data;
806 loff_t tmp = sd->pos;
808 data = kmap(buf->page);
809 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
810 kunmap(buf->page);
812 return ret;
815 static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
816 struct file *out, loff_t *ppos,
817 size_t len, unsigned int flags)
819 ssize_t ret;
821 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
822 if (ret > 0)
823 *ppos += ret;
825 return ret;
829 * generic_splice_sendpage - splice data from a pipe to a socket
830 * @pipe: pipe to splice from
831 * @out: socket to write to
832 * @ppos: position in @out
833 * @len: number of bytes to splice
834 * @flags: splice modifier flags
836 * Description:
837 * Will send @len bytes from the pipe to a network socket. No data copying
838 * is involved.
841 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
842 loff_t *ppos, size_t len, unsigned int flags)
844 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
847 EXPORT_SYMBOL(generic_splice_sendpage);
850 * Attempt to initiate a splice from pipe to file.
852 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
853 loff_t *ppos, size_t len, unsigned int flags)
855 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
856 loff_t *, size_t, unsigned int);
858 if (out->f_op->splice_write)
859 splice_write = out->f_op->splice_write;
860 else
861 splice_write = default_file_splice_write;
863 return splice_write(pipe, out, ppos, len, flags);
867 * Attempt to initiate a splice from a file to a pipe.
869 static long do_splice_to(struct file *in, loff_t *ppos,
870 struct pipe_inode_info *pipe, size_t len,
871 unsigned int flags)
873 ssize_t (*splice_read)(struct file *, loff_t *,
874 struct pipe_inode_info *, size_t, unsigned int);
875 int ret;
877 if (unlikely(!(in->f_mode & FMODE_READ)))
878 return -EBADF;
880 ret = rw_verify_area(READ, in, ppos, len);
881 if (unlikely(ret < 0))
882 return ret;
884 if (unlikely(len > MAX_RW_COUNT))
885 len = MAX_RW_COUNT;
887 if (in->f_op->splice_read)
888 splice_read = in->f_op->splice_read;
889 else
890 splice_read = default_file_splice_read;
892 return splice_read(in, ppos, pipe, len, flags);
896 * splice_direct_to_actor - splices data directly between two non-pipes
897 * @in: file to splice from
898 * @sd: actor information on where to splice to
899 * @actor: handles the data splicing
901 * Description:
902 * This is a special case helper to splice directly between two
903 * points, without requiring an explicit pipe. Internally an allocated
904 * pipe is cached in the process, and reused during the lifetime of
905 * that process.
908 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
909 splice_direct_actor *actor)
911 struct pipe_inode_info *pipe;
912 long ret, bytes;
913 umode_t i_mode;
914 size_t len;
915 int i, flags, more;
918 * We require the input being a regular file, as we don't want to
919 * randomly drop data for eg socket -> socket splicing. Use the
920 * piped splicing for that!
922 i_mode = file_inode(in)->i_mode;
923 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
924 return -EINVAL;
927 * neither in nor out is a pipe, setup an internal pipe attached to
928 * 'out' and transfer the wanted data from 'in' to 'out' through that
930 pipe = current->splice_pipe;
931 if (unlikely(!pipe)) {
932 pipe = alloc_pipe_info();
933 if (!pipe)
934 return -ENOMEM;
937 * We don't have an immediate reader, but we'll read the stuff
938 * out of the pipe right after the splice_to_pipe(). So set
939 * PIPE_READERS appropriately.
941 pipe->readers = 1;
943 current->splice_pipe = pipe;
947 * Do the splice.
949 ret = 0;
950 bytes = 0;
951 len = sd->total_len;
952 flags = sd->flags;
955 * Don't block on output, we have to drain the direct pipe.
957 sd->flags &= ~SPLICE_F_NONBLOCK;
958 more = sd->flags & SPLICE_F_MORE;
960 WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
962 while (len) {
963 unsigned int p_space;
964 size_t read_len;
965 loff_t pos = sd->pos, prev_pos = pos;
967 /* Don't try to read more the pipe has space for. */
968 p_space = pipe->max_usage -
969 pipe_occupancy(pipe->head, pipe->tail);
970 read_len = min_t(size_t, len, p_space << PAGE_SHIFT);
971 ret = do_splice_to(in, &pos, pipe, read_len, flags);
972 if (unlikely(ret <= 0))
973 goto out_release;
975 read_len = ret;
976 sd->total_len = read_len;
979 * If more data is pending, set SPLICE_F_MORE
980 * If this is the last data and SPLICE_F_MORE was not set
981 * initially, clears it.
983 if (read_len < len)
984 sd->flags |= SPLICE_F_MORE;
985 else if (!more)
986 sd->flags &= ~SPLICE_F_MORE;
988 * NOTE: nonblocking mode only applies to the input. We
989 * must not do the output in nonblocking mode as then we
990 * could get stuck data in the internal pipe:
992 ret = actor(pipe, sd);
993 if (unlikely(ret <= 0)) {
994 sd->pos = prev_pos;
995 goto out_release;
998 bytes += ret;
999 len -= ret;
1000 sd->pos = pos;
1002 if (ret < read_len) {
1003 sd->pos = prev_pos + ret;
1004 goto out_release;
1008 done:
1009 pipe->tail = pipe->head = 0;
1010 file_accessed(in);
1011 return bytes;
1013 out_release:
1015 * If we did an incomplete transfer we must release
1016 * the pipe buffers in question:
1018 for (i = 0; i < pipe->ring_size; i++) {
1019 struct pipe_buffer *buf = &pipe->bufs[i];
1021 if (buf->ops)
1022 pipe_buf_release(pipe, buf);
1025 if (!bytes)
1026 bytes = ret;
1028 goto done;
1030 EXPORT_SYMBOL(splice_direct_to_actor);
1032 static int direct_splice_actor(struct pipe_inode_info *pipe,
1033 struct splice_desc *sd)
1035 struct file *file = sd->u.file;
1037 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1038 sd->flags);
1042 * do_splice_direct - splices data directly between two files
1043 * @in: file to splice from
1044 * @ppos: input file offset
1045 * @out: file to splice to
1046 * @opos: output file offset
1047 * @len: number of bytes to splice
1048 * @flags: splice modifier flags
1050 * Description:
1051 * For use by do_sendfile(). splice can easily emulate sendfile, but
1052 * doing it in the application would incur an extra system call
1053 * (splice in + splice out, as compared to just sendfile()). So this helper
1054 * can splice directly through a process-private pipe.
1057 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1058 loff_t *opos, size_t len, unsigned int flags)
1060 struct splice_desc sd = {
1061 .len = len,
1062 .total_len = len,
1063 .flags = flags,
1064 .pos = *ppos,
1065 .u.file = out,
1066 .opos = opos,
1068 long ret;
1070 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1071 return -EBADF;
1073 if (unlikely(out->f_flags & O_APPEND))
1074 return -EINVAL;
1076 ret = rw_verify_area(WRITE, out, opos, len);
1077 if (unlikely(ret < 0))
1078 return ret;
1080 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1081 if (ret > 0)
1082 *ppos = sd.pos;
1084 return ret;
1086 EXPORT_SYMBOL(do_splice_direct);
1088 static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1090 for (;;) {
1091 if (unlikely(!pipe->readers)) {
1092 send_sig(SIGPIPE, current, 0);
1093 return -EPIPE;
1095 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1096 return 0;
1097 if (flags & SPLICE_F_NONBLOCK)
1098 return -EAGAIN;
1099 if (signal_pending(current))
1100 return -ERESTARTSYS;
1101 pipe->waiting_writers++;
1102 pipe_wait(pipe);
1103 pipe->waiting_writers--;
1107 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1108 struct pipe_inode_info *opipe,
1109 size_t len, unsigned int flags);
1112 * Determine where to splice to/from.
1114 static long do_splice(struct file *in, loff_t __user *off_in,
1115 struct file *out, loff_t __user *off_out,
1116 size_t len, unsigned int flags)
1118 struct pipe_inode_info *ipipe;
1119 struct pipe_inode_info *opipe;
1120 loff_t offset;
1121 long ret;
1123 ipipe = get_pipe_info(in);
1124 opipe = get_pipe_info(out);
1126 if (ipipe && opipe) {
1127 if (off_in || off_out)
1128 return -ESPIPE;
1130 if (!(in->f_mode & FMODE_READ))
1131 return -EBADF;
1133 if (!(out->f_mode & FMODE_WRITE))
1134 return -EBADF;
1136 /* Splicing to self would be fun, but... */
1137 if (ipipe == opipe)
1138 return -EINVAL;
1140 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1141 flags |= SPLICE_F_NONBLOCK;
1143 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1146 if (ipipe) {
1147 if (off_in)
1148 return -ESPIPE;
1149 if (off_out) {
1150 if (!(out->f_mode & FMODE_PWRITE))
1151 return -EINVAL;
1152 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1153 return -EFAULT;
1154 } else {
1155 offset = out->f_pos;
1158 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1159 return -EBADF;
1161 if (unlikely(out->f_flags & O_APPEND))
1162 return -EINVAL;
1164 ret = rw_verify_area(WRITE, out, &offset, len);
1165 if (unlikely(ret < 0))
1166 return ret;
1168 if (in->f_flags & O_NONBLOCK)
1169 flags |= SPLICE_F_NONBLOCK;
1171 file_start_write(out);
1172 ret = do_splice_from(ipipe, out, &offset, len, flags);
1173 file_end_write(out);
1175 if (!off_out)
1176 out->f_pos = offset;
1177 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1178 ret = -EFAULT;
1180 return ret;
1183 if (opipe) {
1184 if (off_out)
1185 return -ESPIPE;
1186 if (off_in) {
1187 if (!(in->f_mode & FMODE_PREAD))
1188 return -EINVAL;
1189 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1190 return -EFAULT;
1191 } else {
1192 offset = in->f_pos;
1195 if (out->f_flags & O_NONBLOCK)
1196 flags |= SPLICE_F_NONBLOCK;
1198 pipe_lock(opipe);
1199 ret = wait_for_space(opipe, flags);
1200 if (!ret) {
1201 unsigned int p_space;
1203 /* Don't try to read more the pipe has space for. */
1204 p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail);
1205 len = min_t(size_t, len, p_space << PAGE_SHIFT);
1207 ret = do_splice_to(in, &offset, opipe, len, flags);
1209 pipe_unlock(opipe);
1210 if (ret > 0)
1211 wakeup_pipe_readers(opipe);
1212 if (!off_in)
1213 in->f_pos = offset;
1214 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1215 ret = -EFAULT;
1217 return ret;
1220 return -EINVAL;
1223 static int iter_to_pipe(struct iov_iter *from,
1224 struct pipe_inode_info *pipe,
1225 unsigned flags)
1227 struct pipe_buffer buf = {
1228 .ops = &user_page_pipe_buf_ops,
1229 .flags = flags
1231 size_t total = 0;
1232 int ret = 0;
1233 bool failed = false;
1235 while (iov_iter_count(from) && !failed) {
1236 struct page *pages[16];
1237 ssize_t copied;
1238 size_t start;
1239 int n;
1241 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1242 if (copied <= 0) {
1243 ret = copied;
1244 break;
1247 for (n = 0; copied; n++, start = 0) {
1248 int size = min_t(int, copied, PAGE_SIZE - start);
1249 if (!failed) {
1250 buf.page = pages[n];
1251 buf.offset = start;
1252 buf.len = size;
1253 ret = add_to_pipe(pipe, &buf);
1254 if (unlikely(ret < 0)) {
1255 failed = true;
1256 } else {
1257 iov_iter_advance(from, ret);
1258 total += ret;
1260 } else {
1261 put_page(pages[n]);
1263 copied -= size;
1266 return total ? total : ret;
1269 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1270 struct splice_desc *sd)
1272 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1273 return n == sd->len ? n : -EFAULT;
1277 * For lack of a better implementation, implement vmsplice() to userspace
1278 * as a simple copy of the pipes pages to the user iov.
1280 static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
1281 unsigned int flags)
1283 struct pipe_inode_info *pipe = get_pipe_info(file);
1284 struct splice_desc sd = {
1285 .total_len = iov_iter_count(iter),
1286 .flags = flags,
1287 .u.data = iter
1289 long ret = 0;
1291 if (!pipe)
1292 return -EBADF;
1294 if (sd.total_len) {
1295 pipe_lock(pipe);
1296 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1297 pipe_unlock(pipe);
1300 return ret;
1304 * vmsplice splices a user address range into a pipe. It can be thought of
1305 * as splice-from-memory, where the regular splice is splice-from-file (or
1306 * to file). In both cases the output is a pipe, naturally.
1308 static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1309 unsigned int flags)
1311 struct pipe_inode_info *pipe;
1312 long ret = 0;
1313 unsigned buf_flag = 0;
1315 if (flags & SPLICE_F_GIFT)
1316 buf_flag = PIPE_BUF_FLAG_GIFT;
1318 pipe = get_pipe_info(file);
1319 if (!pipe)
1320 return -EBADF;
1322 pipe_lock(pipe);
1323 ret = wait_for_space(pipe, flags);
1324 if (!ret)
1325 ret = iter_to_pipe(iter, pipe, buf_flag);
1326 pipe_unlock(pipe);
1327 if (ret > 0)
1328 wakeup_pipe_readers(pipe);
1329 return ret;
1332 static int vmsplice_type(struct fd f, int *type)
1334 if (!f.file)
1335 return -EBADF;
1336 if (f.file->f_mode & FMODE_WRITE) {
1337 *type = WRITE;
1338 } else if (f.file->f_mode & FMODE_READ) {
1339 *type = READ;
1340 } else {
1341 fdput(f);
1342 return -EBADF;
1344 return 0;
1348 * Note that vmsplice only really supports true splicing _from_ user memory
1349 * to a pipe, not the other way around. Splicing from user memory is a simple
1350 * operation that can be supported without any funky alignment restrictions
1351 * or nasty vm tricks. We simply map in the user memory and fill them into
1352 * a pipe. The reverse isn't quite as easy, though. There are two possible
1353 * solutions for that:
1355 * - memcpy() the data internally, at which point we might as well just
1356 * do a regular read() on the buffer anyway.
1357 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1358 * has restriction limitations on both ends of the pipe).
1360 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1363 static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
1365 if (unlikely(flags & ~SPLICE_F_ALL))
1366 return -EINVAL;
1368 if (!iov_iter_count(iter))
1369 return 0;
1371 if (iov_iter_rw(iter) == WRITE)
1372 return vmsplice_to_pipe(f, iter, flags);
1373 else
1374 return vmsplice_to_user(f, iter, flags);
1377 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
1378 unsigned long, nr_segs, unsigned int, flags)
1380 struct iovec iovstack[UIO_FASTIOV];
1381 struct iovec *iov = iovstack;
1382 struct iov_iter iter;
1383 ssize_t error;
1384 struct fd f;
1385 int type;
1387 f = fdget(fd);
1388 error = vmsplice_type(f, &type);
1389 if (error)
1390 return error;
1392 error = import_iovec(type, uiov, nr_segs,
1393 ARRAY_SIZE(iovstack), &iov, &iter);
1394 if (error >= 0) {
1395 error = do_vmsplice(f.file, &iter, flags);
1396 kfree(iov);
1398 fdput(f);
1399 return error;
1402 #ifdef CONFIG_COMPAT
1403 COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1404 unsigned int, nr_segs, unsigned int, flags)
1406 struct iovec iovstack[UIO_FASTIOV];
1407 struct iovec *iov = iovstack;
1408 struct iov_iter iter;
1409 ssize_t error;
1410 struct fd f;
1411 int type;
1413 f = fdget(fd);
1414 error = vmsplice_type(f, &type);
1415 if (error)
1416 return error;
1418 error = compat_import_iovec(type, iov32, nr_segs,
1419 ARRAY_SIZE(iovstack), &iov, &iter);
1420 if (error >= 0) {
1421 error = do_vmsplice(f.file, &iter, flags);
1422 kfree(iov);
1424 fdput(f);
1425 return error;
1427 #endif
1429 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1430 int, fd_out, loff_t __user *, off_out,
1431 size_t, len, unsigned int, flags)
1433 struct fd in, out;
1434 long error;
1436 if (unlikely(!len))
1437 return 0;
1439 if (unlikely(flags & ~SPLICE_F_ALL))
1440 return -EINVAL;
1442 error = -EBADF;
1443 in = fdget(fd_in);
1444 if (in.file) {
1445 if (in.file->f_mode & FMODE_READ) {
1446 out = fdget(fd_out);
1447 if (out.file) {
1448 if (out.file->f_mode & FMODE_WRITE)
1449 error = do_splice(in.file, off_in,
1450 out.file, off_out,
1451 len, flags);
1452 fdput(out);
1455 fdput(in);
1457 return error;
1461 * Make sure there's data to read. Wait for input if we can, otherwise
1462 * return an appropriate error.
1464 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1466 int ret;
1469 * Check the pipe occupancy without the inode lock first. This function
1470 * is speculative anyways, so missing one is ok.
1472 if (!pipe_empty(pipe->head, pipe->tail))
1473 return 0;
1475 ret = 0;
1476 pipe_lock(pipe);
1478 while (pipe_empty(pipe->head, pipe->tail)) {
1479 if (signal_pending(current)) {
1480 ret = -ERESTARTSYS;
1481 break;
1483 if (!pipe->writers)
1484 break;
1485 if (!pipe->waiting_writers) {
1486 if (flags & SPLICE_F_NONBLOCK) {
1487 ret = -EAGAIN;
1488 break;
1491 pipe_wait(pipe);
1494 pipe_unlock(pipe);
1495 return ret;
1499 * Make sure there's writeable room. Wait for room if we can, otherwise
1500 * return an appropriate error.
1502 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1504 int ret;
1507 * Check pipe occupancy without the inode lock first. This function
1508 * is speculative anyways, so missing one is ok.
1510 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1511 return 0;
1513 ret = 0;
1514 pipe_lock(pipe);
1516 while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1517 if (!pipe->readers) {
1518 send_sig(SIGPIPE, current, 0);
1519 ret = -EPIPE;
1520 break;
1522 if (flags & SPLICE_F_NONBLOCK) {
1523 ret = -EAGAIN;
1524 break;
1526 if (signal_pending(current)) {
1527 ret = -ERESTARTSYS;
1528 break;
1530 pipe->waiting_writers++;
1531 pipe_wait(pipe);
1532 pipe->waiting_writers--;
1535 pipe_unlock(pipe);
1536 return ret;
1540 * Splice contents of ipipe to opipe.
1542 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1543 struct pipe_inode_info *opipe,
1544 size_t len, unsigned int flags)
1546 struct pipe_buffer *ibuf, *obuf;
1547 unsigned int i_head, o_head;
1548 unsigned int i_tail, o_tail;
1549 unsigned int i_mask, o_mask;
1550 int ret = 0;
1551 bool input_wakeup = false;
1554 retry:
1555 ret = ipipe_prep(ipipe, flags);
1556 if (ret)
1557 return ret;
1559 ret = opipe_prep(opipe, flags);
1560 if (ret)
1561 return ret;
1564 * Potential ABBA deadlock, work around it by ordering lock
1565 * grabbing by pipe info address. Otherwise two different processes
1566 * could deadlock (one doing tee from A -> B, the other from B -> A).
1568 pipe_double_lock(ipipe, opipe);
1570 i_tail = ipipe->tail;
1571 i_mask = ipipe->ring_size - 1;
1572 o_head = opipe->head;
1573 o_mask = opipe->ring_size - 1;
1575 do {
1576 size_t o_len;
1578 if (!opipe->readers) {
1579 send_sig(SIGPIPE, current, 0);
1580 if (!ret)
1581 ret = -EPIPE;
1582 break;
1585 i_head = ipipe->head;
1586 o_tail = opipe->tail;
1588 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
1589 break;
1592 * Cannot make any progress, because either the input
1593 * pipe is empty or the output pipe is full.
1595 if (pipe_empty(i_head, i_tail) ||
1596 pipe_full(o_head, o_tail, opipe->max_usage)) {
1597 /* Already processed some buffers, break */
1598 if (ret)
1599 break;
1601 if (flags & SPLICE_F_NONBLOCK) {
1602 ret = -EAGAIN;
1603 break;
1607 * We raced with another reader/writer and haven't
1608 * managed to process any buffers. A zero return
1609 * value means EOF, so retry instead.
1611 pipe_unlock(ipipe);
1612 pipe_unlock(opipe);
1613 goto retry;
1616 ibuf = &ipipe->bufs[i_tail & i_mask];
1617 obuf = &opipe->bufs[o_head & o_mask];
1619 if (len >= ibuf->len) {
1621 * Simply move the whole buffer from ipipe to opipe
1623 *obuf = *ibuf;
1624 ibuf->ops = NULL;
1625 i_tail++;
1626 ipipe->tail = i_tail;
1627 input_wakeup = true;
1628 o_len = obuf->len;
1629 o_head++;
1630 opipe->head = o_head;
1631 } else {
1633 * Get a reference to this pipe buffer,
1634 * so we can copy the contents over.
1636 if (!pipe_buf_get(ipipe, ibuf)) {
1637 if (ret == 0)
1638 ret = -EFAULT;
1639 break;
1641 *obuf = *ibuf;
1644 * Don't inherit the gift flag, we need to
1645 * prevent multiple steals of this page.
1647 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1649 pipe_buf_mark_unmergeable(obuf);
1651 obuf->len = len;
1652 ibuf->offset += len;
1653 ibuf->len -= len;
1654 o_len = len;
1655 o_head++;
1656 opipe->head = o_head;
1658 ret += o_len;
1659 len -= o_len;
1660 } while (len);
1662 pipe_unlock(ipipe);
1663 pipe_unlock(opipe);
1666 * If we put data in the output pipe, wakeup any potential readers.
1668 if (ret > 0)
1669 wakeup_pipe_readers(opipe);
1671 if (input_wakeup)
1672 wakeup_pipe_writers(ipipe);
1674 return ret;
1678 * Link contents of ipipe to opipe.
1680 static int link_pipe(struct pipe_inode_info *ipipe,
1681 struct pipe_inode_info *opipe,
1682 size_t len, unsigned int flags)
1684 struct pipe_buffer *ibuf, *obuf;
1685 unsigned int i_head, o_head;
1686 unsigned int i_tail, o_tail;
1687 unsigned int i_mask, o_mask;
1688 int ret = 0;
1691 * Potential ABBA deadlock, work around it by ordering lock
1692 * grabbing by pipe info address. Otherwise two different processes
1693 * could deadlock (one doing tee from A -> B, the other from B -> A).
1695 pipe_double_lock(ipipe, opipe);
1697 i_tail = ipipe->tail;
1698 i_mask = ipipe->ring_size - 1;
1699 o_head = opipe->head;
1700 o_mask = opipe->ring_size - 1;
1702 do {
1703 if (!opipe->readers) {
1704 send_sig(SIGPIPE, current, 0);
1705 if (!ret)
1706 ret = -EPIPE;
1707 break;
1710 i_head = ipipe->head;
1711 o_tail = opipe->tail;
1714 * If we have iterated all input buffers or run out of
1715 * output room, break.
1717 if (pipe_empty(i_head, i_tail) ||
1718 pipe_full(o_head, o_tail, opipe->max_usage))
1719 break;
1721 ibuf = &ipipe->bufs[i_tail & i_mask];
1722 obuf = &opipe->bufs[o_head & o_mask];
1725 * Get a reference to this pipe buffer,
1726 * so we can copy the contents over.
1728 if (!pipe_buf_get(ipipe, ibuf)) {
1729 if (ret == 0)
1730 ret = -EFAULT;
1731 break;
1734 *obuf = *ibuf;
1737 * Don't inherit the gift flag, we need to
1738 * prevent multiple steals of this page.
1740 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1742 pipe_buf_mark_unmergeable(obuf);
1744 if (obuf->len > len)
1745 obuf->len = len;
1746 ret += obuf->len;
1747 len -= obuf->len;
1749 o_head++;
1750 opipe->head = o_head;
1751 i_tail++;
1752 } while (len);
1755 * return EAGAIN if we have the potential of some data in the
1756 * future, otherwise just return 0
1758 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1759 ret = -EAGAIN;
1761 pipe_unlock(ipipe);
1762 pipe_unlock(opipe);
1765 * If we put data in the output pipe, wakeup any potential readers.
1767 if (ret > 0)
1768 wakeup_pipe_readers(opipe);
1770 return ret;
1774 * This is a tee(1) implementation that works on pipes. It doesn't copy
1775 * any data, it simply references the 'in' pages on the 'out' pipe.
1776 * The 'flags' used are the SPLICE_F_* variants, currently the only
1777 * applicable one is SPLICE_F_NONBLOCK.
1779 static long do_tee(struct file *in, struct file *out, size_t len,
1780 unsigned int flags)
1782 struct pipe_inode_info *ipipe = get_pipe_info(in);
1783 struct pipe_inode_info *opipe = get_pipe_info(out);
1784 int ret = -EINVAL;
1787 * Duplicate the contents of ipipe to opipe without actually
1788 * copying the data.
1790 if (ipipe && opipe && ipipe != opipe) {
1791 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1792 flags |= SPLICE_F_NONBLOCK;
1795 * Keep going, unless we encounter an error. The ipipe/opipe
1796 * ordering doesn't really matter.
1798 ret = ipipe_prep(ipipe, flags);
1799 if (!ret) {
1800 ret = opipe_prep(opipe, flags);
1801 if (!ret)
1802 ret = link_pipe(ipipe, opipe, len, flags);
1806 return ret;
1809 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1811 struct fd in;
1812 int error;
1814 if (unlikely(flags & ~SPLICE_F_ALL))
1815 return -EINVAL;
1817 if (unlikely(!len))
1818 return 0;
1820 error = -EBADF;
1821 in = fdget(fdin);
1822 if (in.file) {
1823 if (in.file->f_mode & FMODE_READ) {
1824 struct fd out = fdget(fdout);
1825 if (out.file) {
1826 if (out.file->f_mode & FMODE_WRITE)
1827 error = do_tee(in.file, out.file,
1828 len, flags);
1829 fdput(out);
1832 fdput(in);
1835 return error;