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