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