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