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