splice: lift pipe_lock out of splice_to_pipe()
[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 */
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;
206 if (spd->flags & SPLICE_F_GIFT)
207 buf->flags |= PIPE_BUF_FLAG_GIFT;
5274f052 208
8924feff
AV
209 pipe->nrbufs++;
210 page_nr++;
211 ret += buf->len;
29e35094 212
8924feff 213 if (!--spd->nr_pages)
5274f052 214 break;
5274f052
JA
215 }
216
8924feff
AV
217 if (!ret)
218 ret = -EAGAIN;
5274f052 219
8924feff 220out:
00de00bd 221 while (page_nr < spd_pages)
bbdfc2f7 222 spd->spd_release(spd, page_nr++);
5274f052
JA
223
224 return ret;
225}
2b514574 226EXPORT_SYMBOL_GPL(splice_to_pipe);
5274f052 227
708e3508 228void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
bbdfc2f7 229{
09cbfeaf 230 put_page(spd->pages[i]);
bbdfc2f7
JA
231}
232
35f3d14d
JA
233/*
234 * Check if we need to grow the arrays holding pages and partial page
235 * descriptions.
236 */
047fe360 237int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
35f3d14d 238{
047fe360
ED
239 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
240
241 spd->nr_pages_max = buffers;
242 if (buffers <= PIPE_DEF_BUFFERS)
35f3d14d
JA
243 return 0;
244
047fe360
ED
245 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
246 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
35f3d14d
JA
247
248 if (spd->pages && spd->partial)
249 return 0;
250
251 kfree(spd->pages);
252 kfree(spd->partial);
253 return -ENOMEM;
254}
255
047fe360 256void splice_shrink_spd(struct splice_pipe_desc *spd)
35f3d14d 257{
047fe360 258 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
35f3d14d
JA
259 return;
260
261 kfree(spd->pages);
262 kfree(spd->partial);
263}
264
3a326a2c 265static int
cbb7e577
JA
266__generic_file_splice_read(struct file *in, loff_t *ppos,
267 struct pipe_inode_info *pipe, size_t len,
268 unsigned int flags)
5274f052
JA
269{
270 struct address_space *mapping = in->f_mapping;
d8983910 271 unsigned int loff, nr_pages, req_pages;
35f3d14d
JA
272 struct page *pages[PIPE_DEF_BUFFERS];
273 struct partial_page partial[PIPE_DEF_BUFFERS];
5274f052 274 struct page *page;
91ad66ef
JA
275 pgoff_t index, end_index;
276 loff_t isize;
eb20796b 277 int error, page_nr;
912d35f8
JA
278 struct splice_pipe_desc spd = {
279 .pages = pages,
280 .partial = partial,
047fe360 281 .nr_pages_max = PIPE_DEF_BUFFERS,
912d35f8
JA
282 .flags = flags,
283 .ops = &page_cache_pipe_buf_ops,
bbdfc2f7 284 .spd_release = spd_release_page,
912d35f8 285 };
5274f052 286
35f3d14d
JA
287 if (splice_grow_spd(pipe, &spd))
288 return -ENOMEM;
289
09cbfeaf
KS
290 index = *ppos >> PAGE_SHIFT;
291 loff = *ppos & ~PAGE_MASK;
292 req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
047fe360 293 nr_pages = min(req_pages, spd.nr_pages_max);
5274f052 294
eb20796b
JA
295 /*
296 * Lookup the (hopefully) full range of pages we need.
297 */
35f3d14d 298 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
431a4820 299 index += spd.nr_pages;
82aa5d61 300
eb20796b
JA
301 /*
302 * If find_get_pages_contig() returned fewer pages than we needed,
431a4820 303 * readahead/allocate the rest and fill in the holes.
eb20796b 304 */
431a4820 305 if (spd.nr_pages < nr_pages)
cf914a7d
RR
306 page_cache_sync_readahead(mapping, &in->f_ra, in,
307 index, req_pages - spd.nr_pages);
431a4820 308
932cc6d4 309 error = 0;
eb20796b 310 while (spd.nr_pages < nr_pages) {
82aa5d61 311 /*
eb20796b
JA
312 * Page could be there, find_get_pages_contig() breaks on
313 * the first hole.
5274f052 314 */
7480a904
JA
315 page = find_get_page(mapping, index);
316 if (!page) {
7480a904 317 /*
eb20796b 318 * page didn't exist, allocate one.
7480a904
JA
319 */
320 page = page_cache_alloc_cold(mapping);
321 if (!page)
322 break;
323
324 error = add_to_page_cache_lru(page, mapping, index,
c62d2555 325 mapping_gfp_constraint(mapping, GFP_KERNEL));
7480a904 326 if (unlikely(error)) {
09cbfeaf 327 put_page(page);
a0548871
JA
328 if (error == -EEXIST)
329 continue;
7480a904
JA
330 break;
331 }
eb20796b
JA
332 /*
333 * add_to_page_cache() locks the page, unlock it
334 * to avoid convoluting the logic below even more.
335 */
336 unlock_page(page);
7480a904
JA
337 }
338
35f3d14d 339 spd.pages[spd.nr_pages++] = page;
eb20796b
JA
340 index++;
341 }
342
343 /*
344 * Now loop over the map and see if we need to start IO on any
345 * pages, fill in the partial map, etc.
346 */
09cbfeaf 347 index = *ppos >> PAGE_SHIFT;
eb20796b
JA
348 nr_pages = spd.nr_pages;
349 spd.nr_pages = 0;
350 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
351 unsigned int this_len;
352
353 if (!len)
354 break;
355
356 /*
357 * this_len is the max we'll use from this page
358 */
09cbfeaf 359 this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
35f3d14d 360 page = spd.pages[page_nr];
eb20796b 361
a08a166f 362 if (PageReadahead(page))
cf914a7d 363 page_cache_async_readahead(mapping, &in->f_ra, in,
d8983910 364 page, index, req_pages - page_nr);
a08a166f 365
7480a904
JA
366 /*
367 * If the page isn't uptodate, we may need to start io on it
368 */
369 if (!PageUptodate(page)) {
6965031d 370 lock_page(page);
7480a904
JA
371
372 /*
32502b84
MS
373 * Page was truncated, or invalidated by the
374 * filesystem. Redo the find/create, but this time the
375 * page is kept locked, so there's no chance of another
376 * race with truncate/invalidate.
7480a904
JA
377 */
378 if (!page->mapping) {
379 unlock_page(page);
90330e68 380retry_lookup:
32502b84
MS
381 page = find_or_create_page(mapping, index,
382 mapping_gfp_mask(mapping));
383
384 if (!page) {
385 error = -ENOMEM;
386 break;
387 }
09cbfeaf 388 put_page(spd.pages[page_nr]);
35f3d14d 389 spd.pages[page_nr] = page;
7480a904
JA
390 }
391 /*
392 * page was already under io and is now done, great
393 */
394 if (PageUptodate(page)) {
395 unlock_page(page);
396 goto fill_it;
397 }
5274f052 398
7480a904
JA
399 /*
400 * need to read in the page
401 */
402 error = mapping->a_ops->readpage(in, page);
5274f052 403 if (unlikely(error)) {
eb20796b 404 /*
90330e68 405 * Re-lookup the page
eb20796b 406 */
7480a904 407 if (error == AOP_TRUNCATED_PAGE)
90330e68 408 goto retry_lookup;
eb20796b 409
5274f052
JA
410 break;
411 }
620a324b
JA
412 }
413fill_it:
414 /*
415 * i_size must be checked after PageUptodate.
416 */
417 isize = i_size_read(mapping->host);
09cbfeaf 418 end_index = (isize - 1) >> PAGE_SHIFT;
620a324b
JA
419 if (unlikely(!isize || index > end_index))
420 break;
421
422 /*
423 * if this is the last page, see if we need to shrink
424 * the length and stop
425 */
426 if (end_index == index) {
427 unsigned int plen;
91ad66ef
JA
428
429 /*
620a324b 430 * max good bytes in this page
91ad66ef 431 */
09cbfeaf 432 plen = ((isize - 1) & ~PAGE_MASK) + 1;
620a324b 433 if (plen <= loff)
91ad66ef 434 break;
91ad66ef
JA
435
436 /*
620a324b 437 * force quit after adding this page
91ad66ef 438 */
620a324b
JA
439 this_len = min(this_len, plen - loff);
440 len = this_len;
5274f052 441 }
620a324b 442
35f3d14d
JA
443 spd.partial[page_nr].offset = loff;
444 spd.partial[page_nr].len = this_len;
82aa5d61 445 len -= this_len;
91ad66ef 446 loff = 0;
eb20796b
JA
447 spd.nr_pages++;
448 index++;
5274f052
JA
449 }
450
eb20796b 451 /*
475ecade 452 * Release any pages at the end, if we quit early. 'page_nr' is how far
eb20796b
JA
453 * we got, 'nr_pages' is how many pages are in the map.
454 */
455 while (page_nr < nr_pages)
09cbfeaf
KS
456 put_page(spd.pages[page_nr++]);
457 in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
eb20796b 458
912d35f8 459 if (spd.nr_pages)
35f3d14d 460 error = splice_to_pipe(pipe, &spd);
5274f052 461
047fe360 462 splice_shrink_spd(&spd);
7480a904 463 return error;
5274f052
JA
464}
465
83f9135b
JA
466/**
467 * generic_file_splice_read - splice data from file to a pipe
468 * @in: file to splice from
932cc6d4 469 * @ppos: position in @in
83f9135b
JA
470 * @pipe: pipe to splice to
471 * @len: number of bytes to splice
472 * @flags: splice modifier flags
473 *
932cc6d4
JA
474 * Description:
475 * Will read pages from given file and fill them into a pipe. Can be
476 * used as long as the address_space operations for the source implements
477 * a readpage() hook.
478 *
83f9135b 479 */
cbb7e577
JA
480ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
481 struct pipe_inode_info *pipe, size_t len,
482 unsigned int flags)
5274f052 483{
d366d398 484 loff_t isize, left;
8191ecd1 485 int ret;
d366d398 486
be64f884
BH
487 if (IS_DAX(in->f_mapping->host))
488 return default_file_splice_read(in, ppos, pipe, len, flags);
489
d366d398
JA
490 isize = i_size_read(in->f_mapping->host);
491 if (unlikely(*ppos >= isize))
492 return 0;
493
494 left = isize - *ppos;
495 if (unlikely(left < len))
496 len = left;
5274f052 497
8191ecd1 498 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
723590ed 499 if (ret > 0) {
cbb7e577 500 *ppos += ret;
723590ed
MS
501 file_accessed(in);
502 }
5274f052
JA
503
504 return ret;
505}
059a8f37
JA
506EXPORT_SYMBOL(generic_file_splice_read);
507
6818173b
MS
508static const struct pipe_buf_operations default_pipe_buf_ops = {
509 .can_merge = 0,
6818173b
MS
510 .confirm = generic_pipe_buf_confirm,
511 .release = generic_pipe_buf_release,
512 .steal = generic_pipe_buf_steal,
513 .get = generic_pipe_buf_get,
514};
515
28a625cb
MS
516static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
517 struct pipe_buffer *buf)
518{
519 return 1;
520}
521
522/* Pipe buffer operations for a socket and similar. */
523const struct pipe_buf_operations nosteal_pipe_buf_ops = {
524 .can_merge = 0,
28a625cb
MS
525 .confirm = generic_pipe_buf_confirm,
526 .release = generic_pipe_buf_release,
527 .steal = generic_pipe_buf_nosteal,
528 .get = generic_pipe_buf_get,
529};
530EXPORT_SYMBOL(nosteal_pipe_buf_ops);
531
6818173b
MS
532static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
533 unsigned long vlen, loff_t offset)
534{
535 mm_segment_t old_fs;
536 loff_t pos = offset;
537 ssize_t res;
538
539 old_fs = get_fs();
540 set_fs(get_ds());
541 /* The cast to a user pointer is valid due to the set_fs() */
793b80ef 542 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
6818173b
MS
543 set_fs(old_fs);
544
545 return res;
546}
547
7bb307e8 548ssize_t kernel_write(struct file *file, const char *buf, size_t count,
b2858d7d 549 loff_t pos)
0b0a47f5
MS
550{
551 mm_segment_t old_fs;
552 ssize_t res;
553
554 old_fs = get_fs();
555 set_fs(get_ds());
556 /* The cast to a user pointer is valid due to the set_fs() */
7bb307e8 557 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
0b0a47f5
MS
558 set_fs(old_fs);
559
560 return res;
561}
7bb307e8 562EXPORT_SYMBOL(kernel_write);
0b0a47f5 563
6818173b
MS
564ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
565 struct pipe_inode_info *pipe, size_t len,
566 unsigned int flags)
567{
568 unsigned int nr_pages;
569 unsigned int nr_freed;
570 size_t offset;
35f3d14d
JA
571 struct page *pages[PIPE_DEF_BUFFERS];
572 struct partial_page partial[PIPE_DEF_BUFFERS];
573 struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
6818173b
MS
574 ssize_t res;
575 size_t this_len;
576 int error;
577 int i;
578 struct splice_pipe_desc spd = {
579 .pages = pages,
580 .partial = partial,
047fe360 581 .nr_pages_max = PIPE_DEF_BUFFERS,
6818173b
MS
582 .flags = flags,
583 .ops = &default_pipe_buf_ops,
584 .spd_release = spd_release_page,
585 };
586
35f3d14d
JA
587 if (splice_grow_spd(pipe, &spd))
588 return -ENOMEM;
589
590 res = -ENOMEM;
591 vec = __vec;
047fe360
ED
592 if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
593 vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
35f3d14d
JA
594 if (!vec)
595 goto shrink_ret;
596 }
597
09cbfeaf
KS
598 offset = *ppos & ~PAGE_MASK;
599 nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
6818173b 600
047fe360 601 for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
6818173b
MS
602 struct page *page;
603
4f231228 604 page = alloc_page(GFP_USER);
6818173b
MS
605 error = -ENOMEM;
606 if (!page)
607 goto err;
608
09cbfeaf 609 this_len = min_t(size_t, len, PAGE_SIZE - offset);
4f231228 610 vec[i].iov_base = (void __user *) page_address(page);
6818173b 611 vec[i].iov_len = this_len;
35f3d14d 612 spd.pages[i] = page;
6818173b
MS
613 spd.nr_pages++;
614 len -= this_len;
615 offset = 0;
616 }
617
618 res = kernel_readv(in, vec, spd.nr_pages, *ppos);
77f6bf57
AM
619 if (res < 0) {
620 error = res;
6818173b 621 goto err;
77f6bf57 622 }
6818173b
MS
623
624 error = 0;
625 if (!res)
626 goto err;
627
628 nr_freed = 0;
629 for (i = 0; i < spd.nr_pages; i++) {
6818173b 630 this_len = min_t(size_t, vec[i].iov_len, res);
35f3d14d
JA
631 spd.partial[i].offset = 0;
632 spd.partial[i].len = this_len;
6818173b 633 if (!this_len) {
35f3d14d
JA
634 __free_page(spd.pages[i]);
635 spd.pages[i] = NULL;
6818173b
MS
636 nr_freed++;
637 }
638 res -= this_len;
639 }
640 spd.nr_pages -= nr_freed;
641
642 res = splice_to_pipe(pipe, &spd);
643 if (res > 0)
644 *ppos += res;
645
35f3d14d
JA
646shrink_ret:
647 if (vec != __vec)
648 kfree(vec);
047fe360 649 splice_shrink_spd(&spd);
6818173b
MS
650 return res;
651
652err:
4f231228 653 for (i = 0; i < spd.nr_pages; i++)
35f3d14d 654 __free_page(spd.pages[i]);
4f231228 655
35f3d14d
JA
656 res = error;
657 goto shrink_ret;
6818173b
MS
658}
659EXPORT_SYMBOL(default_file_splice_read);
660
5274f052 661/*
4f6f0bd2 662 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
016b661e 663 * using sendpage(). Return the number of bytes sent.
5274f052 664 */
76ad4d11 665static int pipe_to_sendpage(struct pipe_inode_info *pipe,
5274f052
JA
666 struct pipe_buffer *buf, struct splice_desc *sd)
667{
6a14b90b 668 struct file *file = sd->u.file;
5274f052 669 loff_t pos = sd->pos;
a8adbe37 670 int more;
5274f052 671
72c2d531 672 if (!likely(file->f_op->sendpage))
a8adbe37
MM
673 return -EINVAL;
674
35f9c09f 675 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
ae62ca7b
ED
676
677 if (sd->len < sd->total_len && pipe->nrbufs > 1)
35f9c09f 678 more |= MSG_SENDPAGE_NOTLAST;
ae62ca7b 679
a8adbe37
MM
680 return file->f_op->sendpage(file, buf->page, buf->offset,
681 sd->len, &pos, more);
5274f052
JA
682}
683
b3c2d2dd
MS
684static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
685{
686 smp_mb();
687 if (waitqueue_active(&pipe->wait))
688 wake_up_interruptible(&pipe->wait);
689 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
690}
691
932cc6d4 692/**
b3c2d2dd 693 * splice_from_pipe_feed - feed available data from a pipe to a file
932cc6d4
JA
694 * @pipe: pipe to splice from
695 * @sd: information to @actor
696 * @actor: handler that splices the data
697 *
698 * Description:
b3c2d2dd
MS
699 * This function loops over the pipe and calls @actor to do the
700 * actual moving of a single struct pipe_buffer to the desired
701 * destination. It returns when there's no more buffers left in
702 * the pipe or if the requested number of bytes (@sd->total_len)
703 * have been copied. It returns a positive number (one) if the
704 * pipe needs to be filled with more data, zero if the required
705 * number of bytes have been copied and -errno on error.
932cc6d4 706 *
b3c2d2dd
MS
707 * This, together with splice_from_pipe_{begin,end,next}, may be
708 * used to implement the functionality of __splice_from_pipe() when
709 * locking is required around copying the pipe buffers to the
710 * destination.
83f9135b 711 */
96f9bc8f 712static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
b3c2d2dd 713 splice_actor *actor)
5274f052 714{
b3c2d2dd 715 int ret;
5274f052 716
b3c2d2dd
MS
717 while (pipe->nrbufs) {
718 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
719 const struct pipe_buf_operations *ops = buf->ops;
5274f052 720
b3c2d2dd
MS
721 sd->len = buf->len;
722 if (sd->len > sd->total_len)
723 sd->len = sd->total_len;
5274f052 724
a8adbe37
MM
725 ret = buf->ops->confirm(pipe, buf);
726 if (unlikely(ret)) {
b3c2d2dd
MS
727 if (ret == -ENODATA)
728 ret = 0;
729 return ret;
730 }
a8adbe37
MM
731
732 ret = actor(pipe, buf, sd);
733 if (ret <= 0)
734 return ret;
735
b3c2d2dd
MS
736 buf->offset += ret;
737 buf->len -= ret;
738
739 sd->num_spliced += ret;
740 sd->len -= ret;
741 sd->pos += ret;
742 sd->total_len -= ret;
743
744 if (!buf->len) {
745 buf->ops = NULL;
746 ops->release(pipe, buf);
35f3d14d 747 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
b3c2d2dd 748 pipe->nrbufs--;
6447a3cf 749 if (pipe->files)
b3c2d2dd
MS
750 sd->need_wakeup = true;
751 }
5274f052 752
b3c2d2dd
MS
753 if (!sd->total_len)
754 return 0;
755 }
5274f052 756
b3c2d2dd
MS
757 return 1;
758}
5274f052 759
b3c2d2dd
MS
760/**
761 * splice_from_pipe_next - wait for some data to splice from
762 * @pipe: pipe to splice from
763 * @sd: information about the splice operation
764 *
765 * Description:
766 * This function will wait for some data and return a positive
767 * value (one) if pipe buffers are available. It will return zero
768 * or -errno if no more data needs to be spliced.
769 */
96f9bc8f 770static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
b3c2d2dd 771{
c725bfce
JK
772 /*
773 * Check for signal early to make process killable when there are
774 * always buffers available
775 */
776 if (signal_pending(current))
777 return -ERESTARTSYS;
778
b3c2d2dd
MS
779 while (!pipe->nrbufs) {
780 if (!pipe->writers)
781 return 0;
016b661e 782
b3c2d2dd
MS
783 if (!pipe->waiting_writers && sd->num_spliced)
784 return 0;
73d62d83 785
b3c2d2dd
MS
786 if (sd->flags & SPLICE_F_NONBLOCK)
787 return -EAGAIN;
5274f052 788
b3c2d2dd
MS
789 if (signal_pending(current))
790 return -ERESTARTSYS;
5274f052 791
b3c2d2dd
MS
792 if (sd->need_wakeup) {
793 wakeup_pipe_writers(pipe);
794 sd->need_wakeup = false;
5274f052
JA
795 }
796
b3c2d2dd
MS
797 pipe_wait(pipe);
798 }
29e35094 799
b3c2d2dd
MS
800 return 1;
801}
5274f052 802
b3c2d2dd
MS
803/**
804 * splice_from_pipe_begin - start splicing from pipe
b80901bb 805 * @sd: information about the splice operation
b3c2d2dd
MS
806 *
807 * Description:
808 * This function should be called before a loop containing
809 * splice_from_pipe_next() and splice_from_pipe_feed() to
810 * initialize the necessary fields of @sd.
811 */
96f9bc8f 812static void splice_from_pipe_begin(struct splice_desc *sd)
b3c2d2dd
MS
813{
814 sd->num_spliced = 0;
815 sd->need_wakeup = false;
816}
5274f052 817
b3c2d2dd
MS
818/**
819 * splice_from_pipe_end - finish splicing from pipe
820 * @pipe: pipe to splice from
821 * @sd: information about the splice operation
822 *
823 * Description:
824 * This function will wake up pipe writers if necessary. It should
825 * be called after a loop containing splice_from_pipe_next() and
826 * splice_from_pipe_feed().
827 */
96f9bc8f 828static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
b3c2d2dd
MS
829{
830 if (sd->need_wakeup)
831 wakeup_pipe_writers(pipe);
832}
5274f052 833
b3c2d2dd
MS
834/**
835 * __splice_from_pipe - splice data from a pipe to given actor
836 * @pipe: pipe to splice from
837 * @sd: information to @actor
838 * @actor: handler that splices the data
839 *
840 * Description:
841 * This function does little more than loop over the pipe and call
842 * @actor to do the actual moving of a single struct pipe_buffer to
843 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
844 * pipe_to_user.
845 *
846 */
847ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
848 splice_actor *actor)
849{
850 int ret;
5274f052 851
b3c2d2dd
MS
852 splice_from_pipe_begin(sd);
853 do {
c2489e07 854 cond_resched();
b3c2d2dd
MS
855 ret = splice_from_pipe_next(pipe, sd);
856 if (ret > 0)
857 ret = splice_from_pipe_feed(pipe, sd, actor);
858 } while (ret > 0);
859 splice_from_pipe_end(pipe, sd);
860
861 return sd->num_spliced ? sd->num_spliced : ret;
5274f052 862}
40bee44e 863EXPORT_SYMBOL(__splice_from_pipe);
5274f052 864
932cc6d4
JA
865/**
866 * splice_from_pipe - splice data from a pipe to a file
867 * @pipe: pipe to splice from
868 * @out: file to splice to
869 * @ppos: position in @out
870 * @len: how many bytes to splice
871 * @flags: splice modifier flags
872 * @actor: handler that splices the data
873 *
874 * Description:
2933970b 875 * See __splice_from_pipe. This function locks the pipe inode,
932cc6d4
JA
876 * otherwise it's identical to __splice_from_pipe().
877 *
878 */
6da61809
MF
879ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
880 loff_t *ppos, size_t len, unsigned int flags,
881 splice_actor *actor)
882{
883 ssize_t ret;
c66ab6fa
JA
884 struct splice_desc sd = {
885 .total_len = len,
886 .flags = flags,
887 .pos = *ppos,
6a14b90b 888 .u.file = out,
c66ab6fa 889 };
6da61809 890
61e0d47c 891 pipe_lock(pipe);
c66ab6fa 892 ret = __splice_from_pipe(pipe, &sd, actor);
61e0d47c 893 pipe_unlock(pipe);
6da61809
MF
894
895 return ret;
896}
897
8d020765
AV
898/**
899 * iter_file_splice_write - splice data from a pipe to a file
900 * @pipe: pipe info
901 * @out: file to write to
902 * @ppos: position in @out
903 * @len: number of bytes to splice
904 * @flags: splice modifier flags
905 *
906 * Description:
907 * Will either move or copy pages (determined by @flags options) from
908 * the given pipe inode to the given file.
909 * This one is ->write_iter-based.
910 *
911 */
912ssize_t
913iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
914 loff_t *ppos, size_t len, unsigned int flags)
915{
916 struct splice_desc sd = {
917 .total_len = len,
918 .flags = flags,
919 .pos = *ppos,
920 .u.file = out,
921 };
922 int nbufs = pipe->buffers;
923 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
924 GFP_KERNEL);
925 ssize_t ret;
926
927 if (unlikely(!array))
928 return -ENOMEM;
929
930 pipe_lock(pipe);
931
932 splice_from_pipe_begin(&sd);
933 while (sd.total_len) {
934 struct iov_iter from;
8d020765
AV
935 size_t left;
936 int n, idx;
937
938 ret = splice_from_pipe_next(pipe, &sd);
939 if (ret <= 0)
940 break;
941
942 if (unlikely(nbufs < pipe->buffers)) {
943 kfree(array);
944 nbufs = pipe->buffers;
945 array = kcalloc(nbufs, sizeof(struct bio_vec),
946 GFP_KERNEL);
947 if (!array) {
948 ret = -ENOMEM;
949 break;
950 }
951 }
952
953 /* build the vector */
954 left = sd.total_len;
955 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
956 struct pipe_buffer *buf = pipe->bufs + idx;
957 size_t this_len = buf->len;
958
959 if (this_len > left)
960 this_len = left;
961
962 if (idx == pipe->buffers - 1)
963 idx = -1;
964
965 ret = buf->ops->confirm(pipe, buf);
966 if (unlikely(ret)) {
967 if (ret == -ENODATA)
968 ret = 0;
969 goto done;
970 }
971
972 array[n].bv_page = buf->page;
973 array[n].bv_len = this_len;
974 array[n].bv_offset = buf->offset;
975 left -= this_len;
976 }
977
05afcb77
AV
978 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
979 sd.total_len - left);
dbe4e192 980 ret = vfs_iter_write(out, &from, &sd.pos);
8d020765
AV
981 if (ret <= 0)
982 break;
983
984 sd.num_spliced += ret;
985 sd.total_len -= ret;
dbe4e192 986 *ppos = sd.pos;
8d020765
AV
987
988 /* dismiss the fully eaten buffers, adjust the partial one */
989 while (ret) {
990 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
991 if (ret >= buf->len) {
992 const struct pipe_buf_operations *ops = buf->ops;
993 ret -= buf->len;
994 buf->len = 0;
995 buf->ops = NULL;
996 ops->release(pipe, buf);
997 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
998 pipe->nrbufs--;
999 if (pipe->files)
1000 sd.need_wakeup = true;
1001 } else {
1002 buf->offset += ret;
1003 buf->len -= ret;
1004 ret = 0;
1005 }
1006 }
1007 }
1008done:
1009 kfree(array);
1010 splice_from_pipe_end(pipe, &sd);
1011
1012 pipe_unlock(pipe);
1013
1014 if (sd.num_spliced)
1015 ret = sd.num_spliced;
1016
1017 return ret;
1018}
1019
1020EXPORT_SYMBOL(iter_file_splice_write);
1021
b2858d7d
MS
1022static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1023 struct splice_desc *sd)
0b0a47f5 1024{
b2858d7d
MS
1025 int ret;
1026 void *data;
06ae43f3 1027 loff_t tmp = sd->pos;
b2858d7d 1028
fbb32750 1029 data = kmap(buf->page);
06ae43f3 1030 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
fbb32750 1031 kunmap(buf->page);
b2858d7d
MS
1032
1033 return ret;
0b0a47f5
MS
1034}
1035
1036static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
1037 struct file *out, loff_t *ppos,
1038 size_t len, unsigned int flags)
1039{
b2858d7d 1040 ssize_t ret;
0b0a47f5 1041
b2858d7d
MS
1042 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1043 if (ret > 0)
1044 *ppos += ret;
0b0a47f5 1045
b2858d7d 1046 return ret;
0b0a47f5
MS
1047}
1048
83f9135b
JA
1049/**
1050 * generic_splice_sendpage - splice data from a pipe to a socket
932cc6d4 1051 * @pipe: pipe to splice from
83f9135b 1052 * @out: socket to write to
932cc6d4 1053 * @ppos: position in @out
83f9135b
JA
1054 * @len: number of bytes to splice
1055 * @flags: splice modifier flags
1056 *
932cc6d4
JA
1057 * Description:
1058 * Will send @len bytes from the pipe to a network socket. No data copying
1059 * is involved.
83f9135b
JA
1060 *
1061 */
3a326a2c 1062ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
cbb7e577 1063 loff_t *ppos, size_t len, unsigned int flags)
5274f052 1064{
00522fb4 1065 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
5274f052
JA
1066}
1067
059a8f37 1068EXPORT_SYMBOL(generic_splice_sendpage);
a0f06780 1069
83f9135b
JA
1070/*
1071 * Attempt to initiate a splice from pipe to file.
1072 */
3a326a2c 1073static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
cbb7e577 1074 loff_t *ppos, size_t len, unsigned int flags)
5274f052 1075{
0b0a47f5
MS
1076 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
1077 loff_t *, size_t, unsigned int);
5274f052 1078
72c2d531 1079 if (out->f_op->splice_write)
cc56f7de
CG
1080 splice_write = out->f_op->splice_write;
1081 else
0b0a47f5
MS
1082 splice_write = default_file_splice_write;
1083
500368f7 1084 return splice_write(pipe, out, ppos, len, flags);
5274f052
JA
1085}
1086
83f9135b
JA
1087/*
1088 * Attempt to initiate a splice from a file to a pipe.
1089 */
cbb7e577
JA
1090static long do_splice_to(struct file *in, loff_t *ppos,
1091 struct pipe_inode_info *pipe, size_t len,
1092 unsigned int flags)
5274f052 1093{
6818173b
MS
1094 ssize_t (*splice_read)(struct file *, loff_t *,
1095 struct pipe_inode_info *, size_t, unsigned int);
5274f052
JA
1096 int ret;
1097
49570e9b 1098 if (unlikely(!(in->f_mode & FMODE_READ)))
5274f052
JA
1099 return -EBADF;
1100
cbb7e577 1101 ret = rw_verify_area(READ, in, ppos, len);
5274f052
JA
1102 if (unlikely(ret < 0))
1103 return ret;
1104
03cc0789
AV
1105 if (unlikely(len > MAX_RW_COUNT))
1106 len = MAX_RW_COUNT;
1107
72c2d531 1108 if (in->f_op->splice_read)
cc56f7de
CG
1109 splice_read = in->f_op->splice_read;
1110 else
6818173b
MS
1111 splice_read = default_file_splice_read;
1112
1113 return splice_read(in, ppos, pipe, len, flags);
5274f052
JA
1114}
1115
932cc6d4
JA
1116/**
1117 * splice_direct_to_actor - splices data directly between two non-pipes
1118 * @in: file to splice from
1119 * @sd: actor information on where to splice to
1120 * @actor: handles the data splicing
1121 *
1122 * Description:
1123 * This is a special case helper to splice directly between two
1124 * points, without requiring an explicit pipe. Internally an allocated
79685b8d 1125 * pipe is cached in the process, and reused during the lifetime of
932cc6d4
JA
1126 * that process.
1127 *
c66ab6fa
JA
1128 */
1129ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1130 splice_direct_actor *actor)
b92ce558
JA
1131{
1132 struct pipe_inode_info *pipe;
1133 long ret, bytes;
1134 umode_t i_mode;
c66ab6fa 1135 size_t len;
0ff28d9f 1136 int i, flags, more;
b92ce558
JA
1137
1138 /*
1139 * We require the input being a regular file, as we don't want to
1140 * randomly drop data for eg socket -> socket splicing. Use the
1141 * piped splicing for that!
1142 */
496ad9aa 1143 i_mode = file_inode(in)->i_mode;
b92ce558
JA
1144 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1145 return -EINVAL;
1146
1147 /*
1148 * neither in nor out is a pipe, setup an internal pipe attached to
1149 * 'out' and transfer the wanted data from 'in' to 'out' through that
1150 */
1151 pipe = current->splice_pipe;
49570e9b 1152 if (unlikely(!pipe)) {
7bee130e 1153 pipe = alloc_pipe_info();
b92ce558
JA
1154 if (!pipe)
1155 return -ENOMEM;
1156
1157 /*
1158 * We don't have an immediate reader, but we'll read the stuff
00522fb4 1159 * out of the pipe right after the splice_to_pipe(). So set
b92ce558
JA
1160 * PIPE_READERS appropriately.
1161 */
1162 pipe->readers = 1;
1163
1164 current->splice_pipe = pipe;
1165 }
1166
1167 /*
73d62d83 1168 * Do the splice.
b92ce558
JA
1169 */
1170 ret = 0;
1171 bytes = 0;
c66ab6fa
JA
1172 len = sd->total_len;
1173 flags = sd->flags;
1174
1175 /*
1176 * Don't block on output, we have to drain the direct pipe.
1177 */
1178 sd->flags &= ~SPLICE_F_NONBLOCK;
0ff28d9f 1179 more = sd->flags & SPLICE_F_MORE;
b92ce558
JA
1180
1181 while (len) {
51a92c0f 1182 size_t read_len;
a82c53a0 1183 loff_t pos = sd->pos, prev_pos = pos;
b92ce558 1184
bcd4f3ac 1185 ret = do_splice_to(in, &pos, pipe, len, flags);
51a92c0f 1186 if (unlikely(ret <= 0))
b92ce558
JA
1187 goto out_release;
1188
1189 read_len = ret;
c66ab6fa 1190 sd->total_len = read_len;
b92ce558 1191
0ff28d9f
CL
1192 /*
1193 * If more data is pending, set SPLICE_F_MORE
1194 * If this is the last data and SPLICE_F_MORE was not set
1195 * initially, clears it.
1196 */
1197 if (read_len < len)
1198 sd->flags |= SPLICE_F_MORE;
1199 else if (!more)
1200 sd->flags &= ~SPLICE_F_MORE;
b92ce558
JA
1201 /*
1202 * NOTE: nonblocking mode only applies to the input. We
1203 * must not do the output in nonblocking mode as then we
1204 * could get stuck data in the internal pipe:
1205 */
c66ab6fa 1206 ret = actor(pipe, sd);
a82c53a0
TZ
1207 if (unlikely(ret <= 0)) {
1208 sd->pos = prev_pos;
b92ce558 1209 goto out_release;
a82c53a0 1210 }
b92ce558
JA
1211
1212 bytes += ret;
1213 len -= ret;
bcd4f3ac 1214 sd->pos = pos;
b92ce558 1215
a82c53a0
TZ
1216 if (ret < read_len) {
1217 sd->pos = prev_pos + ret;
51a92c0f 1218 goto out_release;
a82c53a0 1219 }
b92ce558
JA
1220 }
1221
9e97198d 1222done:
b92ce558 1223 pipe->nrbufs = pipe->curbuf = 0;
80848708 1224 file_accessed(in);
b92ce558
JA
1225 return bytes;
1226
1227out_release:
1228 /*
1229 * If we did an incomplete transfer we must release
1230 * the pipe buffers in question:
1231 */
35f3d14d 1232 for (i = 0; i < pipe->buffers; i++) {
b92ce558
JA
1233 struct pipe_buffer *buf = pipe->bufs + i;
1234
1235 if (buf->ops) {
1236 buf->ops->release(pipe, buf);
1237 buf->ops = NULL;
1238 }
1239 }
b92ce558 1240
9e97198d
JA
1241 if (!bytes)
1242 bytes = ret;
c66ab6fa 1243
9e97198d 1244 goto done;
c66ab6fa
JA
1245}
1246EXPORT_SYMBOL(splice_direct_to_actor);
1247
1248static int direct_splice_actor(struct pipe_inode_info *pipe,
1249 struct splice_desc *sd)
1250{
6a14b90b 1251 struct file *file = sd->u.file;
c66ab6fa 1252
7995bd28 1253 return do_splice_from(pipe, file, sd->opos, sd->total_len,
2cb4b05e 1254 sd->flags);
c66ab6fa
JA
1255}
1256
932cc6d4
JA
1257/**
1258 * do_splice_direct - splices data directly between two files
1259 * @in: file to splice from
1260 * @ppos: input file offset
1261 * @out: file to splice to
acdb37c3 1262 * @opos: output file offset
932cc6d4
JA
1263 * @len: number of bytes to splice
1264 * @flags: splice modifier flags
1265 *
1266 * Description:
1267 * For use by do_sendfile(). splice can easily emulate sendfile, but
1268 * doing it in the application would incur an extra system call
1269 * (splice in + splice out, as compared to just sendfile()). So this helper
1270 * can splice directly through a process-private pipe.
1271 *
1272 */
c66ab6fa 1273long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
7995bd28 1274 loff_t *opos, size_t len, unsigned int flags)
c66ab6fa
JA
1275{
1276 struct splice_desc sd = {
1277 .len = len,
1278 .total_len = len,
1279 .flags = flags,
1280 .pos = *ppos,
6a14b90b 1281 .u.file = out,
7995bd28 1282 .opos = opos,
c66ab6fa 1283 };
51a92c0f 1284 long ret;
c66ab6fa 1285
18c67cb9
AV
1286 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1287 return -EBADF;
1288
1289 if (unlikely(out->f_flags & O_APPEND))
1290 return -EINVAL;
1291
1292 ret = rw_verify_area(WRITE, out, opos, len);
1293 if (unlikely(ret < 0))
1294 return ret;
1295
c66ab6fa 1296 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
51a92c0f 1297 if (ret > 0)
a82c53a0 1298 *ppos = sd.pos;
51a92c0f 1299
c66ab6fa 1300 return ret;
b92ce558 1301}
1c118596 1302EXPORT_SYMBOL(do_splice_direct);
b92ce558 1303
8924feff
AV
1304static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1305{
1306 while (pipe->nrbufs == pipe->buffers) {
1307 if (flags & SPLICE_F_NONBLOCK)
1308 return -EAGAIN;
1309 if (signal_pending(current))
1310 return -ERESTARTSYS;
1311 pipe->waiting_writers++;
1312 pipe_wait(pipe);
1313 pipe->waiting_writers--;
1314 }
1315 return 0;
1316}
1317
7c77f0b3
MS
1318static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1319 struct pipe_inode_info *opipe,
1320 size_t len, unsigned int flags);
ddac0d39 1321
83f9135b
JA
1322/*
1323 * Determine where to splice to/from.
1324 */
529565dc
IM
1325static long do_splice(struct file *in, loff_t __user *off_in,
1326 struct file *out, loff_t __user *off_out,
1327 size_t len, unsigned int flags)
5274f052 1328{
7c77f0b3
MS
1329 struct pipe_inode_info *ipipe;
1330 struct pipe_inode_info *opipe;
7995bd28 1331 loff_t offset;
a4514ebd 1332 long ret;
5274f052 1333
71993e62
LT
1334 ipipe = get_pipe_info(in);
1335 opipe = get_pipe_info(out);
7c77f0b3
MS
1336
1337 if (ipipe && opipe) {
1338 if (off_in || off_out)
1339 return -ESPIPE;
1340
1341 if (!(in->f_mode & FMODE_READ))
1342 return -EBADF;
1343
1344 if (!(out->f_mode & FMODE_WRITE))
1345 return -EBADF;
1346
1347 /* Splicing to self would be fun, but... */
1348 if (ipipe == opipe)
1349 return -EINVAL;
1350
1351 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1352 }
1353
1354 if (ipipe) {
529565dc
IM
1355 if (off_in)
1356 return -ESPIPE;
b92ce558 1357 if (off_out) {
19c9a49b 1358 if (!(out->f_mode & FMODE_PWRITE))
b92ce558 1359 return -EINVAL;
cbb7e577 1360 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
b92ce558 1361 return -EFAULT;
7995bd28
AV
1362 } else {
1363 offset = out->f_pos;
1364 }
529565dc 1365
18c67cb9
AV
1366 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1367 return -EBADF;
1368
1369 if (unlikely(out->f_flags & O_APPEND))
1370 return -EINVAL;
1371
1372 ret = rw_verify_area(WRITE, out, &offset, len);
1373 if (unlikely(ret < 0))
1374 return ret;
1375
500368f7 1376 file_start_write(out);
7995bd28 1377 ret = do_splice_from(ipipe, out, &offset, len, flags);
500368f7 1378 file_end_write(out);
a4514ebd 1379
7995bd28
AV
1380 if (!off_out)
1381 out->f_pos = offset;
1382 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
a4514ebd
JA
1383 ret = -EFAULT;
1384
1385 return ret;
529565dc 1386 }
5274f052 1387
7c77f0b3 1388 if (opipe) {
529565dc
IM
1389 if (off_out)
1390 return -ESPIPE;
b92ce558 1391 if (off_in) {
19c9a49b 1392 if (!(in->f_mode & FMODE_PREAD))
b92ce558 1393 return -EINVAL;
cbb7e577 1394 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
b92ce558 1395 return -EFAULT;
7995bd28
AV
1396 } else {
1397 offset = in->f_pos;
1398 }
529565dc 1399
8924feff
AV
1400 pipe_lock(opipe);
1401 ret = wait_for_space(opipe, flags);
1402 if (!ret)
1403 ret = do_splice_to(in, &offset, opipe, len, flags);
1404 pipe_unlock(opipe);
1405 if (ret > 0)
1406 wakeup_pipe_readers(opipe);
7995bd28
AV
1407 if (!off_in)
1408 in->f_pos = offset;
1409 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
a4514ebd
JA
1410 ret = -EFAULT;
1411
1412 return ret;
529565dc 1413 }
5274f052
JA
1414
1415 return -EINVAL;
1416}
1417
db85a9eb
AV
1418static int get_iovec_page_array(struct iov_iter *from,
1419 struct page **pages,
1420 struct partial_page *partial,
35f3d14d 1421 unsigned int pipe_buffers)
912d35f8 1422{
db85a9eb
AV
1423 int buffers = 0;
1424 while (iov_iter_count(from)) {
1425 ssize_t copied;
1426 size_t start;
1427
1428 copied = iov_iter_get_pages(from, pages + buffers, ~0UL,
1429 pipe_buffers - buffers, &start);
1430 if (copied <= 0)
1431 return buffers ? buffers : copied;
1432
1433 iov_iter_advance(from, copied);
1434 while (copied) {
1435 int size = min_t(int, copied, PAGE_SIZE - start);
1436 partial[buffers].offset = start;
1437 partial[buffers].len = size;
1438 copied -= size;
1439 start = 0;
912d35f8
JA
1440 buffers++;
1441 }
912d35f8 1442 }
db85a9eb 1443 return buffers;
912d35f8
JA
1444}
1445
6a14b90b
JA
1446static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1447 struct splice_desc *sd)
1448{
6130f531
AV
1449 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1450 return n == sd->len ? n : -EFAULT;
6a14b90b
JA
1451}
1452
1453/*
1454 * For lack of a better implementation, implement vmsplice() to userspace
1455 * as a simple copy of the pipes pages to the user iov.
1456 */
6130f531 1457static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
6a14b90b
JA
1458 unsigned long nr_segs, unsigned int flags)
1459{
1460 struct pipe_inode_info *pipe;
1461 struct splice_desc sd;
6a14b90b 1462 long ret;
6130f531
AV
1463 struct iovec iovstack[UIO_FASTIOV];
1464 struct iovec *iov = iovstack;
1465 struct iov_iter iter;
6a14b90b 1466
71993e62 1467 pipe = get_pipe_info(file);
6a14b90b
JA
1468 if (!pipe)
1469 return -EBADF;
1470
345995fa
AV
1471 ret = import_iovec(READ, uiov, nr_segs,
1472 ARRAY_SIZE(iovstack), &iov, &iter);
1473 if (ret < 0)
1474 return ret;
6a14b90b 1475
345995fa 1476 sd.total_len = iov_iter_count(&iter);
6130f531 1477 sd.len = 0;
6130f531
AV
1478 sd.flags = flags;
1479 sd.u.data = &iter;
1480 sd.pos = 0;
6a14b90b 1481
345995fa
AV
1482 if (sd.total_len) {
1483 pipe_lock(pipe);
1484 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1485 pipe_unlock(pipe);
1486 }
6a14b90b 1487
345995fa 1488 kfree(iov);
6a14b90b
JA
1489 return ret;
1490}
1491
912d35f8
JA
1492/*
1493 * vmsplice splices a user address range into a pipe. It can be thought of
1494 * as splice-from-memory, where the regular splice is splice-from-file (or
1495 * to file). In both cases the output is a pipe, naturally.
912d35f8 1496 */
db85a9eb 1497static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
6a14b90b 1498 unsigned long nr_segs, unsigned int flags)
912d35f8 1499{
ddac0d39 1500 struct pipe_inode_info *pipe;
db85a9eb
AV
1501 struct iovec iovstack[UIO_FASTIOV];
1502 struct iovec *iov = iovstack;
1503 struct iov_iter from;
35f3d14d
JA
1504 struct page *pages[PIPE_DEF_BUFFERS];
1505 struct partial_page partial[PIPE_DEF_BUFFERS];
912d35f8
JA
1506 struct splice_pipe_desc spd = {
1507 .pages = pages,
1508 .partial = partial,
047fe360 1509 .nr_pages_max = PIPE_DEF_BUFFERS,
912d35f8
JA
1510 .flags = flags,
1511 .ops = &user_page_pipe_buf_ops,
bbdfc2f7 1512 .spd_release = spd_release_page,
912d35f8 1513 };
35f3d14d 1514 long ret;
912d35f8 1515
71993e62 1516 pipe = get_pipe_info(file);
ddac0d39 1517 if (!pipe)
912d35f8 1518 return -EBADF;
912d35f8 1519
db85a9eb
AV
1520 ret = import_iovec(WRITE, uiov, nr_segs,
1521 ARRAY_SIZE(iovstack), &iov, &from);
1522 if (ret < 0)
1523 return ret;
1524
1525 if (splice_grow_spd(pipe, &spd)) {
1526 kfree(iov);
35f3d14d 1527 return -ENOMEM;
db85a9eb 1528 }
35f3d14d 1529
8924feff
AV
1530 pipe_lock(pipe);
1531 ret = wait_for_space(pipe, flags);
1532 if (!ret) {
1533 spd.nr_pages = get_iovec_page_array(&from, spd.pages,
1534 spd.partial,
1535 spd.nr_pages_max);
1536 if (spd.nr_pages <= 0)
1537 ret = spd.nr_pages;
1538 else
1539 ret = splice_to_pipe(pipe, &spd);
1540 }
1541 pipe_unlock(pipe);
1542 if (ret > 0)
1543 wakeup_pipe_readers(pipe);
047fe360 1544 splice_shrink_spd(&spd);
db85a9eb 1545 kfree(iov);
35f3d14d 1546 return ret;
912d35f8
JA
1547}
1548
6a14b90b
JA
1549/*
1550 * Note that vmsplice only really supports true splicing _from_ user memory
1551 * to a pipe, not the other way around. Splicing from user memory is a simple
1552 * operation that can be supported without any funky alignment restrictions
1553 * or nasty vm tricks. We simply map in the user memory and fill them into
1554 * a pipe. The reverse isn't quite as easy, though. There are two possible
1555 * solutions for that:
1556 *
1557 * - memcpy() the data internally, at which point we might as well just
1558 * do a regular read() on the buffer anyway.
1559 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1560 * has restriction limitations on both ends of the pipe).
1561 *
1562 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1563 *
1564 */
836f92ad
HC
1565SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1566 unsigned long, nr_segs, unsigned int, flags)
912d35f8 1567{
2903ff01 1568 struct fd f;
912d35f8 1569 long error;
912d35f8 1570
6a14b90b
JA
1571 if (unlikely(nr_segs > UIO_MAXIOV))
1572 return -EINVAL;
1573 else if (unlikely(!nr_segs))
1574 return 0;
1575
912d35f8 1576 error = -EBADF;
2903ff01
AV
1577 f = fdget(fd);
1578 if (f.file) {
1579 if (f.file->f_mode & FMODE_WRITE)
1580 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1581 else if (f.file->f_mode & FMODE_READ)
1582 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
1583
1584 fdput(f);
912d35f8
JA
1585 }
1586
1587 return error;
1588}
1589
76b021d0
AV
1590#ifdef CONFIG_COMPAT
1591COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1592 unsigned int, nr_segs, unsigned int, flags)
1593{
1594 unsigned i;
1595 struct iovec __user *iov;
1596 if (nr_segs > UIO_MAXIOV)
1597 return -EINVAL;
1598 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1599 for (i = 0; i < nr_segs; i++) {
1600 struct compat_iovec v;
1601 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1602 get_user(v.iov_len, &iov32[i].iov_len) ||
1603 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1604 put_user(v.iov_len, &iov[i].iov_len))
1605 return -EFAULT;
1606 }
1607 return sys_vmsplice(fd, iov, nr_segs, flags);
1608}
1609#endif
1610
836f92ad
HC
1611SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1612 int, fd_out, loff_t __user *, off_out,
1613 size_t, len, unsigned int, flags)
5274f052 1614{
2903ff01 1615 struct fd in, out;
5274f052 1616 long error;
5274f052
JA
1617
1618 if (unlikely(!len))
1619 return 0;
1620
1621 error = -EBADF;
2903ff01
AV
1622 in = fdget(fd_in);
1623 if (in.file) {
1624 if (in.file->f_mode & FMODE_READ) {
1625 out = fdget(fd_out);
1626 if (out.file) {
1627 if (out.file->f_mode & FMODE_WRITE)
1628 error = do_splice(in.file, off_in,
1629 out.file, off_out,
529565dc 1630 len, flags);
2903ff01 1631 fdput(out);
5274f052
JA
1632 }
1633 }
2903ff01 1634 fdput(in);
5274f052 1635 }
5274f052
JA
1636 return error;
1637}
70524490 1638
aadd06e5
JA
1639/*
1640 * Make sure there's data to read. Wait for input if we can, otherwise
1641 * return an appropriate error.
1642 */
7c77f0b3 1643static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5
JA
1644{
1645 int ret;
1646
1647 /*
1648 * Check ->nrbufs without the inode lock first. This function
1649 * is speculative anyways, so missing one is ok.
1650 */
1651 if (pipe->nrbufs)
1652 return 0;
1653
1654 ret = 0;
61e0d47c 1655 pipe_lock(pipe);
aadd06e5
JA
1656
1657 while (!pipe->nrbufs) {
1658 if (signal_pending(current)) {
1659 ret = -ERESTARTSYS;
1660 break;
1661 }
1662 if (!pipe->writers)
1663 break;
1664 if (!pipe->waiting_writers) {
1665 if (flags & SPLICE_F_NONBLOCK) {
1666 ret = -EAGAIN;
1667 break;
1668 }
1669 }
1670 pipe_wait(pipe);
1671 }
1672
61e0d47c 1673 pipe_unlock(pipe);
aadd06e5
JA
1674 return ret;
1675}
1676
1677/*
1678 * Make sure there's writeable room. Wait for room if we can, otherwise
1679 * return an appropriate error.
1680 */
7c77f0b3 1681static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5
JA
1682{
1683 int ret;
1684
1685 /*
1686 * Check ->nrbufs without the inode lock first. This function
1687 * is speculative anyways, so missing one is ok.
1688 */
35f3d14d 1689 if (pipe->nrbufs < pipe->buffers)
aadd06e5
JA
1690 return 0;
1691
1692 ret = 0;
61e0d47c 1693 pipe_lock(pipe);
aadd06e5 1694
35f3d14d 1695 while (pipe->nrbufs >= pipe->buffers) {
aadd06e5
JA
1696 if (!pipe->readers) {
1697 send_sig(SIGPIPE, current, 0);
1698 ret = -EPIPE;
1699 break;
1700 }
1701 if (flags & SPLICE_F_NONBLOCK) {
1702 ret = -EAGAIN;
1703 break;
1704 }
1705 if (signal_pending(current)) {
1706 ret = -ERESTARTSYS;
1707 break;
1708 }
1709 pipe->waiting_writers++;
1710 pipe_wait(pipe);
1711 pipe->waiting_writers--;
1712 }
1713
61e0d47c 1714 pipe_unlock(pipe);
aadd06e5
JA
1715 return ret;
1716}
1717
7c77f0b3
MS
1718/*
1719 * Splice contents of ipipe to opipe.
1720 */
1721static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1722 struct pipe_inode_info *opipe,
1723 size_t len, unsigned int flags)
1724{
1725 struct pipe_buffer *ibuf, *obuf;
1726 int ret = 0, nbuf;
1727 bool input_wakeup = false;
1728
1729
1730retry:
1731 ret = ipipe_prep(ipipe, flags);
1732 if (ret)
1733 return ret;
1734
1735 ret = opipe_prep(opipe, flags);
1736 if (ret)
1737 return ret;
1738
1739 /*
1740 * Potential ABBA deadlock, work around it by ordering lock
1741 * grabbing by pipe info address. Otherwise two different processes
1742 * could deadlock (one doing tee from A -> B, the other from B -> A).
1743 */
1744 pipe_double_lock(ipipe, opipe);
1745
1746 do {
1747 if (!opipe->readers) {
1748 send_sig(SIGPIPE, current, 0);
1749 if (!ret)
1750 ret = -EPIPE;
1751 break;
1752 }
1753
1754 if (!ipipe->nrbufs && !ipipe->writers)
1755 break;
1756
1757 /*
1758 * Cannot make any progress, because either the input
1759 * pipe is empty or the output pipe is full.
1760 */
35f3d14d 1761 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
7c77f0b3
MS
1762 /* Already processed some buffers, break */
1763 if (ret)
1764 break;
1765
1766 if (flags & SPLICE_F_NONBLOCK) {
1767 ret = -EAGAIN;
1768 break;
1769 }
1770
1771 /*
1772 * We raced with another reader/writer and haven't
1773 * managed to process any buffers. A zero return
1774 * value means EOF, so retry instead.
1775 */
1776 pipe_unlock(ipipe);
1777 pipe_unlock(opipe);
1778 goto retry;
1779 }
1780
1781 ibuf = ipipe->bufs + ipipe->curbuf;
35f3d14d 1782 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
7c77f0b3
MS
1783 obuf = opipe->bufs + nbuf;
1784
1785 if (len >= ibuf->len) {
1786 /*
1787 * Simply move the whole buffer from ipipe to opipe
1788 */
1789 *obuf = *ibuf;
1790 ibuf->ops = NULL;
1791 opipe->nrbufs++;
35f3d14d 1792 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
7c77f0b3
MS
1793 ipipe->nrbufs--;
1794 input_wakeup = true;
1795 } else {
1796 /*
1797 * Get a reference to this pipe buffer,
1798 * so we can copy the contents over.
1799 */
1800 ibuf->ops->get(ipipe, ibuf);
1801 *obuf = *ibuf;
1802
1803 /*
1804 * Don't inherit the gift flag, we need to
1805 * prevent multiple steals of this page.
1806 */
1807 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1808
1809 obuf->len = len;
1810 opipe->nrbufs++;
1811 ibuf->offset += obuf->len;
1812 ibuf->len -= obuf->len;
1813 }
1814 ret += obuf->len;
1815 len -= obuf->len;
1816 } while (len);
1817
1818 pipe_unlock(ipipe);
1819 pipe_unlock(opipe);
1820
1821 /*
1822 * If we put data in the output pipe, wakeup any potential readers.
1823 */
825cdcb1
NK
1824 if (ret > 0)
1825 wakeup_pipe_readers(opipe);
1826
7c77f0b3
MS
1827 if (input_wakeup)
1828 wakeup_pipe_writers(ipipe);
1829
1830 return ret;
1831}
1832
70524490
JA
1833/*
1834 * Link contents of ipipe to opipe.
1835 */
1836static int link_pipe(struct pipe_inode_info *ipipe,
1837 struct pipe_inode_info *opipe,
1838 size_t len, unsigned int flags)
1839{
1840 struct pipe_buffer *ibuf, *obuf;
aadd06e5 1841 int ret = 0, i = 0, nbuf;
70524490
JA
1842
1843 /*
1844 * Potential ABBA deadlock, work around it by ordering lock
61e0d47c 1845 * grabbing by pipe info address. Otherwise two different processes
70524490
JA
1846 * could deadlock (one doing tee from A -> B, the other from B -> A).
1847 */
61e0d47c 1848 pipe_double_lock(ipipe, opipe);
70524490 1849
aadd06e5 1850 do {
70524490
JA
1851 if (!opipe->readers) {
1852 send_sig(SIGPIPE, current, 0);
1853 if (!ret)
1854 ret = -EPIPE;
1855 break;
1856 }
70524490 1857
aadd06e5
JA
1858 /*
1859 * If we have iterated all input buffers or ran out of
1860 * output room, break.
1861 */
35f3d14d 1862 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
aadd06e5 1863 break;
70524490 1864
35f3d14d
JA
1865 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1866 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
70524490
JA
1867
1868 /*
aadd06e5
JA
1869 * Get a reference to this pipe buffer,
1870 * so we can copy the contents over.
70524490 1871 */
aadd06e5
JA
1872 ibuf->ops->get(ipipe, ibuf);
1873
1874 obuf = opipe->bufs + nbuf;
1875 *obuf = *ibuf;
1876
2a27250e 1877 /*
aadd06e5
JA
1878 * Don't inherit the gift flag, we need to
1879 * prevent multiple steals of this page.
2a27250e 1880 */
aadd06e5 1881 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
70524490 1882
aadd06e5
JA
1883 if (obuf->len > len)
1884 obuf->len = len;
70524490 1885
aadd06e5
JA
1886 opipe->nrbufs++;
1887 ret += obuf->len;
1888 len -= obuf->len;
1889 i++;
1890 } while (len);
70524490 1891
02cf01ae
JA
1892 /*
1893 * return EAGAIN if we have the potential of some data in the
1894 * future, otherwise just return 0
1895 */
1896 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1897 ret = -EAGAIN;
1898
61e0d47c
MS
1899 pipe_unlock(ipipe);
1900 pipe_unlock(opipe);
70524490 1901
aadd06e5
JA
1902 /*
1903 * If we put data in the output pipe, wakeup any potential readers.
1904 */
825cdcb1
NK
1905 if (ret > 0)
1906 wakeup_pipe_readers(opipe);
70524490
JA
1907
1908 return ret;
1909}
1910
1911/*
1912 * This is a tee(1) implementation that works on pipes. It doesn't copy
1913 * any data, it simply references the 'in' pages on the 'out' pipe.
1914 * The 'flags' used are the SPLICE_F_* variants, currently the only
1915 * applicable one is SPLICE_F_NONBLOCK.
1916 */
1917static long do_tee(struct file *in, struct file *out, size_t len,
1918 unsigned int flags)
1919{
71993e62
LT
1920 struct pipe_inode_info *ipipe = get_pipe_info(in);
1921 struct pipe_inode_info *opipe = get_pipe_info(out);
aadd06e5 1922 int ret = -EINVAL;
70524490
JA
1923
1924 /*
aadd06e5
JA
1925 * Duplicate the contents of ipipe to opipe without actually
1926 * copying the data.
70524490 1927 */
aadd06e5
JA
1928 if (ipipe && opipe && ipipe != opipe) {
1929 /*
1930 * Keep going, unless we encounter an error. The ipipe/opipe
1931 * ordering doesn't really matter.
1932 */
7c77f0b3 1933 ret = ipipe_prep(ipipe, flags);
aadd06e5 1934 if (!ret) {
7c77f0b3 1935 ret = opipe_prep(opipe, flags);
02cf01ae 1936 if (!ret)
aadd06e5 1937 ret = link_pipe(ipipe, opipe, len, flags);
aadd06e5
JA
1938 }
1939 }
70524490 1940
aadd06e5 1941 return ret;
70524490
JA
1942}
1943
836f92ad 1944SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
70524490 1945{
2903ff01
AV
1946 struct fd in;
1947 int error;
70524490
JA
1948
1949 if (unlikely(!len))
1950 return 0;
1951
1952 error = -EBADF;
2903ff01
AV
1953 in = fdget(fdin);
1954 if (in.file) {
1955 if (in.file->f_mode & FMODE_READ) {
1956 struct fd out = fdget(fdout);
1957 if (out.file) {
1958 if (out.file->f_mode & FMODE_WRITE)
1959 error = do_tee(in.file, out.file,
1960 len, flags);
1961 fdput(out);
70524490
JA
1962 }
1963 }
2903ff01 1964 fdput(in);
70524490
JA
1965 }
1966
1967 return error;
1968}