blob: 60aed8de21f858554aa4f9786c39063941437606 [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>
Chung-Chiang Cheng983652c2023-03-22 14:25:19 +080033#include <linux/fsnotify.h>
James Morris29ce2052007-07-13 11:44:32 +020034#include <linux/security.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/gfp.h>
David Howells2dc334f2023-06-07 19:19:09 +010036#include <linux/net.h>
Eric Dumazet35f9c092012-04-05 03:05:35 +000037#include <linux/socket.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010038#include <linux/sched/signal.h>
39
Al Viro06ae43f32013-03-20 13:19:30 -040040#include "internal.h"
Jens Axboe5274f052006-03-30 15:15:30 +020041
Jens Axboe83f91352006-04-02 23:05:09 +020042/*
Jens Axboe0f99fc52023-04-24 16:32:55 -060043 * Splice doesn't support FMODE_NOWAIT. Since pipes may set this flag to
44 * indicate they support non-blocking reads or writes, we must clear it
45 * here if set to avoid blocking other users of this pipe if splice is
46 * being done on it.
47 */
48static noinline void noinline pipe_clear_nowait(struct file *file)
49{
50 fmode_t fmode = READ_ONCE(file->f_mode);
51
52 do {
53 if (!(fmode & FMODE_NOWAIT))
54 break;
55 } while (!try_cmpxchg(&file->f_mode, &fmode, fmode & ~FMODE_NOWAIT));
56}
57
58/*
Jens Axboe83f91352006-04-02 23:05:09 +020059 * Attempt to steal a page from a pipe buffer. This should perhaps go into
60 * a vm helper function, it's already simplified quite a bit by the
61 * addition of remove_mapping(). If success is returned, the caller may
62 * attempt to reuse this page for another destination.
63 */
Christoph Hellwigc928f642020-05-20 17:58:16 +020064static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
65 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +020066{
Matthew Wilcox (Oracle)5100da32022-02-12 22:48:55 -050067 struct folio *folio = page_folio(buf->page);
Jens Axboe9e94cd42006-06-20 15:01:12 +020068 struct address_space *mapping;
Jens Axboe5abc97a2006-03-30 15:16:46 +020069
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -050070 folio_lock(folio);
Jens Axboe9e0267c2006-04-19 15:57:31 +020071
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -050072 mapping = folio_mapping(folio);
Jens Axboe9e94cd42006-06-20 15:01:12 +020073 if (mapping) {
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -050074 WARN_ON(!folio_test_uptodate(folio));
Jens Axboe5abc97a2006-03-30 15:16:46 +020075
Jens Axboe9e94cd42006-06-20 15:01:12 +020076 /*
77 * At least for ext2 with nobh option, we need to wait on
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -050078 * writeback completing on this folio, since we'll remove it
Jens Axboe9e94cd42006-06-20 15:01:12 +020079 * from the pagecache. Otherwise truncate wont wait on the
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -050080 * folio, allowing the disk blocks to be reused by someone else
Jens Axboe9e94cd42006-06-20 15:01:12 +020081 * before we actually wrote our data to them. fs corruption
82 * ensues.
83 */
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -050084 folio_wait_writeback(folio);
Jens Axboead8d6f02006-04-02 23:10:32 +020085
David Howells0201ebf2023-06-28 11:48:51 +010086 if (!filemap_release_folio(folio, GFP_KERNEL))
Jens Axboeca39d652008-05-20 21:27:41 +020087 goto out_unlock;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020088
Jens Axboe9e94cd42006-06-20 15:01:12 +020089 /*
90 * If we succeeded in removing the mapping, set LRU flag
91 * and return good.
92 */
Matthew Wilcox (Oracle)5100da32022-02-12 22:48:55 -050093 if (remove_mapping(mapping, folio)) {
Jens Axboe9e94cd42006-06-20 15:01:12 +020094 buf->flags |= PIPE_BUF_FLAG_LRU;
Christoph Hellwigc928f642020-05-20 17:58:16 +020095 return true;
Jens Axboe9e94cd42006-06-20 15:01:12 +020096 }
Jens Axboe9e0267c2006-04-19 15:57:31 +020097 }
Jens Axboe5abc97a2006-03-30 15:16:46 +020098
Jens Axboe9e94cd42006-06-20 15:01:12 +020099 /*
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -0500100 * Raced with truncate or failed to remove folio from current
Jens Axboe9e94cd42006-06-20 15:01:12 +0200101 * address space, unlock and return failure.
102 */
Jens Axboeca39d652008-05-20 21:27:41 +0200103out_unlock:
Matthew Wilcox (Oracle)b9ccad22022-02-11 23:39:03 -0500104 folio_unlock(folio);
Christoph Hellwigc928f642020-05-20 17:58:16 +0200105 return false;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200106}
107
Jens Axboe76ad4d12006-05-03 10:41:33 +0200108static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200109 struct pipe_buffer *buf)
110{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300111 put_page(buf->page);
Jens Axboe14328732006-05-03 10:35:26 +0200112 buf->flags &= ~PIPE_BUF_FLAG_LRU;
Jens Axboe5274f052006-03-30 15:15:30 +0200113}
114
Jens Axboe08457182007-06-12 20:51:32 +0200115/*
116 * Check whether the contents of buf is OK to access. Since the content
117 * is a page cache page, IO may be in flight.
118 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200119static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
120 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +0200121{
Matthew Wilcox (Oracle)781ca602023-08-21 15:15:41 +0100122 struct folio *folio = page_folio(buf->page);
Jens Axboe49d0b212006-04-10 09:04:41 +0200123 int err;
Jens Axboe5274f052006-03-30 15:15:30 +0200124
Matthew Wilcox (Oracle)781ca602023-08-21 15:15:41 +0100125 if (!folio_test_uptodate(folio)) {
126 folio_lock(folio);
Jens Axboe49d0b212006-04-10 09:04:41 +0200127
128 /*
Matthew Wilcox (Oracle)781ca602023-08-21 15:15:41 +0100129 * Folio got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +0200130 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200131 */
Matthew Wilcox (Oracle)781ca602023-08-21 15:15:41 +0100132 if (!folio->mapping) {
Jens Axboe49d0b212006-04-10 09:04:41 +0200133 err = -ENODATA;
134 goto error;
135 }
136
137 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200138 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200139 */
Matthew Wilcox (Oracle)781ca602023-08-21 15:15:41 +0100140 if (!folio_test_uptodate(folio)) {
Jens Axboe49d0b212006-04-10 09:04:41 +0200141 err = -EIO;
142 goto error;
143 }
144
Matthew Wilcox (Oracle)781ca602023-08-21 15:15:41 +0100145 /* Folio is ok after all, we are done */
146 folio_unlock(folio);
Jens Axboe5274f052006-03-30 15:15:30 +0200147 }
148
Jens Axboef84d7512006-05-01 19:59:03 +0200149 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200150error:
Matthew Wilcox (Oracle)781ca602023-08-21 15:15:41 +0100151 folio_unlock(folio);
Jens Axboef84d7512006-05-01 19:59:03 +0200152 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200153}
154
Hugh Dickins708e3502011-07-25 17:12:32 -0700155const struct pipe_buf_operations page_cache_pipe_buf_ops = {
Christoph Hellwigc928f642020-05-20 17:58:16 +0200156 .confirm = page_cache_pipe_buf_confirm,
157 .release = page_cache_pipe_buf_release,
158 .try_steal = page_cache_pipe_buf_try_steal,
159 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200160};
161
Christoph Hellwigc928f642020-05-20 17:58:16 +0200162static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
163 struct pipe_buffer *buf)
Jens Axboe912d35f2006-04-26 10:59:21 +0200164{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200165 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
Christoph Hellwigc928f642020-05-20 17:58:16 +0200166 return false;
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200167
Jens Axboe14328732006-05-03 10:35:26 +0200168 buf->flags |= PIPE_BUF_FLAG_LRU;
Christoph Hellwigc928f642020-05-20 17:58:16 +0200169 return generic_pipe_buf_try_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200170}
171
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800172static const struct pipe_buf_operations user_page_pipe_buf_ops = {
Christoph Hellwigc928f642020-05-20 17:58:16 +0200173 .release = page_cache_pipe_buf_release,
174 .try_steal = user_page_pipe_buf_try_steal,
175 .get = generic_pipe_buf_get,
Jens Axboe912d35f2006-04-26 10:59:21 +0200176};
177
Namhyung Kim825cdcb2011-05-23 19:58:53 +0200178static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
179{
180 smp_mb();
Linus Torvalds0ddad212019-12-09 09:48:27 -0800181 if (waitqueue_active(&pipe->rd_wait))
182 wake_up_interruptible(&pipe->rd_wait);
Namhyung Kim825cdcb2011-05-23 19:58:53 +0200183 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
184}
185
Jens Axboe932cc6d2007-06-21 13:10:21 +0200186/**
187 * splice_to_pipe - fill passed data into a pipe
188 * @pipe: pipe to fill
189 * @spd: data to fill
190 *
191 * Description:
Randy Dunlap79685b82007-07-27 08:08:51 +0200192 * @spd contains a map of pages and len/offset tuples, along with
Jens Axboe932cc6d2007-06-21 13:10:21 +0200193 * the struct pipe_buf_operations associated with these pages. This
194 * function will link that data to the pipe.
195 *
Jens Axboe83f91352006-04-02 23:05:09 +0200196 */
Jens Axboed6b29d72007-06-04 09:59:47 +0200197ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
198 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200199{
Jens Axboe00de00b2007-06-15 13:14:22 +0200200 unsigned int spd_pages = spd->nr_pages;
David Howells8cefc102019-11-15 13:30:32 +0000201 unsigned int tail = pipe->tail;
202 unsigned int head = pipe->head;
203 unsigned int mask = pipe->ring_size - 1;
Amir Goldstein0f292082023-12-12 11:44:36 +0200204 ssize_t ret = 0;
205 int page_nr = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200206
Rabin Vincentd6785d92016-03-10 21:19:06 +0100207 if (!spd_pages)
208 return 0;
209
Al Viro8924fef2016-09-17 20:44:45 -0400210 if (unlikely(!pipe->readers)) {
211 send_sig(SIGPIPE, current, 0);
212 ret = -EPIPE;
213 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200214 }
215
David Howells6718b6f2019-10-16 16:47:32 +0100216 while (!pipe_full(head, tail, pipe->max_usage)) {
David Howells8cefc102019-11-15 13:30:32 +0000217 struct pipe_buffer *buf = &pipe->bufs[head & mask];
Jens Axboe5274f052006-03-30 15:15:30 +0200218
Al Viro8924fef2016-09-17 20:44:45 -0400219 buf->page = spd->pages[page_nr];
220 buf->offset = spd->partial[page_nr].offset;
221 buf->len = spd->partial[page_nr].len;
222 buf->private = spd->partial[page_nr].private;
223 buf->ops = spd->ops;
Miklos Szeredi5a81e6a2017-02-16 17:49:02 +0100224 buf->flags = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200225
David Howells8cefc102019-11-15 13:30:32 +0000226 head++;
227 pipe->head = head;
Al Viro8924fef2016-09-17 20:44:45 -0400228 page_nr++;
229 ret += buf->len;
230
231 if (!--spd->nr_pages)
232 break;
233 }
234
235 if (!ret)
236 ret = -EAGAIN;
237
238out:
Jens Axboe00de00b2007-06-15 13:14:22 +0200239 while (page_nr < spd_pages)
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800240 spd->spd_release(spd, page_nr++);
Jens Axboe5274f052006-03-30 15:15:30 +0200241
242 return ret;
243}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200244EXPORT_SYMBOL_GPL(splice_to_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200245
Al Viro79fddc42016-09-17 22:38:20 -0400246ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
247{
David Howells8cefc102019-11-15 13:30:32 +0000248 unsigned int head = pipe->head;
249 unsigned int tail = pipe->tail;
250 unsigned int mask = pipe->ring_size - 1;
Al Viro79fddc42016-09-17 22:38:20 -0400251 int ret;
252
253 if (unlikely(!pipe->readers)) {
254 send_sig(SIGPIPE, current, 0);
255 ret = -EPIPE;
David Howells6718b6f2019-10-16 16:47:32 +0100256 } else if (pipe_full(head, tail, pipe->max_usage)) {
Al Viro79fddc42016-09-17 22:38:20 -0400257 ret = -EAGAIN;
258 } else {
David Howells8cefc102019-11-15 13:30:32 +0000259 pipe->bufs[head & mask] = *buf;
260 pipe->head = head + 1;
Al Viro79fddc42016-09-17 22:38:20 -0400261 return buf->len;
262 }
Miklos Szeredia7796382016-09-27 10:45:12 +0200263 pipe_buf_release(pipe, buf);
Al Viro79fddc42016-09-17 22:38:20 -0400264 return ret;
265}
266EXPORT_SYMBOL(add_to_pipe);
267
Jens Axboe35f3d142010-05-20 10:43:18 +0200268/*
269 * Check if we need to grow the arrays holding pages and partial page
270 * descriptions.
271 */
Eric Dumazet047fe362012-06-12 15:24:40 +0200272int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200273{
David Howells6718b6f2019-10-16 16:47:32 +0100274 unsigned int max_usage = READ_ONCE(pipe->max_usage);
Eric Dumazet047fe362012-06-12 15:24:40 +0200275
David Howells8cefc102019-11-15 13:30:32 +0000276 spd->nr_pages_max = max_usage;
277 if (max_usage <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200278 return 0;
279
David Howells8cefc102019-11-15 13:30:32 +0000280 spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
281 spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
Kees Cook6da2ec52018-06-12 13:55:00 -0700282 GFP_KERNEL);
Jens Axboe35f3d142010-05-20 10:43:18 +0200283
284 if (spd->pages && spd->partial)
285 return 0;
286
287 kfree(spd->pages);
288 kfree(spd->partial);
289 return -ENOMEM;
290}
291
Eric Dumazet047fe362012-06-12 15:24:40 +0200292void splice_shrink_spd(struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200293{
Eric Dumazet047fe362012-06-12 15:24:40 +0200294 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200295 return;
296
297 kfree(spd->pages);
298 kfree(spd->partial);
299}
300
David Howells9eee8bd2023-05-22 14:50:18 +0100301/**
302 * copy_splice_read - Copy data from a file and splice the copy into a pipe
303 * @in: The file to read from
304 * @ppos: Pointer to the file position to read from
305 * @pipe: The pipe to splice into
306 * @len: The amount to splice
307 * @flags: The SPLICE_F_* flags
308 *
309 * This function allocates a bunch of pages sufficient to hold the requested
310 * amount of data (but limited by the remaining pipe capacity), passes it to
311 * the file's ->read_iter() to read into and then splices the used pages into
312 * the pipe.
313 *
314 * Return: On success, the number of bytes read will be returned and *@ppos
315 * will be updated if appropriate; 0 will be returned if there is no more data
316 * to be read; -EAGAIN will be returned if the pipe had no space, and some
317 * other negative error code will be returned on error. A short read may occur
318 * if the pipe has insufficient space, we reach the end of the data or we hit a
319 * hole.
David Howells33b3b042023-02-07 10:45:40 +0000320 */
David Howells69df79a2023-05-22 14:49:50 +0100321ssize_t copy_splice_read(struct file *in, loff_t *ppos,
322 struct pipe_inode_info *pipe,
323 size_t len, unsigned int flags)
David Howells33b3b042023-02-07 10:45:40 +0000324{
325 struct iov_iter to;
326 struct bio_vec *bv;
327 struct kiocb kiocb;
328 struct page **pages;
329 ssize_t ret;
David Howellse69f37b2023-05-22 14:49:51 +0100330 size_t used, npages, chunk, remain, keep = 0;
David Howells33b3b042023-02-07 10:45:40 +0000331 int i;
332
333 /* Work out how much data we can actually add into the pipe */
334 used = pipe_occupancy(pipe->head, pipe->tail);
335 npages = max_t(ssize_t, pipe->max_usage - used, 0);
336 len = min_t(size_t, len, npages * PAGE_SIZE);
337 npages = DIV_ROUND_UP(len, PAGE_SIZE);
338
339 bv = kzalloc(array_size(npages, sizeof(bv[0])) +
340 array_size(npages, sizeof(struct page *)), GFP_KERNEL);
341 if (!bv)
342 return -ENOMEM;
343
David Howellse69f37b2023-05-22 14:49:51 +0100344 pages = (struct page **)(bv + npages);
David Howells33b3b042023-02-07 10:45:40 +0000345 npages = alloc_pages_bulk_array(GFP_USER, npages, pages);
346 if (!npages) {
347 kfree(bv);
348 return -ENOMEM;
349 }
350
351 remain = len = min_t(size_t, len, npages * PAGE_SIZE);
352
353 for (i = 0; i < npages; i++) {
354 chunk = min_t(size_t, PAGE_SIZE, remain);
355 bv[i].bv_page = pages[i];
356 bv[i].bv_offset = 0;
357 bv[i].bv_len = chunk;
358 remain -= chunk;
359 }
360
361 /* Do the I/O */
362 iov_iter_bvec(&to, ITER_DEST, bv, npages, len);
363 init_sync_kiocb(&kiocb, in);
364 kiocb.ki_pos = *ppos;
Miklos Szeredi7c98f7c2023-08-28 17:13:18 +0200365 ret = in->f_op->read_iter(&kiocb, &to);
David Howells33b3b042023-02-07 10:45:40 +0000366
David Howells33b3b042023-02-07 10:45:40 +0000367 if (ret > 0) {
David Howellse69f37b2023-05-22 14:49:51 +0100368 keep = DIV_ROUND_UP(ret, PAGE_SIZE);
David Howells33b3b042023-02-07 10:45:40 +0000369 *ppos = kiocb.ki_pos;
David Howells33b3b042023-02-07 10:45:40 +0000370 }
371
Christoph Hellwig2e82f6c2023-06-14 16:03:39 +0200372 /*
373 * Callers of ->splice_read() expect -EAGAIN on "can't put anything in
374 * there", rather than -EFAULT.
375 */
376 if (ret == -EFAULT)
377 ret = -EAGAIN;
378
David Howells33b3b042023-02-07 10:45:40 +0000379 /* Free any pages that didn't get touched at all. */
David Howellse69f37b2023-05-22 14:49:51 +0100380 if (keep < npages)
381 release_pages(pages + keep, npages - keep);
David Howells33b3b042023-02-07 10:45:40 +0000382
383 /* Push the remaining pages into the pipe. */
David Howellse69f37b2023-05-22 14:49:51 +0100384 remain = ret;
385 for (i = 0; i < keep; i++) {
David Howells33b3b042023-02-07 10:45:40 +0000386 struct pipe_buffer *buf = pipe_head_buf(pipe);
387
388 chunk = min_t(size_t, remain, PAGE_SIZE);
389 *buf = (struct pipe_buffer) {
390 .ops = &default_pipe_buf_ops,
391 .page = bv[i].bv_page,
392 .offset = 0,
393 .len = chunk,
394 };
395 pipe->head++;
396 remain -= chunk;
397 }
398
399 kfree(bv);
400 return ret;
401}
David Howells69df79a2023-05-22 14:49:50 +0100402EXPORT_SYMBOL(copy_splice_read);
David Howells33b3b042023-02-07 10:45:40 +0000403
Al Viro241699c2016-09-22 16:33:12 -0400404const struct pipe_buf_operations default_pipe_buf_ops = {
Christoph Hellwigc928f642020-05-20 17:58:16 +0200405 .release = generic_pipe_buf_release,
406 .try_steal = generic_pipe_buf_try_steal,
407 .get = generic_pipe_buf_get,
Miklos Szeredi68181732009-05-07 15:37:36 +0200408};
409
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100410/* Pipe buffer operations for a socket and similar. */
411const struct pipe_buf_operations nosteal_pipe_buf_ops = {
Christoph Hellwigc928f642020-05-20 17:58:16 +0200412 .release = generic_pipe_buf_release,
413 .get = generic_pipe_buf_get,
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100414};
415EXPORT_SYMBOL(nosteal_pipe_buf_ops);
416
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200417static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
418{
419 smp_mb();
Linus Torvalds0ddad212019-12-09 09:48:27 -0800420 if (waitqueue_active(&pipe->wr_wait))
421 wake_up_interruptible(&pipe->wr_wait);
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200422 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
423}
424
425/**
426 * splice_from_pipe_feed - feed available data from a pipe to a file
427 * @pipe: pipe to splice from
428 * @sd: information to @actor
429 * @actor: handler that splices the data
430 *
431 * Description:
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200432 * This function loops over the pipe and calls @actor to do the
433 * actual moving of a single struct pipe_buffer to the desired
434 * destination. It returns when there's no more buffers left in
435 * the pipe or if the requested number of bytes (@sd->total_len)
436 * have been copied. It returns a positive number (one) if the
437 * pipe needs to be filled with more data, zero if the required
438 * number of bytes have been copied and -errno on error.
439 *
440 * This, together with splice_from_pipe_{begin,end,next}, may be
441 * used to implement the functionality of __splice_from_pipe() when
442 * locking is required around copying the pipe buffers to the
443 * destination.
444 */
Al Viro96f9bc82014-04-05 04:35:49 -0400445static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200446 splice_actor *actor)
447{
David Howells8cefc102019-11-15 13:30:32 +0000448 unsigned int head = pipe->head;
449 unsigned int tail = pipe->tail;
450 unsigned int mask = pipe->ring_size - 1;
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200451 int ret;
452
Linus Torvaldsec057592019-12-06 12:40:35 -0800453 while (!pipe_empty(head, tail)) {
David Howells8cefc102019-11-15 13:30:32 +0000454 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200455
456 sd->len = buf->len;
457 if (sd->len > sd->total_len)
458 sd->len = sd->total_len;
459
Miklos Szeredifba597d2016-09-27 10:45:12 +0200460 ret = pipe_buf_confirm(pipe, buf);
MichaƂ MirosƂawa8adbe32010-12-17 08:56:44 +0100461 if (unlikely(ret)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200462 if (ret == -ENODATA)
463 ret = 0;
464 return ret;
465 }
MichaƂ MirosƂawa8adbe32010-12-17 08:56:44 +0100466
467 ret = actor(pipe, buf, sd);
468 if (ret <= 0)
469 return ret;
470
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200471 buf->offset += ret;
472 buf->len -= ret;
473
474 sd->num_spliced += ret;
475 sd->len -= ret;
476 sd->pos += ret;
477 sd->total_len -= ret;
478
479 if (!buf->len) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200480 pipe_buf_release(pipe, buf);
David Howells8cefc102019-11-15 13:30:32 +0000481 tail++;
482 pipe->tail = tail;
Al Viro6447a3c2013-03-21 11:01:38 -0400483 if (pipe->files)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200484 sd->need_wakeup = true;
485 }
486
487 if (!sd->total_len)
488 return 0;
489 }
490
491 return 1;
492}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200493
Linus Torvaldsd1a819a2020-10-05 11:26:27 -0700494/* We know we have a pipe buffer, but maybe it's empty? */
495static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
496{
497 unsigned int tail = pipe->tail;
498 unsigned int mask = pipe->ring_size - 1;
499 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
500
501 if (unlikely(!buf->len)) {
502 pipe_buf_release(pipe, buf);
503 pipe->tail = tail+1;
504 return true;
505 }
506
507 return false;
508}
509
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200510/**
511 * splice_from_pipe_next - wait for some data to splice from
512 * @pipe: pipe to splice from
513 * @sd: information about the splice operation
514 *
515 * Description:
516 * This function will wait for some data and return a positive
517 * value (one) if pipe buffers are available. It will return zero
518 * or -errno if no more data needs to be spliced.
519 */
Al Viro96f9bc82014-04-05 04:35:49 -0400520static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200521{
Jan Karac725bfc2015-11-23 13:09:50 +0100522 /*
523 * Check for signal early to make process killable when there are
524 * always buffers available
525 */
526 if (signal_pending(current))
527 return -ERESTARTSYS;
528
Linus Torvaldsd1a819a2020-10-05 11:26:27 -0700529repeat:
David Howells8cefc102019-11-15 13:30:32 +0000530 while (pipe_empty(pipe->head, pipe->tail)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200531 if (!pipe->writers)
532 return 0;
533
Linus Torvaldsa28c8b92019-12-07 13:21:01 -0800534 if (sd->num_spliced)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200535 return 0;
536
537 if (sd->flags & SPLICE_F_NONBLOCK)
538 return -EAGAIN;
539
540 if (signal_pending(current))
541 return -ERESTARTSYS;
542
543 if (sd->need_wakeup) {
544 wakeup_pipe_writers(pipe);
545 sd->need_wakeup = false;
546 }
547
Linus Torvalds472e5b02020-10-01 19:14:36 -0700548 pipe_wait_readable(pipe);
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200549 }
550
Linus Torvaldsd1a819a2020-10-05 11:26:27 -0700551 if (eat_empty_buffer(pipe))
552 goto repeat;
553
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200554 return 1;
555}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200556
557/**
558 * splice_from_pipe_begin - start splicing from pipe
Randy Dunlapb80901b2009-04-16 19:09:55 -0700559 * @sd: information about the splice operation
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200560 *
561 * Description:
562 * This function should be called before a loop containing
563 * splice_from_pipe_next() and splice_from_pipe_feed() to
564 * initialize the necessary fields of @sd.
565 */
Al Viro96f9bc82014-04-05 04:35:49 -0400566static void splice_from_pipe_begin(struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200567{
568 sd->num_spliced = 0;
569 sd->need_wakeup = false;
570}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200571
572/**
573 * splice_from_pipe_end - finish splicing from pipe
574 * @pipe: pipe to splice from
575 * @sd: information about the splice operation
576 *
577 * Description:
578 * This function will wake up pipe writers if necessary. It should
579 * be called after a loop containing splice_from_pipe_next() and
580 * splice_from_pipe_feed().
581 */
Al Viro96f9bc82014-04-05 04:35:49 -0400582static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200583{
584 if (sd->need_wakeup)
585 wakeup_pipe_writers(pipe);
586}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200587
Jens Axboe932cc6d2007-06-21 13:10:21 +0200588/**
589 * __splice_from_pipe - splice data from a pipe to given actor
590 * @pipe: pipe to splice from
591 * @sd: information to @actor
592 * @actor: handler that splices the data
593 *
594 * Description:
595 * This function does little more than loop over the pipe and call
596 * @actor to do the actual moving of a single struct pipe_buffer to
David Howells2dc334f2023-06-07 19:19:09 +0100597 * the desired destination. See pipe_to_file, pipe_to_sendmsg, or
Jens Axboe932cc6d2007-06-21 13:10:21 +0200598 * pipe_to_user.
599 *
Jens Axboe83f91352006-04-02 23:05:09 +0200600 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200601ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
602 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200603{
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200604 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200605
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200606 splice_from_pipe_begin(sd);
607 do {
Jan Karac2489e02015-11-23 13:09:51 +0100608 cond_resched();
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200609 ret = splice_from_pipe_next(pipe, sd);
610 if (ret > 0)
611 ret = splice_from_pipe_feed(pipe, sd, actor);
612 } while (ret > 0);
613 splice_from_pipe_end(pipe, sd);
Jens Axboe5274f052006-03-30 15:15:30 +0200614
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200615 return sd->num_spliced ? sd->num_spliced : ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200616}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100617EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200618
Jens Axboe932cc6d2007-06-21 13:10:21 +0200619/**
620 * splice_from_pipe - splice data from a pipe to a file
621 * @pipe: pipe to splice from
622 * @out: file to splice to
623 * @ppos: position in @out
624 * @len: how many bytes to splice
625 * @flags: splice modifier flags
626 * @actor: handler that splices the data
627 *
628 * Description:
Miklos Szeredi29339702009-04-14 19:48:37 +0200629 * See __splice_from_pipe. This function locks the pipe inode,
Jens Axboe932cc6d2007-06-21 13:10:21 +0200630 * otherwise it's identical to __splice_from_pipe().
631 *
632 */
Mark Fasheh6da61802006-10-17 18:43:07 +0200633ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
634 loff_t *ppos, size_t len, unsigned int flags,
635 splice_actor *actor)
636{
637 ssize_t ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200638 struct splice_desc sd = {
639 .total_len = len,
640 .flags = flags,
641 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200642 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200643 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200644
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200645 pipe_lock(pipe);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200646 ret = __splice_from_pipe(pipe, &sd, actor);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200647 pipe_unlock(pipe);
Mark Fasheh6da61802006-10-17 18:43:07 +0200648
649 return ret;
650}
651
652/**
Al Viro8d020762014-04-05 04:27:08 -0400653 * iter_file_splice_write - splice data from a pipe to a file
654 * @pipe: pipe info
655 * @out: file to write to
656 * @ppos: position in @out
657 * @len: number of bytes to splice
658 * @flags: splice modifier flags
659 *
660 * Description:
661 * Will either move or copy pages (determined by @flags options) from
662 * the given pipe inode to the given file.
663 * This one is ->write_iter-based.
664 *
665 */
666ssize_t
667iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
668 loff_t *ppos, size_t len, unsigned int flags)
669{
670 struct splice_desc sd = {
671 .total_len = len,
672 .flags = flags,
673 .pos = *ppos,
674 .u.file = out,
675 };
David Howells6718b6f2019-10-16 16:47:32 +0100676 int nbufs = pipe->max_usage;
Amir Goldsteind53471ba6f2023-11-23 18:51:44 +0100677 struct bio_vec *array;
Al Viro8d020762014-04-05 04:27:08 -0400678 ssize_t ret;
679
Amir Goldsteind53471ba6f2023-11-23 18:51:44 +0100680 if (!out->f_op->write_iter)
681 return -EINVAL;
682
683 array = kcalloc(nbufs, sizeof(struct bio_vec), GFP_KERNEL);
Al Viro8d020762014-04-05 04:27:08 -0400684 if (unlikely(!array))
685 return -ENOMEM;
686
687 pipe_lock(pipe);
688
689 splice_from_pipe_begin(&sd);
690 while (sd.total_len) {
Amir Goldsteind53471ba6f2023-11-23 18:51:44 +0100691 struct kiocb kiocb;
Al Viro8d020762014-04-05 04:27:08 -0400692 struct iov_iter from;
Linus Torvaldsec057592019-12-06 12:40:35 -0800693 unsigned int head, tail, mask;
Al Viro8d020762014-04-05 04:27:08 -0400694 size_t left;
David Howells8cefc102019-11-15 13:30:32 +0000695 int n;
Al Viro8d020762014-04-05 04:27:08 -0400696
697 ret = splice_from_pipe_next(pipe, &sd);
698 if (ret <= 0)
699 break;
700
David Howells6718b6f2019-10-16 16:47:32 +0100701 if (unlikely(nbufs < pipe->max_usage)) {
Al Viro8d020762014-04-05 04:27:08 -0400702 kfree(array);
David Howells6718b6f2019-10-16 16:47:32 +0100703 nbufs = pipe->max_usage;
Al Viro8d020762014-04-05 04:27:08 -0400704 array = kcalloc(nbufs, sizeof(struct bio_vec),
705 GFP_KERNEL);
706 if (!array) {
707 ret = -ENOMEM;
708 break;
709 }
710 }
711
Linus Torvaldsec057592019-12-06 12:40:35 -0800712 head = pipe->head;
713 tail = pipe->tail;
714 mask = pipe->ring_size - 1;
715
Al Viro8d020762014-04-05 04:27:08 -0400716 /* build the vector */
717 left = sd.total_len;
Pavel Begunkov0f1d3442021-01-09 16:02:57 +0000718 for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
David Howells8cefc102019-11-15 13:30:32 +0000719 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Al Viro8d020762014-04-05 04:27:08 -0400720 size_t this_len = buf->len;
721
Pavel Begunkov0f1d3442021-01-09 16:02:57 +0000722 /* zero-length bvecs are not supported, skip them */
723 if (!this_len)
724 continue;
725 this_len = min(this_len, left);
Al Viro8d020762014-04-05 04:27:08 -0400726
Miklos Szeredifba597d2016-09-27 10:45:12 +0200727 ret = pipe_buf_confirm(pipe, buf);
Al Viro8d020762014-04-05 04:27:08 -0400728 if (unlikely(ret)) {
729 if (ret == -ENODATA)
730 ret = 0;
731 goto done;
732 }
733
Christoph Hellwig664e4072023-02-03 16:06:28 +0100734 bvec_set_page(&array[n], buf->page, this_len,
735 buf->offset);
Al Viro8d020762014-04-05 04:27:08 -0400736 left -= this_len;
Pavel Begunkov0f1d3442021-01-09 16:02:57 +0000737 n++;
Al Viro8d020762014-04-05 04:27:08 -0400738 }
739
Al Virode4eda92022-09-15 20:25:47 -0400740 iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left);
Amir Goldsteind53471ba6f2023-11-23 18:51:44 +0100741 init_sync_kiocb(&kiocb, out);
742 kiocb.ki_pos = sd.pos;
Miklos Szeredi7c98f7c2023-08-28 17:13:18 +0200743 ret = out->f_op->write_iter(&kiocb, &from);
Amir Goldsteind53471ba6f2023-11-23 18:51:44 +0100744 sd.pos = kiocb.ki_pos;
Al Viro8d020762014-04-05 04:27:08 -0400745 if (ret <= 0)
746 break;
747
748 sd.num_spliced += ret;
749 sd.total_len -= ret;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100750 *ppos = sd.pos;
Al Viro8d020762014-04-05 04:27:08 -0400751
752 /* dismiss the fully eaten buffers, adjust the partial one */
David Howells8cefc102019-11-15 13:30:32 +0000753 tail = pipe->tail;
Al Viro8d020762014-04-05 04:27:08 -0400754 while (ret) {
David Howells8cefc102019-11-15 13:30:32 +0000755 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Al Viro8d020762014-04-05 04:27:08 -0400756 if (ret >= buf->len) {
Al Viro8d020762014-04-05 04:27:08 -0400757 ret -= buf->len;
758 buf->len = 0;
Miklos Szeredia7796382016-09-27 10:45:12 +0200759 pipe_buf_release(pipe, buf);
David Howells8cefc102019-11-15 13:30:32 +0000760 tail++;
761 pipe->tail = tail;
Al Viro8d020762014-04-05 04:27:08 -0400762 if (pipe->files)
763 sd.need_wakeup = true;
764 } else {
765 buf->offset += ret;
766 buf->len -= ret;
767 ret = 0;
768 }
769 }
770 }
771done:
772 kfree(array);
773 splice_from_pipe_end(pipe, &sd);
774
775 pipe_unlock(pipe);
776
777 if (sd.num_spliced)
778 ret = sd.num_spliced;
779
780 return ret;
781}
782
783EXPORT_SYMBOL(iter_file_splice_write);
784
David Howells2dc334f2023-06-07 19:19:09 +0100785#ifdef CONFIG_NET
Jens Axboe83f91352006-04-02 23:05:09 +0200786/**
David Howells2dc334f2023-06-07 19:19:09 +0100787 * splice_to_socket - splice data from a pipe to a socket
Jens Axboe932cc6d2007-06-21 13:10:21 +0200788 * @pipe: pipe to splice from
Jens Axboe83f91352006-04-02 23:05:09 +0200789 * @out: socket to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +0200790 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +0200791 * @len: number of bytes to splice
792 * @flags: splice modifier flags
793 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200794 * Description:
795 * Will send @len bytes from the pipe to a network socket. No data copying
796 * is involved.
Jens Axboe83f91352006-04-02 23:05:09 +0200797 *
798 */
David Howells2dc334f2023-06-07 19:19:09 +0100799ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
800 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200801{
David Howells2dc334f2023-06-07 19:19:09 +0100802 struct socket *sock = sock_from_file(out);
803 struct bio_vec bvec[16];
804 struct msghdr msg = {};
805 ssize_t ret = 0;
806 size_t spliced = 0;
807 bool need_wakeup = false;
Jens Axboe5274f052006-03-30 15:15:30 +0200808
David Howells2dc334f2023-06-07 19:19:09 +0100809 pipe_lock(pipe);
810
811 while (len > 0) {
812 unsigned int head, tail, mask, bc = 0;
813 size_t remain = len;
814
815 /*
816 * Check for signal early to make process killable when there
817 * are always buffers available
818 */
819 ret = -ERESTARTSYS;
820 if (signal_pending(current))
821 break;
822
823 while (pipe_empty(pipe->head, pipe->tail)) {
824 ret = 0;
825 if (!pipe->writers)
826 goto out;
827
828 if (spliced)
829 goto out;
830
831 ret = -EAGAIN;
832 if (flags & SPLICE_F_NONBLOCK)
833 goto out;
834
835 ret = -ERESTARTSYS;
836 if (signal_pending(current))
837 goto out;
838
839 if (need_wakeup) {
840 wakeup_pipe_writers(pipe);
841 need_wakeup = false;
842 }
843
844 pipe_wait_readable(pipe);
845 }
846
847 head = pipe->head;
848 tail = pipe->tail;
849 mask = pipe->ring_size - 1;
850
851 while (!pipe_empty(head, tail)) {
852 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
853 size_t seg;
854
855 if (!buf->len) {
856 tail++;
857 continue;
858 }
859
860 seg = min_t(size_t, remain, buf->len);
David Howells2dc334f2023-06-07 19:19:09 +0100861
862 ret = pipe_buf_confirm(pipe, buf);
863 if (unlikely(ret)) {
864 if (ret == -ENODATA)
865 ret = 0;
866 break;
867 }
868
869 bvec_set_page(&bvec[bc++], buf->page, seg, buf->offset);
870 remain -= seg;
David Howellsca2d49f2023-06-14 11:09:48 +0100871 if (remain == 0 || bc >= ARRAY_SIZE(bvec))
David Howells2dc334f2023-06-07 19:19:09 +0100872 break;
David Howellsca2d49f2023-06-14 11:09:48 +0100873 tail++;
David Howells2dc334f2023-06-07 19:19:09 +0100874 }
875
876 if (!bc)
877 break;
878
879 msg.msg_flags = MSG_SPLICE_PAGES;
880 if (flags & SPLICE_F_MORE)
881 msg.msg_flags |= MSG_MORE;
882 if (remain && pipe_occupancy(pipe->head, tail) > 0)
883 msg.msg_flags |= MSG_MORE;
Jan Stancek0f0fa272023-07-24 19:39:04 +0200884 if (out->f_flags & O_NONBLOCK)
885 msg.msg_flags |= MSG_DONTWAIT;
David Howells2dc334f2023-06-07 19:19:09 +0100886
887 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc,
888 len - remain);
889 ret = sock_sendmsg(sock, &msg);
890 if (ret <= 0)
891 break;
892
893 spliced += ret;
894 len -= ret;
895 tail = pipe->tail;
896 while (ret > 0) {
897 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
898 size_t seg = min_t(size_t, ret, buf->len);
899
900 buf->offset += seg;
901 buf->len -= seg;
902 ret -= seg;
903
904 if (!buf->len) {
905 pipe_buf_release(pipe, buf);
906 tail++;
907 }
908 }
909
910 if (tail != pipe->tail) {
911 pipe->tail = tail;
912 if (pipe->files)
913 need_wakeup = true;
914 }
915 }
916
917out:
918 pipe_unlock(pipe);
919 if (need_wakeup)
920 wakeup_pipe_writers(pipe);
921 return spliced ?: ret;
922}
923#endif
Jeff Garzika0f06782006-03-30 23:06:13 -0500924
Christoph Hellwig36e2c742020-09-03 16:22:34 +0200925static int warn_unsupported(struct file *file, const char *op)
926{
927 pr_debug_ratelimited(
928 "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
929 op, file, current->pid, current->comm);
930 return -EINVAL;
931}
932
Jens Axboe83f91352006-04-02 23:05:09 +0200933/*
934 * Attempt to initiate a splice from pipe to file.
935 */
Amir Goldstein0f292082023-12-12 11:44:36 +0200936static ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
937 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200938{
Christoph Hellwig36e2c742020-09-03 16:22:34 +0200939 if (unlikely(!out->f_op->splice_write))
940 return warn_unsupported(out, "write");
941 return out->f_op->splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200942}
943
Jens Axboe83f91352006-04-02 23:05:09 +0200944/*
David Howells2bfc6682023-06-07 19:19:10 +0100945 * Indicate to the caller that there was a premature EOF when reading from the
946 * source and the caller didn't indicate they would be sending more data after
947 * this.
948 */
949static void do_splice_eof(struct splice_desc *sd)
950{
951 if (sd->splice_eof)
952 sd->splice_eof(sd);
953}
954
Amir Goldsteinfeebea72023-11-22 14:27:02 +0200955/*
956 * Callers already called rw_verify_area() on the entire range.
957 * No need to call it for sub ranges.
958 */
Amir Goldstein0f292082023-12-12 11:44:36 +0200959static ssize_t do_splice_read(struct file *in, loff_t *ppos,
960 struct pipe_inode_info *pipe, size_t len,
961 unsigned int flags)
Amir Goldsteinfeebea72023-11-22 14:27:02 +0200962{
963 unsigned int p_space;
964
965 if (unlikely(!(in->f_mode & FMODE_READ)))
966 return -EBADF;
967 if (!len)
968 return 0;
969
970 /* Don't try to read more the pipe has space for. */
971 p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
972 len = min_t(size_t, len, p_space << PAGE_SHIFT);
973
974 if (unlikely(len > MAX_RW_COUNT))
975 len = MAX_RW_COUNT;
976
977 if (unlikely(!in->f_op->splice_read))
978 return warn_unsupported(in, "read");
979 /*
980 * O_DIRECT and DAX don't deal with the pagecache, so we allocate a
981 * buffer, copy into it and splice that into the pipe.
982 */
983 if ((in->f_flags & O_DIRECT) || IS_DAX(in->f_mapping->host))
984 return copy_splice_read(in, ppos, pipe, len, flags);
985 return in->f_op->splice_read(in, ppos, pipe, len, flags);
986}
987
David Howells6a3f30b2023-05-22 14:49:52 +0100988/**
989 * vfs_splice_read - Read data from a file and splice it into a pipe
990 * @in: File to splice from
991 * @ppos: Input file offset
992 * @pipe: Pipe to splice to
993 * @len: Number of bytes to splice
994 * @flags: Splice modifier flags (SPLICE_F_*)
995 *
996 * Splice the requested amount of data from the input file to the pipe. This
997 * is synchronous as the caller must hold the pipe lock across the entire
998 * operation.
999 *
1000 * If successful, it returns the amount of data spliced, 0 if it hit the EOF or
1001 * a hole and a negative error code otherwise.
Jens Axboe83f91352006-04-02 23:05:09 +02001002 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001003ssize_t vfs_splice_read(struct file *in, loff_t *ppos,
1004 struct pipe_inode_info *pipe, size_t len,
1005 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001006{
Amir Goldstein0f292082023-12-12 11:44:36 +02001007 ssize_t ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001008
Jens Axboecbb7e572006-04-11 14:57:50 +02001009 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +02001010 if (unlikely(ret < 0))
1011 return ret;
1012
Amir Goldsteinfeebea72023-11-22 14:27:02 +02001013 return do_splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001014}
David Howells6a3f30b2023-05-22 14:49:52 +01001015EXPORT_SYMBOL_GPL(vfs_splice_read);
Jens Axboe5274f052006-03-30 15:15:30 +02001016
Jens Axboe932cc6d2007-06-21 13:10:21 +02001017/**
1018 * splice_direct_to_actor - splices data directly between two non-pipes
1019 * @in: file to splice from
1020 * @sd: actor information on where to splice to
1021 * @actor: handles the data splicing
1022 *
1023 * Description:
1024 * This is a special case helper to splice directly between two
1025 * points, without requiring an explicit pipe. Internally an allocated
Randy Dunlap79685b82007-07-27 08:08:51 +02001026 * pipe is cached in the process, and reused during the lifetime of
Jens Axboe932cc6d2007-06-21 13:10:21 +02001027 * that process.
1028 *
Jens Axboec66ab6f2007-06-12 21:17:17 +02001029 */
1030ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1031 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +02001032{
1033 struct pipe_inode_info *pipe;
Amir Goldstein0f292082023-12-12 11:44:36 +02001034 ssize_t ret, bytes;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001035 size_t len;
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001036 int i, flags, more;
Jens Axboeb92ce552006-04-11 13:52:07 +02001037
1038 /*
Jason A. Donenfeld97ef77c2022-06-29 15:06:58 +02001039 * We require the input to be seekable, as we don't want to randomly
1040 * drop data for eg socket -> socket splicing. Use the piped splicing
1041 * for that!
Jens Axboeb92ce552006-04-11 13:52:07 +02001042 */
Jason A. Donenfeld97ef77c2022-06-29 15:06:58 +02001043 if (unlikely(!(in->f_mode & FMODE_LSEEK)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001044 return -EINVAL;
1045
1046 /*
1047 * neither in nor out is a pipe, setup an internal pipe attached to
1048 * 'out' and transfer the wanted data from 'in' to 'out' through that
1049 */
1050 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +02001051 if (unlikely(!pipe)) {
Al Viro7bee1302013-03-21 11:04:15 -04001052 pipe = alloc_pipe_info();
Jens Axboeb92ce552006-04-11 13:52:07 +02001053 if (!pipe)
1054 return -ENOMEM;
1055
1056 /*
1057 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +02001058 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +02001059 * PIPE_READERS appropriately.
1060 */
1061 pipe->readers = 1;
1062
1063 current->splice_pipe = pipe;
1064 }
1065
1066 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +02001067 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +02001068 */
Jens Axboeb92ce552006-04-11 13:52:07 +02001069 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001070 len = sd->total_len;
David Howells219d9202023-06-07 19:19:16 +01001071
1072 /* Don't block on output, we have to drain the direct pipe. */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001073 flags = sd->flags;
David Howells219d9202023-06-07 19:19:16 +01001074 sd->flags &= ~SPLICE_F_NONBLOCK;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001075
1076 /*
David Howells219d9202023-06-07 19:19:16 +01001077 * We signal MORE until we've read sufficient data to fulfill the
1078 * request and we keep signalling it if the caller set it.
Jens Axboec66ab6f2007-06-12 21:17:17 +02001079 */
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001080 more = sd->flags & SPLICE_F_MORE;
David Howells219d9202023-06-07 19:19:16 +01001081 sd->flags |= SPLICE_F_MORE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001082
David Howells8cefc102019-11-15 13:30:32 +00001083 WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
Darrick J. Wong17614442018-11-30 10:37:49 -08001084
Jens Axboeb92ce552006-04-11 13:52:07 +02001085 while (len) {
Jens Axboe51a92c02007-07-13 14:11:43 +02001086 size_t read_len;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001087 loff_t pos = sd->pos, prev_pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001088
Amir Goldsteinfeebea72023-11-22 14:27:02 +02001089 ret = do_splice_read(in, &pos, pipe, len, flags);
Jens Axboe51a92c02007-07-13 14:11:43 +02001090 if (unlikely(ret <= 0))
David Howells2bfc6682023-06-07 19:19:10 +01001091 goto read_failure;
Jens Axboeb92ce552006-04-11 13:52:07 +02001092
1093 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001094 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +02001095
1096 /*
David Howells219d9202023-06-07 19:19:16 +01001097 * If we now have sufficient data to fulfill the request then
1098 * we clear SPLICE_F_MORE if it was not set initially.
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001099 */
David Howells219d9202023-06-07 19:19:16 +01001100 if (read_len >= len && !more)
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001101 sd->flags &= ~SPLICE_F_MORE;
David Howells219d9202023-06-07 19:19:16 +01001102
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001103 /*
Jens Axboeb92ce552006-04-11 13:52:07 +02001104 * NOTE: nonblocking mode only applies to the input. We
1105 * must not do the output in nonblocking mode as then we
1106 * could get stuck data in the internal pipe:
1107 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001108 ret = actor(pipe, sd);
Tom Zanussia82c53a2008-05-09 13:28:36 +02001109 if (unlikely(ret <= 0)) {
1110 sd->pos = prev_pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001111 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001112 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001113
1114 bytes += ret;
1115 len -= ret;
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001116 sd->pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001117
Tom Zanussia82c53a2008-05-09 13:28:36 +02001118 if (ret < read_len) {
1119 sd->pos = prev_pos + ret;
Jens Axboe51a92c02007-07-13 14:11:43 +02001120 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001121 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001122 }
1123
Jens Axboe9e971982008-01-29 21:05:57 +01001124done:
David Howells8cefc102019-11-15 13:30:32 +00001125 pipe->tail = pipe->head = 0;
Jens Axboe80848702008-01-30 12:24:48 +01001126 file_accessed(in);
Jens Axboeb92ce552006-04-11 13:52:07 +02001127 return bytes;
1128
David Howells2bfc6682023-06-07 19:19:10 +01001129read_failure:
1130 /*
1131 * If the user did *not* set SPLICE_F_MORE *and* we didn't hit that
1132 * "use all of len" case that cleared SPLICE_F_MORE, *and* we did a
1133 * "->splice_in()" that returned EOF (ie zero) *and* we have sent at
1134 * least 1 byte *then* we will also do the ->splice_eof() call.
1135 */
1136 if (ret == 0 && !more && len > 0 && bytes)
1137 do_splice_eof(sd);
Jens Axboeb92ce552006-04-11 13:52:07 +02001138out_release:
1139 /*
1140 * If we did an incomplete transfer we must release
1141 * the pipe buffers in question:
1142 */
David Howells8cefc102019-11-15 13:30:32 +00001143 for (i = 0; i < pipe->ring_size; i++) {
1144 struct pipe_buffer *buf = &pipe->bufs[i];
Jens Axboeb92ce552006-04-11 13:52:07 +02001145
Miklos Szeredia7796382016-09-27 10:45:12 +02001146 if (buf->ops)
1147 pipe_buf_release(pipe, buf);
Jens Axboeb92ce552006-04-11 13:52:07 +02001148 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001149
Jens Axboe9e971982008-01-29 21:05:57 +01001150 if (!bytes)
1151 bytes = ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001152
Jens Axboe9e971982008-01-29 21:05:57 +01001153 goto done;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001154}
1155EXPORT_SYMBOL(splice_direct_to_actor);
1156
1157static int direct_splice_actor(struct pipe_inode_info *pipe,
1158 struct splice_desc *sd)
1159{
Jens Axboe6a14b902007-06-14 13:08:55 +02001160 struct file *file = sd->u.file;
Amir Goldsteinda404482023-11-30 16:16:23 +02001161 long ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001162
Amir Goldsteinda404482023-11-30 16:16:23 +02001163 file_start_write(file);
1164 ret = do_splice_from(pipe, file, sd->opos, sd->total_len, sd->flags);
1165 file_end_write(file);
1166 return ret;
1167}
1168
1169static int splice_file_range_actor(struct pipe_inode_info *pipe,
1170 struct splice_desc *sd)
1171{
1172 struct file *file = sd->u.file;
1173
1174 return do_splice_from(pipe, file, sd->opos, sd->total_len, sd->flags);
Jens Axboec66ab6f2007-06-12 21:17:17 +02001175}
1176
David Howells2bfc6682023-06-07 19:19:10 +01001177static void direct_file_splice_eof(struct splice_desc *sd)
1178{
1179 struct file *file = sd->u.file;
1180
1181 if (file->f_op->splice_eof)
1182 file->f_op->splice_eof(file);
1183}
1184
Amir Goldstein0f292082023-12-12 11:44:36 +02001185static ssize_t do_splice_direct_actor(struct file *in, loff_t *ppos,
1186 struct file *out, loff_t *opos,
1187 size_t len, unsigned int flags,
1188 splice_direct_actor *actor)
Amir Goldstein488e8f62023-11-30 16:16:22 +02001189{
1190 struct splice_desc sd = {
1191 .len = len,
1192 .total_len = len,
1193 .flags = flags,
1194 .pos = *ppos,
1195 .u.file = out,
1196 .splice_eof = direct_file_splice_eof,
1197 .opos = opos,
1198 };
Amir Goldstein0f292082023-12-12 11:44:36 +02001199 ssize_t ret;
Amir Goldstein488e8f62023-11-30 16:16:22 +02001200
1201 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1202 return -EBADF;
1203
1204 if (unlikely(out->f_flags & O_APPEND))
1205 return -EINVAL;
1206
1207 ret = splice_direct_to_actor(in, &sd, actor);
1208 if (ret > 0)
1209 *ppos = sd.pos;
1210
1211 return ret;
1212}
Jens Axboe932cc6d2007-06-21 13:10:21 +02001213/**
1214 * do_splice_direct - splices data directly between two files
1215 * @in: file to splice from
1216 * @ppos: input file offset
1217 * @out: file to splice to
Randy Dunlapacdb37c2013-06-22 19:44:08 -07001218 * @opos: output file offset
Jens Axboe932cc6d2007-06-21 13:10:21 +02001219 * @len: number of bytes to splice
1220 * @flags: splice modifier flags
1221 *
1222 * Description:
1223 * For use by do_sendfile(). splice can easily emulate sendfile, but
1224 * doing it in the application would incur an extra system call
1225 * (splice in + splice out, as compared to just sendfile()). So this helper
1226 * can splice directly through a process-private pipe.
1227 *
Amir Goldstein2a33e2d2023-11-22 14:27:01 +02001228 * Callers already called rw_verify_area() on the entire range.
Jens Axboe932cc6d2007-06-21 13:10:21 +02001229 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001230ssize_t do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1231 loff_t *opos, size_t len, unsigned int flags)
Jens Axboec66ab6f2007-06-12 21:17:17 +02001232{
Amir Goldstein488e8f62023-11-30 16:16:22 +02001233 return do_splice_direct_actor(in, ppos, out, opos, len, flags,
1234 direct_splice_actor);
Jens Axboeb92ce552006-04-11 13:52:07 +02001235}
Miklos Szeredi1c118592014-10-24 00:14:35 +02001236EXPORT_SYMBOL(do_splice_direct);
Jens Axboeb92ce552006-04-11 13:52:07 +02001237
Amir Goldstein488e8f62023-11-30 16:16:22 +02001238/**
1239 * splice_file_range - splices data between two files for copy_file_range()
1240 * @in: file to splice from
1241 * @ppos: input file offset
1242 * @out: file to splice to
1243 * @opos: output file offset
1244 * @len: number of bytes to splice
1245 *
1246 * Description:
Amir Goldstein705bcfc2023-12-12 11:44:37 +02001247 * For use by ->copy_file_range() methods.
Amir Goldsteinda404482023-11-30 16:16:23 +02001248 * Like do_splice_direct(), but vfs_copy_file_range() already holds
1249 * start_file_write() on @out file.
Amir Goldstein488e8f62023-11-30 16:16:22 +02001250 *
1251 * Callers already called rw_verify_area() on the entire range.
1252 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001253ssize_t splice_file_range(struct file *in, loff_t *ppos, struct file *out,
1254 loff_t *opos, size_t len)
Amir Goldstein488e8f62023-11-30 16:16:22 +02001255{
1256 lockdep_assert(file_write_started(out));
1257
Amir Goldstein705bcfc2023-12-12 11:44:37 +02001258 return do_splice_direct_actor(in, ppos, out, opos,
1259 min_t(size_t, len, MAX_RW_COUNT),
1260 0, splice_file_range_actor);
Amir Goldstein488e8f62023-11-30 16:16:22 +02001261}
1262EXPORT_SYMBOL(splice_file_range);
1263
Al Viro8924fef2016-09-17 20:44:45 -04001264static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1265{
Linus Torvalds52bce912016-12-21 10:59:34 -08001266 for (;;) {
1267 if (unlikely(!pipe->readers)) {
1268 send_sig(SIGPIPE, current, 0);
1269 return -EPIPE;
1270 }
David Howells6718b6f2019-10-16 16:47:32 +01001271 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
Linus Torvalds52bce912016-12-21 10:59:34 -08001272 return 0;
Al Viro8924fef2016-09-17 20:44:45 -04001273 if (flags & SPLICE_F_NONBLOCK)
1274 return -EAGAIN;
1275 if (signal_pending(current))
1276 return -ERESTARTSYS;
Linus Torvalds472e5b02020-10-01 19:14:36 -07001277 pipe_wait_writable(pipe);
Al Viro8924fef2016-09-17 20:44:45 -04001278 }
Al Viro8924fef2016-09-17 20:44:45 -04001279}
1280
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001281static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1282 struct pipe_inode_info *opipe,
1283 size_t len, unsigned int flags);
Jens Axboeddac0d32006-11-04 12:49:32 +01001284
Amir Goldstein0f292082023-12-12 11:44:36 +02001285ssize_t splice_file_to_pipe(struct file *in,
1286 struct pipe_inode_info *opipe,
1287 loff_t *offset,
1288 size_t len, unsigned int flags)
Al Virofaa97c42021-01-25 22:23:03 -05001289{
Amir Goldstein0f292082023-12-12 11:44:36 +02001290 ssize_t ret;
Al Virofaa97c42021-01-25 22:23:03 -05001291
1292 pipe_lock(opipe);
1293 ret = wait_for_space(opipe, flags);
1294 if (!ret)
Amir Goldsteinb70d8e22023-11-22 14:27:03 +02001295 ret = do_splice_read(in, offset, opipe, len, flags);
Al Virofaa97c42021-01-25 22:23:03 -05001296 pipe_unlock(opipe);
1297 if (ret > 0)
1298 wakeup_pipe_readers(opipe);
1299 return ret;
1300}
1301
Jens Axboeddac0d32006-11-04 12:49:32 +01001302/*
Jens Axboe83f91352006-04-02 23:05:09 +02001303 * Determine where to splice to/from.
1304 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001305ssize_t do_splice(struct file *in, loff_t *off_in, struct file *out,
1306 loff_t *off_out, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001307{
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001308 struct pipe_inode_info *ipipe;
1309 struct pipe_inode_info *opipe;
Al Viro7995bd22013-06-20 18:58:36 +04001310 loff_t offset;
Amir Goldstein0f292082023-12-12 11:44:36 +02001311 ssize_t ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001312
Pavel Begunkov90da2e32020-05-04 22:39:35 +03001313 if (unlikely(!(in->f_mode & FMODE_READ) ||
1314 !(out->f_mode & FMODE_WRITE)))
1315 return -EBADF;
1316
David Howellsc73be612020-01-14 17:07:11 +00001317 ipipe = get_pipe_info(in, true);
1318 opipe = get_pipe_info(out, true);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001319
1320 if (ipipe && opipe) {
1321 if (off_in || off_out)
1322 return -ESPIPE;
1323
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001324 /* Splicing to self would be fun, but... */
1325 if (ipipe == opipe)
1326 return -EINVAL;
1327
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001328 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1329 flags |= SPLICE_F_NONBLOCK;
1330
Ahelenia ZiemiaƄska12ee4b62023-07-03 16:42:13 +02001331 ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
1332 } else if (ipipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001333 if (off_in)
1334 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001335 if (off_out) {
Changli Gao19c9a492010-06-29 13:10:36 +02001336 if (!(out->f_mode & FMODE_PWRITE))
Jens Axboeb92ce552006-04-11 13:52:07 +02001337 return -EINVAL;
Jens Axboeee6e00c2020-10-22 14:15:51 -06001338 offset = *off_out;
Al Viro7995bd22013-06-20 18:58:36 +04001339 } else {
1340 offset = out->f_pos;
1341 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001342
Al Viro18c67cb2013-06-19 15:41:54 +04001343 if (unlikely(out->f_flags & O_APPEND))
1344 return -EINVAL;
1345
1346 ret = rw_verify_area(WRITE, out, &offset, len);
1347 if (unlikely(ret < 0))
1348 return ret;
1349
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001350 if (in->f_flags & O_NONBLOCK)
1351 flags |= SPLICE_F_NONBLOCK;
1352
Al Viro500368f2013-05-23 20:07:11 -04001353 file_start_write(out);
Al Viro7995bd22013-06-20 18:58:36 +04001354 ret = do_splice_from(ipipe, out, &offset, len, flags);
Al Viro500368f2013-05-23 20:07:11 -04001355 file_end_write(out);
Jens Axboea4514eb2006-04-19 15:57:05 +02001356
Al Viro7995bd22013-06-20 18:58:36 +04001357 if (!off_out)
1358 out->f_pos = offset;
Jens Axboeee6e00c2020-10-22 14:15:51 -06001359 else
1360 *off_out = offset;
Ahelenia ZiemiaƄska12ee4b62023-07-03 16:42:13 +02001361 } else if (opipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001362 if (off_out)
1363 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001364 if (off_in) {
Changli Gao19c9a492010-06-29 13:10:36 +02001365 if (!(in->f_mode & FMODE_PREAD))
Jens Axboeb92ce552006-04-11 13:52:07 +02001366 return -EINVAL;
Jens Axboeee6e00c2020-10-22 14:15:51 -06001367 offset = *off_in;
Al Viro7995bd22013-06-20 18:58:36 +04001368 } else {
1369 offset = in->f_pos;
1370 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001371
Amir Goldsteinb70d8e22023-11-22 14:27:03 +02001372 ret = rw_verify_area(READ, in, &offset, len);
1373 if (unlikely(ret < 0))
1374 return ret;
1375
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001376 if (out->f_flags & O_NONBLOCK)
1377 flags |= SPLICE_F_NONBLOCK;
1378
Al Virofaa97c42021-01-25 22:23:03 -05001379 ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
Chung-Chiang Cheng983652c2023-03-22 14:25:19 +08001380
Al Viro7995bd22013-06-20 18:58:36 +04001381 if (!off_in)
1382 in->f_pos = offset;
Jens Axboeee6e00c2020-10-22 14:15:51 -06001383 else
1384 *off_in = offset;
Ahelenia ZiemiaƄska12ee4b62023-07-03 16:42:13 +02001385 } else {
1386 ret = -EINVAL;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001387 }
Jens Axboe5274f052006-03-30 15:15:30 +02001388
Ahelenia ZiemiaƄska12ee4b62023-07-03 16:42:13 +02001389 if (ret > 0) {
1390 /*
1391 * Generate modify out before access in:
1392 * do_splice_from() may've already sent modify out,
1393 * and this ensures the events get merged.
1394 */
1395 fsnotify_modify(out);
1396 fsnotify_access(in);
1397 }
1398
1399 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001400}
1401
Amir Goldstein0f292082023-12-12 11:44:36 +02001402static ssize_t __do_splice(struct file *in, loff_t __user *off_in,
1403 struct file *out, loff_t __user *off_out,
1404 size_t len, unsigned int flags)
Jens Axboeee6e00c2020-10-22 14:15:51 -06001405{
1406 struct pipe_inode_info *ipipe;
1407 struct pipe_inode_info *opipe;
1408 loff_t offset, *__off_in = NULL, *__off_out = NULL;
Amir Goldstein0f292082023-12-12 11:44:36 +02001409 ssize_t ret;
Jens Axboeee6e00c2020-10-22 14:15:51 -06001410
1411 ipipe = get_pipe_info(in, true);
1412 opipe = get_pipe_info(out, true);
1413
Jens Axboe0f99fc52023-04-24 16:32:55 -06001414 if (ipipe) {
1415 if (off_in)
1416 return -ESPIPE;
1417 pipe_clear_nowait(in);
1418 }
1419 if (opipe) {
1420 if (off_out)
1421 return -ESPIPE;
1422 pipe_clear_nowait(out);
1423 }
Jens Axboeee6e00c2020-10-22 14:15:51 -06001424
1425 if (off_out) {
1426 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1427 return -EFAULT;
1428 __off_out = &offset;
1429 }
1430 if (off_in) {
1431 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1432 return -EFAULT;
1433 __off_in = &offset;
1434 }
1435
1436 ret = do_splice(in, __off_in, out, __off_out, len, flags);
1437 if (ret < 0)
1438 return ret;
1439
1440 if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1441 return -EFAULT;
1442 if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1443 return -EFAULT;
1444
1445 return ret;
1446}
1447
Amir Goldstein0f292082023-12-12 11:44:36 +02001448static ssize_t iter_to_pipe(struct iov_iter *from,
1449 struct pipe_inode_info *pipe,
1450 unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001451{
Al Viro79fddc42016-09-17 22:38:20 -04001452 struct pipe_buffer buf = {
1453 .ops = &user_page_pipe_buf_ops,
1454 .flags = flags
1455 };
1456 size_t total = 0;
Amir Goldstein0f292082023-12-12 11:44:36 +02001457 ssize_t ret = 0;
Al Viro79fddc42016-09-17 22:38:20 -04001458
Al Viro7d690c12022-06-09 11:07:52 -04001459 while (iov_iter_count(from)) {
Al Viro79fddc42016-09-17 22:38:20 -04001460 struct page *pages[16];
Al Viro7d690c12022-06-09 11:07:52 -04001461 ssize_t left;
Al Virodb85a9e2016-09-17 20:25:06 -04001462 size_t start;
Al Viro7d690c12022-06-09 11:07:52 -04001463 int i, n;
Jens Axboe912d35f2006-04-26 10:59:21 +02001464
Al Viro7d690c12022-06-09 11:07:52 -04001465 left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
1466 if (left <= 0) {
1467 ret = left;
Al Viro79fddc42016-09-17 22:38:20 -04001468 break;
1469 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001470
Al Viro7d690c12022-06-09 11:07:52 -04001471 n = DIV_ROUND_UP(left + start, PAGE_SIZE);
1472 for (i = 0; i < n; i++) {
1473 int size = min_t(int, left, PAGE_SIZE - start);
1474
1475 buf.page = pages[i];
1476 buf.offset = start;
1477 buf.len = size;
1478 ret = add_to_pipe(pipe, &buf);
1479 if (unlikely(ret < 0)) {
1480 iov_iter_revert(from, left);
1481 // this one got dropped by add_to_pipe()
1482 while (++i < n)
1483 put_page(pages[i]);
1484 goto out;
Al Viro79fddc42016-09-17 22:38:20 -04001485 }
Al Viro7d690c12022-06-09 11:07:52 -04001486 total += ret;
1487 left -= size;
1488 start = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +02001489 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001490 }
Al Viro7d690c12022-06-09 11:07:52 -04001491out:
Al Viro79fddc42016-09-17 22:38:20 -04001492 return total ? total : ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001493}
1494
Jens Axboe6a14b902007-06-14 13:08:55 +02001495static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1496 struct splice_desc *sd)
1497{
Al Viro6130f532014-02-03 18:19:51 -05001498 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1499 return n == sd->len ? n : -EFAULT;
Jens Axboe6a14b902007-06-14 13:08:55 +02001500}
1501
1502/*
1503 * For lack of a better implementation, implement vmsplice() to userspace
1504 * as a simple copy of the pipes pages to the user iov.
1505 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001506static ssize_t vmsplice_to_user(struct file *file, struct iov_iter *iter,
1507 unsigned int flags)
Jens Axboe6a14b902007-06-14 13:08:55 +02001508{
David Howellsc73be612020-01-14 17:07:11 +00001509 struct pipe_inode_info *pipe = get_pipe_info(file, true);
Al Viro87a30022018-05-26 21:39:52 -04001510 struct splice_desc sd = {
1511 .total_len = iov_iter_count(iter),
1512 .flags = flags,
1513 .u.data = iter
1514 };
Amir Goldstein0f292082023-12-12 11:44:36 +02001515 ssize_t ret = 0;
Jens Axboe6a14b902007-06-14 13:08:55 +02001516
Jens Axboe6a14b902007-06-14 13:08:55 +02001517 if (!pipe)
1518 return -EBADF;
1519
Jens Axboe0f99fc52023-04-24 16:32:55 -06001520 pipe_clear_nowait(file);
1521
Al Viro345995f2015-03-21 19:17:55 -04001522 if (sd.total_len) {
1523 pipe_lock(pipe);
1524 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1525 pipe_unlock(pipe);
1526 }
Jens Axboe6a14b902007-06-14 13:08:55 +02001527
Ahelenia ZiemiaƄska7f0f1ea02023-07-03 16:42:17 +02001528 if (ret > 0)
1529 fsnotify_access(file);
1530
Jens Axboe6a14b902007-06-14 13:08:55 +02001531 return ret;
1532}
1533
Jens Axboe912d35f2006-04-26 10:59:21 +02001534/*
1535 * vmsplice splices a user address range into a pipe. It can be thought of
1536 * as splice-from-memory, where the regular splice is splice-from-file (or
1537 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001538 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001539static ssize_t vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1540 unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001541{
Jens Axboeddac0d32006-11-04 12:49:32 +01001542 struct pipe_inode_info *pipe;
Amir Goldstein0f292082023-12-12 11:44:36 +02001543 ssize_t ret = 0;
Al Viro79fddc42016-09-17 22:38:20 -04001544 unsigned buf_flag = 0;
1545
1546 if (flags & SPLICE_F_GIFT)
1547 buf_flag = PIPE_BUF_FLAG_GIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +02001548
David Howellsc73be612020-01-14 17:07:11 +00001549 pipe = get_pipe_info(file, true);
Jens Axboeddac0d32006-11-04 12:49:32 +01001550 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001551 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001552
Jens Axboe0f99fc52023-04-24 16:32:55 -06001553 pipe_clear_nowait(file);
1554
Al Viro8924fef2016-09-17 20:44:45 -04001555 pipe_lock(pipe);
1556 ret = wait_for_space(pipe, flags);
Al Viro79fddc42016-09-17 22:38:20 -04001557 if (!ret)
Al Viro87a30022018-05-26 21:39:52 -04001558 ret = iter_to_pipe(iter, pipe, buf_flag);
Al Viro8924fef2016-09-17 20:44:45 -04001559 pipe_unlock(pipe);
Ahelenia ZiemiaƄska7f0f1ea02023-07-03 16:42:17 +02001560 if (ret > 0) {
Al Viro8924fef2016-09-17 20:44:45 -04001561 wakeup_pipe_readers(pipe);
Ahelenia ZiemiaƄska7f0f1ea02023-07-03 16:42:17 +02001562 fsnotify_modify(file);
1563 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001564 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001565}
1566
Al Viro87a30022018-05-26 21:39:52 -04001567static int vmsplice_type(struct fd f, int *type)
1568{
1569 if (!f.file)
1570 return -EBADF;
1571 if (f.file->f_mode & FMODE_WRITE) {
Al Virode4eda92022-09-15 20:25:47 -04001572 *type = ITER_SOURCE;
Al Viro87a30022018-05-26 21:39:52 -04001573 } else if (f.file->f_mode & FMODE_READ) {
Al Virode4eda92022-09-15 20:25:47 -04001574 *type = ITER_DEST;
Al Viro87a30022018-05-26 21:39:52 -04001575 } else {
1576 fdput(f);
1577 return -EBADF;
1578 }
1579 return 0;
1580}
1581
Jens Axboe6a14b902007-06-14 13:08:55 +02001582/*
1583 * Note that vmsplice only really supports true splicing _from_ user memory
1584 * to a pipe, not the other way around. Splicing from user memory is a simple
1585 * operation that can be supported without any funky alignment restrictions
1586 * or nasty vm tricks. We simply map in the user memory and fill them into
1587 * a pipe. The reverse isn't quite as easy, though. There are two possible
1588 * solutions for that:
1589 *
1590 * - memcpy() the data internally, at which point we might as well just
1591 * do a regular read() on the buffer anyway.
1592 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1593 * has restriction limitations on both ends of the pipe).
1594 *
1595 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1596 *
1597 */
Al Viro87a30022018-05-26 21:39:52 -04001598SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
Dominik Brodowski30cfe4e2018-03-17 15:00:24 +01001599 unsigned long, nr_segs, unsigned int, flags)
1600{
Al Viro87a30022018-05-26 21:39:52 -04001601 struct iovec iovstack[UIO_FASTIOV];
1602 struct iovec *iov = iovstack;
1603 struct iov_iter iter;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001604 ssize_t error;
Al Viro87a30022018-05-26 21:39:52 -04001605 struct fd f;
1606 int type;
1607
Christoph Hellwig598b3ce2020-09-25 06:51:44 +02001608 if (unlikely(flags & ~SPLICE_F_ALL))
1609 return -EINVAL;
1610
Al Viro87a30022018-05-26 21:39:52 -04001611 f = fdget(fd);
1612 error = vmsplice_type(f, &type);
1613 if (error)
1614 return error;
1615
1616 error = import_iovec(type, uiov, nr_segs,
1617 ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwig598b3ce2020-09-25 06:51:44 +02001618 if (error < 0)
1619 goto out_fdput;
1620
1621 if (!iov_iter_count(&iter))
1622 error = 0;
Al Virode4eda92022-09-15 20:25:47 -04001623 else if (type == ITER_SOURCE)
Christoph Hellwig598b3ce2020-09-25 06:51:44 +02001624 error = vmsplice_to_pipe(f.file, &iter, flags);
1625 else
1626 error = vmsplice_to_user(f.file, &iter, flags);
1627
1628 kfree(iov);
1629out_fdput:
Al Viro87a30022018-05-26 21:39:52 -04001630 fdput(f);
1631 return error;
Dominik Brodowski30cfe4e2018-03-17 15:00:24 +01001632}
1633
Heiko Carstens836f92a2009-01-14 14:14:33 +01001634SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1635 int, fd_out, loff_t __user *, off_out,
1636 size_t, len, unsigned int, flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001637{
Al Viro2903ff02012-08-28 12:52:22 -04001638 struct fd in, out;
Amir Goldstein0f292082023-12-12 11:44:36 +02001639 ssize_t error;
Jens Axboe5274f052006-03-30 15:15:30 +02001640
1641 if (unlikely(!len))
1642 return 0;
1643
Al Viro3d6ea292016-12-10 13:17:32 -05001644 if (unlikely(flags & ~SPLICE_F_ALL))
1645 return -EINVAL;
1646
Jens Axboe5274f052006-03-30 15:15:30 +02001647 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001648 in = fdget(fd_in);
1649 if (in.file) {
Pavel Begunkov90da2e32020-05-04 22:39:35 +03001650 out = fdget(fd_out);
1651 if (out.file) {
Jens Axboeee6e00c2020-10-22 14:15:51 -06001652 error = __do_splice(in.file, off_in, out.file, off_out,
Amir Goldstein0f292082023-12-12 11:44:36 +02001653 len, flags);
Pavel Begunkov90da2e32020-05-04 22:39:35 +03001654 fdput(out);
Jens Axboe5274f052006-03-30 15:15:30 +02001655 }
Al Viro2903ff02012-08-28 12:52:22 -04001656 fdput(in);
Jens Axboe5274f052006-03-30 15:15:30 +02001657 }
Jens Axboe5274f052006-03-30 15:15:30 +02001658 return error;
1659}
Jens Axboe70524492006-04-11 15:51:17 +02001660
1661/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001662 * Make sure there's data to read. Wait for input if we can, otherwise
1663 * return an appropriate error.
1664 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001665static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001666{
1667 int ret;
1668
1669 /*
David Howells8cefc102019-11-15 13:30:32 +00001670 * Check the pipe occupancy without the inode lock first. This function
Jens Axboeaadd06e2006-07-10 11:00:01 +02001671 * is speculative anyways, so missing one is ok.
1672 */
David Howells8cefc102019-11-15 13:30:32 +00001673 if (!pipe_empty(pipe->head, pipe->tail))
Jens Axboeaadd06e2006-07-10 11:00:01 +02001674 return 0;
1675
1676 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001677 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001678
David Howells8cefc102019-11-15 13:30:32 +00001679 while (pipe_empty(pipe->head, pipe->tail)) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001680 if (signal_pending(current)) {
1681 ret = -ERESTARTSYS;
1682 break;
1683 }
1684 if (!pipe->writers)
1685 break;
Linus Torvaldsa28c8b92019-12-07 13:21:01 -08001686 if (flags & SPLICE_F_NONBLOCK) {
1687 ret = -EAGAIN;
1688 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001689 }
Linus Torvalds472e5b02020-10-01 19:14:36 -07001690 pipe_wait_readable(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001691 }
1692
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001693 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001694 return ret;
1695}
1696
1697/*
1698 * Make sure there's writeable room. Wait for room if we can, otherwise
1699 * return an appropriate error.
1700 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001701static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001702{
1703 int ret;
1704
1705 /*
David Howells8cefc102019-11-15 13:30:32 +00001706 * Check pipe occupancy without the inode lock first. This function
Jens Axboeaadd06e2006-07-10 11:00:01 +02001707 * is speculative anyways, so missing one is ok.
1708 */
Tetsuo Handa566d1362020-05-20 08:51:59 +09001709 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
Jens Axboeaadd06e2006-07-10 11:00:01 +02001710 return 0;
1711
1712 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001713 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001714
David Howells6718b6f2019-10-16 16:47:32 +01001715 while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001716 if (!pipe->readers) {
1717 send_sig(SIGPIPE, current, 0);
1718 ret = -EPIPE;
1719 break;
1720 }
1721 if (flags & SPLICE_F_NONBLOCK) {
1722 ret = -EAGAIN;
1723 break;
1724 }
1725 if (signal_pending(current)) {
1726 ret = -ERESTARTSYS;
1727 break;
1728 }
Linus Torvalds472e5b02020-10-01 19:14:36 -07001729 pipe_wait_writable(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001730 }
1731
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001732 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001733 return ret;
1734}
1735
1736/*
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001737 * Splice contents of ipipe to opipe.
1738 */
1739static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1740 struct pipe_inode_info *opipe,
1741 size_t len, unsigned int flags)
1742{
1743 struct pipe_buffer *ibuf, *obuf;
David Howells8cefc102019-11-15 13:30:32 +00001744 unsigned int i_head, o_head;
1745 unsigned int i_tail, o_tail;
1746 unsigned int i_mask, o_mask;
1747 int ret = 0;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001748 bool input_wakeup = false;
1749
1750
1751retry:
1752 ret = ipipe_prep(ipipe, flags);
1753 if (ret)
1754 return ret;
1755
1756 ret = opipe_prep(opipe, flags);
1757 if (ret)
1758 return ret;
1759
1760 /*
1761 * Potential ABBA deadlock, work around it by ordering lock
1762 * grabbing by pipe info address. Otherwise two different processes
1763 * could deadlock (one doing tee from A -> B, the other from B -> A).
1764 */
1765 pipe_double_lock(ipipe, opipe);
1766
David Howells8cefc102019-11-15 13:30:32 +00001767 i_tail = ipipe->tail;
1768 i_mask = ipipe->ring_size - 1;
1769 o_head = opipe->head;
1770 o_mask = opipe->ring_size - 1;
1771
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001772 do {
David Howells8cefc102019-11-15 13:30:32 +00001773 size_t o_len;
1774
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001775 if (!opipe->readers) {
1776 send_sig(SIGPIPE, current, 0);
1777 if (!ret)
1778 ret = -EPIPE;
1779 break;
1780 }
1781
David Howells8cefc102019-11-15 13:30:32 +00001782 i_head = ipipe->head;
1783 o_tail = opipe->tail;
1784
1785 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001786 break;
1787
1788 /*
1789 * Cannot make any progress, because either the input
1790 * pipe is empty or the output pipe is full.
1791 */
David Howells8cefc102019-11-15 13:30:32 +00001792 if (pipe_empty(i_head, i_tail) ||
David Howells6718b6f2019-10-16 16:47:32 +01001793 pipe_full(o_head, o_tail, opipe->max_usage)) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001794 /* Already processed some buffers, break */
1795 if (ret)
1796 break;
1797
1798 if (flags & SPLICE_F_NONBLOCK) {
1799 ret = -EAGAIN;
1800 break;
1801 }
1802
1803 /*
1804 * We raced with another reader/writer and haven't
1805 * managed to process any buffers. A zero return
1806 * value means EOF, so retry instead.
1807 */
1808 pipe_unlock(ipipe);
1809 pipe_unlock(opipe);
1810 goto retry;
1811 }
1812
David Howells8cefc102019-11-15 13:30:32 +00001813 ibuf = &ipipe->bufs[i_tail & i_mask];
1814 obuf = &opipe->bufs[o_head & o_mask];
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001815
1816 if (len >= ibuf->len) {
1817 /*
1818 * Simply move the whole buffer from ipipe to opipe
1819 */
1820 *obuf = *ibuf;
1821 ibuf->ops = NULL;
David Howells8cefc102019-11-15 13:30:32 +00001822 i_tail++;
1823 ipipe->tail = i_tail;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001824 input_wakeup = true;
David Howells8cefc102019-11-15 13:30:32 +00001825 o_len = obuf->len;
1826 o_head++;
1827 opipe->head = o_head;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001828 } else {
1829 /*
1830 * Get a reference to this pipe buffer,
1831 * so we can copy the contents over.
1832 */
Matthew Wilcox15fab632019-04-05 14:02:10 -07001833 if (!pipe_buf_get(ipipe, ibuf)) {
1834 if (ret == 0)
1835 ret = -EFAULT;
1836 break;
1837 }
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001838 *obuf = *ibuf;
1839
1840 /*
Christoph Hellwigf6dd9752020-05-20 17:58:12 +02001841 * Don't inherit the gift and merge flags, we need to
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001842 * prevent multiple steals of this page.
1843 */
1844 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
Christoph Hellwigf6dd9752020-05-20 17:58:12 +02001845 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
Jann Horna0ce2f02019-01-23 15:19:17 +01001846
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001847 obuf->len = len;
David Howells8cefc102019-11-15 13:30:32 +00001848 ibuf->offset += len;
1849 ibuf->len -= len;
1850 o_len = len;
1851 o_head++;
1852 opipe->head = o_head;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001853 }
David Howells8cefc102019-11-15 13:30:32 +00001854 ret += o_len;
1855 len -= o_len;
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001856 } while (len);
1857
1858 pipe_unlock(ipipe);
1859 pipe_unlock(opipe);
1860
1861 /*
1862 * If we put data in the output pipe, wakeup any potential readers.
1863 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001864 if (ret > 0)
1865 wakeup_pipe_readers(opipe);
1866
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001867 if (input_wakeup)
1868 wakeup_pipe_writers(ipipe);
1869
1870 return ret;
1871}
1872
1873/*
Jens Axboe70524492006-04-11 15:51:17 +02001874 * Link contents of ipipe to opipe.
1875 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001876static ssize_t link_pipe(struct pipe_inode_info *ipipe,
1877 struct pipe_inode_info *opipe,
1878 size_t len, unsigned int flags)
Jens Axboe70524492006-04-11 15:51:17 +02001879{
1880 struct pipe_buffer *ibuf, *obuf;
David Howells8cefc102019-11-15 13:30:32 +00001881 unsigned int i_head, o_head;
1882 unsigned int i_tail, o_tail;
1883 unsigned int i_mask, o_mask;
Amir Goldstein0f292082023-12-12 11:44:36 +02001884 ssize_t ret = 0;
Jens Axboe70524492006-04-11 15:51:17 +02001885
1886 /*
1887 * Potential ABBA deadlock, work around it by ordering lock
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001888 * grabbing by pipe info address. Otherwise two different processes
Jens Axboe70524492006-04-11 15:51:17 +02001889 * could deadlock (one doing tee from A -> B, the other from B -> A).
1890 */
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001891 pipe_double_lock(ipipe, opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001892
David Howells8cefc102019-11-15 13:30:32 +00001893 i_tail = ipipe->tail;
1894 i_mask = ipipe->ring_size - 1;
1895 o_head = opipe->head;
1896 o_mask = opipe->ring_size - 1;
1897
Jens Axboeaadd06e2006-07-10 11:00:01 +02001898 do {
Jens Axboe70524492006-04-11 15:51:17 +02001899 if (!opipe->readers) {
1900 send_sig(SIGPIPE, current, 0);
1901 if (!ret)
1902 ret = -EPIPE;
1903 break;
1904 }
Jens Axboe70524492006-04-11 15:51:17 +02001905
David Howells8cefc102019-11-15 13:30:32 +00001906 i_head = ipipe->head;
1907 o_tail = opipe->tail;
1908
Jens Axboe70524492006-04-11 15:51:17 +02001909 /*
David Howells8cefc102019-11-15 13:30:32 +00001910 * If we have iterated all input buffers or run out of
Jens Axboeaadd06e2006-07-10 11:00:01 +02001911 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001912 */
David Howells8cefc102019-11-15 13:30:32 +00001913 if (pipe_empty(i_head, i_tail) ||
David Howells6718b6f2019-10-16 16:47:32 +01001914 pipe_full(o_head, o_tail, opipe->max_usage))
Jens Axboe70524492006-04-11 15:51:17 +02001915 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001916
David Howells8cefc102019-11-15 13:30:32 +00001917 ibuf = &ipipe->bufs[i_tail & i_mask];
1918 obuf = &opipe->bufs[o_head & o_mask];
Jens Axboeaadd06e2006-07-10 11:00:01 +02001919
Jens Axboe2a27250e2006-04-19 15:56:40 +02001920 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001921 * Get a reference to this pipe buffer,
1922 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001923 */
Matthew Wilcox15fab632019-04-05 14:02:10 -07001924 if (!pipe_buf_get(ipipe, ibuf)) {
1925 if (ret == 0)
1926 ret = -EFAULT;
1927 break;
1928 }
Jens Axboe70524492006-04-11 15:51:17 +02001929
Jens Axboeaadd06e2006-07-10 11:00:01 +02001930 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001931
Jens Axboeaadd06e2006-07-10 11:00:01 +02001932 /*
Christoph Hellwigf6dd9752020-05-20 17:58:12 +02001933 * Don't inherit the gift and merge flag, we need to prevent
1934 * multiple steals of this page.
Jens Axboeaadd06e2006-07-10 11:00:01 +02001935 */
1936 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
Christoph Hellwigf6dd9752020-05-20 17:58:12 +02001937 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
Jann Horna0ce2f02019-01-23 15:19:17 +01001938
Jens Axboeaadd06e2006-07-10 11:00:01 +02001939 if (obuf->len > len)
1940 obuf->len = len;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001941 ret += obuf->len;
1942 len -= obuf->len;
David Howells8cefc102019-11-15 13:30:32 +00001943
1944 o_head++;
1945 opipe->head = o_head;
1946 i_tail++;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001947 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001948
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001949 pipe_unlock(ipipe);
1950 pipe_unlock(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001951
Jens Axboeaadd06e2006-07-10 11:00:01 +02001952 /*
1953 * If we put data in the output pipe, wakeup any potential readers.
1954 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001955 if (ret > 0)
1956 wakeup_pipe_readers(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001957
1958 return ret;
1959}
1960
1961/*
1962 * This is a tee(1) implementation that works on pipes. It doesn't copy
1963 * any data, it simply references the 'in' pages on the 'out' pipe.
1964 * The 'flags' used are the SPLICE_F_* variants, currently the only
1965 * applicable one is SPLICE_F_NONBLOCK.
1966 */
Amir Goldstein0f292082023-12-12 11:44:36 +02001967ssize_t do_tee(struct file *in, struct file *out, size_t len,
1968 unsigned int flags)
Jens Axboe70524492006-04-11 15:51:17 +02001969{
David Howellsc73be612020-01-14 17:07:11 +00001970 struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1971 struct pipe_inode_info *opipe = get_pipe_info(out, true);
Amir Goldstein0f292082023-12-12 11:44:36 +02001972 ssize_t ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001973
Pavel Begunkov90da2e32020-05-04 22:39:35 +03001974 if (unlikely(!(in->f_mode & FMODE_READ) ||
1975 !(out->f_mode & FMODE_WRITE)))
1976 return -EBADF;
1977
Jens Axboe70524492006-04-11 15:51:17 +02001978 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001979 * Duplicate the contents of ipipe to opipe without actually
1980 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001981 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001982 if (ipipe && opipe && ipipe != opipe) {
Slavomir Kaslevee5e0012019-02-07 17:45:19 +02001983 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1984 flags |= SPLICE_F_NONBLOCK;
1985
Jens Axboeaadd06e2006-07-10 11:00:01 +02001986 /*
1987 * Keep going, unless we encounter an error. The ipipe/opipe
1988 * ordering doesn't really matter.
1989 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001990 ret = ipipe_prep(ipipe, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001991 if (!ret) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001992 ret = opipe_prep(opipe, flags);
Jens Axboe02cf01a2008-02-20 10:34:51 +01001993 if (!ret)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001994 ret = link_pipe(ipipe, opipe, len, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001995 }
1996 }
Jens Axboe70524492006-04-11 15:51:17 +02001997
Ahelenia ZiemiaƄska576d4982023-07-03 16:42:21 +02001998 if (ret > 0) {
1999 fsnotify_access(in);
2000 fsnotify_modify(out);
2001 }
2002
Jens Axboeaadd06e2006-07-10 11:00:01 +02002003 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02002004}
2005
Heiko Carstens836f92a2009-01-14 14:14:33 +01002006SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
Jens Axboe70524492006-04-11 15:51:17 +02002007{
Pavel Begunkov90da2e32020-05-04 22:39:35 +03002008 struct fd in, out;
Amir Goldstein0f292082023-12-12 11:44:36 +02002009 ssize_t error;
Jens Axboe70524492006-04-11 15:51:17 +02002010
Al Viro3d6ea292016-12-10 13:17:32 -05002011 if (unlikely(flags & ~SPLICE_F_ALL))
2012 return -EINVAL;
2013
Jens Axboe70524492006-04-11 15:51:17 +02002014 if (unlikely(!len))
2015 return 0;
2016
2017 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04002018 in = fdget(fdin);
2019 if (in.file) {
Pavel Begunkov90da2e32020-05-04 22:39:35 +03002020 out = fdget(fdout);
2021 if (out.file) {
2022 error = do_tee(in.file, out.file, len, flags);
2023 fdput(out);
Jens Axboe70524492006-04-11 15:51:17 +02002024 }
Al Viro2903ff02012-08-28 12:52:22 -04002025 fdput(in);
Jens Axboe70524492006-04-11 15:51:17 +02002026 }
2027
2028 return error;
2029}