splice: always fsnotify_access(in), fsnotify_modify(out) on success
[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 */
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;
879
880 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc,
881 len - remain);
882 ret = sock_sendmsg(sock, &msg);
883 if (ret <= 0)
884 break;
885
886 spliced += ret;
887 len -= ret;
888 tail = pipe->tail;
889 while (ret > 0) {
890 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
891 size_t seg = min_t(size_t, ret, buf->len);
892
893 buf->offset += seg;
894 buf->len -= seg;
895 ret -= seg;
896
897 if (!buf->len) {
898 pipe_buf_release(pipe, buf);
899 tail++;
900 }
901 }
902
903 if (tail != pipe->tail) {
904 pipe->tail = tail;
905 if (pipe->files)
906 need_wakeup = true;
907 }
908 }
909
910out:
911 pipe_unlock(pipe);
912 if (need_wakeup)
913 wakeup_pipe_writers(pipe);
914 return spliced ?: ret;
915}
916#endif
a0f06780 917
36e2c742
CH
918static int warn_unsupported(struct file *file, const char *op)
919{
920 pr_debug_ratelimited(
921 "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
922 op, file, current->pid, current->comm);
923 return -EINVAL;
924}
925
83f9135b
JA
926/*
927 * Attempt to initiate a splice from pipe to file.
928 */
3a326a2c 929static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
cbb7e577 930 loff_t *ppos, size_t len, unsigned int flags)
5274f052 931{
36e2c742
CH
932 if (unlikely(!out->f_op->splice_write))
933 return warn_unsupported(out, "write");
934 return out->f_op->splice_write(pipe, out, ppos, len, flags);
5274f052
JA
935}
936
2bfc6685
DH
937/*
938 * Indicate to the caller that there was a premature EOF when reading from the
939 * source and the caller didn't indicate they would be sending more data after
940 * this.
941 */
942static void do_splice_eof(struct splice_desc *sd)
943{
944 if (sd->splice_eof)
945 sd->splice_eof(sd);
946}
947
6a3f30b8
DH
948/**
949 * vfs_splice_read - Read data from a file and splice it into a pipe
950 * @in: File to splice from
951 * @ppos: Input file offset
952 * @pipe: Pipe to splice to
953 * @len: Number of bytes to splice
954 * @flags: Splice modifier flags (SPLICE_F_*)
955 *
956 * Splice the requested amount of data from the input file to the pipe. This
957 * is synchronous as the caller must hold the pipe lock across the entire
958 * operation.
959 *
960 * If successful, it returns the amount of data spliced, 0 if it hit the EOF or
961 * a hole and a negative error code otherwise.
83f9135b 962 */
6a3f30b8
DH
963long vfs_splice_read(struct file *in, loff_t *ppos,
964 struct pipe_inode_info *pipe, size_t len,
965 unsigned int flags)
5274f052 966{
313d64a3 967 unsigned int p_space;
5274f052
JA
968 int ret;
969
49570e9b 970 if (unlikely(!(in->f_mode & FMODE_READ)))
5274f052 971 return -EBADF;
123856f0
DH
972 if (!len)
973 return 0;
5274f052 974
313d64a3
AV
975 /* Don't try to read more the pipe has space for. */
976 p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
977 len = min_t(size_t, len, p_space << PAGE_SHIFT);
978
cbb7e577 979 ret = rw_verify_area(READ, in, ppos, len);
5274f052
JA
980 if (unlikely(ret < 0))
981 return ret;
982
03cc0789
AV
983 if (unlikely(len > MAX_RW_COUNT))
984 len = MAX_RW_COUNT;
985
36e2c742
CH
986 if (unlikely(!in->f_op->splice_read))
987 return warn_unsupported(in, "read");
aa3dbde8 988 /*
b85930a0
DH
989 * O_DIRECT and DAX don't deal with the pagecache, so we allocate a
990 * buffer, copy into it and splice that into the pipe.
aa3dbde8 991 */
b85930a0 992 if ((in->f_flags & O_DIRECT) || IS_DAX(in->f_mapping->host))
aa3dbde8 993 return copy_splice_read(in, ppos, pipe, len, flags);
36e2c742 994 return in->f_op->splice_read(in, ppos, pipe, len, flags);
5274f052 995}
6a3f30b8 996EXPORT_SYMBOL_GPL(vfs_splice_read);
5274f052 997
932cc6d4
JA
998/**
999 * splice_direct_to_actor - splices data directly between two non-pipes
1000 * @in: file to splice from
1001 * @sd: actor information on where to splice to
1002 * @actor: handles the data splicing
1003 *
1004 * Description:
1005 * This is a special case helper to splice directly between two
1006 * points, without requiring an explicit pipe. Internally an allocated
79685b8d 1007 * pipe is cached in the process, and reused during the lifetime of
932cc6d4
JA
1008 * that process.
1009 *
c66ab6fa
JA
1010 */
1011ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1012 splice_direct_actor *actor)
b92ce558
JA
1013{
1014 struct pipe_inode_info *pipe;
1015 long ret, bytes;
c66ab6fa 1016 size_t len;
0ff28d9f 1017 int i, flags, more;
b92ce558
JA
1018
1019 /*
97ef77c5
JD
1020 * We require the input to be seekable, as we don't want to randomly
1021 * drop data for eg socket -> socket splicing. Use the piped splicing
1022 * for that!
b92ce558 1023 */
97ef77c5 1024 if (unlikely(!(in->f_mode & FMODE_LSEEK)))
b92ce558
JA
1025 return -EINVAL;
1026
1027 /*
1028 * neither in nor out is a pipe, setup an internal pipe attached to
1029 * 'out' and transfer the wanted data from 'in' to 'out' through that
1030 */
1031 pipe = current->splice_pipe;
49570e9b 1032 if (unlikely(!pipe)) {
7bee130e 1033 pipe = alloc_pipe_info();
b92ce558
JA
1034 if (!pipe)
1035 return -ENOMEM;
1036
1037 /*
1038 * We don't have an immediate reader, but we'll read the stuff
00522fb4 1039 * out of the pipe right after the splice_to_pipe(). So set
b92ce558
JA
1040 * PIPE_READERS appropriately.
1041 */
1042 pipe->readers = 1;
1043
1044 current->splice_pipe = pipe;
1045 }
1046
1047 /*
73d62d83 1048 * Do the splice.
b92ce558 1049 */
b92ce558 1050 bytes = 0;
c66ab6fa 1051 len = sd->total_len;
219d9205
DH
1052
1053 /* Don't block on output, we have to drain the direct pipe. */
c66ab6fa 1054 flags = sd->flags;
219d9205 1055 sd->flags &= ~SPLICE_F_NONBLOCK;
c66ab6fa
JA
1056
1057 /*
219d9205
DH
1058 * We signal MORE until we've read sufficient data to fulfill the
1059 * request and we keep signalling it if the caller set it.
c66ab6fa 1060 */
0ff28d9f 1061 more = sd->flags & SPLICE_F_MORE;
219d9205 1062 sd->flags |= SPLICE_F_MORE;
b92ce558 1063
8cefc107 1064 WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
17614445 1065
b92ce558 1066 while (len) {
51a92c0f 1067 size_t read_len;
a82c53a0 1068 loff_t pos = sd->pos, prev_pos = pos;
b92ce558 1069
6a3f30b8 1070 ret = vfs_splice_read(in, &pos, pipe, len, flags);
51a92c0f 1071 if (unlikely(ret <= 0))
2bfc6685 1072 goto read_failure;
b92ce558
JA
1073
1074 read_len = ret;
c66ab6fa 1075 sd->total_len = read_len;
b92ce558 1076
0ff28d9f 1077 /*
219d9205
DH
1078 * If we now have sufficient data to fulfill the request then
1079 * we clear SPLICE_F_MORE if it was not set initially.
0ff28d9f 1080 */
219d9205 1081 if (read_len >= len && !more)
0ff28d9f 1082 sd->flags &= ~SPLICE_F_MORE;
219d9205 1083
b92ce558
JA
1084 /*
1085 * NOTE: nonblocking mode only applies to the input. We
1086 * must not do the output in nonblocking mode as then we
1087 * could get stuck data in the internal pipe:
1088 */
c66ab6fa 1089 ret = actor(pipe, sd);
a82c53a0
TZ
1090 if (unlikely(ret <= 0)) {
1091 sd->pos = prev_pos;
b92ce558 1092 goto out_release;
a82c53a0 1093 }
b92ce558
JA
1094
1095 bytes += ret;
1096 len -= ret;
bcd4f3ac 1097 sd->pos = pos;
b92ce558 1098
a82c53a0
TZ
1099 if (ret < read_len) {
1100 sd->pos = prev_pos + ret;
51a92c0f 1101 goto out_release;
a82c53a0 1102 }
b92ce558
JA
1103 }
1104
9e97198d 1105done:
8cefc107 1106 pipe->tail = pipe->head = 0;
80848708 1107 file_accessed(in);
b92ce558
JA
1108 return bytes;
1109
2bfc6685
DH
1110read_failure:
1111 /*
1112 * If the user did *not* set SPLICE_F_MORE *and* we didn't hit that
1113 * "use all of len" case that cleared SPLICE_F_MORE, *and* we did a
1114 * "->splice_in()" that returned EOF (ie zero) *and* we have sent at
1115 * least 1 byte *then* we will also do the ->splice_eof() call.
1116 */
1117 if (ret == 0 && !more && len > 0 && bytes)
1118 do_splice_eof(sd);
b92ce558
JA
1119out_release:
1120 /*
1121 * If we did an incomplete transfer we must release
1122 * the pipe buffers in question:
1123 */
8cefc107
DH
1124 for (i = 0; i < pipe->ring_size; i++) {
1125 struct pipe_buffer *buf = &pipe->bufs[i];
b92ce558 1126
a779638c
MS
1127 if (buf->ops)
1128 pipe_buf_release(pipe, buf);
b92ce558 1129 }
b92ce558 1130
9e97198d
JA
1131 if (!bytes)
1132 bytes = ret;
c66ab6fa 1133
9e97198d 1134 goto done;
c66ab6fa
JA
1135}
1136EXPORT_SYMBOL(splice_direct_to_actor);
1137
1138static int direct_splice_actor(struct pipe_inode_info *pipe,
1139 struct splice_desc *sd)
1140{
6a14b90b 1141 struct file *file = sd->u.file;
c66ab6fa 1142
7995bd28 1143 return do_splice_from(pipe, file, sd->opos, sd->total_len,
2cb4b05e 1144 sd->flags);
c66ab6fa
JA
1145}
1146
2bfc6685
DH
1147static void direct_file_splice_eof(struct splice_desc *sd)
1148{
1149 struct file *file = sd->u.file;
1150
1151 if (file->f_op->splice_eof)
1152 file->f_op->splice_eof(file);
1153}
1154
932cc6d4
JA
1155/**
1156 * do_splice_direct - splices data directly between two files
1157 * @in: file to splice from
1158 * @ppos: input file offset
1159 * @out: file to splice to
acdb37c3 1160 * @opos: output file offset
932cc6d4
JA
1161 * @len: number of bytes to splice
1162 * @flags: splice modifier flags
1163 *
1164 * Description:
1165 * For use by do_sendfile(). splice can easily emulate sendfile, but
1166 * doing it in the application would incur an extra system call
1167 * (splice in + splice out, as compared to just sendfile()). So this helper
1168 * can splice directly through a process-private pipe.
1169 *
1170 */
c66ab6fa 1171long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
7995bd28 1172 loff_t *opos, size_t len, unsigned int flags)
c66ab6fa
JA
1173{
1174 struct splice_desc sd = {
1175 .len = len,
1176 .total_len = len,
1177 .flags = flags,
1178 .pos = *ppos,
6a14b90b 1179 .u.file = out,
2bfc6685 1180 .splice_eof = direct_file_splice_eof,
7995bd28 1181 .opos = opos,
c66ab6fa 1182 };
51a92c0f 1183 long ret;
c66ab6fa 1184
18c67cb9
AV
1185 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1186 return -EBADF;
1187
1188 if (unlikely(out->f_flags & O_APPEND))
1189 return -EINVAL;
1190
1191 ret = rw_verify_area(WRITE, out, opos, len);
1192 if (unlikely(ret < 0))
1193 return ret;
1194
c66ab6fa 1195 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
51a92c0f 1196 if (ret > 0)
a82c53a0 1197 *ppos = sd.pos;
51a92c0f 1198
c66ab6fa 1199 return ret;
b92ce558 1200}
1c118596 1201EXPORT_SYMBOL(do_splice_direct);
b92ce558 1202
8924feff
AV
1203static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1204{
52bce911
LT
1205 for (;;) {
1206 if (unlikely(!pipe->readers)) {
1207 send_sig(SIGPIPE, current, 0);
1208 return -EPIPE;
1209 }
6718b6f8 1210 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
52bce911 1211 return 0;
8924feff
AV
1212 if (flags & SPLICE_F_NONBLOCK)
1213 return -EAGAIN;
1214 if (signal_pending(current))
1215 return -ERESTARTSYS;
472e5b05 1216 pipe_wait_writable(pipe);
8924feff 1217 }
8924feff
AV
1218}
1219
7c77f0b3
MS
1220static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1221 struct pipe_inode_info *opipe,
1222 size_t len, unsigned int flags);
ddac0d39 1223
b964bf53 1224long splice_file_to_pipe(struct file *in,
faa97c48
AV
1225 struct pipe_inode_info *opipe,
1226 loff_t *offset,
1227 size_t len, unsigned int flags)
1228{
1229 long ret;
1230
1231 pipe_lock(opipe);
1232 ret = wait_for_space(opipe, flags);
1233 if (!ret)
6a3f30b8 1234 ret = vfs_splice_read(in, offset, opipe, len, flags);
faa97c48
AV
1235 pipe_unlock(opipe);
1236 if (ret > 0)
1237 wakeup_pipe_readers(opipe);
1238 return ret;
1239}
1240
83f9135b
JA
1241/*
1242 * Determine where to splice to/from.
1243 */
ee6e00c8
JA
1244long do_splice(struct file *in, loff_t *off_in, struct file *out,
1245 loff_t *off_out, size_t len, unsigned int flags)
5274f052 1246{
7c77f0b3
MS
1247 struct pipe_inode_info *ipipe;
1248 struct pipe_inode_info *opipe;
7995bd28 1249 loff_t offset;
a4514ebd 1250 long ret;
5274f052 1251
90da2e3f
PB
1252 if (unlikely(!(in->f_mode & FMODE_READ) ||
1253 !(out->f_mode & FMODE_WRITE)))
1254 return -EBADF;
1255
c73be61c
DH
1256 ipipe = get_pipe_info(in, true);
1257 opipe = get_pipe_info(out, true);
7c77f0b3
MS
1258
1259 if (ipipe && opipe) {
1260 if (off_in || off_out)
1261 return -ESPIPE;
1262
7c77f0b3
MS
1263 /* Splicing to self would be fun, but... */
1264 if (ipipe == opipe)
1265 return -EINVAL;
1266
ee5e0011
SK
1267 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1268 flags |= SPLICE_F_NONBLOCK;
1269
12ee4b66
AZ
1270 ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
1271 } else if (ipipe) {
529565dc
IM
1272 if (off_in)
1273 return -ESPIPE;
b92ce558 1274 if (off_out) {
19c9a49b 1275 if (!(out->f_mode & FMODE_PWRITE))
b92ce558 1276 return -EINVAL;
ee6e00c8 1277 offset = *off_out;
7995bd28
AV
1278 } else {
1279 offset = out->f_pos;
1280 }
529565dc 1281
18c67cb9
AV
1282 if (unlikely(out->f_flags & O_APPEND))
1283 return -EINVAL;
1284
1285 ret = rw_verify_area(WRITE, out, &offset, len);
1286 if (unlikely(ret < 0))
1287 return ret;
1288
ee5e0011
SK
1289 if (in->f_flags & O_NONBLOCK)
1290 flags |= SPLICE_F_NONBLOCK;
1291
500368f7 1292 file_start_write(out);
7995bd28 1293 ret = do_splice_from(ipipe, out, &offset, len, flags);
500368f7 1294 file_end_write(out);
a4514ebd 1295
7995bd28
AV
1296 if (!off_out)
1297 out->f_pos = offset;
ee6e00c8
JA
1298 else
1299 *off_out = offset;
12ee4b66 1300 } else if (opipe) {
529565dc
IM
1301 if (off_out)
1302 return -ESPIPE;
b92ce558 1303 if (off_in) {
19c9a49b 1304 if (!(in->f_mode & FMODE_PREAD))
b92ce558 1305 return -EINVAL;
ee6e00c8 1306 offset = *off_in;
7995bd28
AV
1307 } else {
1308 offset = in->f_pos;
1309 }
529565dc 1310
ee5e0011
SK
1311 if (out->f_flags & O_NONBLOCK)
1312 flags |= SPLICE_F_NONBLOCK;
1313
faa97c48 1314 ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
983652c6 1315
7995bd28
AV
1316 if (!off_in)
1317 in->f_pos = offset;
ee6e00c8
JA
1318 else
1319 *off_in = offset;
12ee4b66
AZ
1320 } else {
1321 ret = -EINVAL;
1322 }
a4514ebd 1323
12ee4b66
AZ
1324 if (ret > 0) {
1325 /*
1326 * Generate modify out before access in:
1327 * do_splice_from() may've already sent modify out,
1328 * and this ensures the events get merged.
1329 */
1330 fsnotify_modify(out);
1331 fsnotify_access(in);
529565dc 1332 }
5274f052 1333
12ee4b66 1334 return ret;
5274f052
JA
1335}
1336
ee6e00c8
JA
1337static long __do_splice(struct file *in, loff_t __user *off_in,
1338 struct file *out, loff_t __user *off_out,
1339 size_t len, unsigned int flags)
1340{
1341 struct pipe_inode_info *ipipe;
1342 struct pipe_inode_info *opipe;
1343 loff_t offset, *__off_in = NULL, *__off_out = NULL;
1344 long ret;
1345
1346 ipipe = get_pipe_info(in, true);
1347 opipe = get_pipe_info(out, true);
1348
0f99fc51
JA
1349 if (ipipe) {
1350 if (off_in)
1351 return -ESPIPE;
1352 pipe_clear_nowait(in);
1353 }
1354 if (opipe) {
1355 if (off_out)
1356 return -ESPIPE;
1357 pipe_clear_nowait(out);
1358 }
ee6e00c8
JA
1359
1360 if (off_out) {
1361 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1362 return -EFAULT;
1363 __off_out = &offset;
1364 }
1365 if (off_in) {
1366 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1367 return -EFAULT;
1368 __off_in = &offset;
1369 }
1370
1371 ret = do_splice(in, __off_in, out, __off_out, len, flags);
1372 if (ret < 0)
1373 return ret;
1374
1375 if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1376 return -EFAULT;
1377 if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1378 return -EFAULT;
1379
1380 return ret;
1381}
1382
79fddc4e
AV
1383static int iter_to_pipe(struct iov_iter *from,
1384 struct pipe_inode_info *pipe,
1385 unsigned flags)
912d35f8 1386{
79fddc4e
AV
1387 struct pipe_buffer buf = {
1388 .ops = &user_page_pipe_buf_ops,
1389 .flags = flags
1390 };
1391 size_t total = 0;
1392 int ret = 0;
79fddc4e 1393
7d690c15 1394 while (iov_iter_count(from)) {
79fddc4e 1395 struct page *pages[16];
7d690c15 1396 ssize_t left;
db85a9eb 1397 size_t start;
7d690c15 1398 int i, n;
db85a9eb 1399
7d690c15
AV
1400 left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
1401 if (left <= 0) {
1402 ret = left;
79fddc4e
AV
1403 break;
1404 }
db85a9eb 1405
7d690c15
AV
1406 n = DIV_ROUND_UP(left + start, PAGE_SIZE);
1407 for (i = 0; i < n; i++) {
1408 int size = min_t(int, left, PAGE_SIZE - start);
1409
1410 buf.page = pages[i];
1411 buf.offset = start;
1412 buf.len = size;
1413 ret = add_to_pipe(pipe, &buf);
1414 if (unlikely(ret < 0)) {
1415 iov_iter_revert(from, left);
1416 // this one got dropped by add_to_pipe()
1417 while (++i < n)
1418 put_page(pages[i]);
1419 goto out;
79fddc4e 1420 }
7d690c15
AV
1421 total += ret;
1422 left -= size;
1423 start = 0;
912d35f8 1424 }
912d35f8 1425 }
7d690c15 1426out:
79fddc4e 1427 return total ? total : ret;
912d35f8
JA
1428}
1429
6a14b90b
JA
1430static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1431 struct splice_desc *sd)
1432{
6130f531
AV
1433 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1434 return n == sd->len ? n : -EFAULT;
6a14b90b
JA
1435}
1436
1437/*
1438 * For lack of a better implementation, implement vmsplice() to userspace
1439 * as a simple copy of the pipes pages to the user iov.
1440 */
87a3002a
AV
1441static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
1442 unsigned int flags)
6a14b90b 1443{
c73be61c 1444 struct pipe_inode_info *pipe = get_pipe_info(file, true);
87a3002a
AV
1445 struct splice_desc sd = {
1446 .total_len = iov_iter_count(iter),
1447 .flags = flags,
1448 .u.data = iter
1449 };
1450 long ret = 0;
6a14b90b 1451
6a14b90b
JA
1452 if (!pipe)
1453 return -EBADF;
1454
0f99fc51
JA
1455 pipe_clear_nowait(file);
1456
345995fa
AV
1457 if (sd.total_len) {
1458 pipe_lock(pipe);
1459 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1460 pipe_unlock(pipe);
1461 }
6a14b90b
JA
1462
1463 return ret;
1464}
1465
912d35f8
JA
1466/*
1467 * vmsplice splices a user address range into a pipe. It can be thought of
1468 * as splice-from-memory, where the regular splice is splice-from-file (or
1469 * to file). In both cases the output is a pipe, naturally.
912d35f8 1470 */
87a3002a
AV
1471static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1472 unsigned int flags)
912d35f8 1473{
ddac0d39 1474 struct pipe_inode_info *pipe;
87a3002a 1475 long ret = 0;
79fddc4e
AV
1476 unsigned buf_flag = 0;
1477
1478 if (flags & SPLICE_F_GIFT)
1479 buf_flag = PIPE_BUF_FLAG_GIFT;
912d35f8 1480
c73be61c 1481 pipe = get_pipe_info(file, true);
ddac0d39 1482 if (!pipe)
912d35f8 1483 return -EBADF;
912d35f8 1484
0f99fc51
JA
1485 pipe_clear_nowait(file);
1486
8924feff
AV
1487 pipe_lock(pipe);
1488 ret = wait_for_space(pipe, flags);
79fddc4e 1489 if (!ret)
87a3002a 1490 ret = iter_to_pipe(iter, pipe, buf_flag);
8924feff
AV
1491 pipe_unlock(pipe);
1492 if (ret > 0)
1493 wakeup_pipe_readers(pipe);
35f3d14d 1494 return ret;
912d35f8
JA
1495}
1496
87a3002a
AV
1497static int vmsplice_type(struct fd f, int *type)
1498{
1499 if (!f.file)
1500 return -EBADF;
1501 if (f.file->f_mode & FMODE_WRITE) {
de4eda9d 1502 *type = ITER_SOURCE;
87a3002a 1503 } else if (f.file->f_mode & FMODE_READ) {
de4eda9d 1504 *type = ITER_DEST;
87a3002a
AV
1505 } else {
1506 fdput(f);
1507 return -EBADF;
1508 }
1509 return 0;
1510}
1511
6a14b90b
JA
1512/*
1513 * Note that vmsplice only really supports true splicing _from_ user memory
1514 * to a pipe, not the other way around. Splicing from user memory is a simple
1515 * operation that can be supported without any funky alignment restrictions
1516 * or nasty vm tricks. We simply map in the user memory and fill them into
1517 * a pipe. The reverse isn't quite as easy, though. There are two possible
1518 * solutions for that:
1519 *
1520 * - memcpy() the data internally, at which point we might as well just
1521 * do a regular read() on the buffer anyway.
1522 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1523 * has restriction limitations on both ends of the pipe).
1524 *
1525 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1526 *
1527 */
87a3002a 1528SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
30cfe4ef
DB
1529 unsigned long, nr_segs, unsigned int, flags)
1530{
87a3002a
AV
1531 struct iovec iovstack[UIO_FASTIOV];
1532 struct iovec *iov = iovstack;
1533 struct iov_iter iter;
87e5e6da 1534 ssize_t error;
87a3002a
AV
1535 struct fd f;
1536 int type;
1537
598b3cec
CH
1538 if (unlikely(flags & ~SPLICE_F_ALL))
1539 return -EINVAL;
1540
87a3002a
AV
1541 f = fdget(fd);
1542 error = vmsplice_type(f, &type);
1543 if (error)
1544 return error;
1545
1546 error = import_iovec(type, uiov, nr_segs,
1547 ARRAY_SIZE(iovstack), &iov, &iter);
598b3cec
CH
1548 if (error < 0)
1549 goto out_fdput;
30cfe4ef 1550
598b3cec
CH
1551 if (!iov_iter_count(&iter))
1552 error = 0;
de4eda9d 1553 else if (type == ITER_SOURCE)
598b3cec
CH
1554 error = vmsplice_to_pipe(f.file, &iter, flags);
1555 else
1556 error = vmsplice_to_user(f.file, &iter, flags);
87a3002a 1557
598b3cec
CH
1558 kfree(iov);
1559out_fdput:
87a3002a
AV
1560 fdput(f);
1561 return error;
76b021d0 1562}
76b021d0 1563
836f92ad
HC
1564SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1565 int, fd_out, loff_t __user *, off_out,
1566 size_t, len, unsigned int, flags)
5274f052 1567{
2903ff01 1568 struct fd in, out;
5274f052 1569 long error;
5274f052
JA
1570
1571 if (unlikely(!len))
1572 return 0;
1573
3d6ea290
AV
1574 if (unlikely(flags & ~SPLICE_F_ALL))
1575 return -EINVAL;
1576
5274f052 1577 error = -EBADF;
2903ff01
AV
1578 in = fdget(fd_in);
1579 if (in.file) {
90da2e3f
PB
1580 out = fdget(fd_out);
1581 if (out.file) {
ee6e00c8
JA
1582 error = __do_splice(in.file, off_in, out.file, off_out,
1583 len, flags);
90da2e3f 1584 fdput(out);
5274f052 1585 }
2903ff01 1586 fdput(in);
5274f052 1587 }
5274f052
JA
1588 return error;
1589}
70524490 1590
aadd06e5
JA
1591/*
1592 * Make sure there's data to read. Wait for input if we can, otherwise
1593 * return an appropriate error.
1594 */
7c77f0b3 1595static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5
JA
1596{
1597 int ret;
1598
1599 /*
8cefc107 1600 * Check the pipe occupancy without the inode lock first. This function
aadd06e5
JA
1601 * is speculative anyways, so missing one is ok.
1602 */
8cefc107 1603 if (!pipe_empty(pipe->head, pipe->tail))
aadd06e5
JA
1604 return 0;
1605
1606 ret = 0;
61e0d47c 1607 pipe_lock(pipe);
aadd06e5 1608
8cefc107 1609 while (pipe_empty(pipe->head, pipe->tail)) {
aadd06e5
JA
1610 if (signal_pending(current)) {
1611 ret = -ERESTARTSYS;
1612 break;
1613 }
1614 if (!pipe->writers)
1615 break;
a28c8b9d
LT
1616 if (flags & SPLICE_F_NONBLOCK) {
1617 ret = -EAGAIN;
1618 break;
aadd06e5 1619 }
472e5b05 1620 pipe_wait_readable(pipe);
aadd06e5
JA
1621 }
1622
61e0d47c 1623 pipe_unlock(pipe);
aadd06e5
JA
1624 return ret;
1625}
1626
1627/*
1628 * Make sure there's writeable room. Wait for room if we can, otherwise
1629 * return an appropriate error.
1630 */
7c77f0b3 1631static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5
JA
1632{
1633 int ret;
1634
1635 /*
8cefc107 1636 * Check pipe occupancy without the inode lock first. This function
aadd06e5
JA
1637 * is speculative anyways, so missing one is ok.
1638 */
566d1362 1639 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
aadd06e5
JA
1640 return 0;
1641
1642 ret = 0;
61e0d47c 1643 pipe_lock(pipe);
aadd06e5 1644
6718b6f8 1645 while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
aadd06e5
JA
1646 if (!pipe->readers) {
1647 send_sig(SIGPIPE, current, 0);
1648 ret = -EPIPE;
1649 break;
1650 }
1651 if (flags & SPLICE_F_NONBLOCK) {
1652 ret = -EAGAIN;
1653 break;
1654 }
1655 if (signal_pending(current)) {
1656 ret = -ERESTARTSYS;
1657 break;
1658 }
472e5b05 1659 pipe_wait_writable(pipe);
aadd06e5
JA
1660 }
1661
61e0d47c 1662 pipe_unlock(pipe);
aadd06e5
JA
1663 return ret;
1664}
1665
7c77f0b3
MS
1666/*
1667 * Splice contents of ipipe to opipe.
1668 */
1669static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1670 struct pipe_inode_info *opipe,
1671 size_t len, unsigned int flags)
1672{
1673 struct pipe_buffer *ibuf, *obuf;
8cefc107
DH
1674 unsigned int i_head, o_head;
1675 unsigned int i_tail, o_tail;
1676 unsigned int i_mask, o_mask;
1677 int ret = 0;
7c77f0b3
MS
1678 bool input_wakeup = false;
1679
1680
1681retry:
1682 ret = ipipe_prep(ipipe, flags);
1683 if (ret)
1684 return ret;
1685
1686 ret = opipe_prep(opipe, flags);
1687 if (ret)
1688 return ret;
1689
1690 /*
1691 * Potential ABBA deadlock, work around it by ordering lock
1692 * grabbing by pipe info address. Otherwise two different processes
1693 * could deadlock (one doing tee from A -> B, the other from B -> A).
1694 */
1695 pipe_double_lock(ipipe, opipe);
1696
8cefc107
DH
1697 i_tail = ipipe->tail;
1698 i_mask = ipipe->ring_size - 1;
1699 o_head = opipe->head;
1700 o_mask = opipe->ring_size - 1;
1701
7c77f0b3 1702 do {
8cefc107
DH
1703 size_t o_len;
1704
7c77f0b3
MS
1705 if (!opipe->readers) {
1706 send_sig(SIGPIPE, current, 0);
1707 if (!ret)
1708 ret = -EPIPE;
1709 break;
1710 }
1711
8cefc107
DH
1712 i_head = ipipe->head;
1713 o_tail = opipe->tail;
1714
1715 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
7c77f0b3
MS
1716 break;
1717
1718 /*
1719 * Cannot make any progress, because either the input
1720 * pipe is empty or the output pipe is full.
1721 */
8cefc107 1722 if (pipe_empty(i_head, i_tail) ||
6718b6f8 1723 pipe_full(o_head, o_tail, opipe->max_usage)) {
7c77f0b3
MS
1724 /* Already processed some buffers, break */
1725 if (ret)
1726 break;
1727
1728 if (flags & SPLICE_F_NONBLOCK) {
1729 ret = -EAGAIN;
1730 break;
1731 }
1732
1733 /*
1734 * We raced with another reader/writer and haven't
1735 * managed to process any buffers. A zero return
1736 * value means EOF, so retry instead.
1737 */
1738 pipe_unlock(ipipe);
1739 pipe_unlock(opipe);
1740 goto retry;
1741 }
1742
8cefc107
DH
1743 ibuf = &ipipe->bufs[i_tail & i_mask];
1744 obuf = &opipe->bufs[o_head & o_mask];
7c77f0b3
MS
1745
1746 if (len >= ibuf->len) {
1747 /*
1748 * Simply move the whole buffer from ipipe to opipe
1749 */
1750 *obuf = *ibuf;
1751 ibuf->ops = NULL;
8cefc107
DH
1752 i_tail++;
1753 ipipe->tail = i_tail;
7c77f0b3 1754 input_wakeup = true;
8cefc107
DH
1755 o_len = obuf->len;
1756 o_head++;
1757 opipe->head = o_head;
7c77f0b3
MS
1758 } else {
1759 /*
1760 * Get a reference to this pipe buffer,
1761 * so we can copy the contents over.
1762 */
15fab63e
MW
1763 if (!pipe_buf_get(ipipe, ibuf)) {
1764 if (ret == 0)
1765 ret = -EFAULT;
1766 break;
1767 }
7c77f0b3
MS
1768 *obuf = *ibuf;
1769
1770 /*
f6dd9755 1771 * Don't inherit the gift and merge flags, we need to
7c77f0b3
MS
1772 * prevent multiple steals of this page.
1773 */
1774 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
f6dd9755 1775 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
a0ce2f0a 1776
7c77f0b3 1777 obuf->len = len;
8cefc107
DH
1778 ibuf->offset += len;
1779 ibuf->len -= len;
1780 o_len = len;
1781 o_head++;
1782 opipe->head = o_head;
7c77f0b3 1783 }
8cefc107
DH
1784 ret += o_len;
1785 len -= o_len;
7c77f0b3
MS
1786 } while (len);
1787
1788 pipe_unlock(ipipe);
1789 pipe_unlock(opipe);
1790
1791 /*
1792 * If we put data in the output pipe, wakeup any potential readers.
1793 */
825cdcb1
NK
1794 if (ret > 0)
1795 wakeup_pipe_readers(opipe);
1796
7c77f0b3
MS
1797 if (input_wakeup)
1798 wakeup_pipe_writers(ipipe);
1799
1800 return ret;
1801}
1802
70524490
JA
1803/*
1804 * Link contents of ipipe to opipe.
1805 */
1806static int link_pipe(struct pipe_inode_info *ipipe,
1807 struct pipe_inode_info *opipe,
1808 size_t len, unsigned int flags)
1809{
1810 struct pipe_buffer *ibuf, *obuf;
8cefc107
DH
1811 unsigned int i_head, o_head;
1812 unsigned int i_tail, o_tail;
1813 unsigned int i_mask, o_mask;
1814 int ret = 0;
70524490
JA
1815
1816 /*
1817 * Potential ABBA deadlock, work around it by ordering lock
61e0d47c 1818 * grabbing by pipe info address. Otherwise two different processes
70524490
JA
1819 * could deadlock (one doing tee from A -> B, the other from B -> A).
1820 */
61e0d47c 1821 pipe_double_lock(ipipe, opipe);
70524490 1822
8cefc107
DH
1823 i_tail = ipipe->tail;
1824 i_mask = ipipe->ring_size - 1;
1825 o_head = opipe->head;
1826 o_mask = opipe->ring_size - 1;
1827
aadd06e5 1828 do {
70524490
JA
1829 if (!opipe->readers) {
1830 send_sig(SIGPIPE, current, 0);
1831 if (!ret)
1832 ret = -EPIPE;
1833 break;
1834 }
70524490 1835
8cefc107
DH
1836 i_head = ipipe->head;
1837 o_tail = opipe->tail;
1838
aadd06e5 1839 /*
8cefc107 1840 * If we have iterated all input buffers or run out of
aadd06e5
JA
1841 * output room, break.
1842 */
8cefc107 1843 if (pipe_empty(i_head, i_tail) ||
6718b6f8 1844 pipe_full(o_head, o_tail, opipe->max_usage))
aadd06e5 1845 break;
70524490 1846
8cefc107
DH
1847 ibuf = &ipipe->bufs[i_tail & i_mask];
1848 obuf = &opipe->bufs[o_head & o_mask];
70524490
JA
1849
1850 /*
aadd06e5
JA
1851 * Get a reference to this pipe buffer,
1852 * so we can copy the contents over.
70524490 1853 */
15fab63e
MW
1854 if (!pipe_buf_get(ipipe, ibuf)) {
1855 if (ret == 0)
1856 ret = -EFAULT;
1857 break;
1858 }
aadd06e5 1859
aadd06e5
JA
1860 *obuf = *ibuf;
1861
2a27250e 1862 /*
f6dd9755
CH
1863 * Don't inherit the gift and merge flag, we need to prevent
1864 * multiple steals of this page.
2a27250e 1865 */
aadd06e5 1866 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
f6dd9755 1867 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
a0ce2f0a 1868
aadd06e5
JA
1869 if (obuf->len > len)
1870 obuf->len = len;
aadd06e5
JA
1871 ret += obuf->len;
1872 len -= obuf->len;
8cefc107
DH
1873
1874 o_head++;
1875 opipe->head = o_head;
1876 i_tail++;
aadd06e5 1877 } while (len);
70524490 1878
61e0d47c
MS
1879 pipe_unlock(ipipe);
1880 pipe_unlock(opipe);
70524490 1881
aadd06e5
JA
1882 /*
1883 * If we put data in the output pipe, wakeup any potential readers.
1884 */
825cdcb1
NK
1885 if (ret > 0)
1886 wakeup_pipe_readers(opipe);
70524490
JA
1887
1888 return ret;
1889}
1890
1891/*
1892 * This is a tee(1) implementation that works on pipes. It doesn't copy
1893 * any data, it simply references the 'in' pages on the 'out' pipe.
1894 * The 'flags' used are the SPLICE_F_* variants, currently the only
1895 * applicable one is SPLICE_F_NONBLOCK.
1896 */
9dafdfc2 1897long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
70524490 1898{
c73be61c
DH
1899 struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1900 struct pipe_inode_info *opipe = get_pipe_info(out, true);
aadd06e5 1901 int ret = -EINVAL;
70524490 1902
90da2e3f
PB
1903 if (unlikely(!(in->f_mode & FMODE_READ) ||
1904 !(out->f_mode & FMODE_WRITE)))
1905 return -EBADF;
1906
70524490 1907 /*
aadd06e5
JA
1908 * Duplicate the contents of ipipe to opipe without actually
1909 * copying the data.
70524490 1910 */
aadd06e5 1911 if (ipipe && opipe && ipipe != opipe) {
ee5e0011
SK
1912 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1913 flags |= SPLICE_F_NONBLOCK;
1914
aadd06e5
JA
1915 /*
1916 * Keep going, unless we encounter an error. The ipipe/opipe
1917 * ordering doesn't really matter.
1918 */
7c77f0b3 1919 ret = ipipe_prep(ipipe, flags);
aadd06e5 1920 if (!ret) {
7c77f0b3 1921 ret = opipe_prep(opipe, flags);
02cf01ae 1922 if (!ret)
aadd06e5 1923 ret = link_pipe(ipipe, opipe, len, flags);
aadd06e5
JA
1924 }
1925 }
70524490 1926
aadd06e5 1927 return ret;
70524490
JA
1928}
1929
836f92ad 1930SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
70524490 1931{
90da2e3f 1932 struct fd in, out;
2903ff01 1933 int error;
70524490 1934
3d6ea290
AV
1935 if (unlikely(flags & ~SPLICE_F_ALL))
1936 return -EINVAL;
1937
70524490
JA
1938 if (unlikely(!len))
1939 return 0;
1940
1941 error = -EBADF;
2903ff01
AV
1942 in = fdget(fdin);
1943 if (in.file) {
90da2e3f
PB
1944 out = fdget(fdout);
1945 if (out.file) {
1946 error = do_tee(in.file, out.file, len, flags);
1947 fdput(out);
70524490 1948 }
2903ff01 1949 fdput(in);
70524490
JA
1950 }
1951
1952 return error;
1953}