blob: c521090a04696f1d993661722cb0111a7f720214 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Jens Axboe5274f052006-03-30 15:15:30 +02002/*
3 * "splice": joining two ropes together by interweaving their strands.
4 *
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.
8 *
9 * The traditional unix read/write is extended with a "splice()" operation
10 * that transfers data buffers to or from a pipe buffer.
11 *
12 * Named by Larry McVoy, original implementation from Linus, extended by
Jens Axboec2058e02006-04-11 13:56:34 +020013 * Jens to support splicing to files, network, direct splicing, etc and
14 * fixing lots of bugs.
Jens Axboe5274f052006-03-30 15:15:30 +020015 *
Jens Axboe0fe23472006-09-04 15:41:16 +020016 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
Jens Axboec2058e02006-04-11 13:56:34 +020017 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
Jens Axboe5274f052006-03-30 15:15:30 +020019 *
20 */
Christoph Hellwigbe297962016-11-01 07:40:16 -060021#include <linux/bvec.h>
Jens Axboe5274f052006-03-30 15:15:30 +020022#include <linux/fs.h>
23#include <linux/file.h>
24#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020025#include <linux/splice.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080026#include <linux/memcontrol.h>
Jens Axboe5274f052006-03-30 15:15:30 +020027#include <linux/mm_inline.h>
Jens Axboe5abc97a2006-03-30 15:16:46 +020028#include <linux/swap.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020029#include <linux/writeback.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050030#include <linux/export.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020031#include <linux/syscalls.h>
Jens Axboe912d35f2006-04-26 10:59:21 +020032#include <linux/uio.h>
James Morris29ce2052007-07-13 11:44:32 +020033#include <linux/security.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Eric Dumazet35f9c092012-04-05 03:05:35 +000035#include <linux/socket.h>
Al Viro76b021d2013-03-02 10:19:56 -050036#include <linux/compat.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010037#include <linux/sched/signal.h>
38
Al Viro06ae43f2013-03-20 13:19:30 -040039#include "internal.h"
Jens Axboe5274f052006-03-30 15:15:30 +020040
Jens Axboe83f91352006-04-02 23:05:09 +020041/*
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.
46 */
Jens Axboe76ad4d12006-05-03 10:41:33 +020047static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
Jens Axboe5abc97a2006-03-30 15:16:46 +020048 struct pipe_buffer *buf)
49{
50 struct page *page = buf->page;
Jens Axboe9e94cd42006-06-20 15:01:12 +020051 struct address_space *mapping;
Jens Axboe5abc97a2006-03-30 15:16:46 +020052
Jens Axboe9e0267c2006-04-19 15:57:31 +020053 lock_page(page);
54
Jens Axboe9e94cd42006-06-20 15:01:12 +020055 mapping = page_mapping(page);
56 if (mapping) {
57 WARN_ON(!PageUptodate(page));
Jens Axboe5abc97a2006-03-30 15:16:46 +020058
Jens Axboe9e94cd42006-06-20 15:01:12 +020059 /*
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.
66 */
67 wait_on_page_writeback(page);
Jens Axboead8d6f02006-04-02 23:10:32 +020068
David Howells266cf652009-04-03 16:42:36 +010069 if (page_has_private(page) &&
70 !try_to_release_page(page, GFP_KERNEL))
Jens Axboeca39d652008-05-20 21:27:41 +020071 goto out_unlock;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020072
Jens Axboe9e94cd42006-06-20 15:01:12 +020073 /*
74 * If we succeeded in removing the mapping, set LRU flag
75 * and return good.
76 */
77 if (remove_mapping(mapping, page)) {
78 buf->flags |= PIPE_BUF_FLAG_LRU;
79 return 0;
80 }
Jens Axboe9e0267c2006-04-19 15:57:31 +020081 }
Jens Axboe5abc97a2006-03-30 15:16:46 +020082
Jens Axboe9e94cd42006-06-20 15:01:12 +020083 /*
84 * Raced with truncate or failed to remove page from current
85 * address space, unlock and return failure.
86 */
Jens Axboeca39d652008-05-20 21:27:41 +020087out_unlock:
Jens Axboe9e94cd42006-06-20 15:01:12 +020088 unlock_page(page);
89 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +020090}
91
Jens Axboe76ad4d12006-05-03 10:41:33 +020092static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +020093 struct pipe_buffer *buf)
94{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030095 put_page(buf->page);
Jens Axboe14328732006-05-03 10:35:26 +020096 buf->flags &= ~PIPE_BUF_FLAG_LRU;
Jens Axboe5274f052006-03-30 15:15:30 +020097}
98
Jens Axboe08457182007-06-12 20:51:32 +020099/*
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.
102 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200103static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
104 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +0200105{
106 struct page *page = buf->page;
Jens Axboe49d0b212006-04-10 09:04:41 +0200107 int err;
Jens Axboe5274f052006-03-30 15:15:30 +0200108
109 if (!PageUptodate(page)) {
Jens Axboe49d0b212006-04-10 09:04:41 +0200110 lock_page(page);
111
112 /*
113 * Page got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +0200114 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200115 */
116 if (!page->mapping) {
117 err = -ENODATA;
118 goto error;
119 }
120
121 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200122 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200123 */
124 if (!PageUptodate(page)) {
125 err = -EIO;
126 goto error;
127 }
128
129 /*
Jens Axboef84d7512006-05-01 19:59:03 +0200130 * Page is ok afterall, we are done.
Jens Axboe49d0b212006-04-10 09:04:41 +0200131 */
Jens Axboe5274f052006-03-30 15:15:30 +0200132 unlock_page(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200133 }
134
Jens Axboef84d7512006-05-01 19:59:03 +0200135 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200136error:
137 unlock_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200138 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200139}
140
Hugh Dickins708e3502011-07-25 17:12:32 -0700141const struct pipe_buf_operations page_cache_pipe_buf_ops = {
Jens Axboecac36bb02007-06-14 13:10:48 +0200142 .confirm = page_cache_pipe_buf_confirm,
Jens Axboe5274f052006-03-30 15:15:30 +0200143 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200144 .steal = page_cache_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200145 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200146};
147
Jens Axboe912d35f2006-04-26 10:59:21 +0200148static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
149 struct pipe_buffer *buf)
150{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200151 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
152 return 1;
153
Jens Axboe14328732006-05-03 10:35:26 +0200154 buf->flags |= PIPE_BUF_FLAG_LRU;
Jens Axboe330ab712006-05-02 15:29:57 +0200155 return generic_pipe_buf_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200156}
157
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800158static const struct pipe_buf_operations user_page_pipe_buf_ops = {
Jens Axboecac36bb02007-06-14 13:10:48 +0200159 .confirm = generic_pipe_buf_confirm,
Jens Axboe912d35f2006-04-26 10:59:21 +0200160 .release = page_cache_pipe_buf_release,
161 .steal = user_page_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200162 .get = generic_pipe_buf_get,
Jens Axboe912d35f2006-04-26 10:59:21 +0200163};
164
Namhyung Kim825cdcb2011-05-23 19:58:53 +0200165static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
166{
167 smp_mb();
168 if (waitqueue_active(&pipe->wait))
169 wake_up_interruptible(&pipe->wait);
170 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
171}
172
Jens Axboe932cc6d2007-06-21 13:10:21 +0200173/**
174 * splice_to_pipe - fill passed data into a pipe
175 * @pipe: pipe to fill
176 * @spd: data to fill
177 *
178 * Description:
Randy Dunlap79685b82007-07-27 08:08:51 +0200179 * @spd contains a map of pages and len/offset tuples, along with
Jens Axboe932cc6d2007-06-21 13:10:21 +0200180 * the struct pipe_buf_operations associated with these pages. This
181 * function will link that data to the pipe.
182 *
Jens Axboe83f91352006-04-02 23:05:09 +0200183 */
Jens Axboed6b29d72007-06-04 09:59:47 +0200184ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
185 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200186{
Jens Axboe00de00b2007-06-15 13:14:22 +0200187 unsigned int spd_pages = spd->nr_pages;
David Howells8cefc102019-11-15 13:30:32 +0000188 unsigned int tail = pipe->tail;
189 unsigned int head = pipe->head;
190 unsigned int mask = pipe->ring_size - 1;
Al Viro8924fef2016-09-17 20:44:45 -0400191 int ret = 0, page_nr = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200192
Rabin Vincentd6785d92016-03-10 21:19:06 +0100193 if (!spd_pages)
194 return 0;
195
Al Viro8924fef2016-09-17 20:44:45 -0400196 if (unlikely(!pipe->readers)) {
197 send_sig(SIGPIPE, current, 0);
198 ret = -EPIPE;
199 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200200 }
201
David Howells6718b6f2019-10-16 16:47:32 +0100202 while (!pipe_full(head, tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000203 struct pipe_buffer *buf = &pipe->bufs[head & mask];
Jens Axboe5274f052006-03-30 15:15:30 +0200204
Al Viro8924fef2016-09-17 20:44:45 -0400205 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;
Miklos Szeredi5a81e6a2017-02-16 17:49:02 +0100210 buf->flags = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200211
David Howells8cefc102019-11-15 13:30:32 +0000212 head++;
213 pipe->head = head;
Al Viro8924fef2016-09-17 20:44:45 -0400214 page_nr++;
215 ret += buf->len;
216
217 if (!--spd->nr_pages)
218 break;
219 }
220
221 if (!ret)
222 ret = -EAGAIN;
223
224out:
Jens Axboe00de00b2007-06-15 13:14:22 +0200225 while (page_nr < spd_pages)
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800226 spd->spd_release(spd, page_nr++);
Jens Axboe5274f052006-03-30 15:15:30 +0200227
228 return ret;
229}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200230EXPORT_SYMBOL_GPL(splice_to_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200231
Al Viro79fddc42016-09-17 22:38:20 -0400232ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
233{
David Howells8cefc102019-11-15 13:30:32 +0000234 unsigned int head = pipe->head;
235 unsigned int tail = pipe->tail;
236 unsigned int mask = pipe->ring_size - 1;
Al Viro79fddc42016-09-17 22:38:20 -0400237 int ret;
238
239 if (unlikely(!pipe->readers)) {
240 send_sig(SIGPIPE, current, 0);
241 ret = -EPIPE;
David Howells6718b6f2019-10-16 16:47:32 +0100242 } else if (pipe_full(head, tail, pipe->max_usage)) {
Al Viro79fddc42016-09-17 22:38:20 -0400243 ret = -EAGAIN;
244 } else {
David Howells8cefc102019-11-15 13:30:32 +0000245 pipe->bufs[head & mask] = *buf;
246 pipe->head = head + 1;
Al Viro79fddc42016-09-17 22:38:20 -0400247 return buf->len;
248 }
Miklos Szeredia7796382016-09-27 10:45:12 +0200249 pipe_buf_release(pipe, buf);
Al Viro79fddc42016-09-17 22:38:20 -0400250 return ret;
251}
252EXPORT_SYMBOL(add_to_pipe);
253
Jens Axboe35f3d142010-05-20 10:43:18 +0200254/*
255 * Check if we need to grow the arrays holding pages and partial page
256 * descriptions.
257 */
Eric Dumazet047fe362012-06-12 15:24:40 +0200258int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200259{
David Howells6718b6f2019-10-16 16:47:32 +0100260 unsigned int max_usage = READ_ONCE(pipe->max_usage);
Eric Dumazet047fe362012-06-12 15:24:40 +0200261
David Howells8cefc102019-11-15 13:30:32 +0000262 spd->nr_pages_max = max_usage;
263 if (max_usage <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200264 return 0;
265
David Howells8cefc102019-11-15 13:30:32 +0000266 spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
267 spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
Kees Cook6da2ec52018-06-12 13:55:00 -0700268 GFP_KERNEL);
Jens Axboe35f3d142010-05-20 10:43:18 +0200269
270 if (spd->pages && spd->partial)
271 return 0;
272
273 kfree(spd->pages);
274 kfree(spd->partial);
275 return -ENOMEM;
276}
277
Eric Dumazet047fe362012-06-12 15:24:40 +0200278void splice_shrink_spd(struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200279{
Eric Dumazet047fe362012-06-12 15:24:40 +0200280 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200281 return;
282
283 kfree(spd->pages);
284 kfree(spd->partial);
285}
286
Jens Axboe83f91352006-04-02 23:05:09 +0200287/**
288 * generic_file_splice_read - splice data from file to a pipe
289 * @in: file to splice from
Jens Axboe932cc6d2007-06-21 13:10:21 +0200290 * @ppos: position in @in
Jens Axboe83f91352006-04-02 23:05:09 +0200291 * @pipe: pipe to splice to
292 * @len: number of bytes to splice
293 * @flags: splice modifier flags
294 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200295 * Description:
296 * Will read pages from given file and fill them into a pipe. Can be
Al Viro82c156f2016-09-22 23:35:42 -0400297 * used as long as it has more or less sane ->read_iter().
Jens Axboe932cc6d2007-06-21 13:10:21 +0200298 *
Jens Axboe83f91352006-04-02 23:05:09 +0200299 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200300ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
301 struct pipe_inode_info *pipe, size_t len,
302 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200303{
Al Viro82c156f2016-09-22 23:35:42 -0400304 struct iov_iter to;
305 struct kiocb kiocb;
David Howells8cefc102019-11-15 13:30:32 +0000306 unsigned int i_head;
307 int ret;
Boaz Harroshbe64f882015-04-15 16:15:17 -0700308
David Howellsaa563d72018-10-20 00:57:56 +0100309 iov_iter_pipe(&to, READ, pipe, len);
David Howells8cefc102019-11-15 13:30:32 +0000310 i_head = to.head;
Al Viro82c156f2016-09-22 23:35:42 -0400311 init_sync_kiocb(&kiocb, in);
312 kiocb.ki_pos = *ppos;
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100313 ret = call_read_iter(in, &kiocb, &to);
Miklos Szeredi723590e2009-08-15 08:43:22 +0200314 if (ret > 0) {
Al Viro82c156f2016-09-22 23:35:42 -0400315 *ppos = kiocb.ki_pos;
Miklos Szeredi723590e2009-08-15 08:43:22 +0200316 file_accessed(in);
Al Viro82c156f2016-09-22 23:35:42 -0400317 } else if (ret < 0) {
David Howells8cefc102019-11-15 13:30:32 +0000318 to.head = i_head;
Al Viroc3a69022016-10-10 13:26:27 -0400319 to.iov_offset = 0;
320 iov_iter_advance(&to, 0); /* to free what was emitted */
Al Viro82c156f2016-09-22 23:35:42 -0400321 /*
322 * callers of ->splice_read() expect -EAGAIN on
323 * "can't put anything in there", rather than -EFAULT.
324 */
325 if (ret == -EFAULT)
326 ret = -EAGAIN;
Miklos Szeredi723590e2009-08-15 08:43:22 +0200327 }
Jens Axboe5274f052006-03-30 15:15:30 +0200328
329 return ret;
330}
Jens Axboe059a8f32006-04-02 23:06:05 +0200331EXPORT_SYMBOL(generic_file_splice_read);
332
Al Viro241699c2016-09-22 16:33:12 -0400333const struct pipe_buf_operations default_pipe_buf_ops = {
Miklos Szeredi68181732009-05-07 15:37:36 +0200334 .confirm = generic_pipe_buf_confirm,
335 .release = generic_pipe_buf_release,
336 .steal = generic_pipe_buf_steal,
337 .get = generic_pipe_buf_get,
338};
339
Jann Hornb9872222019-04-04 23:59:25 +0200340int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
341 struct pipe_buffer *buf)
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100342{
343 return 1;
344}
345
346/* Pipe buffer operations for a socket and similar. */
347const struct pipe_buf_operations nosteal_pipe_buf_ops = {
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100348 .confirm = generic_pipe_buf_confirm,
349 .release = generic_pipe_buf_release,
350 .steal = generic_pipe_buf_nosteal,
351 .get = generic_pipe_buf_get,
352};
353EXPORT_SYMBOL(nosteal_pipe_buf_ops);
354
Al Viro523ac9a2016-09-23 15:34:57 -0400355static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
Miklos Szeredi68181732009-05-07 15:37:36 +0200356 unsigned long vlen, loff_t offset)
357{
358 mm_segment_t old_fs;
359 loff_t pos = offset;
360 ssize_t res;
361
362 old_fs = get_fs();
Linus Torvalds736706b2019-03-04 10:39:05 -0800363 set_fs(KERNEL_DS);
Miklos Szeredi68181732009-05-07 15:37:36 +0200364 /* The cast to a user pointer is valid due to the set_fs() */
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100365 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
Miklos Szeredi68181732009-05-07 15:37:36 +0200366 set_fs(old_fs);
367
368 return res;
369}
370
Al Viro82c156f2016-09-22 23:35:42 -0400371static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
Miklos Szeredi68181732009-05-07 15:37:36 +0200372 struct pipe_inode_info *pipe, size_t len,
373 unsigned int flags)
374{
Al Viro523ac9a2016-09-23 15:34:57 -0400375 struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
376 struct iov_iter to;
377 struct page **pages;
Miklos Szeredi68181732009-05-07 15:37:36 +0200378 unsigned int nr_pages;
David Howells8cefc102019-11-15 13:30:32 +0000379 unsigned int mask;
Al Viro13c0f522016-12-10 13:20:53 -0500380 size_t offset, base, copied = 0;
Miklos Szeredi68181732009-05-07 15:37:36 +0200381 ssize_t res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200382 int i;
Miklos Szeredi68181732009-05-07 15:37:36 +0200383
David Howells6718b6f2019-10-16 16:47:32 +0100384 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
Al Viro523ac9a2016-09-23 15:34:57 -0400385 return -EAGAIN;
386
387 /*
388 * Try to keep page boundaries matching to source pagecache ones -
389 * it probably won't be much help, but...
390 */
391 offset = *ppos & ~PAGE_MASK;
392
David Howellsaa563d72018-10-20 00:57:56 +0100393 iov_iter_pipe(&to, READ, pipe, len + offset);
Al Viro523ac9a2016-09-23 15:34:57 -0400394
Al Viro13c0f522016-12-10 13:20:53 -0500395 res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
Al Viro523ac9a2016-09-23 15:34:57 -0400396 if (res <= 0)
Jens Axboe35f3d142010-05-20 10:43:18 +0200397 return -ENOMEM;
398
Al Viro13c0f522016-12-10 13:20:53 -0500399 nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
Al Viro523ac9a2016-09-23 15:34:57 -0400400
Jens Axboe35f3d142010-05-20 10:43:18 +0200401 vec = __vec;
Al Viro523ac9a2016-09-23 15:34:57 -0400402 if (nr_pages > PIPE_DEF_BUFFERS) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700403 vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
Al Viro523ac9a2016-09-23 15:34:57 -0400404 if (unlikely(!vec)) {
405 res = -ENOMEM;
406 goto out;
407 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200408 }
409
David Howells8cefc102019-11-15 13:30:32 +0000410 mask = pipe->ring_size - 1;
411 pipe->bufs[to.head & mask].offset = offset;
412 pipe->bufs[to.head & mask].len -= offset;
Miklos Szeredi68181732009-05-07 15:37:36 +0200413
Al Viro523ac9a2016-09-23 15:34:57 -0400414 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;
Miklos Szeredi68181732009-05-07 15:37:36 +0200417 vec[i].iov_len = this_len;
Miklos Szeredi68181732009-05-07 15:37:36 +0200418 len -= this_len;
419 offset = 0;
420 }
421
Al Viro523ac9a2016-09-23 15:34:57 -0400422 res = kernel_readv(in, vec, nr_pages, *ppos);
423 if (res > 0) {
424 copied = res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200425 *ppos += res;
Al Viro523ac9a2016-09-23 15:34:57 -0400426 }
Miklos Szeredi68181732009-05-07 15:37:36 +0200427
Jens Axboe35f3d142010-05-20 10:43:18 +0200428 if (vec != __vec)
429 kfree(vec);
Al Viro523ac9a2016-09-23 15:34:57 -0400430out:
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 */
Miklos Szeredi68181732009-05-07 15:37:36 +0200435 return res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200436}
Miklos Szeredi68181732009-05-07 15:37:36 +0200437
Jens Axboe5274f052006-03-30 15:15:30 +0200438/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200439 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200440 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200441 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200442static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200443 struct pipe_buffer *buf, struct splice_desc *sd)
444{
Jens Axboe6a14b902007-06-14 13:08:55 +0200445 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200446 loff_t pos = sd->pos;
Michał Mirosława8adbe32010-12-17 08:56:44 +0100447 int more;
Jens Axboe5274f052006-03-30 15:15:30 +0200448
Al Viro72c2d532013-09-22 16:27:52 -0400449 if (!likely(file->f_op->sendpage))
Michał Mirosława8adbe32010-12-17 08:56:44 +0100450 return -EINVAL;
Jens Axboe5274f052006-03-30 15:15:30 +0200451
Eric Dumazet35f9c092012-04-05 03:05:35 +0000452 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000453
David Howells8cefc102019-11-15 13:30:32 +0000454 if (sd->len < sd->total_len &&
455 pipe_occupancy(pipe->head, pipe->tail) > 1)
Eric Dumazet35f9c092012-04-05 03:05:35 +0000456 more |= MSG_SENDPAGE_NOTLAST;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000457
Michał Mirosława8adbe32010-12-17 08:56:44 +0100458 return file->f_op->sendpage(file, buf->page, buf->offset,
459 sd->len, &pos, more);
Jens Axboe5274f052006-03-30 15:15:30 +0200460}
461
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200462static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
463{
464 smp_mb();
465 if (waitqueue_active(&pipe->wait))
466 wake_up_interruptible(&pipe->wait);
467 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
468}
469
470/**
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
475 *
476 * Description:
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200477 * 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.
484 *
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.
489 */
Al Viro96f9bc82014-04-05 04:35:49 -0400490static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200491 splice_actor *actor)
492{
David Howells8cefc102019-11-15 13:30:32 +0000493 unsigned int head = pipe->head;
494 unsigned int tail = pipe->tail;
495 unsigned int mask = pipe->ring_size - 1;
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200496 int ret;
497
David Howells8cefc102019-11-15 13:30:32 +0000498 while (!pipe_empty(tail, head)) {
499 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200500
501 sd->len = buf->len;
502 if (sd->len > sd->total_len)
503 sd->len = sd->total_len;
504
Miklos Szeredifba597d2016-09-27 10:45:12 +0200505 ret = pipe_buf_confirm(pipe, buf);
Michał Mirosława8adbe32010-12-17 08:56:44 +0100506 if (unlikely(ret)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200507 if (ret == -ENODATA)
508 ret = 0;
509 return ret;
510 }
Michał Mirosława8adbe32010-12-17 08:56:44 +0100511
512 ret = actor(pipe, buf, sd);
513 if (ret <= 0)
514 return ret;
515
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200516 buf->offset += ret;
517 buf->len -= ret;
518
519 sd->num_spliced += ret;
520 sd->len -= ret;
521 sd->pos += ret;
522 sd->total_len -= ret;
523
524 if (!buf->len) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200525 pipe_buf_release(pipe, buf);
David Howells8cefc102019-11-15 13:30:32 +0000526 tail++;
527 pipe->tail = tail;
Al Viro6447a3c2013-03-21 11:01:38 -0400528 if (pipe->files)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200529 sd->need_wakeup = true;
530 }
531
532 if (!sd->total_len)
533 return 0;
534 }
535
536 return 1;
537}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200538
539/**
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
543 *
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.
548 */
Al Viro96f9bc82014-04-05 04:35:49 -0400549static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200550{
Jan Karac725bfc2015-11-23 13:09:50 +0100551 /*
552 * Check for signal early to make process killable when there are
553 * always buffers available
554 */
555 if (signal_pending(current))
556 return -ERESTARTSYS;
557
David Howells8cefc102019-11-15 13:30:32 +0000558 while (pipe_empty(pipe->head, pipe->tail)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200559 if (!pipe->writers)
560 return 0;
561
562 if (!pipe->waiting_writers && sd->num_spliced)
563 return 0;
564
565 if (sd->flags & SPLICE_F_NONBLOCK)
566 return -EAGAIN;
567
568 if (signal_pending(current))
569 return -ERESTARTSYS;
570
571 if (sd->need_wakeup) {
572 wakeup_pipe_writers(pipe);
573 sd->need_wakeup = false;
574 }
575
576 pipe_wait(pipe);
577 }
578
579 return 1;
580}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200581
582/**
583 * splice_from_pipe_begin - start splicing from pipe
Randy Dunlapb80901b2009-04-16 19:09:55 -0700584 * @sd: information about the splice operation
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200585 *
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.
590 */
Al Viro96f9bc82014-04-05 04:35:49 -0400591static void splice_from_pipe_begin(struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200592{
593 sd->num_spliced = 0;
594 sd->need_wakeup = false;
595}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200596
597/**
598 * splice_from_pipe_end - finish splicing from pipe
599 * @pipe: pipe to splice from
600 * @sd: information about the splice operation
601 *
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().
606 */
Al Viro96f9bc82014-04-05 04:35:49 -0400607static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200608{
609 if (sd->need_wakeup)
610 wakeup_pipe_writers(pipe);
611}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200612
Jens Axboe932cc6d2007-06-21 13:10:21 +0200613/**
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
618 *
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.
624 *
Jens Axboe83f91352006-04-02 23:05:09 +0200625 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200626ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
627 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200628{
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200629 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200630
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200631 splice_from_pipe_begin(sd);
632 do {
Jan Karac2489e02015-11-23 13:09:51 +0100633 cond_resched();
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200634 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);
Jens Axboe5274f052006-03-30 15:15:30 +0200639
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200640 return sd->num_spliced ? sd->num_spliced : ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200641}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100642EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200643
Jens Axboe932cc6d2007-06-21 13:10:21 +0200644/**
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
652 *
653 * Description:
Miklos Szeredi29339702009-04-14 19:48:37 +0200654 * See __splice_from_pipe. This function locks the pipe inode,
Jens Axboe932cc6d2007-06-21 13:10:21 +0200655 * otherwise it's identical to __splice_from_pipe().
656 *
657 */
Mark Fasheh6da61802006-10-17 18:43:07 +0200658ssize_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)
661{
662 ssize_t ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200663 struct splice_desc sd = {
664 .total_len = len,
665 .flags = flags,
666 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200667 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200668 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200669
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200670 pipe_lock(pipe);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200671 ret = __splice_from_pipe(pipe, &sd, actor);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200672 pipe_unlock(pipe);
Mark Fasheh6da61802006-10-17 18:43:07 +0200673
674 return ret;
675}
676
677/**
Al Viro8d020762014-04-05 04:27:08 -0400678 * 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
684 *
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.
689 *
690 */
691ssize_t
692iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
693 loff_t *ppos, size_t len, unsigned int flags)
694{
695 struct splice_desc sd = {
696 .total_len = len,
697 .flags = flags,
698 .pos = *ppos,
699 .u.file = out,
700 };
David Howells6718b6f2019-10-16 16:47:32 +0100701 int nbufs = pipe->max_usage;
Al Viro8d020762014-04-05 04:27:08 -0400702 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
703 GFP_KERNEL);
704 ssize_t ret;
705
706 if (unlikely(!array))
707 return -ENOMEM;
708
709 pipe_lock(pipe);
710
711 splice_from_pipe_begin(&sd);
712 while (sd.total_len) {
713 struct iov_iter from;
David Howells8cefc102019-11-15 13:30:32 +0000714 unsigned int head = pipe->head;
715 unsigned int tail = pipe->tail;
716 unsigned int mask = pipe->ring_size - 1;
Al Viro8d020762014-04-05 04:27:08 -0400717 size_t left;
David Howells8cefc102019-11-15 13:30:32 +0000718 int n;
Al Viro8d020762014-04-05 04:27:08 -0400719
720 ret = splice_from_pipe_next(pipe, &sd);
721 if (ret <= 0)
722 break;
723
David Howells6718b6f2019-10-16 16:47:32 +0100724 if (unlikely(nbufs < pipe->max_usage)) {
Al Viro8d020762014-04-05 04:27:08 -0400725 kfree(array);
David Howells6718b6f2019-10-16 16:47:32 +0100726 nbufs = pipe->max_usage;
Al Viro8d020762014-04-05 04:27:08 -0400727 array = kcalloc(nbufs, sizeof(struct bio_vec),
728 GFP_KERNEL);
729 if (!array) {
730 ret = -ENOMEM;
731 break;
732 }
733 }
734
735 /* build the vector */
736 left = sd.total_len;
David Howells8cefc102019-11-15 13:30:32 +0000737 for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
738 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Al Viro8d020762014-04-05 04:27:08 -0400739 size_t this_len = buf->len;
740
741 if (this_len > left)
742 this_len = left;
743
Miklos Szeredifba597d2016-09-27 10:45:12 +0200744 ret = pipe_buf_confirm(pipe, buf);
Al Viro8d020762014-04-05 04:27:08 -0400745 if (unlikely(ret)) {
746 if (ret == -ENODATA)
747 ret = 0;
748 goto done;
749 }
750
751 array[n].bv_page = buf->page;
752 array[n].bv_len = this_len;
753 array[n].bv_offset = buf->offset;
754 left -= this_len;
755 }
756
David Howellsaa563d72018-10-20 00:57:56 +0100757 iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300758 ret = vfs_iter_write(out, &from, &sd.pos, 0);
Al Viro8d020762014-04-05 04:27:08 -0400759 if (ret <= 0)
760 break;
761
762 sd.num_spliced += ret;
763 sd.total_len -= ret;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100764 *ppos = sd.pos;
Al Viro8d020762014-04-05 04:27:08 -0400765
766 /* dismiss the fully eaten buffers, adjust the partial one */
David Howells8cefc102019-11-15 13:30:32 +0000767 tail = pipe->tail;
Al Viro8d020762014-04-05 04:27:08 -0400768 while (ret) {
David Howells8cefc102019-11-15 13:30:32 +0000769 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Al Viro8d020762014-04-05 04:27:08 -0400770 if (ret >= buf->len) {
Al Viro8d020762014-04-05 04:27:08 -0400771 ret -= buf->len;
772 buf->len = 0;
Miklos Szeredia7796382016-09-27 10:45:12 +0200773 pipe_buf_release(pipe, buf);
David Howells8cefc102019-11-15 13:30:32 +0000774 tail++;
775 pipe->tail = tail;
Al Viro8d020762014-04-05 04:27:08 -0400776 if (pipe->files)
777 sd.need_wakeup = true;
778 } else {
779 buf->offset += ret;
780 buf->len -= ret;
781 ret = 0;
782 }
783 }
784 }
785done:
786 kfree(array);
787 splice_from_pipe_end(pipe, &sd);
788
789 pipe_unlock(pipe);
790
791 if (sd.num_spliced)
792 ret = sd.num_spliced;
793
794 return ret;
795}
796
797EXPORT_SYMBOL(iter_file_splice_write);
798
Miklos Szeredib2858d72009-05-19 11:37:46 +0200799static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
800 struct splice_desc *sd)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200801{
Miklos Szeredib2858d72009-05-19 11:37:46 +0200802 int ret;
803 void *data;
Al Viro06ae43f2013-03-20 13:19:30 -0400804 loff_t tmp = sd->pos;
Miklos Szeredib2858d72009-05-19 11:37:46 +0200805
Al Virofbb32752014-02-02 21:09:54 -0500806 data = kmap(buf->page);
Al Viro06ae43f2013-03-20 13:19:30 -0400807 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
Al Virofbb32752014-02-02 21:09:54 -0500808 kunmap(buf->page);
Miklos Szeredib2858d72009-05-19 11:37:46 +0200809
810 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200811}
812
813static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
814 struct file *out, loff_t *ppos,
815 size_t len, unsigned int flags)
816{
Miklos Szeredib2858d72009-05-19 11:37:46 +0200817 ssize_t ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200818
Miklos Szeredib2858d72009-05-19 11:37:46 +0200819 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
820 if (ret > 0)
821 *ppos += ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200822
Miklos Szeredib2858d72009-05-19 11:37:46 +0200823 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200824}
825
Jens Axboe83f91352006-04-02 23:05:09 +0200826/**
827 * generic_splice_sendpage - splice data from a pipe to a socket
Jens Axboe932cc6d2007-06-21 13:10:21 +0200828 * @pipe: pipe to splice from
Jens Axboe83f91352006-04-02 23:05:09 +0200829 * @out: socket to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +0200830 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +0200831 * @len: number of bytes to splice
832 * @flags: splice modifier flags
833 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200834 * Description:
835 * Will send @len bytes from the pipe to a network socket. No data copying
836 * is involved.
Jens Axboe83f91352006-04-02 23:05:09 +0200837 *
838 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200839ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200840 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200841{
Jens Axboe00522fb2006-04-26 14:39:29 +0200842 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200843}
844
Jens Axboe059a8f32006-04-02 23:06:05 +0200845EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500846
Jens Axboe83f91352006-04-02 23:05:09 +0200847/*
848 * Attempt to initiate a splice from pipe to file.
849 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200850static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200851 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200852{
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200853 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
854 loff_t *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +0200855
Al Viro72c2d532013-09-22 16:27:52 -0400856 if (out->f_op->splice_write)
Changli Gaocc56f7d2009-11-04 09:09:52 +0100857 splice_write = out->f_op->splice_write;
858 else
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200859 splice_write = default_file_splice_write;
860
Al Viro500368f2013-05-23 20:07:11 -0400861 return splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200862}
863
Jens Axboe83f91352006-04-02 23:05:09 +0200864/*
865 * Attempt to initiate a splice from a file to a pipe.
866 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200867static long do_splice_to(struct file *in, loff_t *ppos,
868 struct pipe_inode_info *pipe, size_t len,
869 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200870{
Miklos Szeredi68181732009-05-07 15:37:36 +0200871 ssize_t (*splice_read)(struct file *, loff_t *,
872 struct pipe_inode_info *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +0200873 int ret;
874
Jens Axboe49570e92006-04-11 13:56:09 +0200875 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200876 return -EBADF;
877
Jens Axboecbb7e572006-04-11 14:57:50 +0200878 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200879 if (unlikely(ret < 0))
880 return ret;
881
Al Viro03cc0782016-04-02 14:56:58 -0400882 if (unlikely(len > MAX_RW_COUNT))
883 len = MAX_RW_COUNT;
884
Al Viro72c2d532013-09-22 16:27:52 -0400885 if (in->f_op->splice_read)
Changli Gaocc56f7d2009-11-04 09:09:52 +0100886 splice_read = in->f_op->splice_read;
887 else
Miklos Szeredi68181732009-05-07 15:37:36 +0200888 splice_read = default_file_splice_read;
889
890 return splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200891}
892
Jens Axboe932cc6d2007-06-21 13:10:21 +0200893/**
894 * splice_direct_to_actor - splices data directly between two non-pipes
895 * @in: file to splice from
896 * @sd: actor information on where to splice to
897 * @actor: handles the data splicing
898 *
899 * Description:
900 * This is a special case helper to splice directly between two
901 * points, without requiring an explicit pipe. Internally an allocated
Randy Dunlap79685b82007-07-27 08:08:51 +0200902 * pipe is cached in the process, and reused during the lifetime of
Jens Axboe932cc6d2007-06-21 13:10:21 +0200903 * that process.
904 *
Jens Axboec66ab6f2007-06-12 21:17:17 +0200905 */
906ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
907 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +0200908{
909 struct pipe_inode_info *pipe;
910 long ret, bytes;
911 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200912 size_t len;
Christophe Leroy0ff28d92015-05-06 17:26:47 +0200913 int i, flags, more;
Jens Axboeb92ce552006-04-11 13:52:07 +0200914
915 /*
916 * We require the input being a regular file, as we don't want to
917 * randomly drop data for eg socket -> socket splicing. Use the
918 * piped splicing for that!
919 */
Al Viro496ad9a2013-01-23 17:07:38 -0500920 i_mode = file_inode(in)->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +0200921 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
922 return -EINVAL;
923
924 /*
925 * neither in nor out is a pipe, setup an internal pipe attached to
926 * 'out' and transfer the wanted data from 'in' to 'out' through that
927 */
928 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200929 if (unlikely(!pipe)) {
Al Viro7bee1302013-03-21 11:04:15 -0400930 pipe = alloc_pipe_info();
Jens Axboeb92ce552006-04-11 13:52:07 +0200931 if (!pipe)
932 return -ENOMEM;
933
934 /*
935 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200936 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200937 * PIPE_READERS appropriately.
938 */
939 pipe->readers = 1;
940
941 current->splice_pipe = pipe;
942 }
943
944 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200945 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200946 */
947 ret = 0;
948 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200949 len = sd->total_len;
950 flags = sd->flags;
951
952 /*
953 * Don't block on output, we have to drain the direct pipe.
954 */
955 sd->flags &= ~SPLICE_F_NONBLOCK;
Christophe Leroy0ff28d92015-05-06 17:26:47 +0200956 more = sd->flags & SPLICE_F_MORE;
Jens Axboeb92ce552006-04-11 13:52:07 +0200957
David Howells8cefc102019-11-15 13:30:32 +0000958 WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
Darrick J. Wong17614442018-11-30 10:37:49 -0800959
Jens Axboeb92ce552006-04-11 13:52:07 +0200960 while (len) {
David Howells8cefc102019-11-15 13:30:32 +0000961 unsigned int p_space;
Jens Axboe51a92c02007-07-13 14:11:43 +0200962 size_t read_len;
Tom Zanussia82c53a2008-05-09 13:28:36 +0200963 loff_t pos = sd->pos, prev_pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +0200964
Darrick J. Wong17614442018-11-30 10:37:49 -0800965 /* Don't try to read more the pipe has space for. */
David Howells6718b6f2019-10-16 16:47:32 +0100966 p_space = pipe->max_usage -
David Howells8cefc102019-11-15 13:30:32 +0000967 pipe_occupancy(pipe->head, pipe->tail);
968 read_len = min_t(size_t, len, p_space << PAGE_SHIFT);
Darrick J. Wong17614442018-11-30 10:37:49 -0800969 ret = do_splice_to(in, &pos, pipe, read_len, flags);
Jens Axboe51a92c02007-07-13 14:11:43 +0200970 if (unlikely(ret <= 0))
Jens Axboeb92ce552006-04-11 13:52:07 +0200971 goto out_release;
972
973 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200974 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +0200975
976 /*
Christophe Leroy0ff28d92015-05-06 17:26:47 +0200977 * If more data is pending, set SPLICE_F_MORE
978 * If this is the last data and SPLICE_F_MORE was not set
979 * initially, clears it.
980 */
981 if (read_len < len)
982 sd->flags |= SPLICE_F_MORE;
983 else if (!more)
984 sd->flags &= ~SPLICE_F_MORE;
985 /*
Jens Axboeb92ce552006-04-11 13:52:07 +0200986 * NOTE: nonblocking mode only applies to the input. We
987 * must not do the output in nonblocking mode as then we
988 * could get stuck data in the internal pipe:
989 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200990 ret = actor(pipe, sd);
Tom Zanussia82c53a2008-05-09 13:28:36 +0200991 if (unlikely(ret <= 0)) {
992 sd->pos = prev_pos;
Jens Axboeb92ce552006-04-11 13:52:07 +0200993 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +0200994 }
Jens Axboeb92ce552006-04-11 13:52:07 +0200995
996 bytes += ret;
997 len -= ret;
Jens Axboebcd4f3a2007-07-16 14:41:49 +0200998 sd->pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +0200999
Tom Zanussia82c53a2008-05-09 13:28:36 +02001000 if (ret < read_len) {
1001 sd->pos = prev_pos + ret;
Jens Axboe51a92c02007-07-13 14:11:43 +02001002 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001003 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001004 }
1005
Jens Axboe9e971982008-01-29 21:05:57 +01001006done:
David Howells8cefc102019-11-15 13:30:32 +00001007 pipe->tail = pipe->head = 0;
Jens Axboe80848702008-01-30 12:24:48 +01001008 file_accessed(in);
Jens Axboeb92ce552006-04-11 13:52:07 +02001009 return bytes;
1010
1011out_release:
1012 /*
1013 * If we did an incomplete transfer we must release
1014 * the pipe buffers in question:
1015 */
David Howells8cefc102019-11-15 13:30:32 +00001016 for (i = 0; i < pipe->ring_size; i++) {
1017 struct pipe_buffer *buf = &pipe->bufs[i];
Jens Axboeb92ce552006-04-11 13:52:07 +02001018
Miklos Szeredia7796382016-09-27 10:45:12 +02001019 if (buf->ops)
1020 pipe_buf_release(pipe, buf);
Jens Axboeb92ce552006-04-11 13:52:07 +02001021 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001022
Jens Axboe9e971982008-01-29 21:05:57 +01001023 if (!bytes)
1024 bytes = ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001025
Jens Axboe9e971982008-01-29 21:05:57 +01001026 goto done;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001027}
1028EXPORT_SYMBOL(splice_direct_to_actor);
1029
1030static int direct_splice_actor(struct pipe_inode_info *pipe,
1031 struct splice_desc *sd)
1032{
Jens Axboe6a14b902007-06-14 13:08:55 +02001033 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001034
Al Viro7995bd22013-06-20 18:58:36 +04001035 return do_splice_from(pipe, file, sd->opos, sd->total_len,
Changli Gao2cb4b052010-06-29 13:09:18 +02001036 sd->flags);
Jens Axboec66ab6f2007-06-12 21:17:17 +02001037}
1038
Jens Axboe932cc6d2007-06-21 13:10:21 +02001039/**
1040 * do_splice_direct - splices data directly between two files
1041 * @in: file to splice from
1042 * @ppos: input file offset
1043 * @out: file to splice to
Randy Dunlapacdb37c2013-06-22 19:44:08 -07001044 * @opos: output file offset
Jens Axboe932cc6d2007-06-21 13:10:21 +02001045 * @len: number of bytes to splice
1046 * @flags: splice modifier flags
1047 *
1048 * Description:
1049 * For use by do_sendfile(). splice can easily emulate sendfile, but
1050 * doing it in the application would incur an extra system call
1051 * (splice in + splice out, as compared to just sendfile()). So this helper
1052 * can splice directly through a process-private pipe.
1053 *
1054 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001055long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
Al Viro7995bd22013-06-20 18:58:36 +04001056 loff_t *opos, size_t len, unsigned int flags)
Jens Axboec66ab6f2007-06-12 21:17:17 +02001057{
1058 struct splice_desc sd = {
1059 .len = len,
1060 .total_len = len,
1061 .flags = flags,
1062 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001063 .u.file = out,
Al Viro7995bd22013-06-20 18:58:36 +04001064 .opos = opos,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001065 };
Jens Axboe51a92c02007-07-13 14:11:43 +02001066 long ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001067
Al Viro18c67cb2013-06-19 15:41:54 +04001068 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1069 return -EBADF;
1070
1071 if (unlikely(out->f_flags & O_APPEND))
1072 return -EINVAL;
1073
1074 ret = rw_verify_area(WRITE, out, opos, len);
1075 if (unlikely(ret < 0))
1076 return ret;
1077
Jens Axboec66ab6f2007-06-12 21:17:17 +02001078 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
Jens Axboe51a92c02007-07-13 14:11:43 +02001079 if (ret > 0)
Tom Zanussia82c53a2008-05-09 13:28:36 +02001080 *ppos = sd.pos;
Jens Axboe51a92c02007-07-13 14:11:43 +02001081
Jens Axboec66ab6f2007-06-12 21:17:17 +02001082 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001083}
Miklos Szeredi1c118592014-10-24 00:14:35 +02001084EXPORT_SYMBOL(do_splice_direct);
Jens Axboeb92ce552006-04-11 13:52:07 +02001085
Al Viro8924fef2016-09-17 20:44:45 -04001086static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1087{
Linus Torvalds52bce912016-12-21 10:59:34 -08001088 for (;;) {
1089 if (unlikely(!pipe->readers)) {
1090 send_sig(SIGPIPE, current, 0);
1091 return -EPIPE;
1092 }
David Howells6718b6f2019-10-16 16:47:32 +01001093 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
Linus Torvalds52bce912016-12-21 10:59:34 -08001094 return 0;
Al Viro8924fef2016-09-17 20:44:45 -04001095 if (flags & SPLICE_F_NONBLOCK)
1096 return -EAGAIN;
1097 if (signal_pending(current))
1098 return -ERESTARTSYS;
1099 pipe->waiting_writers++;
1100 pipe_wait(pipe);
1101 pipe->waiting_writers--;
1102 }
Al Viro8924fef2016-09-17 20:44:45 -04001103}
1104
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001105static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1106 struct pipe_inode_info *opipe,
1107 size_t len, unsigned int flags);
Jens Axboeddac0d32006-11-04 12:49:32 +01001108
1109/*
Jens Axboe83f91352006-04-02 23:05:09 +02001110 * Determine where to splice to/from.
1111 */
Ingo Molnar529565dc2006-04-10 15:18:58 +02001112static long do_splice(struct file *in, loff_t __user *off_in,
1113 struct file *out, loff_t __user *off_out,
1114 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001115{
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001116 struct pipe_inode_info *ipipe;
1117 struct pipe_inode_info *opipe;
Al Viro7995bd22013-06-20 18:58:36 +04001118 loff_t offset;
Jens Axboea4514eb2006-04-19 15:57:05 +02001119 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001120
Linus Torvalds71993e62010-11-28 13:56:09 -08001121 ipipe = get_pipe_info(in);
1122 opipe = get_pipe_info(out);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001123
1124 if (ipipe && opipe) {
1125 if (off_in || off_out)
1126 return -ESPIPE;
1127
1128 if (!(in->f_mode & FMODE_READ))
1129 return -EBADF;
1130
1131 if (!(out->f_mode & FMODE_WRITE))
1132 return -EBADF;
1133
1134 /* Splicing to self would be fun, but... */
1135 if (ipipe == opipe)
1136 return -EINVAL;
1137
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001138 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1139 flags |= SPLICE_F_NONBLOCK;
1140
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001141 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1142 }
1143
1144 if (ipipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001145 if (off_in)
1146 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001147 if (off_out) {
Changli Gao19c9a492010-06-29 13:10:36 +02001148 if (!(out->f_mode & FMODE_PWRITE))
Jens Axboeb92ce552006-04-11 13:52:07 +02001149 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001150 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001151 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001152 } else {
1153 offset = out->f_pos;
1154 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001155
Al Viro18c67cb2013-06-19 15:41:54 +04001156 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1157 return -EBADF;
1158
1159 if (unlikely(out->f_flags & O_APPEND))
1160 return -EINVAL;
1161
1162 ret = rw_verify_area(WRITE, out, &offset, len);
1163 if (unlikely(ret < 0))
1164 return ret;
1165
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001166 if (in->f_flags & O_NONBLOCK)
1167 flags |= SPLICE_F_NONBLOCK;
1168
Al Viro500368f2013-05-23 20:07:11 -04001169 file_start_write(out);
Al Viro7995bd22013-06-20 18:58:36 +04001170 ret = do_splice_from(ipipe, out, &offset, len, flags);
Al Viro500368f2013-05-23 20:07:11 -04001171 file_end_write(out);
Jens Axboea4514eb2006-04-19 15:57:05 +02001172
Al Viro7995bd22013-06-20 18:58:36 +04001173 if (!off_out)
1174 out->f_pos = offset;
1175 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001176 ret = -EFAULT;
1177
1178 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001179 }
Jens Axboe5274f052006-03-30 15:15:30 +02001180
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001181 if (opipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001182 if (off_out)
1183 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001184 if (off_in) {
Changli Gao19c9a492010-06-29 13:10:36 +02001185 if (!(in->f_mode & FMODE_PREAD))
Jens Axboeb92ce552006-04-11 13:52:07 +02001186 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001187 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001188 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001189 } else {
1190 offset = in->f_pos;
1191 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001192
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001193 if (out->f_flags & O_NONBLOCK)
1194 flags |= SPLICE_F_NONBLOCK;
1195
Al Viro8924fef2016-09-17 20:44:45 -04001196 pipe_lock(opipe);
1197 ret = wait_for_space(opipe, flags);
1198 if (!ret)
1199 ret = do_splice_to(in, &offset, opipe, len, flags);
1200 pipe_unlock(opipe);
1201 if (ret > 0)
1202 wakeup_pipe_readers(opipe);
Al Viro7995bd22013-06-20 18:58:36 +04001203 if (!off_in)
1204 in->f_pos = offset;
1205 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001206 ret = -EFAULT;
1207
1208 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001209 }
Jens Axboe5274f052006-03-30 15:15:30 +02001210
1211 return -EINVAL;
1212}
1213
Al Viro79fddc42016-09-17 22:38:20 -04001214static int iter_to_pipe(struct iov_iter *from,
1215 struct pipe_inode_info *pipe,
1216 unsigned flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001217{
Al Viro79fddc42016-09-17 22:38:20 -04001218 struct pipe_buffer buf = {
1219 .ops = &user_page_pipe_buf_ops,
1220 .flags = flags
1221 };
1222 size_t total = 0;
1223 int ret = 0;
1224 bool failed = false;
1225
1226 while (iov_iter_count(from) && !failed) {
1227 struct page *pages[16];
Al Virodb85a9e2016-09-17 20:25:06 -04001228 ssize_t copied;
1229 size_t start;
Al Viro79fddc42016-09-17 22:38:20 -04001230 int n;
Jens Axboe912d35f2006-04-26 10:59:21 +02001231
Al Viro79fddc42016-09-17 22:38:20 -04001232 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1233 if (copied <= 0) {
1234 ret = copied;
1235 break;
1236 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001237
Al Viro79fddc42016-09-17 22:38:20 -04001238 for (n = 0; copied; n++, start = 0) {
Al Virodb85a9e2016-09-17 20:25:06 -04001239 int size = min_t(int, copied, PAGE_SIZE - start);
Al Viro79fddc42016-09-17 22:38:20 -04001240 if (!failed) {
1241 buf.page = pages[n];
1242 buf.offset = start;
1243 buf.len = size;
1244 ret = add_to_pipe(pipe, &buf);
1245 if (unlikely(ret < 0)) {
1246 failed = true;
1247 } else {
1248 iov_iter_advance(from, ret);
1249 total += ret;
1250 }
1251 } else {
1252 put_page(pages[n]);
1253 }
Al Virodb85a9e2016-09-17 20:25:06 -04001254 copied -= size;
Jens Axboe912d35f2006-04-26 10:59:21 +02001255 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001256 }
Al Viro79fddc42016-09-17 22:38:20 -04001257 return total ? total : ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001258}
1259
Jens Axboe6a14b902007-06-14 13:08:55 +02001260static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1261 struct splice_desc *sd)
1262{
Al Viro6130f532014-02-03 18:19:51 -05001263 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1264 return n == sd->len ? n : -EFAULT;
Jens Axboe6a14b902007-06-14 13:08:55 +02001265}
1266
1267/*
1268 * For lack of a better implementation, implement vmsplice() to userspace
1269 * as a simple copy of the pipes pages to the user iov.
1270 */
Al Viro87a30022018-05-26 21:39:52 -04001271static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
1272 unsigned int flags)
Jens Axboe6a14b902007-06-14 13:08:55 +02001273{
Al Viro87a30022018-05-26 21:39:52 -04001274 struct pipe_inode_info *pipe = get_pipe_info(file);
1275 struct splice_desc sd = {
1276 .total_len = iov_iter_count(iter),
1277 .flags = flags,
1278 .u.data = iter
1279 };
1280 long ret = 0;
Jens Axboe6a14b902007-06-14 13:08:55 +02001281
Jens Axboe6a14b902007-06-14 13:08:55 +02001282 if (!pipe)
1283 return -EBADF;
1284
Al Viro345995f2015-03-21 19:17:55 -04001285 if (sd.total_len) {
1286 pipe_lock(pipe);
1287 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1288 pipe_unlock(pipe);
1289 }
Jens Axboe6a14b902007-06-14 13:08:55 +02001290
Jens Axboe6a14b902007-06-14 13:08:55 +02001291 return ret;
1292}
1293
Jens Axboe912d35f2006-04-26 10:59:21 +02001294/*
1295 * vmsplice splices a user address range into a pipe. It can be thought of
1296 * as splice-from-memory, where the regular splice is splice-from-file (or
1297 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001298 */
Al Viro87a30022018-05-26 21:39:52 -04001299static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1300 unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001301{
Jens Axboeddac0d32006-11-04 12:49:32 +01001302 struct pipe_inode_info *pipe;
Al Viro87a30022018-05-26 21:39:52 -04001303 long ret = 0;
Al Viro79fddc42016-09-17 22:38:20 -04001304 unsigned buf_flag = 0;
1305
1306 if (flags & SPLICE_F_GIFT)
1307 buf_flag = PIPE_BUF_FLAG_GIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +02001308
Linus Torvalds71993e62010-11-28 13:56:09 -08001309 pipe = get_pipe_info(file);
Jens Axboeddac0d32006-11-04 12:49:32 +01001310 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001311 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001312
Al Viro8924fef2016-09-17 20:44:45 -04001313 pipe_lock(pipe);
1314 ret = wait_for_space(pipe, flags);
Al Viro79fddc42016-09-17 22:38:20 -04001315 if (!ret)
Al Viro87a30022018-05-26 21:39:52 -04001316 ret = iter_to_pipe(iter, pipe, buf_flag);
Al Viro8924fef2016-09-17 20:44:45 -04001317 pipe_unlock(pipe);
1318 if (ret > 0)
1319 wakeup_pipe_readers(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001320 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001321}
1322
Al Viro87a30022018-05-26 21:39:52 -04001323static int vmsplice_type(struct fd f, int *type)
1324{
1325 if (!f.file)
1326 return -EBADF;
1327 if (f.file->f_mode & FMODE_WRITE) {
1328 *type = WRITE;
1329 } else if (f.file->f_mode & FMODE_READ) {
1330 *type = READ;
1331 } else {
1332 fdput(f);
1333 return -EBADF;
1334 }
1335 return 0;
1336}
1337
Jens Axboe6a14b902007-06-14 13:08:55 +02001338/*
1339 * Note that vmsplice only really supports true splicing _from_ user memory
1340 * to a pipe, not the other way around. Splicing from user memory is a simple
1341 * operation that can be supported without any funky alignment restrictions
1342 * or nasty vm tricks. We simply map in the user memory and fill them into
1343 * a pipe. The reverse isn't quite as easy, though. There are two possible
1344 * solutions for that:
1345 *
1346 * - memcpy() the data internally, at which point we might as well just
1347 * do a regular read() on the buffer anyway.
1348 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1349 * has restriction limitations on both ends of the pipe).
1350 *
1351 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1352 *
1353 */
Al Viro87a30022018-05-26 21:39:52 -04001354static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001355{
Al Viro3d6ea292016-12-10 13:17:32 -05001356 if (unlikely(flags & ~SPLICE_F_ALL))
1357 return -EINVAL;
Al Viro87a30022018-05-26 21:39:52 -04001358
1359 if (!iov_iter_count(iter))
Jens Axboe6a14b902007-06-14 13:08:55 +02001360 return 0;
1361
Al Viro87a30022018-05-26 21:39:52 -04001362 if (iov_iter_rw(iter) == WRITE)
1363 return vmsplice_to_pipe(f, iter, flags);
1364 else
1365 return vmsplice_to_user(f, iter, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001366}
1367
Al Viro87a30022018-05-26 21:39:52 -04001368SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
Dominik Brodowski30cfe4e2018-03-17 15:00:24 +01001369 unsigned long, nr_segs, unsigned int, flags)
1370{
Al Viro87a30022018-05-26 21:39:52 -04001371 struct iovec iovstack[UIO_FASTIOV];
1372 struct iovec *iov = iovstack;
1373 struct iov_iter iter;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001374 ssize_t error;
Al Viro87a30022018-05-26 21:39:52 -04001375 struct fd f;
1376 int type;
1377
1378 f = fdget(fd);
1379 error = vmsplice_type(f, &type);
1380 if (error)
1381 return error;
1382
1383 error = import_iovec(type, uiov, nr_segs,
1384 ARRAY_SIZE(iovstack), &iov, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001385 if (error >= 0) {
Al Viro87a30022018-05-26 21:39:52 -04001386 error = do_vmsplice(f.file, &iter, flags);
1387 kfree(iov);
1388 }
1389 fdput(f);
1390 return error;
Dominik Brodowski30cfe4e2018-03-17 15:00:24 +01001391}
1392
Al Viro76b021d2013-03-02 10:19:56 -05001393#ifdef CONFIG_COMPAT
1394COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1395 unsigned int, nr_segs, unsigned int, flags)
1396{
Al Viro87a30022018-05-26 21:39:52 -04001397 struct iovec iovstack[UIO_FASTIOV];
1398 struct iovec *iov = iovstack;
1399 struct iov_iter iter;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001400 ssize_t error;
Al Viro87a30022018-05-26 21:39:52 -04001401 struct fd f;
1402 int type;
1403
1404 f = fdget(fd);
1405 error = vmsplice_type(f, &type);
1406 if (error)
1407 return error;
1408
1409 error = compat_import_iovec(type, iov32, nr_segs,
1410 ARRAY_SIZE(iovstack), &iov, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001411 if (error >= 0) {
Al Viro87a30022018-05-26 21:39:52 -04001412 error = do_vmsplice(f.file, &iter, flags);
1413 kfree(iov);
Al Viro76b021d2013-03-02 10:19:56 -05001414 }
Al Viro87a30022018-05-26 21:39:52 -04001415 fdput(f);
1416 return error;
Al Viro76b021d2013-03-02 10:19:56 -05001417}
1418#endif
1419
Heiko Carstens836f92a2009-01-14 14:14:33 +01001420SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1421 int, fd_out, loff_t __user *, off_out,
1422 size_t, len, unsigned int, flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001423{
Al Viro2903ff02012-08-28 12:52:22 -04001424 struct fd in, out;
Jens Axboe5274f052006-03-30 15:15:30 +02001425 long error;
Jens Axboe5274f052006-03-30 15:15:30 +02001426
1427 if (unlikely(!len))
1428 return 0;
1429
Al Viro3d6ea292016-12-10 13:17:32 -05001430 if (unlikely(flags & ~SPLICE_F_ALL))
1431 return -EINVAL;
1432
Jens Axboe5274f052006-03-30 15:15:30 +02001433 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001434 in = fdget(fd_in);
1435 if (in.file) {
1436 if (in.file->f_mode & FMODE_READ) {
1437 out = fdget(fd_out);
1438 if (out.file) {
1439 if (out.file->f_mode & FMODE_WRITE)
1440 error = do_splice(in.file, off_in,
1441 out.file, off_out,
Ingo Molnar529565dc2006-04-10 15:18:58 +02001442 len, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001443 fdput(out);
Jens Axboe5274f052006-03-30 15:15:30 +02001444 }
1445 }
Al Viro2903ff02012-08-28 12:52:22 -04001446 fdput(in);
Jens Axboe5274f052006-03-30 15:15:30 +02001447 }
Jens Axboe5274f052006-03-30 15:15:30 +02001448 return error;
1449}
Jens Axboe70524492006-04-11 15:51:17 +02001450
1451/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001452 * Make sure there's data to read. Wait for input if we can, otherwise
1453 * return an appropriate error.
1454 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001455static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001456{
1457 int ret;
1458
1459 /*
David Howells8cefc102019-11-15 13:30:32 +00001460 * Check the pipe occupancy without the inode lock first. This function
Jens Axboeaadd06e2006-07-10 11:00:01 +02001461 * is speculative anyways, so missing one is ok.
1462 */
David Howells8cefc102019-11-15 13:30:32 +00001463 if (!pipe_empty(pipe->head, pipe->tail))
Jens Axboeaadd06e2006-07-10 11:00:01 +02001464 return 0;
1465
1466 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001467 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001468
David Howells8cefc102019-11-15 13:30:32 +00001469 while (pipe_empty(pipe->head, pipe->tail)) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001470 if (signal_pending(current)) {
1471 ret = -ERESTARTSYS;
1472 break;
1473 }
1474 if (!pipe->writers)
1475 break;
1476 if (!pipe->waiting_writers) {
1477 if (flags & SPLICE_F_NONBLOCK) {
1478 ret = -EAGAIN;
1479 break;
1480 }
1481 }
1482 pipe_wait(pipe);
1483 }
1484
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001485 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001486 return ret;
1487}
1488
1489/*
1490 * Make sure there's writeable room. Wait for room if we can, otherwise
1491 * return an appropriate error.
1492 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001493static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001494{
1495 int ret;
1496
1497 /*
David Howells8cefc102019-11-15 13:30:32 +00001498 * Check pipe occupancy without the inode lock first. This function
Jens Axboeaadd06e2006-07-10 11:00:01 +02001499 * is speculative anyways, so missing one is ok.
1500 */
David Howells6718b6f2019-10-16 16:47:32 +01001501 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
Jens Axboeaadd06e2006-07-10 11:00:01 +02001502 return 0;
1503
1504 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001505 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001506
David Howells6718b6f2019-10-16 16:47:32 +01001507 while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001508 if (!pipe->readers) {
1509 send_sig(SIGPIPE, current, 0);
1510 ret = -EPIPE;
1511 break;
1512 }
1513 if (flags & SPLICE_F_NONBLOCK) {
1514 ret = -EAGAIN;
1515 break;
1516 }
1517 if (signal_pending(current)) {
1518 ret = -ERESTARTSYS;
1519 break;
1520 }
1521 pipe->waiting_writers++;
1522 pipe_wait(pipe);
1523 pipe->waiting_writers--;
1524 }
1525
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001526 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001527 return ret;
1528}
1529
1530/*
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001531 * Splice contents of ipipe to opipe.
1532 */
1533static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1534 struct pipe_inode_info *opipe,
1535 size_t len, unsigned int flags)
1536{
1537 struct pipe_buffer *ibuf, *obuf;
David Howells8cefc102019-11-15 13:30:32 +00001538 unsigned int i_head, o_head;
1539 unsigned int i_tail, o_tail;
1540 unsigned int i_mask, o_mask;
1541 int ret = 0;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001542 bool input_wakeup = false;
1543
1544
1545retry:
1546 ret = ipipe_prep(ipipe, flags);
1547 if (ret)
1548 return ret;
1549
1550 ret = opipe_prep(opipe, flags);
1551 if (ret)
1552 return ret;
1553
1554 /*
1555 * Potential ABBA deadlock, work around it by ordering lock
1556 * grabbing by pipe info address. Otherwise two different processes
1557 * could deadlock (one doing tee from A -> B, the other from B -> A).
1558 */
1559 pipe_double_lock(ipipe, opipe);
1560
David Howells8cefc102019-11-15 13:30:32 +00001561 i_tail = ipipe->tail;
1562 i_mask = ipipe->ring_size - 1;
1563 o_head = opipe->head;
1564 o_mask = opipe->ring_size - 1;
1565
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001566 do {
David Howells8cefc102019-11-15 13:30:32 +00001567 size_t o_len;
1568
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001569 if (!opipe->readers) {
1570 send_sig(SIGPIPE, current, 0);
1571 if (!ret)
1572 ret = -EPIPE;
1573 break;
1574 }
1575
David Howells8cefc102019-11-15 13:30:32 +00001576 i_head = ipipe->head;
1577 o_tail = opipe->tail;
1578
1579 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001580 break;
1581
1582 /*
1583 * Cannot make any progress, because either the input
1584 * pipe is empty or the output pipe is full.
1585 */
David Howells8cefc102019-11-15 13:30:32 +00001586 if (pipe_empty(i_head, i_tail) ||
David Howells6718b6f2019-10-16 16:47:32 +01001587 pipe_full(o_head, o_tail, opipe->max_usage)) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001588 /* Already processed some buffers, break */
1589 if (ret)
1590 break;
1591
1592 if (flags & SPLICE_F_NONBLOCK) {
1593 ret = -EAGAIN;
1594 break;
1595 }
1596
1597 /*
1598 * We raced with another reader/writer and haven't
1599 * managed to process any buffers. A zero return
1600 * value means EOF, so retry instead.
1601 */
1602 pipe_unlock(ipipe);
1603 pipe_unlock(opipe);
1604 goto retry;
1605 }
1606
David Howells8cefc102019-11-15 13:30:32 +00001607 ibuf = &ipipe->bufs[i_tail & i_mask];
1608 obuf = &opipe->bufs[o_head & o_mask];
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001609
1610 if (len >= ibuf->len) {
1611 /*
1612 * Simply move the whole buffer from ipipe to opipe
1613 */
1614 *obuf = *ibuf;
1615 ibuf->ops = NULL;
David Howells8cefc102019-11-15 13:30:32 +00001616 i_tail++;
1617 ipipe->tail = i_tail;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001618 input_wakeup = true;
David Howells8cefc102019-11-15 13:30:32 +00001619 o_len = obuf->len;
1620 o_head++;
1621 opipe->head = o_head;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001622 } else {
1623 /*
1624 * Get a reference to this pipe buffer,
1625 * so we can copy the contents over.
1626 */
Matthew Wilcox15fab632019-04-05 14:02:10 -07001627 if (!pipe_buf_get(ipipe, ibuf)) {
1628 if (ret == 0)
1629 ret = -EFAULT;
1630 break;
1631 }
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001632 *obuf = *ibuf;
1633
1634 /*
1635 * Don't inherit the gift flag, we need to
1636 * prevent multiple steals of this page.
1637 */
1638 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1639
Jann Horna0ce2f02019-01-23 15:19:17 +01001640 pipe_buf_mark_unmergeable(obuf);
1641
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001642 obuf->len = len;
David Howells8cefc102019-11-15 13:30:32 +00001643 ibuf->offset += len;
1644 ibuf->len -= len;
1645 o_len = len;
1646 o_head++;
1647 opipe->head = o_head;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001648 }
David Howells8cefc102019-11-15 13:30:32 +00001649 ret += o_len;
1650 len -= o_len;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001651 } while (len);
1652
1653 pipe_unlock(ipipe);
1654 pipe_unlock(opipe);
1655
1656 /*
1657 * If we put data in the output pipe, wakeup any potential readers.
1658 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001659 if (ret > 0)
1660 wakeup_pipe_readers(opipe);
1661
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001662 if (input_wakeup)
1663 wakeup_pipe_writers(ipipe);
1664
1665 return ret;
1666}
1667
1668/*
Jens Axboe70524492006-04-11 15:51:17 +02001669 * Link contents of ipipe to opipe.
1670 */
1671static int link_pipe(struct pipe_inode_info *ipipe,
1672 struct pipe_inode_info *opipe,
1673 size_t len, unsigned int flags)
1674{
1675 struct pipe_buffer *ibuf, *obuf;
David Howells8cefc102019-11-15 13:30:32 +00001676 unsigned int i_head, o_head;
1677 unsigned int i_tail, o_tail;
1678 unsigned int i_mask, o_mask;
1679 int ret = 0;
Jens Axboe70524492006-04-11 15:51:17 +02001680
1681 /*
1682 * Potential ABBA deadlock, work around it by ordering lock
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001683 * grabbing by pipe info address. Otherwise two different processes
Jens Axboe70524492006-04-11 15:51:17 +02001684 * could deadlock (one doing tee from A -> B, the other from B -> A).
1685 */
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001686 pipe_double_lock(ipipe, opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001687
David Howells8cefc102019-11-15 13:30:32 +00001688 i_tail = ipipe->tail;
1689 i_mask = ipipe->ring_size - 1;
1690 o_head = opipe->head;
1691 o_mask = opipe->ring_size - 1;
1692
Jens Axboeaadd06e2006-07-10 11:00:01 +02001693 do {
Jens Axboe70524492006-04-11 15:51:17 +02001694 if (!opipe->readers) {
1695 send_sig(SIGPIPE, current, 0);
1696 if (!ret)
1697 ret = -EPIPE;
1698 break;
1699 }
Jens Axboe70524492006-04-11 15:51:17 +02001700
David Howells8cefc102019-11-15 13:30:32 +00001701 i_head = ipipe->head;
1702 o_tail = opipe->tail;
1703
Jens Axboe70524492006-04-11 15:51:17 +02001704 /*
David Howells8cefc102019-11-15 13:30:32 +00001705 * If we have iterated all input buffers or run out of
Jens Axboeaadd06e2006-07-10 11:00:01 +02001706 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001707 */
David Howells8cefc102019-11-15 13:30:32 +00001708 if (pipe_empty(i_head, i_tail) ||
David Howells6718b6f2019-10-16 16:47:32 +01001709 pipe_full(o_head, o_tail, opipe->max_usage))
Jens Axboe70524492006-04-11 15:51:17 +02001710 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001711
David Howells8cefc102019-11-15 13:30:32 +00001712 ibuf = &ipipe->bufs[i_tail & i_mask];
1713 obuf = &opipe->bufs[o_head & o_mask];
Jens Axboeaadd06e2006-07-10 11:00:01 +02001714
Jens Axboe2a27250e2006-04-19 15:56:40 +02001715 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001716 * Get a reference to this pipe buffer,
1717 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001718 */
Matthew Wilcox15fab632019-04-05 14:02:10 -07001719 if (!pipe_buf_get(ipipe, ibuf)) {
1720 if (ret == 0)
1721 ret = -EFAULT;
1722 break;
1723 }
Jens Axboe70524492006-04-11 15:51:17 +02001724
Jens Axboeaadd06e2006-07-10 11:00:01 +02001725 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001726
Jens Axboeaadd06e2006-07-10 11:00:01 +02001727 /*
1728 * Don't inherit the gift flag, we need to
1729 * prevent multiple steals of this page.
1730 */
1731 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1732
Jann Horna0ce2f02019-01-23 15:19:17 +01001733 pipe_buf_mark_unmergeable(obuf);
1734
Jens Axboeaadd06e2006-07-10 11:00:01 +02001735 if (obuf->len > len)
1736 obuf->len = len;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001737 ret += obuf->len;
1738 len -= obuf->len;
David Howells8cefc102019-11-15 13:30:32 +00001739
1740 o_head++;
1741 opipe->head = o_head;
1742 i_tail++;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001743 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001744
Jens Axboe02cf01a2008-02-20 10:34:51 +01001745 /*
1746 * return EAGAIN if we have the potential of some data in the
1747 * future, otherwise just return 0
1748 */
1749 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1750 ret = -EAGAIN;
1751
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001752 pipe_unlock(ipipe);
1753 pipe_unlock(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001754
Jens Axboeaadd06e2006-07-10 11:00:01 +02001755 /*
1756 * If we put data in the output pipe, wakeup any potential readers.
1757 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001758 if (ret > 0)
1759 wakeup_pipe_readers(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001760
1761 return ret;
1762}
1763
1764/*
1765 * This is a tee(1) implementation that works on pipes. It doesn't copy
1766 * any data, it simply references the 'in' pages on the 'out' pipe.
1767 * The 'flags' used are the SPLICE_F_* variants, currently the only
1768 * applicable one is SPLICE_F_NONBLOCK.
1769 */
1770static long do_tee(struct file *in, struct file *out, size_t len,
1771 unsigned int flags)
1772{
Linus Torvalds71993e62010-11-28 13:56:09 -08001773 struct pipe_inode_info *ipipe = get_pipe_info(in);
1774 struct pipe_inode_info *opipe = get_pipe_info(out);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001775 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001776
1777 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001778 * Duplicate the contents of ipipe to opipe without actually
1779 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001780 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001781 if (ipipe && opipe && ipipe != opipe) {
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001782 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1783 flags |= SPLICE_F_NONBLOCK;
1784
Jens Axboeaadd06e2006-07-10 11:00:01 +02001785 /*
1786 * Keep going, unless we encounter an error. The ipipe/opipe
1787 * ordering doesn't really matter.
1788 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001789 ret = ipipe_prep(ipipe, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001790 if (!ret) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001791 ret = opipe_prep(opipe, flags);
Jens Axboe02cf01a2008-02-20 10:34:51 +01001792 if (!ret)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001793 ret = link_pipe(ipipe, opipe, len, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001794 }
1795 }
Jens Axboe70524492006-04-11 15:51:17 +02001796
Jens Axboeaadd06e2006-07-10 11:00:01 +02001797 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001798}
1799
Heiko Carstens836f92a2009-01-14 14:14:33 +01001800SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
Jens Axboe70524492006-04-11 15:51:17 +02001801{
Al Viro2903ff02012-08-28 12:52:22 -04001802 struct fd in;
1803 int error;
Jens Axboe70524492006-04-11 15:51:17 +02001804
Al Viro3d6ea292016-12-10 13:17:32 -05001805 if (unlikely(flags & ~SPLICE_F_ALL))
1806 return -EINVAL;
1807
Jens Axboe70524492006-04-11 15:51:17 +02001808 if (unlikely(!len))
1809 return 0;
1810
1811 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001812 in = fdget(fdin);
1813 if (in.file) {
1814 if (in.file->f_mode & FMODE_READ) {
1815 struct fd out = fdget(fdout);
1816 if (out.file) {
1817 if (out.file->f_mode & FMODE_WRITE)
1818 error = do_tee(in.file, out.file,
1819 len, flags);
1820 fdput(out);
Jens Axboe70524492006-04-11 15:51:17 +02001821 }
1822 }
Al Viro2903ff02012-08-28 12:52:22 -04001823 fdput(in);
Jens Axboe70524492006-04-11 15:51:17 +02001824 }
1825
1826 return error;
1827}