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