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