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