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