Linux 6.16-rc6
[linux-2.6-block.git] / fs / splice.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
5274f052
JA
2/*
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
c2058e06
JA
13 * Jens to support splicing to files, network, direct splicing, etc and
14 * fixing lots of bugs.
5274f052 15 *
0fe23479 16 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
c2058e06
JA
17 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
5274f052
JA
19 *
20 */
be297968 21#include <linux/bvec.h>
5274f052
JA
22#include <linux/fs.h>
23#include <linux/file.h>
24#include <linux/pagemap.h>
d6b29d7c 25#include <linux/splice.h>
08e552c6 26#include <linux/memcontrol.h>
5274f052 27#include <linux/mm_inline.h>
5abc97aa 28#include <linux/swap.h>
4f6f0bd2 29#include <linux/writeback.h>
630d9c47 30#include <linux/export.h>
4f6f0bd2 31#include <linux/syscalls.h>
912d35f8 32#include <linux/uio.h>
983652c6 33#include <linux/fsnotify.h>
29ce2058 34#include <linux/security.h>
5a0e3ad6 35#include <linux/gfp.h>
2dc334f1 36#include <linux/net.h>
35f9c09f 37#include <linux/socket.h>
174cd4b1
IM
38#include <linux/sched/signal.h>
39
06ae43f3 40#include "internal.h"
5274f052 41
0f99fc51
JA
42/*
43 * 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 */
e6f141b3 48static noinline void pipe_clear_nowait(struct file *file)
0f99fc51
JA
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
83f9135b
JA
58/*
59 * 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 */
c928f642
CH
64static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
65 struct pipe_buffer *buf)
5abc97aa 66{
5100da38 67 struct folio *folio = page_folio(buf->page);
9e94cd4f 68 struct address_space *mapping;
5abc97aa 69
b9ccad2e 70 folio_lock(folio);
9e0267c2 71
b9ccad2e 72 mapping = folio_mapping(folio);
9e94cd4f 73 if (mapping) {
b9ccad2e 74 WARN_ON(!folio_test_uptodate(folio));
5abc97aa 75
9e94cd4f
JA
76 /*
77 * At least for ext2 with nobh option, we need to wait on
b9ccad2e 78 * writeback completing on this folio, since we'll remove it
9e94cd4f 79 * from the pagecache. Otherwise truncate wont wait on the
b9ccad2e 80 * folio, allowing the disk blocks to be reused by someone else
9e94cd4f
JA
81 * before we actually wrote our data to them. fs corruption
82 * ensues.
83 */
b9ccad2e 84 folio_wait_writeback(folio);
ad8d6f0a 85
0201ebf2 86 if (!filemap_release_folio(folio, GFP_KERNEL))
ca39d651 87 goto out_unlock;
4f6f0bd2 88
9e94cd4f
JA
89 /*
90 * If we succeeded in removing the mapping, set LRU flag
91 * and return good.
92 */
5100da38 93 if (remove_mapping(mapping, folio)) {
9e94cd4f 94 buf->flags |= PIPE_BUF_FLAG_LRU;
c928f642 95 return true;
9e94cd4f 96 }
9e0267c2 97 }
5abc97aa 98
9e94cd4f 99 /*
b9ccad2e 100 * Raced with truncate or failed to remove folio from current
9e94cd4f
JA
101 * address space, unlock and return failure.
102 */
ca39d651 103out_unlock:
b9ccad2e 104 folio_unlock(folio);
c928f642 105 return false;
5abc97aa
JA
106}
107
76ad4d11 108static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
5274f052
JA
109 struct pipe_buffer *buf)
110{
09cbfeaf 111 put_page(buf->page);
1432873a 112 buf->flags &= ~PIPE_BUF_FLAG_LRU;
5274f052
JA
113}
114
0845718d
JA
115/*
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 */
cac36bb0
JA
119static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
120 struct pipe_buffer *buf)
5274f052 121{
781ca602 122 struct folio *folio = page_folio(buf->page);
49d0b21b 123 int err;
5274f052 124
781ca602
MWO
125 if (!folio_test_uptodate(folio)) {
126 folio_lock(folio);
49d0b21b
JA
127
128 /*
781ca602 129 * Folio got truncated/unhashed. This will cause a 0-byte
73d62d83 130 * splice, if this is the first page.
49d0b21b 131 */
781ca602 132 if (!folio->mapping) {
49d0b21b
JA
133 err = -ENODATA;
134 goto error;
135 }
5274f052 136
49d0b21b 137 /*
73d62d83 138 * Uh oh, read-error from disk.
49d0b21b 139 */
781ca602 140 if (!folio_test_uptodate(folio)) {
49d0b21b
JA
141 err = -EIO;
142 goto error;
143 }
144
781ca602
MWO
145 /* Folio is ok after all, we are done */
146 folio_unlock(folio);
5274f052
JA
147 }
148
f84d7519 149 return 0;
49d0b21b 150error:
781ca602 151 folio_unlock(folio);
f84d7519 152 return err;
70524490
JA
153}
154
708e3508 155const struct pipe_buf_operations page_cache_pipe_buf_ops = {
c928f642
CH
156 .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,
5274f052
JA
160};
161
c928f642
CH
162static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
163 struct pipe_buffer *buf)
912d35f8 164{
7afa6fd0 165 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
c928f642 166 return false;
7afa6fd0 167
1432873a 168 buf->flags |= PIPE_BUF_FLAG_LRU;
c928f642 169 return generic_pipe_buf_try_steal(pipe, buf);
912d35f8
JA
170}
171
d4c3cca9 172static const struct pipe_buf_operations user_page_pipe_buf_ops = {
c928f642
CH
173 .release = page_cache_pipe_buf_release,
174 .try_steal = user_page_pipe_buf_try_steal,
175 .get = generic_pipe_buf_get,
912d35f8
JA
176};
177
825cdcb1
NK
178static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
179{
180 smp_mb();
0ddad21d
LT
181 if (waitqueue_active(&pipe->rd_wait))
182 wake_up_interruptible(&pipe->rd_wait);
825cdcb1
NK
183 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
184}
185
932cc6d4
JA
186/**
187 * splice_to_pipe - fill passed data into a pipe
188 * @pipe: pipe to fill
189 * @spd: data to fill
190 *
191 * Description:
79685b8d 192 * @spd contains a map of pages and len/offset tuples, along with
932cc6d4
JA
193 * the struct pipe_buf_operations associated with these pages. This
194 * function will link that data to the pipe.
195 *
83f9135b 196 */
d6b29d7c
JA
197ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
198 struct splice_pipe_desc *spd)
5274f052 199{
00de00bd 200 unsigned int spd_pages = spd->nr_pages;
8cefc107
DH
201 unsigned int tail = pipe->tail;
202 unsigned int head = pipe->head;
0f292086
AG
203 ssize_t ret = 0;
204 int page_nr = 0;
5274f052 205
d6785d91
RV
206 if (!spd_pages)
207 return 0;
208
8924feff
AV
209 if (unlikely(!pipe->readers)) {
210 send_sig(SIGPIPE, current, 0);
211 ret = -EPIPE;
212 goto out;
213 }
5274f052 214
6718b6f8 215 while (!pipe_full(head, tail, pipe->max_usage)) {
d5c6cb01 216 struct pipe_buffer *buf = pipe_buf(pipe, head);
5274f052 217
8924feff
AV
218 buf->page = spd->pages[page_nr];
219 buf->offset = spd->partial[page_nr].offset;
220 buf->len = spd->partial[page_nr].len;
221 buf->private = spd->partial[page_nr].private;
222 buf->ops = spd->ops;
5a81e6a1 223 buf->flags = 0;
5274f052 224
8cefc107
DH
225 head++;
226 pipe->head = head;
8924feff
AV
227 page_nr++;
228 ret += buf->len;
29e35094 229
8924feff 230 if (!--spd->nr_pages)
5274f052 231 break;
5274f052
JA
232 }
233
8924feff
AV
234 if (!ret)
235 ret = -EAGAIN;
5274f052 236
8924feff 237out:
00de00bd 238 while (page_nr < spd_pages)
bbdfc2f7 239 spd->spd_release(spd, page_nr++);
5274f052
JA
240
241 return ret;
242}
2b514574 243EXPORT_SYMBOL_GPL(splice_to_pipe);
5274f052 244
79fddc4e
AV
245ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
246{
8cefc107
DH
247 unsigned int head = pipe->head;
248 unsigned int tail = pipe->tail;
79fddc4e
AV
249 int ret;
250
251 if (unlikely(!pipe->readers)) {
252 send_sig(SIGPIPE, current, 0);
253 ret = -EPIPE;
6718b6f8 254 } else if (pipe_full(head, tail, pipe->max_usage)) {
79fddc4e
AV
255 ret = -EAGAIN;
256 } else {
d5c6cb01 257 *pipe_buf(pipe, head) = *buf;
8cefc107 258 pipe->head = head + 1;
79fddc4e
AV
259 return buf->len;
260 }
a779638c 261 pipe_buf_release(pipe, buf);
79fddc4e
AV
262 return ret;
263}
264EXPORT_SYMBOL(add_to_pipe);
265
35f3d14d
JA
266/*
267 * Check if we need to grow the arrays holding pages and partial page
268 * descriptions.
269 */
047fe360 270int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
35f3d14d 271{
6718b6f8 272 unsigned int max_usage = READ_ONCE(pipe->max_usage);
047fe360 273
8cefc107
DH
274 spd->nr_pages_max = max_usage;
275 if (max_usage <= PIPE_DEF_BUFFERS)
35f3d14d
JA
276 return 0;
277
8cefc107
DH
278 spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
279 spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
6da2ec56 280 GFP_KERNEL);
35f3d14d
JA
281
282 if (spd->pages && spd->partial)
283 return 0;
284
285 kfree(spd->pages);
286 kfree(spd->partial);
287 return -ENOMEM;
288}
289
047fe360 290void splice_shrink_spd(struct splice_pipe_desc *spd)
35f3d14d 291{
047fe360 292 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
35f3d14d
JA
293 return;
294
295 kfree(spd->pages);
296 kfree(spd->partial);
297}
298
9eee8bd8
DH
299/**
300 * copy_splice_read - Copy data from a file and splice the copy into a pipe
301 * @in: The file to read from
302 * @ppos: Pointer to the file position to read from
303 * @pipe: The pipe to splice into
304 * @len: The amount to splice
305 * @flags: The SPLICE_F_* flags
306 *
307 * This function allocates a bunch of pages sufficient to hold the requested
308 * amount of data (but limited by the remaining pipe capacity), passes it to
309 * the file's ->read_iter() to read into and then splices the used pages into
310 * the pipe.
311 *
312 * Return: On success, the number of bytes read will be returned and *@ppos
313 * will be updated if appropriate; 0 will be returned if there is no more data
314 * to be read; -EAGAIN will be returned if the pipe had no space, and some
315 * other negative error code will be returned on error. A short read may occur
316 * if the pipe has insufficient space, we reach the end of the data or we hit a
317 * hole.
33b3b041 318 */
69df79a4
DH
319ssize_t copy_splice_read(struct file *in, loff_t *ppos,
320 struct pipe_inode_info *pipe,
321 size_t len, unsigned int flags)
33b3b041
DH
322{
323 struct iov_iter to;
324 struct bio_vec *bv;
325 struct kiocb kiocb;
326 struct page **pages;
327 ssize_t ret;
e69f37bc 328 size_t used, npages, chunk, remain, keep = 0;
33b3b041
DH
329 int i;
330
331 /* Work out how much data we can actually add into the pipe */
00a7d398 332 used = pipe_buf_usage(pipe);
33b3b041
DH
333 npages = max_t(ssize_t, pipe->max_usage - used, 0);
334 len = min_t(size_t, len, npages * PAGE_SIZE);
335 npages = DIV_ROUND_UP(len, PAGE_SIZE);
336
337 bv = kzalloc(array_size(npages, sizeof(bv[0])) +
338 array_size(npages, sizeof(struct page *)), GFP_KERNEL);
339 if (!bv)
340 return -ENOMEM;
341
e69f37bc 342 pages = (struct page **)(bv + npages);
6bf9b5b4 343 npages = alloc_pages_bulk(GFP_USER, npages, pages);
33b3b041
DH
344 if (!npages) {
345 kfree(bv);
346 return -ENOMEM;
347 }
348
349 remain = len = min_t(size_t, len, npages * PAGE_SIZE);
350
351 for (i = 0; i < npages; i++) {
352 chunk = min_t(size_t, PAGE_SIZE, remain);
353 bv[i].bv_page = pages[i];
354 bv[i].bv_offset = 0;
355 bv[i].bv_len = chunk;
356 remain -= chunk;
357 }
358
359 /* Do the I/O */
360 iov_iter_bvec(&to, ITER_DEST, bv, npages, len);
361 init_sync_kiocb(&kiocb, in);
362 kiocb.ki_pos = *ppos;
7c98f7cb 363 ret = in->f_op->read_iter(&kiocb, &to);
33b3b041 364
33b3b041 365 if (ret > 0) {
e69f37bc 366 keep = DIV_ROUND_UP(ret, PAGE_SIZE);
33b3b041 367 *ppos = kiocb.ki_pos;
33b3b041
DH
368 }
369
2e82f6c3
CH
370 /*
371 * Callers of ->splice_read() expect -EAGAIN on "can't put anything in
372 * there", rather than -EFAULT.
373 */
374 if (ret == -EFAULT)
375 ret = -EAGAIN;
376
33b3b041 377 /* Free any pages that didn't get touched at all. */
e69f37bc
DH
378 if (keep < npages)
379 release_pages(pages + keep, npages - keep);
33b3b041
DH
380
381 /* Push the remaining pages into the pipe. */
e69f37bc
DH
382 remain = ret;
383 for (i = 0; i < keep; i++) {
33b3b041
DH
384 struct pipe_buffer *buf = pipe_head_buf(pipe);
385
386 chunk = min_t(size_t, remain, PAGE_SIZE);
387 *buf = (struct pipe_buffer) {
388 .ops = &default_pipe_buf_ops,
389 .page = bv[i].bv_page,
390 .offset = 0,
391 .len = chunk,
392 };
393 pipe->head++;
394 remain -= chunk;
395 }
396
397 kfree(bv);
398 return ret;
399}
69df79a4 400EXPORT_SYMBOL(copy_splice_read);
33b3b041 401
241699cd 402const struct pipe_buf_operations default_pipe_buf_ops = {
c928f642
CH
403 .release = generic_pipe_buf_release,
404 .try_steal = generic_pipe_buf_try_steal,
405 .get = generic_pipe_buf_get,
6818173b
MS
406};
407
28a625cb
MS
408/* Pipe buffer operations for a socket and similar. */
409const struct pipe_buf_operations nosteal_pipe_buf_ops = {
c928f642
CH
410 .release = generic_pipe_buf_release,
411 .get = generic_pipe_buf_get,
28a625cb
MS
412};
413EXPORT_SYMBOL(nosteal_pipe_buf_ops);
414
b3c2d2dd
MS
415static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
416{
417 smp_mb();
0ddad21d
LT
418 if (waitqueue_active(&pipe->wr_wait))
419 wake_up_interruptible(&pipe->wr_wait);
b3c2d2dd
MS
420 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
421}
422
932cc6d4 423/**
b3c2d2dd 424 * splice_from_pipe_feed - feed available data from a pipe to a file
932cc6d4
JA
425 * @pipe: pipe to splice from
426 * @sd: information to @actor
427 * @actor: handler that splices the data
428 *
429 * Description:
b3c2d2dd
MS
430 * This function loops over the pipe and calls @actor to do the
431 * actual moving of a single struct pipe_buffer to the desired
432 * destination. It returns when there's no more buffers left in
433 * the pipe or if the requested number of bytes (@sd->total_len)
434 * have been copied. It returns a positive number (one) if the
435 * pipe needs to be filled with more data, zero if the required
436 * number of bytes have been copied and -errno on error.
932cc6d4 437 *
b3c2d2dd
MS
438 * This, together with splice_from_pipe_{begin,end,next}, may be
439 * used to implement the functionality of __splice_from_pipe() when
440 * locking is required around copying the pipe buffers to the
441 * destination.
83f9135b 442 */
96f9bc8f 443static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
b3c2d2dd 444 splice_actor *actor)
5274f052 445{
8cefc107
DH
446 unsigned int head = pipe->head;
447 unsigned int tail = pipe->tail;
b3c2d2dd 448 int ret;
5274f052 449
ec057595 450 while (!pipe_empty(head, tail)) {
d5c6cb01 451 struct pipe_buffer *buf = pipe_buf(pipe, tail);
5274f052 452
b3c2d2dd
MS
453 sd->len = buf->len;
454 if (sd->len > sd->total_len)
455 sd->len = sd->total_len;
5274f052 456
fba597db 457 ret = pipe_buf_confirm(pipe, buf);
a8adbe37 458 if (unlikely(ret)) {
b3c2d2dd
MS
459 if (ret == -ENODATA)
460 ret = 0;
461 return ret;
462 }
a8adbe37
MM
463
464 ret = actor(pipe, buf, sd);
465 if (ret <= 0)
466 return ret;
467
b3c2d2dd
MS
468 buf->offset += ret;
469 buf->len -= ret;
470
471 sd->num_spliced += ret;
472 sd->len -= ret;
473 sd->pos += ret;
474 sd->total_len -= ret;
475
476 if (!buf->len) {
a779638c 477 pipe_buf_release(pipe, buf);
8cefc107
DH
478 tail++;
479 pipe->tail = tail;
6447a3cf 480 if (pipe->files)
b3c2d2dd
MS
481 sd->need_wakeup = true;
482 }
5274f052 483
b3c2d2dd
MS
484 if (!sd->total_len)
485 return 0;
486 }
5274f052 487
b3c2d2dd
MS
488 return 1;
489}
5274f052 490
d1a819a2
LT
491/* We know we have a pipe buffer, but maybe it's empty? */
492static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
493{
494 unsigned int tail = pipe->tail;
d5c6cb01 495 struct pipe_buffer *buf = pipe_buf(pipe, tail);
d1a819a2
LT
496
497 if (unlikely(!buf->len)) {
498 pipe_buf_release(pipe, buf);
499 pipe->tail = tail+1;
500 return true;
501 }
502
503 return false;
504}
505
b3c2d2dd
MS
506/**
507 * splice_from_pipe_next - wait for some data to splice from
508 * @pipe: pipe to splice from
509 * @sd: information about the splice operation
510 *
511 * Description:
512 * This function will wait for some data and return a positive
513 * value (one) if pipe buffers are available. It will return zero
514 * or -errno if no more data needs to be spliced.
515 */
96f9bc8f 516static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
b3c2d2dd 517{
c725bfce
JK
518 /*
519 * Check for signal early to make process killable when there are
520 * always buffers available
521 */
522 if (signal_pending(current))
523 return -ERESTARTSYS;
524
d1a819a2 525repeat:
00a7d398 526 while (pipe_is_empty(pipe)) {
b3c2d2dd
MS
527 if (!pipe->writers)
528 return 0;
016b661e 529
a28c8b9d 530 if (sd->num_spliced)
b3c2d2dd 531 return 0;
73d62d83 532
b3c2d2dd
MS
533 if (sd->flags & SPLICE_F_NONBLOCK)
534 return -EAGAIN;
5274f052 535
b3c2d2dd
MS
536 if (signal_pending(current))
537 return -ERESTARTSYS;
5274f052 538
b3c2d2dd
MS
539 if (sd->need_wakeup) {
540 wakeup_pipe_writers(pipe);
541 sd->need_wakeup = false;
5274f052
JA
542 }
543
472e5b05 544 pipe_wait_readable(pipe);
b3c2d2dd 545 }
29e35094 546
d1a819a2
LT
547 if (eat_empty_buffer(pipe))
548 goto repeat;
549
b3c2d2dd
MS
550 return 1;
551}
5274f052 552
b3c2d2dd
MS
553/**
554 * splice_from_pipe_begin - start splicing from pipe
b80901bb 555 * @sd: information about the splice operation
b3c2d2dd
MS
556 *
557 * Description:
558 * This function should be called before a loop containing
559 * splice_from_pipe_next() and splice_from_pipe_feed() to
560 * initialize the necessary fields of @sd.
561 */
96f9bc8f 562static void splice_from_pipe_begin(struct splice_desc *sd)
b3c2d2dd
MS
563{
564 sd->num_spliced = 0;
565 sd->need_wakeup = false;
566}
5274f052 567
b3c2d2dd
MS
568/**
569 * splice_from_pipe_end - finish splicing from pipe
570 * @pipe: pipe to splice from
571 * @sd: information about the splice operation
572 *
573 * Description:
574 * This function will wake up pipe writers if necessary. It should
575 * be called after a loop containing splice_from_pipe_next() and
576 * splice_from_pipe_feed().
577 */
96f9bc8f 578static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
b3c2d2dd
MS
579{
580 if (sd->need_wakeup)
581 wakeup_pipe_writers(pipe);
582}
5274f052 583
b3c2d2dd
MS
584/**
585 * __splice_from_pipe - splice data from a pipe to given actor
586 * @pipe: pipe to splice from
587 * @sd: information to @actor
588 * @actor: handler that splices the data
589 *
590 * Description:
591 * This function does little more than loop over the pipe and call
592 * @actor to do the actual moving of a single struct pipe_buffer to
2dc334f1 593 * the desired destination. See pipe_to_file, pipe_to_sendmsg, or
b3c2d2dd
MS
594 * pipe_to_user.
595 *
596 */
597ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
598 splice_actor *actor)
599{
600 int ret;
5274f052 601
b3c2d2dd
MS
602 splice_from_pipe_begin(sd);
603 do {
c2489e07 604 cond_resched();
b3c2d2dd
MS
605 ret = splice_from_pipe_next(pipe, sd);
606 if (ret > 0)
607 ret = splice_from_pipe_feed(pipe, sd, actor);
608 } while (ret > 0);
609 splice_from_pipe_end(pipe, sd);
610
611 return sd->num_spliced ? sd->num_spliced : ret;
5274f052 612}
40bee44e 613EXPORT_SYMBOL(__splice_from_pipe);
5274f052 614
932cc6d4
JA
615/**
616 * splice_from_pipe - splice data from a pipe to a file
617 * @pipe: pipe to splice from
618 * @out: file to splice to
619 * @ppos: position in @out
620 * @len: how many bytes to splice
621 * @flags: splice modifier flags
622 * @actor: handler that splices the data
623 *
624 * Description:
2933970b 625 * See __splice_from_pipe. This function locks the pipe inode,
932cc6d4
JA
626 * otherwise it's identical to __splice_from_pipe().
627 *
628 */
6da61809
MF
629ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
630 loff_t *ppos, size_t len, unsigned int flags,
631 splice_actor *actor)
632{
633 ssize_t ret;
c66ab6fa
JA
634 struct splice_desc sd = {
635 .total_len = len,
636 .flags = flags,
637 .pos = *ppos,
6a14b90b 638 .u.file = out,
c66ab6fa 639 };
6da61809 640
61e0d47c 641 pipe_lock(pipe);
c66ab6fa 642 ret = __splice_from_pipe(pipe, &sd, actor);
61e0d47c 643 pipe_unlock(pipe);
6da61809
MF
644
645 return ret;
646}
647
8d020765
AV
648/**
649 * iter_file_splice_write - splice data from a pipe to a file
650 * @pipe: pipe info
651 * @out: file to write to
652 * @ppos: position in @out
653 * @len: number of bytes to splice
654 * @flags: splice modifier flags
655 *
656 * Description:
657 * Will either move or copy pages (determined by @flags options) from
658 * the given pipe inode to the given file.
659 * This one is ->write_iter-based.
660 *
661 */
662ssize_t
663iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
664 loff_t *ppos, size_t len, unsigned int flags)
665{
666 struct splice_desc sd = {
667 .total_len = len,
668 .flags = flags,
669 .pos = *ppos,
670 .u.file = out,
671 };
6718b6f8 672 int nbufs = pipe->max_usage;
d53471ba 673 struct bio_vec *array;
8d020765
AV
674 ssize_t ret;
675
d53471ba
AG
676 if (!out->f_op->write_iter)
677 return -EINVAL;
678
679 array = kcalloc(nbufs, sizeof(struct bio_vec), GFP_KERNEL);
8d020765
AV
680 if (unlikely(!array))
681 return -ENOMEM;
682
683 pipe_lock(pipe);
684
685 splice_from_pipe_begin(&sd);
686 while (sd.total_len) {
d53471ba 687 struct kiocb kiocb;
8d020765 688 struct iov_iter from;
d5c6cb01 689 unsigned int head, tail;
8d020765 690 size_t left;
8cefc107 691 int n;
8d020765
AV
692
693 ret = splice_from_pipe_next(pipe, &sd);
694 if (ret <= 0)
695 break;
696
6718b6f8 697 if (unlikely(nbufs < pipe->max_usage)) {
8d020765 698 kfree(array);
6718b6f8 699 nbufs = pipe->max_usage;
8d020765
AV
700 array = kcalloc(nbufs, sizeof(struct bio_vec),
701 GFP_KERNEL);
702 if (!array) {
703 ret = -ENOMEM;
704 break;
705 }
706 }
707
ec057595
LT
708 head = pipe->head;
709 tail = pipe->tail;
ec057595 710
8d020765
AV
711 /* build the vector */
712 left = sd.total_len;
0f1d344f 713 for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
d5c6cb01 714 struct pipe_buffer *buf = pipe_buf(pipe, tail);
8d020765
AV
715 size_t this_len = buf->len;
716
0f1d344f
PB
717 /* zero-length bvecs are not supported, skip them */
718 if (!this_len)
719 continue;
720 this_len = min(this_len, left);
8d020765 721
fba597db 722 ret = pipe_buf_confirm(pipe, buf);
8d020765
AV
723 if (unlikely(ret)) {
724 if (ret == -ENODATA)
725 ret = 0;
726 goto done;
727 }
728
664e4078
CH
729 bvec_set_page(&array[n], buf->page, this_len,
730 buf->offset);
8d020765 731 left -= this_len;
0f1d344f 732 n++;
8d020765
AV
733 }
734
de4eda9d 735 iov_iter_bvec(&from, ITER_SOURCE, array, n, sd.total_len - left);
d53471ba
AG
736 init_sync_kiocb(&kiocb, out);
737 kiocb.ki_pos = sd.pos;
7c98f7cb 738 ret = out->f_op->write_iter(&kiocb, &from);
d53471ba 739 sd.pos = kiocb.ki_pos;
8d020765
AV
740 if (ret <= 0)
741 break;
742
743 sd.num_spliced += ret;
744 sd.total_len -= ret;
dbe4e192 745 *ppos = sd.pos;
8d020765
AV
746
747 /* dismiss the fully eaten buffers, adjust the partial one */
8cefc107 748 tail = pipe->tail;
8d020765 749 while (ret) {
d5c6cb01 750 struct pipe_buffer *buf = pipe_buf(pipe, tail);
8d020765 751 if (ret >= buf->len) {
8d020765
AV
752 ret -= buf->len;
753 buf->len = 0;
a779638c 754 pipe_buf_release(pipe, buf);
8cefc107
DH
755 tail++;
756 pipe->tail = tail;
8d020765
AV
757 if (pipe->files)
758 sd.need_wakeup = true;
759 } else {
760 buf->offset += ret;
761 buf->len -= ret;
762 ret = 0;
763 }
764 }
765 }
766done:
767 kfree(array);
768 splice_from_pipe_end(pipe, &sd);
769
770 pipe_unlock(pipe);
771
772 if (sd.num_spliced)
773 ret = sd.num_spliced;
774
775 return ret;
776}
777
778EXPORT_SYMBOL(iter_file_splice_write);
779
2dc334f1 780#ifdef CONFIG_NET
83f9135b 781/**
2dc334f1 782 * splice_to_socket - splice data from a pipe to a socket
932cc6d4 783 * @pipe: pipe to splice from
83f9135b 784 * @out: socket to write to
932cc6d4 785 * @ppos: position in @out
83f9135b
JA
786 * @len: number of bytes to splice
787 * @flags: splice modifier flags
788 *
932cc6d4
JA
789 * Description:
790 * Will send @len bytes from the pipe to a network socket. No data copying
791 * is involved.
83f9135b
JA
792 *
793 */
2dc334f1
DH
794ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out,
795 loff_t *ppos, size_t len, unsigned int flags)
5274f052 796{
2dc334f1
DH
797 struct socket *sock = sock_from_file(out);
798 struct bio_vec bvec[16];
799 struct msghdr msg = {};
800 ssize_t ret = 0;
801 size_t spliced = 0;
802 bool need_wakeup = false;
803
804 pipe_lock(pipe);
805
806 while (len > 0) {
d5c6cb01 807 unsigned int head, tail, bc = 0;
2dc334f1
DH
808 size_t remain = len;
809
810 /*
811 * Check for signal early to make process killable when there
812 * are always buffers available
813 */
814 ret = -ERESTARTSYS;
815 if (signal_pending(current))
816 break;
817
00a7d398 818 while (pipe_is_empty(pipe)) {
2dc334f1
DH
819 ret = 0;
820 if (!pipe->writers)
821 goto out;
822
823 if (spliced)
824 goto out;
825
826 ret = -EAGAIN;
827 if (flags & SPLICE_F_NONBLOCK)
828 goto out;
829
830 ret = -ERESTARTSYS;
831 if (signal_pending(current))
832 goto out;
833
834 if (need_wakeup) {
835 wakeup_pipe_writers(pipe);
836 need_wakeup = false;
837 }
838
839 pipe_wait_readable(pipe);
840 }
841
842 head = pipe->head;
843 tail = pipe->tail;
2dc334f1
DH
844
845 while (!pipe_empty(head, tail)) {
d5c6cb01 846 struct pipe_buffer *buf = pipe_buf(pipe, tail);
2dc334f1
DH
847 size_t seg;
848
849 if (!buf->len) {
850 tail++;
851 continue;
852 }
5274f052 853
2dc334f1 854 seg = min_t(size_t, remain, buf->len);
2dc334f1
DH
855
856 ret = pipe_buf_confirm(pipe, buf);
857 if (unlikely(ret)) {
858 if (ret == -ENODATA)
859 ret = 0;
860 break;
861 }
5274f052 862
2dc334f1
DH
863 bvec_set_page(&bvec[bc++], buf->page, seg, buf->offset);
864 remain -= seg;
ca2d49f7 865 if (remain == 0 || bc >= ARRAY_SIZE(bvec))
2dc334f1 866 break;
ca2d49f7 867 tail++;
2dc334f1
DH
868 }
869
870 if (!bc)
871 break;
872
873 msg.msg_flags = MSG_SPLICE_PAGES;
874 if (flags & SPLICE_F_MORE)
875 msg.msg_flags |= MSG_MORE;
876 if (remain && pipe_occupancy(pipe->head, tail) > 0)
877 msg.msg_flags |= MSG_MORE;
0f0fa27b
JS
878 if (out->f_flags & O_NONBLOCK)
879 msg.msg_flags |= MSG_DONTWAIT;
2dc334f1
DH
880
881 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc,
882 len - remain);
883 ret = sock_sendmsg(sock, &msg);
884 if (ret <= 0)
885 break;
886
887 spliced += ret;
888 len -= ret;
889 tail = pipe->tail;
890 while (ret > 0) {
d5c6cb01 891 struct pipe_buffer *buf = pipe_buf(pipe, tail);
2dc334f1
DH
892 size_t seg = min_t(size_t, ret, buf->len);
893
894 buf->offset += seg;
895 buf->len -= seg;
896 ret -= seg;
897
898 if (!buf->len) {
899 pipe_buf_release(pipe, buf);
900 tail++;
901 }
902 }
903
904 if (tail != pipe->tail) {
905 pipe->tail = tail;
906 if (pipe->files)
907 need_wakeup = true;
908 }
909 }
910
911out:
912 pipe_unlock(pipe);
913 if (need_wakeup)
914 wakeup_pipe_writers(pipe);
915 return spliced ?: ret;
916}
917#endif
a0f06780 918
36e2c742
CH
919static int warn_unsupported(struct file *file, const char *op)
920{
921 pr_debug_ratelimited(
922 "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
923 op, file, current->pid, current->comm);
924 return -EINVAL;
925}
926
83f9135b
JA
927/*
928 * Attempt to initiate a splice from pipe to file.
929 */
0f292086
AG
930static ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
931 loff_t *ppos, size_t len, unsigned int flags)
5274f052 932{
36e2c742
CH
933 if (unlikely(!out->f_op->splice_write))
934 return warn_unsupported(out, "write");
935 return out->f_op->splice_write(pipe, out, ppos, len, flags);
5274f052
JA
936}
937
2bfc6685
DH
938/*
939 * Indicate to the caller that there was a premature EOF when reading from the
940 * source and the caller didn't indicate they would be sending more data after
941 * this.
942 */
943static void do_splice_eof(struct splice_desc *sd)
944{
945 if (sd->splice_eof)
946 sd->splice_eof(sd);
947}
948
feebea75
AG
949/*
950 * Callers already called rw_verify_area() on the entire range.
951 * No need to call it for sub ranges.
83f9135b 952 */
0f292086
AG
953static ssize_t do_splice_read(struct file *in, loff_t *ppos,
954 struct pipe_inode_info *pipe, size_t len,
955 unsigned int flags)
5274f052 956{
313d64a3 957 unsigned int p_space;
5274f052 958
49570e9b 959 if (unlikely(!(in->f_mode & FMODE_READ)))
5274f052 960 return -EBADF;
123856f0
DH
961 if (!len)
962 return 0;
5274f052 963
313d64a3 964 /* Don't try to read more the pipe has space for. */
00a7d398 965 p_space = pipe->max_usage - pipe_buf_usage(pipe);
313d64a3
AV
966 len = min_t(size_t, len, p_space << PAGE_SHIFT);
967
03cc0789
AV
968 if (unlikely(len > MAX_RW_COUNT))
969 len = MAX_RW_COUNT;
970
36e2c742
CH
971 if (unlikely(!in->f_op->splice_read))
972 return warn_unsupported(in, "read");
aa3dbde8 973 /*
b85930a0
DH
974 * O_DIRECT and DAX don't deal with the pagecache, so we allocate a
975 * buffer, copy into it and splice that into the pipe.
aa3dbde8 976 */
b85930a0 977 if ((in->f_flags & O_DIRECT) || IS_DAX(in->f_mapping->host))
aa3dbde8 978 return copy_splice_read(in, ppos, pipe, len, flags);
36e2c742 979 return in->f_op->splice_read(in, ppos, pipe, len, flags);
5274f052 980}
feebea75
AG
981
982/**
983 * vfs_splice_read - Read data from a file and splice it into a pipe
984 * @in: File to splice from
985 * @ppos: Input file offset
986 * @pipe: Pipe to splice to
987 * @len: Number of bytes to splice
988 * @flags: Splice modifier flags (SPLICE_F_*)
989 *
990 * Splice the requested amount of data from the input file to the pipe. This
991 * is synchronous as the caller must hold the pipe lock across the entire
992 * operation.
993 *
994 * If successful, it returns the amount of data spliced, 0 if it hit the EOF or
995 * a hole and a negative error code otherwise.
996 */
0f292086
AG
997ssize_t vfs_splice_read(struct file *in, loff_t *ppos,
998 struct pipe_inode_info *pipe, size_t len,
999 unsigned int flags)
feebea75 1000{
0f292086 1001 ssize_t ret;
feebea75
AG
1002
1003 ret = rw_verify_area(READ, in, ppos, len);
1004 if (unlikely(ret < 0))
1005 return ret;
1006
1007 return do_splice_read(in, ppos, pipe, len, flags);
1008}
6a3f30b8 1009EXPORT_SYMBOL_GPL(vfs_splice_read);
5274f052 1010
932cc6d4
JA
1011/**
1012 * splice_direct_to_actor - splices data directly between two non-pipes
1013 * @in: file to splice from
1014 * @sd: actor information on where to splice to
1015 * @actor: handles the data splicing
1016 *
1017 * Description:
1018 * This is a special case helper to splice directly between two
1019 * points, without requiring an explicit pipe. Internally an allocated
79685b8d 1020 * pipe is cached in the process, and reused during the lifetime of
932cc6d4
JA
1021 * that process.
1022 *
c66ab6fa
JA
1023 */
1024ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1025 splice_direct_actor *actor)
b92ce558
JA
1026{
1027 struct pipe_inode_info *pipe;
0f292086 1028 ssize_t ret, bytes;
c66ab6fa 1029 size_t len;
0ff28d9f 1030 int i, flags, more;
b92ce558
JA
1031
1032 /*
97ef77c5
JD
1033 * We require the input to be seekable, as we don't want to randomly
1034 * drop data for eg socket -> socket splicing. Use the piped splicing
1035 * for that!
b92ce558 1036 */
97ef77c5 1037 if (unlikely(!(in->f_mode & FMODE_LSEEK)))
b92ce558
JA
1038 return -EINVAL;
1039
1040 /*
1041 * neither in nor out is a pipe, setup an internal pipe attached to
1042 * 'out' and transfer the wanted data from 'in' to 'out' through that
1043 */
1044 pipe = current->splice_pipe;
49570e9b 1045 if (unlikely(!pipe)) {
7bee130e 1046 pipe = alloc_pipe_info();
b92ce558
JA
1047 if (!pipe)
1048 return -ENOMEM;
1049
1050 /*
1051 * We don't have an immediate reader, but we'll read the stuff
00522fb4 1052 * out of the pipe right after the splice_to_pipe(). So set
b92ce558
JA
1053 * PIPE_READERS appropriately.
1054 */
1055 pipe->readers = 1;
1056
1057 current->splice_pipe = pipe;
1058 }
1059
1060 /*
73d62d83 1061 * Do the splice.
b92ce558 1062 */
b92ce558 1063 bytes = 0;
c66ab6fa 1064 len = sd->total_len;
219d9205
DH
1065
1066 /* Don't block on output, we have to drain the direct pipe. */
c66ab6fa 1067 flags = sd->flags;
219d9205 1068 sd->flags &= ~SPLICE_F_NONBLOCK;
c66ab6fa
JA
1069
1070 /*
219d9205
DH
1071 * We signal MORE until we've read sufficient data to fulfill the
1072 * request and we keep signalling it if the caller set it.
c66ab6fa 1073 */
0ff28d9f 1074 more = sd->flags & SPLICE_F_MORE;
219d9205 1075 sd->flags |= SPLICE_F_MORE;
b92ce558 1076
00a7d398 1077 WARN_ON_ONCE(!pipe_is_empty(pipe));
17614445 1078
b92ce558 1079 while (len) {
51a92c0f 1080 size_t read_len;
a82c53a0 1081 loff_t pos = sd->pos, prev_pos = pos;
b92ce558 1082
feebea75 1083 ret = do_splice_read(in, &pos, pipe, len, flags);
51a92c0f 1084 if (unlikely(ret <= 0))
2bfc6685 1085 goto read_failure;
b92ce558
JA
1086
1087 read_len = ret;
c66ab6fa 1088 sd->total_len = read_len;
b92ce558 1089
0ff28d9f 1090 /*
219d9205
DH
1091 * If we now have sufficient data to fulfill the request then
1092 * we clear SPLICE_F_MORE if it was not set initially.
0ff28d9f 1093 */
219d9205 1094 if (read_len >= len && !more)
0ff28d9f 1095 sd->flags &= ~SPLICE_F_MORE;
219d9205 1096
b92ce558
JA
1097 /*
1098 * NOTE: nonblocking mode only applies to the input. We
1099 * must not do the output in nonblocking mode as then we
1100 * could get stuck data in the internal pipe:
1101 */
c66ab6fa 1102 ret = actor(pipe, sd);
a82c53a0
TZ
1103 if (unlikely(ret <= 0)) {
1104 sd->pos = prev_pos;
b92ce558 1105 goto out_release;
a82c53a0 1106 }
b92ce558
JA
1107
1108 bytes += ret;
1109 len -= ret;
bcd4f3ac 1110 sd->pos = pos;
b92ce558 1111
a82c53a0
TZ
1112 if (ret < read_len) {
1113 sd->pos = prev_pos + ret;
51a92c0f 1114 goto out_release;
a82c53a0 1115 }
b92ce558
JA
1116 }
1117
9e97198d 1118done:
8cefc107 1119 pipe->tail = pipe->head = 0;
80848708 1120 file_accessed(in);
b92ce558
JA
1121 return bytes;
1122
2bfc6685
DH
1123read_failure:
1124 /*
1125 * If the user did *not* set SPLICE_F_MORE *and* we didn't hit that
1126 * "use all of len" case that cleared SPLICE_F_MORE, *and* we did a
1127 * "->splice_in()" that returned EOF (ie zero) *and* we have sent at
1128 * least 1 byte *then* we will also do the ->splice_eof() call.
1129 */
1130 if (ret == 0 && !more && len > 0 && bytes)
1131 do_splice_eof(sd);
b92ce558
JA
1132out_release:
1133 /*
1134 * If we did an incomplete transfer we must release
1135 * the pipe buffers in question:
1136 */
8cefc107
DH
1137 for (i = 0; i < pipe->ring_size; i++) {
1138 struct pipe_buffer *buf = &pipe->bufs[i];
b92ce558 1139
a779638c
MS
1140 if (buf->ops)
1141 pipe_buf_release(pipe, buf);
b92ce558 1142 }
b92ce558 1143
9e97198d
JA
1144 if (!bytes)
1145 bytes = ret;
c66ab6fa 1146
9e97198d 1147 goto done;
c66ab6fa
JA
1148}
1149EXPORT_SYMBOL(splice_direct_to_actor);
1150
1151static int direct_splice_actor(struct pipe_inode_info *pipe,
1152 struct splice_desc *sd)
1153{
6a14b90b 1154 struct file *file = sd->u.file;
da40448c
AG
1155 long ret;
1156
1157 file_start_write(file);
1158 ret = do_splice_from(pipe, file, sd->opos, sd->total_len, sd->flags);
1159 file_end_write(file);
1160 return ret;
1161}
c66ab6fa 1162
da40448c
AG
1163static int splice_file_range_actor(struct pipe_inode_info *pipe,
1164 struct splice_desc *sd)
1165{
1166 struct file *file = sd->u.file;
1167
1168 return do_splice_from(pipe, file, sd->opos, sd->total_len, sd->flags);
c66ab6fa
JA
1169}
1170
2bfc6685
DH
1171static void direct_file_splice_eof(struct splice_desc *sd)
1172{
1173 struct file *file = sd->u.file;
1174
1175 if (file->f_op->splice_eof)
1176 file->f_op->splice_eof(file);
1177}
1178
0f292086
AG
1179static ssize_t do_splice_direct_actor(struct file *in, loff_t *ppos,
1180 struct file *out, loff_t *opos,
1181 size_t len, unsigned int flags,
1182 splice_direct_actor *actor)
c66ab6fa
JA
1183{
1184 struct splice_desc sd = {
1185 .len = len,
1186 .total_len = len,
1187 .flags = flags,
1188 .pos = *ppos,
6a14b90b 1189 .u.file = out,
2bfc6685 1190 .splice_eof = direct_file_splice_eof,
7995bd28 1191 .opos = opos,
c66ab6fa 1192 };
0f292086 1193 ssize_t ret;
c66ab6fa 1194
18c67cb9
AV
1195 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1196 return -EBADF;
1197
1198 if (unlikely(out->f_flags & O_APPEND))
1199 return -EINVAL;
1200
488e8f68 1201 ret = splice_direct_to_actor(in, &sd, actor);
51a92c0f 1202 if (ret > 0)
a82c53a0 1203 *ppos = sd.pos;
51a92c0f 1204
c66ab6fa 1205 return ret;
b92ce558 1206}
488e8f68
AG
1207/**
1208 * do_splice_direct - splices data directly between two files
1209 * @in: file to splice from
1210 * @ppos: input file offset
1211 * @out: file to splice to
1212 * @opos: output file offset
1213 * @len: number of bytes to splice
1214 * @flags: splice modifier flags
1215 *
1216 * Description:
1217 * For use by do_sendfile(). splice can easily emulate sendfile, but
1218 * doing it in the application would incur an extra system call
1219 * (splice in + splice out, as compared to just sendfile()). So this helper
1220 * can splice directly through a process-private pipe.
1221 *
1222 * Callers already called rw_verify_area() on the entire range.
1223 */
0f292086
AG
1224ssize_t do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1225 loff_t *opos, size_t len, unsigned int flags)
488e8f68
AG
1226{
1227 return do_splice_direct_actor(in, ppos, out, opos, len, flags,
1228 direct_splice_actor);
1229}
1c118596 1230EXPORT_SYMBOL(do_splice_direct);
b92ce558 1231
488e8f68
AG
1232/**
1233 * splice_file_range - splices data between two files for copy_file_range()
1234 * @in: file to splice from
1235 * @ppos: input file offset
1236 * @out: file to splice to
1237 * @opos: output file offset
1238 * @len: number of bytes to splice
1239 *
1240 * Description:
705bcfcb 1241 * For use by ->copy_file_range() methods.
da40448c
AG
1242 * Like do_splice_direct(), but vfs_copy_file_range() already holds
1243 * start_file_write() on @out file.
488e8f68
AG
1244 *
1245 * Callers already called rw_verify_area() on the entire range.
1246 */
0f292086
AG
1247ssize_t splice_file_range(struct file *in, loff_t *ppos, struct file *out,
1248 loff_t *opos, size_t len)
488e8f68
AG
1249{
1250 lockdep_assert(file_write_started(out));
1251
705bcfcb
AG
1252 return do_splice_direct_actor(in, ppos, out, opos,
1253 min_t(size_t, len, MAX_RW_COUNT),
1254 0, splice_file_range_actor);
488e8f68
AG
1255}
1256EXPORT_SYMBOL(splice_file_range);
1257
8924feff
AV
1258static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1259{
52bce911
LT
1260 for (;;) {
1261 if (unlikely(!pipe->readers)) {
1262 send_sig(SIGPIPE, current, 0);
1263 return -EPIPE;
1264 }
00a7d398 1265 if (!pipe_is_full(pipe))
52bce911 1266 return 0;
8924feff
AV
1267 if (flags & SPLICE_F_NONBLOCK)
1268 return -EAGAIN;
1269 if (signal_pending(current))
1270 return -ERESTARTSYS;
472e5b05 1271 pipe_wait_writable(pipe);
8924feff 1272 }
8924feff
AV
1273}
1274
7c77f0b3
MS
1275static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1276 struct pipe_inode_info *opipe,
1277 size_t len, unsigned int flags);
ddac0d39 1278
0f292086
AG
1279ssize_t splice_file_to_pipe(struct file *in,
1280 struct pipe_inode_info *opipe,
1281 loff_t *offset,
1282 size_t len, unsigned int flags)
faa97c48 1283{
0f292086 1284 ssize_t ret;
faa97c48
AV
1285
1286 pipe_lock(opipe);
1287 ret = wait_for_space(opipe, flags);
1288 if (!ret)
b70d8e2b 1289 ret = do_splice_read(in, offset, opipe, len, flags);
faa97c48
AV
1290 pipe_unlock(opipe);
1291 if (ret > 0)
1292 wakeup_pipe_readers(opipe);
1293 return ret;
1294}
1295
83f9135b
JA
1296/*
1297 * Determine where to splice to/from.
1298 */
0f292086
AG
1299ssize_t do_splice(struct file *in, loff_t *off_in, struct file *out,
1300 loff_t *off_out, size_t len, unsigned int flags)
5274f052 1301{
7c77f0b3
MS
1302 struct pipe_inode_info *ipipe;
1303 struct pipe_inode_info *opipe;
7995bd28 1304 loff_t offset;
0f292086 1305 ssize_t ret;
5274f052 1306
90da2e3f
PB
1307 if (unlikely(!(in->f_mode & FMODE_READ) ||
1308 !(out->f_mode & FMODE_WRITE)))
1309 return -EBADF;
1310
c73be61c
DH
1311 ipipe = get_pipe_info(in, true);
1312 opipe = get_pipe_info(out, true);
7c77f0b3
MS
1313
1314 if (ipipe && opipe) {
1315 if (off_in || off_out)
1316 return -ESPIPE;
1317
7c77f0b3
MS
1318 /* Splicing to self would be fun, but... */
1319 if (ipipe == opipe)
1320 return -EINVAL;
1321
ee5e0011
SK
1322 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1323 flags |= SPLICE_F_NONBLOCK;
1324
12ee4b66
AZ
1325 ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
1326 } else if (ipipe) {
529565dc
IM
1327 if (off_in)
1328 return -ESPIPE;
b92ce558 1329 if (off_out) {
19c9a49b 1330 if (!(out->f_mode & FMODE_PWRITE))
b92ce558 1331 return -EINVAL;
ee6e00c8 1332 offset = *off_out;
7995bd28
AV
1333 } else {
1334 offset = out->f_pos;
1335 }
529565dc 1336
18c67cb9
AV
1337 if (unlikely(out->f_flags & O_APPEND))
1338 return -EINVAL;
1339
1340 ret = rw_verify_area(WRITE, out, &offset, len);
1341 if (unlikely(ret < 0))
1342 return ret;
1343
ee5e0011
SK
1344 if (in->f_flags & O_NONBLOCK)
1345 flags |= SPLICE_F_NONBLOCK;
1346
500368f7 1347 file_start_write(out);
7995bd28 1348 ret = do_splice_from(ipipe, out, &offset, len, flags);
500368f7 1349 file_end_write(out);
a4514ebd 1350
7995bd28
AV
1351 if (!off_out)
1352 out->f_pos = offset;
ee6e00c8
JA
1353 else
1354 *off_out = offset;
12ee4b66 1355 } else if (opipe) {
529565dc
IM
1356 if (off_out)
1357 return -ESPIPE;
b92ce558 1358 if (off_in) {
19c9a49b 1359 if (!(in->f_mode & FMODE_PREAD))
b92ce558 1360 return -EINVAL;
ee6e00c8 1361 offset = *off_in;
7995bd28
AV
1362 } else {
1363 offset = in->f_pos;
1364 }
529565dc 1365
b70d8e2b
AG
1366 ret = rw_verify_area(READ, in, &offset, len);
1367 if (unlikely(ret < 0))
1368 return ret;
1369
ee5e0011
SK
1370 if (out->f_flags & O_NONBLOCK)
1371 flags |= SPLICE_F_NONBLOCK;
1372
faa97c48 1373 ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
983652c6 1374
7995bd28
AV
1375 if (!off_in)
1376 in->f_pos = offset;
ee6e00c8
JA
1377 else
1378 *off_in = offset;
12ee4b66
AZ
1379 } else {
1380 ret = -EINVAL;
1381 }
a4514ebd 1382
12ee4b66
AZ
1383 if (ret > 0) {
1384 /*
1385 * Generate modify out before access in:
1386 * do_splice_from() may've already sent modify out,
1387 * and this ensures the events get merged.
1388 */
1389 fsnotify_modify(out);
1390 fsnotify_access(in);
529565dc 1391 }
5274f052 1392
12ee4b66 1393 return ret;
5274f052
JA
1394}
1395
0f292086
AG
1396static ssize_t __do_splice(struct file *in, loff_t __user *off_in,
1397 struct file *out, loff_t __user *off_out,
1398 size_t len, unsigned int flags)
ee6e00c8
JA
1399{
1400 struct pipe_inode_info *ipipe;
1401 struct pipe_inode_info *opipe;
1402 loff_t offset, *__off_in = NULL, *__off_out = NULL;
0f292086 1403 ssize_t ret;
ee6e00c8
JA
1404
1405 ipipe = get_pipe_info(in, true);
1406 opipe = get_pipe_info(out, true);
1407
0f99fc51
JA
1408 if (ipipe) {
1409 if (off_in)
1410 return -ESPIPE;
1411 pipe_clear_nowait(in);
1412 }
1413 if (opipe) {
1414 if (off_out)
1415 return -ESPIPE;
1416 pipe_clear_nowait(out);
1417 }
ee6e00c8
JA
1418
1419 if (off_out) {
1420 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1421 return -EFAULT;
1422 __off_out = &offset;
1423 }
1424 if (off_in) {
1425 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1426 return -EFAULT;
1427 __off_in = &offset;
1428 }
1429
1430 ret = do_splice(in, __off_in, out, __off_out, len, flags);
1431 if (ret < 0)
1432 return ret;
1433
1434 if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1435 return -EFAULT;
1436 if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1437 return -EFAULT;
1438
1439 return ret;
1440}
1441
0f292086
AG
1442static ssize_t iter_to_pipe(struct iov_iter *from,
1443 struct pipe_inode_info *pipe,
1444 unsigned int flags)
912d35f8 1445{
79fddc4e
AV
1446 struct pipe_buffer buf = {
1447 .ops = &user_page_pipe_buf_ops,
1448 .flags = flags
1449 };
1450 size_t total = 0;
0f292086 1451 ssize_t ret = 0;
79fddc4e 1452
7d690c15 1453 while (iov_iter_count(from)) {
79fddc4e 1454 struct page *pages[16];
7d690c15 1455 ssize_t left;
db85a9eb 1456 size_t start;
7d690c15 1457 int i, n;
db85a9eb 1458
7d690c15
AV
1459 left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
1460 if (left <= 0) {
1461 ret = left;
79fddc4e
AV
1462 break;
1463 }
db85a9eb 1464
7d690c15
AV
1465 n = DIV_ROUND_UP(left + start, PAGE_SIZE);
1466 for (i = 0; i < n; i++) {
1467 int size = min_t(int, left, PAGE_SIZE - start);
1468
1469 buf.page = pages[i];
1470 buf.offset = start;
1471 buf.len = size;
1472 ret = add_to_pipe(pipe, &buf);
1473 if (unlikely(ret < 0)) {
1474 iov_iter_revert(from, left);
1475 // this one got dropped by add_to_pipe()
1476 while (++i < n)
1477 put_page(pages[i]);
1478 goto out;
79fddc4e 1479 }
7d690c15
AV
1480 total += ret;
1481 left -= size;
1482 start = 0;
912d35f8 1483 }
912d35f8 1484 }
7d690c15 1485out:
79fddc4e 1486 return total ? total : ret;
912d35f8
JA
1487}
1488
6a14b90b
JA
1489static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1490 struct splice_desc *sd)
1491{
6130f531
AV
1492 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1493 return n == sd->len ? n : -EFAULT;
6a14b90b
JA
1494}
1495
1496/*
1497 * For lack of a better implementation, implement vmsplice() to userspace
1498 * as a simple copy of the pipes pages to the user iov.
1499 */
0f292086
AG
1500static ssize_t vmsplice_to_user(struct file *file, struct iov_iter *iter,
1501 unsigned int flags)
6a14b90b 1502{
c73be61c 1503 struct pipe_inode_info *pipe = get_pipe_info(file, true);
87a3002a
AV
1504 struct splice_desc sd = {
1505 .total_len = iov_iter_count(iter),
1506 .flags = flags,
1507 .u.data = iter
1508 };
0f292086 1509 ssize_t ret = 0;
6a14b90b 1510
6a14b90b
JA
1511 if (!pipe)
1512 return -EBADF;
1513
0f99fc51
JA
1514 pipe_clear_nowait(file);
1515
345995fa
AV
1516 if (sd.total_len) {
1517 pipe_lock(pipe);
1518 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1519 pipe_unlock(pipe);
1520 }
6a14b90b 1521
7f0f1ea0
AZ
1522 if (ret > 0)
1523 fsnotify_access(file);
1524
6a14b90b
JA
1525 return ret;
1526}
1527
912d35f8
JA
1528/*
1529 * vmsplice splices a user address range into a pipe. It can be thought of
1530 * as splice-from-memory, where the regular splice is splice-from-file (or
1531 * to file). In both cases the output is a pipe, naturally.
912d35f8 1532 */
0f292086
AG
1533static ssize_t vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1534 unsigned int flags)
912d35f8 1535{
ddac0d39 1536 struct pipe_inode_info *pipe;
0f292086 1537 ssize_t ret = 0;
79fddc4e
AV
1538 unsigned buf_flag = 0;
1539
1540 if (flags & SPLICE_F_GIFT)
1541 buf_flag = PIPE_BUF_FLAG_GIFT;
912d35f8 1542
c73be61c 1543 pipe = get_pipe_info(file, true);
ddac0d39 1544 if (!pipe)
912d35f8 1545 return -EBADF;
912d35f8 1546
0f99fc51
JA
1547 pipe_clear_nowait(file);
1548
8924feff
AV
1549 pipe_lock(pipe);
1550 ret = wait_for_space(pipe, flags);
79fddc4e 1551 if (!ret)
87a3002a 1552 ret = iter_to_pipe(iter, pipe, buf_flag);
8924feff 1553 pipe_unlock(pipe);
7f0f1ea0 1554 if (ret > 0) {
8924feff 1555 wakeup_pipe_readers(pipe);
7f0f1ea0
AZ
1556 fsnotify_modify(file);
1557 }
35f3d14d 1558 return ret;
912d35f8
JA
1559}
1560
6a14b90b
JA
1561/*
1562 * Note that vmsplice only really supports true splicing _from_ user memory
1563 * to a pipe, not the other way around. Splicing from user memory is a simple
1564 * operation that can be supported without any funky alignment restrictions
1565 * or nasty vm tricks. We simply map in the user memory and fill them into
1566 * a pipe. The reverse isn't quite as easy, though. There are two possible
1567 * solutions for that:
1568 *
1569 * - memcpy() the data internally, at which point we might as well just
1570 * do a regular read() on the buffer anyway.
1571 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1572 * has restriction limitations on both ends of the pipe).
1573 *
1574 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1575 *
1576 */
87a3002a 1577SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
30cfe4ef
DB
1578 unsigned long, nr_segs, unsigned int, flags)
1579{
87a3002a
AV
1580 struct iovec iovstack[UIO_FASTIOV];
1581 struct iovec *iov = iovstack;
1582 struct iov_iter iter;
87e5e6da 1583 ssize_t error;
87a3002a
AV
1584 int type;
1585
598b3cec
CH
1586 if (unlikely(flags & ~SPLICE_F_ALL))
1587 return -EINVAL;
1588
a6f46579
AV
1589 CLASS(fd, f)(fd);
1590 if (fd_empty(f))
1591 return -EBADF;
1592 if (fd_file(f)->f_mode & FMODE_WRITE)
1593 type = ITER_SOURCE;
1594 else if (fd_file(f)->f_mode & FMODE_READ)
1595 type = ITER_DEST;
1596 else
1597 return -EBADF;
87a3002a
AV
1598
1599 error = import_iovec(type, uiov, nr_segs,
1600 ARRAY_SIZE(iovstack), &iov, &iter);
598b3cec 1601 if (error < 0)
a6f46579 1602 return error;
30cfe4ef 1603
598b3cec
CH
1604 if (!iov_iter_count(&iter))
1605 error = 0;
de4eda9d 1606 else if (type == ITER_SOURCE)
1da91ea8 1607 error = vmsplice_to_pipe(fd_file(f), &iter, flags);
598b3cec 1608 else
1da91ea8 1609 error = vmsplice_to_user(fd_file(f), &iter, flags);
87a3002a 1610
598b3cec 1611 kfree(iov);
87a3002a 1612 return error;
76b021d0 1613}
76b021d0 1614
836f92ad
HC
1615SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1616 int, fd_out, loff_t __user *, off_out,
1617 size_t, len, unsigned int, flags)
5274f052 1618{
5274f052
JA
1619 if (unlikely(!len))
1620 return 0;
1621
3d6ea290
AV
1622 if (unlikely(flags & ~SPLICE_F_ALL))
1623 return -EINVAL;
1624
8152f820
AV
1625 CLASS(fd, in)(fd_in);
1626 if (fd_empty(in))
1627 return -EBADF;
1628
1629 CLASS(fd, out)(fd_out);
1630 if (fd_empty(out))
1631 return -EBADF;
1632
1633 return __do_splice(fd_file(in), off_in, fd_file(out), off_out,
0f292086 1634 len, flags);
5274f052 1635}
70524490 1636
aadd06e5
JA
1637/*
1638 * Make sure there's data to read. Wait for input if we can, otherwise
1639 * return an appropriate error.
1640 */
7c77f0b3 1641static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5
JA
1642{
1643 int ret;
1644
1645 /*
8cefc107 1646 * Check the pipe occupancy without the inode lock first. This function
aadd06e5
JA
1647 * is speculative anyways, so missing one is ok.
1648 */
00a7d398 1649 if (!pipe_is_empty(pipe))
aadd06e5
JA
1650 return 0;
1651
1652 ret = 0;
61e0d47c 1653 pipe_lock(pipe);
aadd06e5 1654
00a7d398 1655 while (pipe_is_empty(pipe)) {
aadd06e5
JA
1656 if (signal_pending(current)) {
1657 ret = -ERESTARTSYS;
1658 break;
1659 }
1660 if (!pipe->writers)
1661 break;
a28c8b9d
LT
1662 if (flags & SPLICE_F_NONBLOCK) {
1663 ret = -EAGAIN;
1664 break;
aadd06e5 1665 }
472e5b05 1666 pipe_wait_readable(pipe);
aadd06e5
JA
1667 }
1668
61e0d47c 1669 pipe_unlock(pipe);
aadd06e5
JA
1670 return ret;
1671}
1672
1673/*
1674 * Make sure there's writeable room. Wait for room if we can, otherwise
1675 * return an appropriate error.
1676 */
7c77f0b3 1677static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5
JA
1678{
1679 int ret;
1680
1681 /*
8cefc107 1682 * Check pipe occupancy without the inode lock first. This function
aadd06e5
JA
1683 * is speculative anyways, so missing one is ok.
1684 */
00a7d398 1685 if (!pipe_is_full(pipe))
aadd06e5
JA
1686 return 0;
1687
1688 ret = 0;
61e0d47c 1689 pipe_lock(pipe);
aadd06e5 1690
00a7d398 1691 while (pipe_is_full(pipe)) {
aadd06e5
JA
1692 if (!pipe->readers) {
1693 send_sig(SIGPIPE, current, 0);
1694 ret = -EPIPE;
1695 break;
1696 }
1697 if (flags & SPLICE_F_NONBLOCK) {
1698 ret = -EAGAIN;
1699 break;
1700 }
1701 if (signal_pending(current)) {
1702 ret = -ERESTARTSYS;
1703 break;
1704 }
472e5b05 1705 pipe_wait_writable(pipe);
aadd06e5
JA
1706 }
1707
61e0d47c 1708 pipe_unlock(pipe);
aadd06e5
JA
1709 return ret;
1710}
1711
7c77f0b3
MS
1712/*
1713 * Splice contents of ipipe to opipe.
1714 */
1715static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1716 struct pipe_inode_info *opipe,
1717 size_t len, unsigned int flags)
1718{
1719 struct pipe_buffer *ibuf, *obuf;
8cefc107
DH
1720 unsigned int i_head, o_head;
1721 unsigned int i_tail, o_tail;
8cefc107 1722 int ret = 0;
7c77f0b3
MS
1723 bool input_wakeup = false;
1724
1725
1726retry:
1727 ret = ipipe_prep(ipipe, flags);
1728 if (ret)
1729 return ret;
1730
1731 ret = opipe_prep(opipe, flags);
1732 if (ret)
1733 return ret;
1734
1735 /*
1736 * Potential ABBA deadlock, work around it by ordering lock
1737 * grabbing by pipe info address. Otherwise two different processes
1738 * could deadlock (one doing tee from A -> B, the other from B -> A).
1739 */
1740 pipe_double_lock(ipipe, opipe);
1741
8cefc107 1742 i_tail = ipipe->tail;
8cefc107 1743 o_head = opipe->head;
8cefc107 1744
7c77f0b3 1745 do {
8cefc107
DH
1746 size_t o_len;
1747
7c77f0b3
MS
1748 if (!opipe->readers) {
1749 send_sig(SIGPIPE, current, 0);
1750 if (!ret)
1751 ret = -EPIPE;
1752 break;
1753 }
1754
8cefc107
DH
1755 i_head = ipipe->head;
1756 o_tail = opipe->tail;
1757
1758 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
7c77f0b3
MS
1759 break;
1760
1761 /*
1762 * Cannot make any progress, because either the input
1763 * pipe is empty or the output pipe is full.
1764 */
8cefc107 1765 if (pipe_empty(i_head, i_tail) ||
6718b6f8 1766 pipe_full(o_head, o_tail, opipe->max_usage)) {
7c77f0b3
MS
1767 /* Already processed some buffers, break */
1768 if (ret)
1769 break;
1770
1771 if (flags & SPLICE_F_NONBLOCK) {
1772 ret = -EAGAIN;
1773 break;
1774 }
1775
1776 /*
1777 * We raced with another reader/writer and haven't
1778 * managed to process any buffers. A zero return
1779 * value means EOF, so retry instead.
1780 */
1781 pipe_unlock(ipipe);
1782 pipe_unlock(opipe);
1783 goto retry;
1784 }
1785
d5c6cb01
PN
1786 ibuf = pipe_buf(ipipe, i_tail);
1787 obuf = pipe_buf(opipe, o_head);
7c77f0b3
MS
1788
1789 if (len >= ibuf->len) {
1790 /*
1791 * Simply move the whole buffer from ipipe to opipe
1792 */
1793 *obuf = *ibuf;
1794 ibuf->ops = NULL;
8cefc107
DH
1795 i_tail++;
1796 ipipe->tail = i_tail;
7c77f0b3 1797 input_wakeup = true;
8cefc107
DH
1798 o_len = obuf->len;
1799 o_head++;
1800 opipe->head = o_head;
7c77f0b3
MS
1801 } else {
1802 /*
1803 * Get a reference to this pipe buffer,
1804 * so we can copy the contents over.
1805 */
15fab63e
MW
1806 if (!pipe_buf_get(ipipe, ibuf)) {
1807 if (ret == 0)
1808 ret = -EFAULT;
1809 break;
1810 }
7c77f0b3
MS
1811 *obuf = *ibuf;
1812
1813 /*
f6dd9755 1814 * Don't inherit the gift and merge flags, we need to
7c77f0b3
MS
1815 * prevent multiple steals of this page.
1816 */
1817 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
f6dd9755 1818 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
a0ce2f0a 1819
7c77f0b3 1820 obuf->len = len;
8cefc107
DH
1821 ibuf->offset += len;
1822 ibuf->len -= len;
1823 o_len = len;
1824 o_head++;
1825 opipe->head = o_head;
7c77f0b3 1826 }
8cefc107
DH
1827 ret += o_len;
1828 len -= o_len;
7c77f0b3
MS
1829 } while (len);
1830
1831 pipe_unlock(ipipe);
1832 pipe_unlock(opipe);
1833
1834 /*
1835 * If we put data in the output pipe, wakeup any potential readers.
1836 */
825cdcb1
NK
1837 if (ret > 0)
1838 wakeup_pipe_readers(opipe);
1839
7c77f0b3
MS
1840 if (input_wakeup)
1841 wakeup_pipe_writers(ipipe);
1842
1843 return ret;
1844}
1845
70524490
JA
1846/*
1847 * Link contents of ipipe to opipe.
1848 */
0f292086
AG
1849static ssize_t link_pipe(struct pipe_inode_info *ipipe,
1850 struct pipe_inode_info *opipe,
1851 size_t len, unsigned int flags)
70524490
JA
1852{
1853 struct pipe_buffer *ibuf, *obuf;
8cefc107
DH
1854 unsigned int i_head, o_head;
1855 unsigned int i_tail, o_tail;
0f292086 1856 ssize_t ret = 0;
70524490
JA
1857
1858 /*
1859 * Potential ABBA deadlock, work around it by ordering lock
61e0d47c 1860 * grabbing by pipe info address. Otherwise two different processes
70524490
JA
1861 * could deadlock (one doing tee from A -> B, the other from B -> A).
1862 */
61e0d47c 1863 pipe_double_lock(ipipe, opipe);
70524490 1864
8cefc107 1865 i_tail = ipipe->tail;
8cefc107 1866 o_head = opipe->head;
8cefc107 1867
aadd06e5 1868 do {
70524490
JA
1869 if (!opipe->readers) {
1870 send_sig(SIGPIPE, current, 0);
1871 if (!ret)
1872 ret = -EPIPE;
1873 break;
1874 }
70524490 1875
8cefc107
DH
1876 i_head = ipipe->head;
1877 o_tail = opipe->tail;
1878
aadd06e5 1879 /*
8cefc107 1880 * If we have iterated all input buffers or run out of
aadd06e5
JA
1881 * output room, break.
1882 */
8cefc107 1883 if (pipe_empty(i_head, i_tail) ||
6718b6f8 1884 pipe_full(o_head, o_tail, opipe->max_usage))
aadd06e5 1885 break;
70524490 1886
d5c6cb01
PN
1887 ibuf = pipe_buf(ipipe, i_tail);
1888 obuf = pipe_buf(opipe, o_head);
70524490
JA
1889
1890 /*
aadd06e5
JA
1891 * Get a reference to this pipe buffer,
1892 * so we can copy the contents over.
70524490 1893 */
15fab63e
MW
1894 if (!pipe_buf_get(ipipe, ibuf)) {
1895 if (ret == 0)
1896 ret = -EFAULT;
1897 break;
1898 }
aadd06e5 1899
aadd06e5
JA
1900 *obuf = *ibuf;
1901
2a27250e 1902 /*
f6dd9755
CH
1903 * Don't inherit the gift and merge flag, we need to prevent
1904 * multiple steals of this page.
2a27250e 1905 */
aadd06e5 1906 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
f6dd9755 1907 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
a0ce2f0a 1908
aadd06e5
JA
1909 if (obuf->len > len)
1910 obuf->len = len;
aadd06e5
JA
1911 ret += obuf->len;
1912 len -= obuf->len;
8cefc107
DH
1913
1914 o_head++;
1915 opipe->head = o_head;
1916 i_tail++;
aadd06e5 1917 } while (len);
70524490 1918
61e0d47c
MS
1919 pipe_unlock(ipipe);
1920 pipe_unlock(opipe);
70524490 1921
aadd06e5
JA
1922 /*
1923 * If we put data in the output pipe, wakeup any potential readers.
1924 */
825cdcb1
NK
1925 if (ret > 0)
1926 wakeup_pipe_readers(opipe);
70524490
JA
1927
1928 return ret;
1929}
1930
1931/*
1932 * This is a tee(1) implementation that works on pipes. It doesn't copy
1933 * any data, it simply references the 'in' pages on the 'out' pipe.
1934 * The 'flags' used are the SPLICE_F_* variants, currently the only
1935 * applicable one is SPLICE_F_NONBLOCK.
1936 */
0f292086
AG
1937ssize_t do_tee(struct file *in, struct file *out, size_t len,
1938 unsigned int flags)
70524490 1939{
c73be61c
DH
1940 struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1941 struct pipe_inode_info *opipe = get_pipe_info(out, true);
0f292086 1942 ssize_t ret = -EINVAL;
70524490 1943
90da2e3f
PB
1944 if (unlikely(!(in->f_mode & FMODE_READ) ||
1945 !(out->f_mode & FMODE_WRITE)))
1946 return -EBADF;
1947
70524490 1948 /*
aadd06e5
JA
1949 * Duplicate the contents of ipipe to opipe without actually
1950 * copying the data.
70524490 1951 */
aadd06e5 1952 if (ipipe && opipe && ipipe != opipe) {
ee5e0011
SK
1953 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1954 flags |= SPLICE_F_NONBLOCK;
1955
aadd06e5
JA
1956 /*
1957 * Keep going, unless we encounter an error. The ipipe/opipe
1958 * ordering doesn't really matter.
1959 */
7c77f0b3 1960 ret = ipipe_prep(ipipe, flags);
aadd06e5 1961 if (!ret) {
7c77f0b3 1962 ret = opipe_prep(opipe, flags);
02cf01ae 1963 if (!ret)
aadd06e5 1964 ret = link_pipe(ipipe, opipe, len, flags);
aadd06e5
JA
1965 }
1966 }
70524490 1967
576d498e
AZ
1968 if (ret > 0) {
1969 fsnotify_access(in);
1970 fsnotify_modify(out);
1971 }
1972
aadd06e5 1973 return ret;
70524490
JA
1974}
1975
836f92ad 1976SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
70524490 1977{
3d6ea290
AV
1978 if (unlikely(flags & ~SPLICE_F_ALL))
1979 return -EINVAL;
1980
70524490
JA
1981 if (unlikely(!len))
1982 return 0;
1983
8152f820
AV
1984 CLASS(fd, in)(fdin);
1985 if (fd_empty(in))
1986 return -EBADF;
70524490 1987
8152f820
AV
1988 CLASS(fd, out)(fdout);
1989 if (fd_empty(out))
1990 return -EBADF;
1991
1992 return do_tee(fd_file(in), fd_file(out), len, flags);
70524490 1993}