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