blob: 2ca7e6931ad560893e463df72b42a738b8c129f5 [file] [log] [blame]
Jens Axboe5274f052006-03-30 15:15:30 +02001/*
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
Jens Axboec2058e02006-04-11 13:56:34 +020012 * Jens to support splicing to files, network, direct splicing, etc and
13 * fixing lots of bugs.
Jens Axboe5274f052006-03-30 15:15:30 +020014 *
Jens Axboe0fe23472006-09-04 15:41:16 +020015 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
Jens Axboec2058e02006-04-11 13:56:34 +020016 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
17 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
Jens Axboe5274f052006-03-30 15:15:30 +020018 *
19 */
20#include <linux/fs.h>
21#include <linux/file.h>
22#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020023#include <linux/splice.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080024#include <linux/memcontrol.h>
Jens Axboe5274f052006-03-30 15:15:30 +020025#include <linux/mm_inline.h>
Jens Axboe5abc97a2006-03-30 15:16:46 +020026#include <linux/swap.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020027#include <linux/writeback.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050028#include <linux/export.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020029#include <linux/syscalls.h>
Jens Axboe912d35f2006-04-26 10:59:21 +020030#include <linux/uio.h>
James Morris29ce2052007-07-13 11:44:32 +020031#include <linux/security.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Eric Dumazet35f9c092012-04-05 03:05:35 +000033#include <linux/socket.h>
Al Viro76b021d2013-03-02 10:19:56 -050034#include <linux/compat.h>
Al Viro06ae43f2013-03-20 13:19:30 -040035#include "internal.h"
Jens Axboe5274f052006-03-30 15:15:30 +020036
Jens Axboe83f91352006-04-02 23:05:09 +020037/*
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 */
Jens Axboe76ad4d12006-05-03 10:41:33 +020043static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
Jens Axboe5abc97a2006-03-30 15:16:46 +020044 struct pipe_buffer *buf)
45{
46 struct page *page = buf->page;
Jens Axboe9e94cd42006-06-20 15:01:12 +020047 struct address_space *mapping;
Jens Axboe5abc97a2006-03-30 15:16:46 +020048
Jens Axboe9e0267c2006-04-19 15:57:31 +020049 lock_page(page);
50
Jens Axboe9e94cd42006-06-20 15:01:12 +020051 mapping = page_mapping(page);
52 if (mapping) {
53 WARN_ON(!PageUptodate(page));
Jens Axboe5abc97a2006-03-30 15:16:46 +020054
Jens Axboe9e94cd42006-06-20 15:01:12 +020055 /*
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);
Jens Axboead8d6f02006-04-02 23:10:32 +020064
David Howells266cf652009-04-03 16:42:36 +010065 if (page_has_private(page) &&
66 !try_to_release_page(page, GFP_KERNEL))
Jens Axboeca39d652008-05-20 21:27:41 +020067 goto out_unlock;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020068
Jens Axboe9e94cd42006-06-20 15:01:12 +020069 /*
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 }
Jens Axboe9e0267c2006-04-19 15:57:31 +020077 }
Jens Axboe5abc97a2006-03-30 15:16:46 +020078
Jens Axboe9e94cd42006-06-20 15:01:12 +020079 /*
80 * Raced with truncate or failed to remove page from current
81 * address space, unlock and return failure.
82 */
Jens Axboeca39d652008-05-20 21:27:41 +020083out_unlock:
Jens Axboe9e94cd42006-06-20 15:01:12 +020084 unlock_page(page);
85 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +020086}
87
Jens Axboe76ad4d12006-05-03 10:41:33 +020088static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +020089 struct pipe_buffer *buf)
90{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030091 put_page(buf->page);
Jens Axboe14328732006-05-03 10:35:26 +020092 buf->flags &= ~PIPE_BUF_FLAG_LRU;
Jens Axboe5274f052006-03-30 15:15:30 +020093}
94
Jens Axboe08457182007-06-12 20:51:32 +020095/*
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 */
Jens Axboecac36bb02007-06-14 13:10:48 +020099static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
100 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +0200101{
102 struct page *page = buf->page;
Jens Axboe49d0b212006-04-10 09:04:41 +0200103 int err;
Jens Axboe5274f052006-03-30 15:15:30 +0200104
105 if (!PageUptodate(page)) {
Jens Axboe49d0b212006-04-10 09:04:41 +0200106 lock_page(page);
107
108 /*
109 * Page got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +0200110 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200111 */
112 if (!page->mapping) {
113 err = -ENODATA;
114 goto error;
115 }
116
117 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200118 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200119 */
120 if (!PageUptodate(page)) {
121 err = -EIO;
122 goto error;
123 }
124
125 /*
Jens Axboef84d7512006-05-01 19:59:03 +0200126 * Page is ok afterall, we are done.
Jens Axboe49d0b212006-04-10 09:04:41 +0200127 */
Jens Axboe5274f052006-03-30 15:15:30 +0200128 unlock_page(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200129 }
130
Jens Axboef84d7512006-05-01 19:59:03 +0200131 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200132error:
133 unlock_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200134 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200135}
136
Hugh Dickins708e3502011-07-25 17:12:32 -0700137const struct pipe_buf_operations page_cache_pipe_buf_ops = {
Jens Axboe5274f052006-03-30 15:15:30 +0200138 .can_merge = 0,
Jens Axboecac36bb02007-06-14 13:10:48 +0200139 .confirm = page_cache_pipe_buf_confirm,
Jens Axboe5274f052006-03-30 15:15:30 +0200140 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200141 .steal = page_cache_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200142 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200143};
144
Jens Axboe912d35f2006-04-26 10:59:21 +0200145static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
146 struct pipe_buffer *buf)
147{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200148 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
149 return 1;
150
Jens Axboe14328732006-05-03 10:35:26 +0200151 buf->flags |= PIPE_BUF_FLAG_LRU;
Jens Axboe330ab712006-05-02 15:29:57 +0200152 return generic_pipe_buf_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200153}
154
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800155static const struct pipe_buf_operations user_page_pipe_buf_ops = {
Jens Axboe912d35f2006-04-26 10:59:21 +0200156 .can_merge = 0,
Jens Axboecac36bb02007-06-14 13:10:48 +0200157 .confirm = generic_pipe_buf_confirm,
Jens Axboe912d35f2006-04-26 10:59:21 +0200158 .release = page_cache_pipe_buf_release,
159 .steal = user_page_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200160 .get = generic_pipe_buf_get,
Jens Axboe912d35f2006-04-26 10:59:21 +0200161};
162
Namhyung Kim825cdcb2011-05-23 19:58:53 +0200163static 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
Jens Axboe932cc6d2007-06-21 13:10:21 +0200171/**
172 * splice_to_pipe - fill passed data into a pipe
173 * @pipe: pipe to fill
174 * @spd: data to fill
175 *
176 * Description:
Randy Dunlap79685b82007-07-27 08:08:51 +0200177 * @spd contains a map of pages and len/offset tuples, along with
Jens Axboe932cc6d2007-06-21 13:10:21 +0200178 * the struct pipe_buf_operations associated with these pages. This
179 * function will link that data to the pipe.
180 *
Jens Axboe83f91352006-04-02 23:05:09 +0200181 */
Jens Axboed6b29d72007-06-04 09:59:47 +0200182ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
183 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200184{
Jens Axboe00de00b2007-06-15 13:14:22 +0200185 unsigned int spd_pages = spd->nr_pages;
Al Viro8924fef2016-09-17 20:44:45 -0400186 int ret = 0, page_nr = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200187
Rabin Vincentd6785d92016-03-10 21:19:06 +0100188 if (!spd_pages)
189 return 0;
190
Al Viro8924fef2016-09-17 20:44:45 -0400191 if (unlikely(!pipe->readers)) {
192 send_sig(SIGPIPE, current, 0);
193 ret = -EPIPE;
194 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200195 }
196
Al Viro8924fef2016-09-17 20:44:45 -0400197 while (pipe->nrbufs < pipe->buffers) {
198 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
199 struct pipe_buffer *buf = pipe->bufs + newbuf;
Jens Axboe5274f052006-03-30 15:15:30 +0200200
Al Viro8924fef2016-09-17 20:44:45 -0400201 buf->page = spd->pages[page_nr];
202 buf->offset = spd->partial[page_nr].offset;
203 buf->len = spd->partial[page_nr].len;
204 buf->private = spd->partial[page_nr].private;
205 buf->ops = spd->ops;
206 if (spd->flags & SPLICE_F_GIFT)
207 buf->flags |= PIPE_BUF_FLAG_GIFT;
Jens Axboe5274f052006-03-30 15:15:30 +0200208
Al Viro8924fef2016-09-17 20:44:45 -0400209 pipe->nrbufs++;
210 page_nr++;
211 ret += buf->len;
212
213 if (!--spd->nr_pages)
214 break;
215 }
216
217 if (!ret)
218 ret = -EAGAIN;
219
220out:
Jens Axboe00de00b2007-06-15 13:14:22 +0200221 while (page_nr < spd_pages)
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800222 spd->spd_release(spd, page_nr++);
Jens Axboe5274f052006-03-30 15:15:30 +0200223
224 return ret;
225}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200226EXPORT_SYMBOL_GPL(splice_to_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200227
Hugh Dickins708e3502011-07-25 17:12:32 -0700228void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800229{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300230 put_page(spd->pages[i]);
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800231}
232
Jens Axboe35f3d142010-05-20 10:43:18 +0200233/*
234 * Check if we need to grow the arrays holding pages and partial page
235 * descriptions.
236 */
Eric Dumazet047fe362012-06-12 15:24:40 +0200237int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200238{
Eric Dumazet047fe362012-06-12 15:24:40 +0200239 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
240
241 spd->nr_pages_max = buffers;
242 if (buffers <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200243 return 0;
244
Eric Dumazet047fe362012-06-12 15:24:40 +0200245 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
246 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
Jens Axboe35f3d142010-05-20 10:43:18 +0200247
248 if (spd->pages && spd->partial)
249 return 0;
250
251 kfree(spd->pages);
252 kfree(spd->partial);
253 return -ENOMEM;
254}
255
Eric Dumazet047fe362012-06-12 15:24:40 +0200256void splice_shrink_spd(struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200257{
Eric Dumazet047fe362012-06-12 15:24:40 +0200258 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200259 return;
260
261 kfree(spd->pages);
262 kfree(spd->partial);
263}
264
Ingo Molnar3a326a22006-04-10 15:18:35 +0200265static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200266__generic_file_splice_read(struct file *in, loff_t *ppos,
267 struct pipe_inode_info *pipe, size_t len,
268 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200269{
270 struct address_space *mapping = in->f_mapping;
Fengguang Wud8983912007-07-19 01:48:06 -0700271 unsigned int loff, nr_pages, req_pages;
Jens Axboe35f3d142010-05-20 10:43:18 +0200272 struct page *pages[PIPE_DEF_BUFFERS];
273 struct partial_page partial[PIPE_DEF_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200274 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200275 pgoff_t index, end_index;
276 loff_t isize;
Jens Axboeeb207962006-04-27 11:05:22 +0200277 int error, page_nr;
Jens Axboe912d35f2006-04-26 10:59:21 +0200278 struct splice_pipe_desc spd = {
279 .pages = pages,
280 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +0200281 .nr_pages_max = PIPE_DEF_BUFFERS,
Jens Axboe912d35f2006-04-26 10:59:21 +0200282 .flags = flags,
283 .ops = &page_cache_pipe_buf_ops,
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800284 .spd_release = spd_release_page,
Jens Axboe912d35f2006-04-26 10:59:21 +0200285 };
Jens Axboe5274f052006-03-30 15:15:30 +0200286
Jens Axboe35f3d142010-05-20 10:43:18 +0200287 if (splice_grow_spd(pipe, &spd))
288 return -ENOMEM;
289
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300290 index = *ppos >> PAGE_SHIFT;
291 loff = *ppos & ~PAGE_MASK;
292 req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
Eric Dumazet047fe362012-06-12 15:24:40 +0200293 nr_pages = min(req_pages, spd.nr_pages_max);
Jens Axboe5274f052006-03-30 15:15:30 +0200294
295 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200296 * Lookup the (hopefully) full range of pages we need.
297 */
Jens Axboe35f3d142010-05-20 10:43:18 +0200298 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
Fengguang Wu431a4822007-07-19 01:48:05 -0700299 index += spd.nr_pages;
Jens Axboeeb207962006-04-27 11:05:22 +0200300
301 /*
302 * If find_get_pages_contig() returned fewer pages than we needed,
Fengguang Wu431a4822007-07-19 01:48:05 -0700303 * readahead/allocate the rest and fill in the holes.
Jens Axboeeb207962006-04-27 11:05:22 +0200304 */
Fengguang Wu431a4822007-07-19 01:48:05 -0700305 if (spd.nr_pages < nr_pages)
Rusty Russellcf914a72007-07-19 01:48:08 -0700306 page_cache_sync_readahead(mapping, &in->f_ra, in,
307 index, req_pages - spd.nr_pages);
Fengguang Wu431a4822007-07-19 01:48:05 -0700308
Jens Axboe932cc6d2007-06-21 13:10:21 +0200309 error = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200310 while (spd.nr_pages < nr_pages) {
311 /*
312 * Page could be there, find_get_pages_contig() breaks on
313 * the first hole.
314 */
315 page = find_get_page(mapping, index);
316 if (!page) {
Jens Axboee27dedd2006-05-01 19:59:54 +0200317 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200318 * page didn't exist, allocate one.
319 */
320 page = page_cache_alloc_cold(mapping);
321 if (!page)
322 break;
323
324 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -0800325 mapping_gfp_constraint(mapping, GFP_KERNEL));
Jens Axboeeb207962006-04-27 11:05:22 +0200326 if (unlikely(error)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 put_page(page);
Jens Axboea0548872006-05-03 10:58:22 +0200328 if (error == -EEXIST)
329 continue;
Jens Axboeeb207962006-04-27 11:05:22 +0200330 break;
331 }
332 /*
333 * add_to_page_cache() locks the page, unlock it
334 * to avoid convoluting the logic below even more.
335 */
336 unlock_page(page);
337 }
338
Jens Axboe35f3d142010-05-20 10:43:18 +0200339 spd.pages[spd.nr_pages++] = page;
Jens Axboeeb207962006-04-27 11:05:22 +0200340 index++;
341 }
342
343 /*
344 * Now loop over the map and see if we need to start IO on any
345 * pages, fill in the partial map, etc.
346 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300347 index = *ppos >> PAGE_SHIFT;
Jens Axboeeb207962006-04-27 11:05:22 +0200348 nr_pages = spd.nr_pages;
349 spd.nr_pages = 0;
350 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200351 unsigned int this_len;
352
353 if (!len)
354 break;
355
356 /*
357 * this_len is the max we'll use from this page
358 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300359 this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
Jens Axboe35f3d142010-05-20 10:43:18 +0200360 page = spd.pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200361
Fengguang Wua08a1662007-07-19 01:48:03 -0700362 if (PageReadahead(page))
Rusty Russellcf914a72007-07-19 01:48:08 -0700363 page_cache_async_readahead(mapping, &in->f_ra, in,
Fengguang Wud8983912007-07-19 01:48:06 -0700364 page, index, req_pages - page_nr);
Fengguang Wua08a1662007-07-19 01:48:03 -0700365
Jens Axboe7480a902006-04-11 13:52:47 +0200366 /*
367 * If the page isn't uptodate, we may need to start io on it
368 */
369 if (!PageUptodate(page)) {
Miklos Szeredi69650312010-08-03 12:48:50 +0200370 lock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200371
372 /*
Miklos Szeredi32502b82008-07-04 09:35:17 +0200373 * Page was truncated, or invalidated by the
374 * filesystem. Redo the find/create, but this time the
375 * page is kept locked, so there's no chance of another
376 * race with truncate/invalidate.
Jens Axboe7480a902006-04-11 13:52:47 +0200377 */
378 if (!page->mapping) {
379 unlock_page(page);
Abhi Das90330e62015-12-18 14:11:36 -0600380retry_lookup:
Miklos Szeredi32502b82008-07-04 09:35:17 +0200381 page = find_or_create_page(mapping, index,
382 mapping_gfp_mask(mapping));
383
384 if (!page) {
385 error = -ENOMEM;
386 break;
387 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300388 put_page(spd.pages[page_nr]);
Jens Axboe35f3d142010-05-20 10:43:18 +0200389 spd.pages[page_nr] = page;
Jens Axboe7480a902006-04-11 13:52:47 +0200390 }
391 /*
392 * page was already under io and is now done, great
393 */
394 if (PageUptodate(page)) {
395 unlock_page(page);
396 goto fill_it;
397 }
398
Jens Axboe7480a902006-04-11 13:52:47 +0200399 /*
400 * need to read in the page
401 */
402 error = mapping->a_ops->readpage(in, page);
Jens Axboe7480a902006-04-11 13:52:47 +0200403 if (unlikely(error)) {
Jens Axboeeb207962006-04-27 11:05:22 +0200404 /*
Abhi Das90330e62015-12-18 14:11:36 -0600405 * Re-lookup the page
Jens Axboeeb207962006-04-27 11:05:22 +0200406 */
Jens Axboe7480a902006-04-11 13:52:47 +0200407 if (error == AOP_TRUNCATED_PAGE)
Abhi Das90330e62015-12-18 14:11:36 -0600408 goto retry_lookup;
Jens Axboeeb207962006-04-27 11:05:22 +0200409
Jens Axboe7480a902006-04-11 13:52:47 +0200410 break;
411 }
Jens Axboe620a3242007-06-07 09:39:42 +0200412 }
413fill_it:
414 /*
415 * i_size must be checked after PageUptodate.
416 */
417 isize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300418 end_index = (isize - 1) >> PAGE_SHIFT;
Jens Axboe620a3242007-06-07 09:39:42 +0200419 if (unlikely(!isize || index > end_index))
420 break;
421
422 /*
423 * if this is the last page, see if we need to shrink
424 * the length and stop
425 */
426 if (end_index == index) {
427 unsigned int plen;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200428
429 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200430 * max good bytes in this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200431 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300432 plen = ((isize - 1) & ~PAGE_MASK) + 1;
Jens Axboe620a3242007-06-07 09:39:42 +0200433 if (plen <= loff)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200434 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200435
436 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200437 * force quit after adding this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200438 */
Jens Axboe620a3242007-06-07 09:39:42 +0200439 this_len = min(this_len, plen - loff);
440 len = this_len;
Jens Axboe7480a902006-04-11 13:52:47 +0200441 }
Jens Axboe620a3242007-06-07 09:39:42 +0200442
Jens Axboe35f3d142010-05-20 10:43:18 +0200443 spd.partial[page_nr].offset = loff;
444 spd.partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200445 len -= this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200446 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200447 spd.nr_pages++;
448 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200449 }
450
Jens Axboeeb207962006-04-27 11:05:22 +0200451 /*
Hugh Dickins475ecad2007-06-07 09:36:00 +0200452 * Release any pages at the end, if we quit early. 'page_nr' is how far
Jens Axboeeb207962006-04-27 11:05:22 +0200453 * we got, 'nr_pages' is how many pages are in the map.
454 */
455 while (page_nr < nr_pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300456 put_page(spd.pages[page_nr++]);
457 in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
Jens Axboeeb207962006-04-27 11:05:22 +0200458
Jens Axboe912d35f2006-04-26 10:59:21 +0200459 if (spd.nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +0200460 error = splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200461
Eric Dumazet047fe362012-06-12 15:24:40 +0200462 splice_shrink_spd(&spd);
Jens Axboe7480a902006-04-11 13:52:47 +0200463 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200464}
465
Jens Axboe83f91352006-04-02 23:05:09 +0200466/**
467 * generic_file_splice_read - splice data from file to a pipe
468 * @in: file to splice from
Jens Axboe932cc6d2007-06-21 13:10:21 +0200469 * @ppos: position in @in
Jens Axboe83f91352006-04-02 23:05:09 +0200470 * @pipe: pipe to splice to
471 * @len: number of bytes to splice
472 * @flags: splice modifier flags
473 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200474 * Description:
475 * Will read pages from given file and fill them into a pipe. Can be
476 * used as long as the address_space operations for the source implements
477 * a readpage() hook.
478 *
Jens Axboe83f91352006-04-02 23:05:09 +0200479 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200480ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
481 struct pipe_inode_info *pipe, size_t len,
482 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200483{
Jens Axboed366d3982007-06-01 14:54:11 +0200484 loff_t isize, left;
Jens Axboe8191ecd2008-04-10 08:24:25 +0200485 int ret;
Jens Axboed366d3982007-06-01 14:54:11 +0200486
Boaz Harroshbe64f882015-04-15 16:15:17 -0700487 if (IS_DAX(in->f_mapping->host))
488 return default_file_splice_read(in, ppos, pipe, len, flags);
489
Jens Axboed366d3982007-06-01 14:54:11 +0200490 isize = i_size_read(in->f_mapping->host);
491 if (unlikely(*ppos >= isize))
492 return 0;
493
494 left = isize - *ppos;
495 if (unlikely(left < len))
496 len = left;
Jens Axboe5274f052006-03-30 15:15:30 +0200497
Jens Axboe8191ecd2008-04-10 08:24:25 +0200498 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
Miklos Szeredi723590e2009-08-15 08:43:22 +0200499 if (ret > 0) {
Jens Axboecbb7e572006-04-11 14:57:50 +0200500 *ppos += ret;
Miklos Szeredi723590e2009-08-15 08:43:22 +0200501 file_accessed(in);
502 }
Jens Axboe5274f052006-03-30 15:15:30 +0200503
504 return ret;
505}
Jens Axboe059a8f32006-04-02 23:06:05 +0200506EXPORT_SYMBOL(generic_file_splice_read);
507
Miklos Szeredi68181732009-05-07 15:37:36 +0200508static const struct pipe_buf_operations default_pipe_buf_ops = {
509 .can_merge = 0,
Miklos Szeredi68181732009-05-07 15:37:36 +0200510 .confirm = generic_pipe_buf_confirm,
511 .release = generic_pipe_buf_release,
512 .steal = generic_pipe_buf_steal,
513 .get = generic_pipe_buf_get,
514};
515
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100516static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
517 struct pipe_buffer *buf)
518{
519 return 1;
520}
521
522/* Pipe buffer operations for a socket and similar. */
523const struct pipe_buf_operations nosteal_pipe_buf_ops = {
524 .can_merge = 0,
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100525 .confirm = generic_pipe_buf_confirm,
526 .release = generic_pipe_buf_release,
527 .steal = generic_pipe_buf_nosteal,
528 .get = generic_pipe_buf_get,
529};
530EXPORT_SYMBOL(nosteal_pipe_buf_ops);
531
Miklos Szeredi68181732009-05-07 15:37:36 +0200532static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
533 unsigned long vlen, loff_t offset)
534{
535 mm_segment_t old_fs;
536 loff_t pos = offset;
537 ssize_t res;
538
539 old_fs = get_fs();
540 set_fs(get_ds());
541 /* The cast to a user pointer is valid due to the set_fs() */
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100542 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
Miklos Szeredi68181732009-05-07 15:37:36 +0200543 set_fs(old_fs);
544
545 return res;
546}
547
Al Viro7bb307e2013-02-23 14:51:48 -0500548ssize_t kernel_write(struct file *file, const char *buf, size_t count,
Miklos Szeredib2858d72009-05-19 11:37:46 +0200549 loff_t pos)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200550{
551 mm_segment_t old_fs;
552 ssize_t res;
553
554 old_fs = get_fs();
555 set_fs(get_ds());
556 /* The cast to a user pointer is valid due to the set_fs() */
Al Viro7bb307e2013-02-23 14:51:48 -0500557 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200558 set_fs(old_fs);
559
560 return res;
561}
Al Viro7bb307e2013-02-23 14:51:48 -0500562EXPORT_SYMBOL(kernel_write);
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200563
Miklos Szeredi68181732009-05-07 15:37:36 +0200564ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
565 struct pipe_inode_info *pipe, size_t len,
566 unsigned int flags)
567{
568 unsigned int nr_pages;
569 unsigned int nr_freed;
570 size_t offset;
Jens Axboe35f3d142010-05-20 10:43:18 +0200571 struct page *pages[PIPE_DEF_BUFFERS];
572 struct partial_page partial[PIPE_DEF_BUFFERS];
573 struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
Miklos Szeredi68181732009-05-07 15:37:36 +0200574 ssize_t res;
575 size_t this_len;
576 int error;
577 int i;
578 struct splice_pipe_desc spd = {
579 .pages = pages,
580 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +0200581 .nr_pages_max = PIPE_DEF_BUFFERS,
Miklos Szeredi68181732009-05-07 15:37:36 +0200582 .flags = flags,
583 .ops = &default_pipe_buf_ops,
584 .spd_release = spd_release_page,
585 };
586
Jens Axboe35f3d142010-05-20 10:43:18 +0200587 if (splice_grow_spd(pipe, &spd))
588 return -ENOMEM;
589
590 res = -ENOMEM;
591 vec = __vec;
Eric Dumazet047fe362012-06-12 15:24:40 +0200592 if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
593 vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
Jens Axboe35f3d142010-05-20 10:43:18 +0200594 if (!vec)
595 goto shrink_ret;
596 }
597
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300598 offset = *ppos & ~PAGE_MASK;
599 nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi68181732009-05-07 15:37:36 +0200600
Eric Dumazet047fe362012-06-12 15:24:40 +0200601 for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
Miklos Szeredi68181732009-05-07 15:37:36 +0200602 struct page *page;
603
Jens Axboe4f231222009-05-13 08:35:35 +0200604 page = alloc_page(GFP_USER);
Miklos Szeredi68181732009-05-07 15:37:36 +0200605 error = -ENOMEM;
606 if (!page)
607 goto err;
608
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300609 this_len = min_t(size_t, len, PAGE_SIZE - offset);
Jens Axboe4f231222009-05-13 08:35:35 +0200610 vec[i].iov_base = (void __user *) page_address(page);
Miklos Szeredi68181732009-05-07 15:37:36 +0200611 vec[i].iov_len = this_len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200612 spd.pages[i] = page;
Miklos Szeredi68181732009-05-07 15:37:36 +0200613 spd.nr_pages++;
614 len -= this_len;
615 offset = 0;
616 }
617
618 res = kernel_readv(in, vec, spd.nr_pages, *ppos);
Andrew Morton77f6bf52009-05-14 09:49:44 +0200619 if (res < 0) {
620 error = res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200621 goto err;
Andrew Morton77f6bf52009-05-14 09:49:44 +0200622 }
Miklos Szeredi68181732009-05-07 15:37:36 +0200623
624 error = 0;
625 if (!res)
626 goto err;
627
628 nr_freed = 0;
629 for (i = 0; i < spd.nr_pages; i++) {
Miklos Szeredi68181732009-05-07 15:37:36 +0200630 this_len = min_t(size_t, vec[i].iov_len, res);
Jens Axboe35f3d142010-05-20 10:43:18 +0200631 spd.partial[i].offset = 0;
632 spd.partial[i].len = this_len;
Miklos Szeredi68181732009-05-07 15:37:36 +0200633 if (!this_len) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200634 __free_page(spd.pages[i]);
635 spd.pages[i] = NULL;
Miklos Szeredi68181732009-05-07 15:37:36 +0200636 nr_freed++;
637 }
638 res -= this_len;
639 }
640 spd.nr_pages -= nr_freed;
641
642 res = splice_to_pipe(pipe, &spd);
643 if (res > 0)
644 *ppos += res;
645
Jens Axboe35f3d142010-05-20 10:43:18 +0200646shrink_ret:
647 if (vec != __vec)
648 kfree(vec);
Eric Dumazet047fe362012-06-12 15:24:40 +0200649 splice_shrink_spd(&spd);
Miklos Szeredi68181732009-05-07 15:37:36 +0200650 return res;
651
652err:
Jens Axboe4f231222009-05-13 08:35:35 +0200653 for (i = 0; i < spd.nr_pages; i++)
Jens Axboe35f3d142010-05-20 10:43:18 +0200654 __free_page(spd.pages[i]);
Jens Axboe4f231222009-05-13 08:35:35 +0200655
Jens Axboe35f3d142010-05-20 10:43:18 +0200656 res = error;
657 goto shrink_ret;
Miklos Szeredi68181732009-05-07 15:37:36 +0200658}
659EXPORT_SYMBOL(default_file_splice_read);
660
Jens Axboe5274f052006-03-30 15:15:30 +0200661/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200662 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200663 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200664 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200665static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200666 struct pipe_buffer *buf, struct splice_desc *sd)
667{
Jens Axboe6a14b902007-06-14 13:08:55 +0200668 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200669 loff_t pos = sd->pos;
Michał Mirosława8adbe32010-12-17 08:56:44 +0100670 int more;
Jens Axboe5274f052006-03-30 15:15:30 +0200671
Al Viro72c2d532013-09-22 16:27:52 -0400672 if (!likely(file->f_op->sendpage))
Michał Mirosława8adbe32010-12-17 08:56:44 +0100673 return -EINVAL;
Jens Axboe5274f052006-03-30 15:15:30 +0200674
Eric Dumazet35f9c092012-04-05 03:05:35 +0000675 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000676
677 if (sd->len < sd->total_len && pipe->nrbufs > 1)
Eric Dumazet35f9c092012-04-05 03:05:35 +0000678 more |= MSG_SENDPAGE_NOTLAST;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000679
Michał Mirosława8adbe32010-12-17 08:56:44 +0100680 return file->f_op->sendpage(file, buf->page, buf->offset,
681 sd->len, &pos, more);
Jens Axboe5274f052006-03-30 15:15:30 +0200682}
683
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200684static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
685{
686 smp_mb();
687 if (waitqueue_active(&pipe->wait))
688 wake_up_interruptible(&pipe->wait);
689 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
690}
691
692/**
693 * splice_from_pipe_feed - feed available data from a pipe to a file
694 * @pipe: pipe to splice from
695 * @sd: information to @actor
696 * @actor: handler that splices the data
697 *
698 * Description:
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200699 * This function loops over the pipe and calls @actor to do the
700 * actual moving of a single struct pipe_buffer to the desired
701 * destination. It returns when there's no more buffers left in
702 * the pipe or if the requested number of bytes (@sd->total_len)
703 * have been copied. It returns a positive number (one) if the
704 * pipe needs to be filled with more data, zero if the required
705 * number of bytes have been copied and -errno on error.
706 *
707 * This, together with splice_from_pipe_{begin,end,next}, may be
708 * used to implement the functionality of __splice_from_pipe() when
709 * locking is required around copying the pipe buffers to the
710 * destination.
711 */
Al Viro96f9bc82014-04-05 04:35:49 -0400712static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200713 splice_actor *actor)
714{
715 int ret;
716
717 while (pipe->nrbufs) {
718 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
719 const struct pipe_buf_operations *ops = buf->ops;
720
721 sd->len = buf->len;
722 if (sd->len > sd->total_len)
723 sd->len = sd->total_len;
724
Michał Mirosława8adbe32010-12-17 08:56:44 +0100725 ret = buf->ops->confirm(pipe, buf);
726 if (unlikely(ret)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200727 if (ret == -ENODATA)
728 ret = 0;
729 return ret;
730 }
Michał Mirosława8adbe32010-12-17 08:56:44 +0100731
732 ret = actor(pipe, buf, sd);
733 if (ret <= 0)
734 return ret;
735
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200736 buf->offset += ret;
737 buf->len -= ret;
738
739 sd->num_spliced += ret;
740 sd->len -= ret;
741 sd->pos += ret;
742 sd->total_len -= ret;
743
744 if (!buf->len) {
745 buf->ops = NULL;
746 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200747 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200748 pipe->nrbufs--;
Al Viro6447a3c2013-03-21 11:01:38 -0400749 if (pipe->files)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200750 sd->need_wakeup = true;
751 }
752
753 if (!sd->total_len)
754 return 0;
755 }
756
757 return 1;
758}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200759
760/**
761 * splice_from_pipe_next - wait for some data to splice from
762 * @pipe: pipe to splice from
763 * @sd: information about the splice operation
764 *
765 * Description:
766 * This function will wait for some data and return a positive
767 * value (one) if pipe buffers are available. It will return zero
768 * or -errno if no more data needs to be spliced.
769 */
Al Viro96f9bc82014-04-05 04:35:49 -0400770static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200771{
Jan Karac725bfc2015-11-23 13:09:50 +0100772 /*
773 * Check for signal early to make process killable when there are
774 * always buffers available
775 */
776 if (signal_pending(current))
777 return -ERESTARTSYS;
778
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200779 while (!pipe->nrbufs) {
780 if (!pipe->writers)
781 return 0;
782
783 if (!pipe->waiting_writers && sd->num_spliced)
784 return 0;
785
786 if (sd->flags & SPLICE_F_NONBLOCK)
787 return -EAGAIN;
788
789 if (signal_pending(current))
790 return -ERESTARTSYS;
791
792 if (sd->need_wakeup) {
793 wakeup_pipe_writers(pipe);
794 sd->need_wakeup = false;
795 }
796
797 pipe_wait(pipe);
798 }
799
800 return 1;
801}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200802
803/**
804 * splice_from_pipe_begin - start splicing from pipe
Randy Dunlapb80901b2009-04-16 19:09:55 -0700805 * @sd: information about the splice operation
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200806 *
807 * Description:
808 * This function should be called before a loop containing
809 * splice_from_pipe_next() and splice_from_pipe_feed() to
810 * initialize the necessary fields of @sd.
811 */
Al Viro96f9bc82014-04-05 04:35:49 -0400812static void splice_from_pipe_begin(struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200813{
814 sd->num_spliced = 0;
815 sd->need_wakeup = false;
816}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200817
818/**
819 * splice_from_pipe_end - finish splicing from pipe
820 * @pipe: pipe to splice from
821 * @sd: information about the splice operation
822 *
823 * Description:
824 * This function will wake up pipe writers if necessary. It should
825 * be called after a loop containing splice_from_pipe_next() and
826 * splice_from_pipe_feed().
827 */
Al Viro96f9bc82014-04-05 04:35:49 -0400828static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200829{
830 if (sd->need_wakeup)
831 wakeup_pipe_writers(pipe);
832}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200833
Jens Axboe932cc6d2007-06-21 13:10:21 +0200834/**
835 * __splice_from_pipe - splice data from a pipe to given actor
836 * @pipe: pipe to splice from
837 * @sd: information to @actor
838 * @actor: handler that splices the data
839 *
840 * Description:
841 * This function does little more than loop over the pipe and call
842 * @actor to do the actual moving of a single struct pipe_buffer to
843 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
844 * pipe_to_user.
845 *
Jens Axboe83f91352006-04-02 23:05:09 +0200846 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200847ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
848 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200849{
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200850 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200851
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200852 splice_from_pipe_begin(sd);
853 do {
Jan Karac2489e02015-11-23 13:09:51 +0100854 cond_resched();
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200855 ret = splice_from_pipe_next(pipe, sd);
856 if (ret > 0)
857 ret = splice_from_pipe_feed(pipe, sd, actor);
858 } while (ret > 0);
859 splice_from_pipe_end(pipe, sd);
Jens Axboe5274f052006-03-30 15:15:30 +0200860
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200861 return sd->num_spliced ? sd->num_spliced : ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200862}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100863EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200864
Jens Axboe932cc6d2007-06-21 13:10:21 +0200865/**
866 * splice_from_pipe - splice data from a pipe to a file
867 * @pipe: pipe to splice from
868 * @out: file to splice to
869 * @ppos: position in @out
870 * @len: how many bytes to splice
871 * @flags: splice modifier flags
872 * @actor: handler that splices the data
873 *
874 * Description:
Miklos Szeredi29339702009-04-14 19:48:37 +0200875 * See __splice_from_pipe. This function locks the pipe inode,
Jens Axboe932cc6d2007-06-21 13:10:21 +0200876 * otherwise it's identical to __splice_from_pipe().
877 *
878 */
Mark Fasheh6da61802006-10-17 18:43:07 +0200879ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
880 loff_t *ppos, size_t len, unsigned int flags,
881 splice_actor *actor)
882{
883 ssize_t ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200884 struct splice_desc sd = {
885 .total_len = len,
886 .flags = flags,
887 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200888 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200889 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200890
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200891 pipe_lock(pipe);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200892 ret = __splice_from_pipe(pipe, &sd, actor);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200893 pipe_unlock(pipe);
Mark Fasheh6da61802006-10-17 18:43:07 +0200894
895 return ret;
896}
897
898/**
Al Viro8d020762014-04-05 04:27:08 -0400899 * iter_file_splice_write - splice data from a pipe to a file
900 * @pipe: pipe info
901 * @out: file to write to
902 * @ppos: position in @out
903 * @len: number of bytes to splice
904 * @flags: splice modifier flags
905 *
906 * Description:
907 * Will either move or copy pages (determined by @flags options) from
908 * the given pipe inode to the given file.
909 * This one is ->write_iter-based.
910 *
911 */
912ssize_t
913iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
914 loff_t *ppos, size_t len, unsigned int flags)
915{
916 struct splice_desc sd = {
917 .total_len = len,
918 .flags = flags,
919 .pos = *ppos,
920 .u.file = out,
921 };
922 int nbufs = pipe->buffers;
923 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
924 GFP_KERNEL);
925 ssize_t ret;
926
927 if (unlikely(!array))
928 return -ENOMEM;
929
930 pipe_lock(pipe);
931
932 splice_from_pipe_begin(&sd);
933 while (sd.total_len) {
934 struct iov_iter from;
Al Viro8d020762014-04-05 04:27:08 -0400935 size_t left;
936 int n, idx;
937
938 ret = splice_from_pipe_next(pipe, &sd);
939 if (ret <= 0)
940 break;
941
942 if (unlikely(nbufs < pipe->buffers)) {
943 kfree(array);
944 nbufs = pipe->buffers;
945 array = kcalloc(nbufs, sizeof(struct bio_vec),
946 GFP_KERNEL);
947 if (!array) {
948 ret = -ENOMEM;
949 break;
950 }
951 }
952
953 /* build the vector */
954 left = sd.total_len;
955 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
956 struct pipe_buffer *buf = pipe->bufs + idx;
957 size_t this_len = buf->len;
958
959 if (this_len > left)
960 this_len = left;
961
962 if (idx == pipe->buffers - 1)
963 idx = -1;
964
965 ret = buf->ops->confirm(pipe, buf);
966 if (unlikely(ret)) {
967 if (ret == -ENODATA)
968 ret = 0;
969 goto done;
970 }
971
972 array[n].bv_page = buf->page;
973 array[n].bv_len = this_len;
974 array[n].bv_offset = buf->offset;
975 left -= this_len;
976 }
977
Al Viro05afcb72015-01-23 01:08:07 -0500978 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
979 sd.total_len - left);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100980 ret = vfs_iter_write(out, &from, &sd.pos);
Al Viro8d020762014-04-05 04:27:08 -0400981 if (ret <= 0)
982 break;
983
984 sd.num_spliced += ret;
985 sd.total_len -= ret;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100986 *ppos = sd.pos;
Al Viro8d020762014-04-05 04:27:08 -0400987
988 /* dismiss the fully eaten buffers, adjust the partial one */
989 while (ret) {
990 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
991 if (ret >= buf->len) {
992 const struct pipe_buf_operations *ops = buf->ops;
993 ret -= buf->len;
994 buf->len = 0;
995 buf->ops = NULL;
996 ops->release(pipe, buf);
997 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
998 pipe->nrbufs--;
999 if (pipe->files)
1000 sd.need_wakeup = true;
1001 } else {
1002 buf->offset += ret;
1003 buf->len -= ret;
1004 ret = 0;
1005 }
1006 }
1007 }
1008done:
1009 kfree(array);
1010 splice_from_pipe_end(pipe, &sd);
1011
1012 pipe_unlock(pipe);
1013
1014 if (sd.num_spliced)
1015 ret = sd.num_spliced;
1016
1017 return ret;
1018}
1019
1020EXPORT_SYMBOL(iter_file_splice_write);
1021
Miklos Szeredib2858d72009-05-19 11:37:46 +02001022static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1023 struct splice_desc *sd)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001024{
Miklos Szeredib2858d72009-05-19 11:37:46 +02001025 int ret;
1026 void *data;
Al Viro06ae43f2013-03-20 13:19:30 -04001027 loff_t tmp = sd->pos;
Miklos Szeredib2858d72009-05-19 11:37:46 +02001028
Al Virofbb32752014-02-02 21:09:54 -05001029 data = kmap(buf->page);
Al Viro06ae43f2013-03-20 13:19:30 -04001030 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
Al Virofbb32752014-02-02 21:09:54 -05001031 kunmap(buf->page);
Miklos Szeredib2858d72009-05-19 11:37:46 +02001032
1033 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001034}
1035
1036static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
1037 struct file *out, loff_t *ppos,
1038 size_t len, unsigned int flags)
1039{
Miklos Szeredib2858d72009-05-19 11:37:46 +02001040 ssize_t ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001041
Miklos Szeredib2858d72009-05-19 11:37:46 +02001042 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1043 if (ret > 0)
1044 *ppos += ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001045
Miklos Szeredib2858d72009-05-19 11:37:46 +02001046 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001047}
1048
Jens Axboe83f91352006-04-02 23:05:09 +02001049/**
1050 * generic_splice_sendpage - splice data from a pipe to a socket
Jens Axboe932cc6d2007-06-21 13:10:21 +02001051 * @pipe: pipe to splice from
Jens Axboe83f91352006-04-02 23:05:09 +02001052 * @out: socket to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +02001053 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +02001054 * @len: number of bytes to splice
1055 * @flags: splice modifier flags
1056 *
Jens Axboe932cc6d2007-06-21 13:10:21 +02001057 * Description:
1058 * Will send @len bytes from the pipe to a network socket. No data copying
1059 * is involved.
Jens Axboe83f91352006-04-02 23:05:09 +02001060 *
1061 */
Ingo Molnar3a326a22006-04-10 15:18:35 +02001062ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +02001063 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001064{
Jens Axboe00522fb2006-04-26 14:39:29 +02001065 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +02001066}
1067
Jens Axboe059a8f32006-04-02 23:06:05 +02001068EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -05001069
Jens Axboe83f91352006-04-02 23:05:09 +02001070/*
1071 * Attempt to initiate a splice from pipe to file.
1072 */
Ingo Molnar3a326a22006-04-10 15:18:35 +02001073static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +02001074 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001075{
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001076 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
1077 loff_t *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +02001078
Al Viro72c2d532013-09-22 16:27:52 -04001079 if (out->f_op->splice_write)
Changli Gaocc56f7d2009-11-04 09:09:52 +01001080 splice_write = out->f_op->splice_write;
1081 else
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001082 splice_write = default_file_splice_write;
1083
Al Viro500368f2013-05-23 20:07:11 -04001084 return splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001085}
1086
Jens Axboe83f91352006-04-02 23:05:09 +02001087/*
1088 * Attempt to initiate a splice from a file to a pipe.
1089 */
Jens Axboecbb7e572006-04-11 14:57:50 +02001090static long do_splice_to(struct file *in, loff_t *ppos,
1091 struct pipe_inode_info *pipe, size_t len,
1092 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001093{
Miklos Szeredi68181732009-05-07 15:37:36 +02001094 ssize_t (*splice_read)(struct file *, loff_t *,
1095 struct pipe_inode_info *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +02001096 int ret;
1097
Jens Axboe49570e92006-04-11 13:56:09 +02001098 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +02001099 return -EBADF;
1100
Jens Axboecbb7e572006-04-11 14:57:50 +02001101 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +02001102 if (unlikely(ret < 0))
1103 return ret;
1104
Al Viro03cc0782016-04-02 14:56:58 -04001105 if (unlikely(len > MAX_RW_COUNT))
1106 len = MAX_RW_COUNT;
1107
Al Viro72c2d532013-09-22 16:27:52 -04001108 if (in->f_op->splice_read)
Changli Gaocc56f7d2009-11-04 09:09:52 +01001109 splice_read = in->f_op->splice_read;
1110 else
Miklos Szeredi68181732009-05-07 15:37:36 +02001111 splice_read = default_file_splice_read;
1112
1113 return splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001114}
1115
Jens Axboe932cc6d2007-06-21 13:10:21 +02001116/**
1117 * splice_direct_to_actor - splices data directly between two non-pipes
1118 * @in: file to splice from
1119 * @sd: actor information on where to splice to
1120 * @actor: handles the data splicing
1121 *
1122 * Description:
1123 * This is a special case helper to splice directly between two
1124 * points, without requiring an explicit pipe. Internally an allocated
Randy Dunlap79685b82007-07-27 08:08:51 +02001125 * pipe is cached in the process, and reused during the lifetime of
Jens Axboe932cc6d2007-06-21 13:10:21 +02001126 * that process.
1127 *
Jens Axboec66ab6f2007-06-12 21:17:17 +02001128 */
1129ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1130 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +02001131{
1132 struct pipe_inode_info *pipe;
1133 long ret, bytes;
1134 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001135 size_t len;
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001136 int i, flags, more;
Jens Axboeb92ce552006-04-11 13:52:07 +02001137
1138 /*
1139 * We require the input being a regular file, as we don't want to
1140 * randomly drop data for eg socket -> socket splicing. Use the
1141 * piped splicing for that!
1142 */
Al Viro496ad9a2013-01-23 17:07:38 -05001143 i_mode = file_inode(in)->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +02001144 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1145 return -EINVAL;
1146
1147 /*
1148 * neither in nor out is a pipe, setup an internal pipe attached to
1149 * 'out' and transfer the wanted data from 'in' to 'out' through that
1150 */
1151 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +02001152 if (unlikely(!pipe)) {
Al Viro7bee1302013-03-21 11:04:15 -04001153 pipe = alloc_pipe_info();
Jens Axboeb92ce552006-04-11 13:52:07 +02001154 if (!pipe)
1155 return -ENOMEM;
1156
1157 /*
1158 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +02001159 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +02001160 * PIPE_READERS appropriately.
1161 */
1162 pipe->readers = 1;
1163
1164 current->splice_pipe = pipe;
1165 }
1166
1167 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +02001168 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +02001169 */
1170 ret = 0;
1171 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001172 len = sd->total_len;
1173 flags = sd->flags;
1174
1175 /*
1176 * Don't block on output, we have to drain the direct pipe.
1177 */
1178 sd->flags &= ~SPLICE_F_NONBLOCK;
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001179 more = sd->flags & SPLICE_F_MORE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001180
1181 while (len) {
Jens Axboe51a92c02007-07-13 14:11:43 +02001182 size_t read_len;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001183 loff_t pos = sd->pos, prev_pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001184
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001185 ret = do_splice_to(in, &pos, pipe, len, flags);
Jens Axboe51a92c02007-07-13 14:11:43 +02001186 if (unlikely(ret <= 0))
Jens Axboeb92ce552006-04-11 13:52:07 +02001187 goto out_release;
1188
1189 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001190 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +02001191
1192 /*
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001193 * If more data is pending, set SPLICE_F_MORE
1194 * If this is the last data and SPLICE_F_MORE was not set
1195 * initially, clears it.
1196 */
1197 if (read_len < len)
1198 sd->flags |= SPLICE_F_MORE;
1199 else if (!more)
1200 sd->flags &= ~SPLICE_F_MORE;
1201 /*
Jens Axboeb92ce552006-04-11 13:52:07 +02001202 * NOTE: nonblocking mode only applies to the input. We
1203 * must not do the output in nonblocking mode as then we
1204 * could get stuck data in the internal pipe:
1205 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001206 ret = actor(pipe, sd);
Tom Zanussia82c53a2008-05-09 13:28:36 +02001207 if (unlikely(ret <= 0)) {
1208 sd->pos = prev_pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001209 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001210 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001211
1212 bytes += ret;
1213 len -= ret;
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001214 sd->pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001215
Tom Zanussia82c53a2008-05-09 13:28:36 +02001216 if (ret < read_len) {
1217 sd->pos = prev_pos + ret;
Jens Axboe51a92c02007-07-13 14:11:43 +02001218 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001219 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001220 }
1221
Jens Axboe9e971982008-01-29 21:05:57 +01001222done:
Jens Axboeb92ce552006-04-11 13:52:07 +02001223 pipe->nrbufs = pipe->curbuf = 0;
Jens Axboe80848702008-01-30 12:24:48 +01001224 file_accessed(in);
Jens Axboeb92ce552006-04-11 13:52:07 +02001225 return bytes;
1226
1227out_release:
1228 /*
1229 * If we did an incomplete transfer we must release
1230 * the pipe buffers in question:
1231 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001232 for (i = 0; i < pipe->buffers; i++) {
Jens Axboeb92ce552006-04-11 13:52:07 +02001233 struct pipe_buffer *buf = pipe->bufs + i;
1234
1235 if (buf->ops) {
1236 buf->ops->release(pipe, buf);
1237 buf->ops = NULL;
1238 }
1239 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001240
Jens Axboe9e971982008-01-29 21:05:57 +01001241 if (!bytes)
1242 bytes = ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001243
Jens Axboe9e971982008-01-29 21:05:57 +01001244 goto done;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001245}
1246EXPORT_SYMBOL(splice_direct_to_actor);
1247
1248static int direct_splice_actor(struct pipe_inode_info *pipe,
1249 struct splice_desc *sd)
1250{
Jens Axboe6a14b902007-06-14 13:08:55 +02001251 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001252
Al Viro7995bd22013-06-20 18:58:36 +04001253 return do_splice_from(pipe, file, sd->opos, sd->total_len,
Changli Gao2cb4b052010-06-29 13:09:18 +02001254 sd->flags);
Jens Axboec66ab6f2007-06-12 21:17:17 +02001255}
1256
Jens Axboe932cc6d2007-06-21 13:10:21 +02001257/**
1258 * do_splice_direct - splices data directly between two files
1259 * @in: file to splice from
1260 * @ppos: input file offset
1261 * @out: file to splice to
Randy Dunlapacdb37c2013-06-22 19:44:08 -07001262 * @opos: output file offset
Jens Axboe932cc6d2007-06-21 13:10:21 +02001263 * @len: number of bytes to splice
1264 * @flags: splice modifier flags
1265 *
1266 * Description:
1267 * For use by do_sendfile(). splice can easily emulate sendfile, but
1268 * doing it in the application would incur an extra system call
1269 * (splice in + splice out, as compared to just sendfile()). So this helper
1270 * can splice directly through a process-private pipe.
1271 *
1272 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001273long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
Al Viro7995bd22013-06-20 18:58:36 +04001274 loff_t *opos, size_t len, unsigned int flags)
Jens Axboec66ab6f2007-06-12 21:17:17 +02001275{
1276 struct splice_desc sd = {
1277 .len = len,
1278 .total_len = len,
1279 .flags = flags,
1280 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001281 .u.file = out,
Al Viro7995bd22013-06-20 18:58:36 +04001282 .opos = opos,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001283 };
Jens Axboe51a92c02007-07-13 14:11:43 +02001284 long ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001285
Al Viro18c67cb2013-06-19 15:41:54 +04001286 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1287 return -EBADF;
1288
1289 if (unlikely(out->f_flags & O_APPEND))
1290 return -EINVAL;
1291
1292 ret = rw_verify_area(WRITE, out, opos, len);
1293 if (unlikely(ret < 0))
1294 return ret;
1295
Jens Axboec66ab6f2007-06-12 21:17:17 +02001296 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
Jens Axboe51a92c02007-07-13 14:11:43 +02001297 if (ret > 0)
Tom Zanussia82c53a2008-05-09 13:28:36 +02001298 *ppos = sd.pos;
Jens Axboe51a92c02007-07-13 14:11:43 +02001299
Jens Axboec66ab6f2007-06-12 21:17:17 +02001300 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001301}
Miklos Szeredi1c118592014-10-24 00:14:35 +02001302EXPORT_SYMBOL(do_splice_direct);
Jens Axboeb92ce552006-04-11 13:52:07 +02001303
Al Viro8924fef2016-09-17 20:44:45 -04001304static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1305{
1306 while (pipe->nrbufs == pipe->buffers) {
1307 if (flags & SPLICE_F_NONBLOCK)
1308 return -EAGAIN;
1309 if (signal_pending(current))
1310 return -ERESTARTSYS;
1311 pipe->waiting_writers++;
1312 pipe_wait(pipe);
1313 pipe->waiting_writers--;
1314 }
1315 return 0;
1316}
1317
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001318static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1319 struct pipe_inode_info *opipe,
1320 size_t len, unsigned int flags);
Jens Axboeddac0d32006-11-04 12:49:32 +01001321
1322/*
Jens Axboe83f91352006-04-02 23:05:09 +02001323 * Determine where to splice to/from.
1324 */
Ingo Molnar529565dc2006-04-10 15:18:58 +02001325static long do_splice(struct file *in, loff_t __user *off_in,
1326 struct file *out, loff_t __user *off_out,
1327 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001328{
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001329 struct pipe_inode_info *ipipe;
1330 struct pipe_inode_info *opipe;
Al Viro7995bd22013-06-20 18:58:36 +04001331 loff_t offset;
Jens Axboea4514eb2006-04-19 15:57:05 +02001332 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001333
Linus Torvalds71993e62010-11-28 13:56:09 -08001334 ipipe = get_pipe_info(in);
1335 opipe = get_pipe_info(out);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001336
1337 if (ipipe && opipe) {
1338 if (off_in || off_out)
1339 return -ESPIPE;
1340
1341 if (!(in->f_mode & FMODE_READ))
1342 return -EBADF;
1343
1344 if (!(out->f_mode & FMODE_WRITE))
1345 return -EBADF;
1346
1347 /* Splicing to self would be fun, but... */
1348 if (ipipe == opipe)
1349 return -EINVAL;
1350
1351 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1352 }
1353
1354 if (ipipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001355 if (off_in)
1356 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001357 if (off_out) {
Changli Gao19c9a492010-06-29 13:10:36 +02001358 if (!(out->f_mode & FMODE_PWRITE))
Jens Axboeb92ce552006-04-11 13:52:07 +02001359 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001360 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001361 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001362 } else {
1363 offset = out->f_pos;
1364 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001365
Al Viro18c67cb2013-06-19 15:41:54 +04001366 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1367 return -EBADF;
1368
1369 if (unlikely(out->f_flags & O_APPEND))
1370 return -EINVAL;
1371
1372 ret = rw_verify_area(WRITE, out, &offset, len);
1373 if (unlikely(ret < 0))
1374 return ret;
1375
Al Viro500368f2013-05-23 20:07:11 -04001376 file_start_write(out);
Al Viro7995bd22013-06-20 18:58:36 +04001377 ret = do_splice_from(ipipe, out, &offset, len, flags);
Al Viro500368f2013-05-23 20:07:11 -04001378 file_end_write(out);
Jens Axboea4514eb2006-04-19 15:57:05 +02001379
Al Viro7995bd22013-06-20 18:58:36 +04001380 if (!off_out)
1381 out->f_pos = offset;
1382 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001383 ret = -EFAULT;
1384
1385 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001386 }
Jens Axboe5274f052006-03-30 15:15:30 +02001387
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001388 if (opipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001389 if (off_out)
1390 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001391 if (off_in) {
Changli Gao19c9a492010-06-29 13:10:36 +02001392 if (!(in->f_mode & FMODE_PREAD))
Jens Axboeb92ce552006-04-11 13:52:07 +02001393 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001394 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001395 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001396 } else {
1397 offset = in->f_pos;
1398 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001399
Al Viro8924fef2016-09-17 20:44:45 -04001400 pipe_lock(opipe);
1401 ret = wait_for_space(opipe, flags);
1402 if (!ret)
1403 ret = do_splice_to(in, &offset, opipe, len, flags);
1404 pipe_unlock(opipe);
1405 if (ret > 0)
1406 wakeup_pipe_readers(opipe);
Al Viro7995bd22013-06-20 18:58:36 +04001407 if (!off_in)
1408 in->f_pos = offset;
1409 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001410 ret = -EFAULT;
1411
1412 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001413 }
Jens Axboe5274f052006-03-30 15:15:30 +02001414
1415 return -EINVAL;
1416}
1417
Al Virodb85a9e2016-09-17 20:25:06 -04001418static int get_iovec_page_array(struct iov_iter *from,
1419 struct page **pages,
1420 struct partial_page *partial,
Jens Axboe35f3d142010-05-20 10:43:18 +02001421 unsigned int pipe_buffers)
Jens Axboe912d35f2006-04-26 10:59:21 +02001422{
Al Virodb85a9e2016-09-17 20:25:06 -04001423 int buffers = 0;
1424 while (iov_iter_count(from)) {
1425 ssize_t copied;
1426 size_t start;
Jens Axboe912d35f2006-04-26 10:59:21 +02001427
Al Virodb85a9e2016-09-17 20:25:06 -04001428 copied = iov_iter_get_pages(from, pages + buffers, ~0UL,
1429 pipe_buffers - buffers, &start);
1430 if (copied <= 0)
1431 return buffers ? buffers : copied;
Jens Axboe912d35f2006-04-26 10:59:21 +02001432
Al Virodb85a9e2016-09-17 20:25:06 -04001433 iov_iter_advance(from, copied);
1434 while (copied) {
1435 int size = min_t(int, copied, PAGE_SIZE - start);
1436 partial[buffers].offset = start;
1437 partial[buffers].len = size;
1438 copied -= size;
1439 start = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +02001440 buffers++;
1441 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001442 }
Al Virodb85a9e2016-09-17 20:25:06 -04001443 return buffers;
Jens Axboe912d35f2006-04-26 10:59:21 +02001444}
1445
Jens Axboe6a14b902007-06-14 13:08:55 +02001446static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1447 struct splice_desc *sd)
1448{
Al Viro6130f532014-02-03 18:19:51 -05001449 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1450 return n == sd->len ? n : -EFAULT;
Jens Axboe6a14b902007-06-14 13:08:55 +02001451}
1452
1453/*
1454 * For lack of a better implementation, implement vmsplice() to userspace
1455 * as a simple copy of the pipes pages to the user iov.
1456 */
Al Viro6130f532014-02-03 18:19:51 -05001457static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001458 unsigned long nr_segs, unsigned int flags)
1459{
1460 struct pipe_inode_info *pipe;
1461 struct splice_desc sd;
Jens Axboe6a14b902007-06-14 13:08:55 +02001462 long ret;
Al Viro6130f532014-02-03 18:19:51 -05001463 struct iovec iovstack[UIO_FASTIOV];
1464 struct iovec *iov = iovstack;
1465 struct iov_iter iter;
Jens Axboe6a14b902007-06-14 13:08:55 +02001466
Linus Torvalds71993e62010-11-28 13:56:09 -08001467 pipe = get_pipe_info(file);
Jens Axboe6a14b902007-06-14 13:08:55 +02001468 if (!pipe)
1469 return -EBADF;
1470
Al Viro345995f2015-03-21 19:17:55 -04001471 ret = import_iovec(READ, uiov, nr_segs,
1472 ARRAY_SIZE(iovstack), &iov, &iter);
1473 if (ret < 0)
1474 return ret;
Al Viro6130f532014-02-03 18:19:51 -05001475
Al Viro345995f2015-03-21 19:17:55 -04001476 sd.total_len = iov_iter_count(&iter);
Al Viro6130f532014-02-03 18:19:51 -05001477 sd.len = 0;
Al Viro6130f532014-02-03 18:19:51 -05001478 sd.flags = flags;
1479 sd.u.data = &iter;
1480 sd.pos = 0;
1481
Al Viro345995f2015-03-21 19:17:55 -04001482 if (sd.total_len) {
1483 pipe_lock(pipe);
1484 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1485 pipe_unlock(pipe);
1486 }
Jens Axboe6a14b902007-06-14 13:08:55 +02001487
Al Viro345995f2015-03-21 19:17:55 -04001488 kfree(iov);
Jens Axboe6a14b902007-06-14 13:08:55 +02001489 return ret;
1490}
1491
Jens Axboe912d35f2006-04-26 10:59:21 +02001492/*
1493 * vmsplice splices a user address range into a pipe. It can be thought of
1494 * as splice-from-memory, where the regular splice is splice-from-file (or
1495 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001496 */
Al Virodb85a9e2016-09-17 20:25:06 -04001497static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001498 unsigned long nr_segs, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001499{
Jens Axboeddac0d32006-11-04 12:49:32 +01001500 struct pipe_inode_info *pipe;
Al Virodb85a9e2016-09-17 20:25:06 -04001501 struct iovec iovstack[UIO_FASTIOV];
1502 struct iovec *iov = iovstack;
1503 struct iov_iter from;
Jens Axboe35f3d142010-05-20 10:43:18 +02001504 struct page *pages[PIPE_DEF_BUFFERS];
1505 struct partial_page partial[PIPE_DEF_BUFFERS];
Jens Axboe912d35f2006-04-26 10:59:21 +02001506 struct splice_pipe_desc spd = {
1507 .pages = pages,
1508 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001509 .nr_pages_max = PIPE_DEF_BUFFERS,
Jens Axboe912d35f2006-04-26 10:59:21 +02001510 .flags = flags,
1511 .ops = &user_page_pipe_buf_ops,
Jens Axboebbdfc2f2007-11-06 23:29:47 -08001512 .spd_release = spd_release_page,
Jens Axboe912d35f2006-04-26 10:59:21 +02001513 };
Jens Axboe35f3d142010-05-20 10:43:18 +02001514 long ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001515
Linus Torvalds71993e62010-11-28 13:56:09 -08001516 pipe = get_pipe_info(file);
Jens Axboeddac0d32006-11-04 12:49:32 +01001517 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001518 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001519
Al Virodb85a9e2016-09-17 20:25:06 -04001520 ret = import_iovec(WRITE, uiov, nr_segs,
1521 ARRAY_SIZE(iovstack), &iov, &from);
1522 if (ret < 0)
1523 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001524
Al Virodb85a9e2016-09-17 20:25:06 -04001525 if (splice_grow_spd(pipe, &spd)) {
1526 kfree(iov);
1527 return -ENOMEM;
1528 }
1529
Al Viro8924fef2016-09-17 20:44:45 -04001530 pipe_lock(pipe);
1531 ret = wait_for_space(pipe, flags);
1532 if (!ret) {
1533 spd.nr_pages = get_iovec_page_array(&from, spd.pages,
1534 spd.partial,
1535 spd.nr_pages_max);
1536 if (spd.nr_pages <= 0)
1537 ret = spd.nr_pages;
1538 else
1539 ret = splice_to_pipe(pipe, &spd);
1540 }
1541 pipe_unlock(pipe);
1542 if (ret > 0)
1543 wakeup_pipe_readers(pipe);
Eric Dumazet047fe362012-06-12 15:24:40 +02001544 splice_shrink_spd(&spd);
Al Virodb85a9e2016-09-17 20:25:06 -04001545 kfree(iov);
Jens Axboe35f3d142010-05-20 10:43:18 +02001546 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001547}
1548
Jens Axboe6a14b902007-06-14 13:08:55 +02001549/*
1550 * Note that vmsplice only really supports true splicing _from_ user memory
1551 * to a pipe, not the other way around. Splicing from user memory is a simple
1552 * operation that can be supported without any funky alignment restrictions
1553 * or nasty vm tricks. We simply map in the user memory and fill them into
1554 * a pipe. The reverse isn't quite as easy, though. There are two possible
1555 * solutions for that:
1556 *
1557 * - memcpy() the data internally, at which point we might as well just
1558 * do a regular read() on the buffer anyway.
1559 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1560 * has restriction limitations on both ends of the pipe).
1561 *
1562 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1563 *
1564 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01001565SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1566 unsigned long, nr_segs, unsigned int, flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001567{
Al Viro2903ff02012-08-28 12:52:22 -04001568 struct fd f;
Jens Axboe912d35f2006-04-26 10:59:21 +02001569 long error;
Jens Axboe912d35f2006-04-26 10:59:21 +02001570
Jens Axboe6a14b902007-06-14 13:08:55 +02001571 if (unlikely(nr_segs > UIO_MAXIOV))
1572 return -EINVAL;
1573 else if (unlikely(!nr_segs))
1574 return 0;
1575
Jens Axboe912d35f2006-04-26 10:59:21 +02001576 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001577 f = fdget(fd);
1578 if (f.file) {
1579 if (f.file->f_mode & FMODE_WRITE)
1580 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1581 else if (f.file->f_mode & FMODE_READ)
1582 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001583
Al Viro2903ff02012-08-28 12:52:22 -04001584 fdput(f);
Jens Axboe912d35f2006-04-26 10:59:21 +02001585 }
1586
1587 return error;
1588}
1589
Al Viro76b021d2013-03-02 10:19:56 -05001590#ifdef CONFIG_COMPAT
1591COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1592 unsigned int, nr_segs, unsigned int, flags)
1593{
1594 unsigned i;
1595 struct iovec __user *iov;
1596 if (nr_segs > UIO_MAXIOV)
1597 return -EINVAL;
1598 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1599 for (i = 0; i < nr_segs; i++) {
1600 struct compat_iovec v;
1601 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1602 get_user(v.iov_len, &iov32[i].iov_len) ||
1603 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1604 put_user(v.iov_len, &iov[i].iov_len))
1605 return -EFAULT;
1606 }
1607 return sys_vmsplice(fd, iov, nr_segs, flags);
1608}
1609#endif
1610
Heiko Carstens836f92a2009-01-14 14:14:33 +01001611SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1612 int, fd_out, loff_t __user *, off_out,
1613 size_t, len, unsigned int, flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001614{
Al Viro2903ff02012-08-28 12:52:22 -04001615 struct fd in, out;
Jens Axboe5274f052006-03-30 15:15:30 +02001616 long error;
Jens Axboe5274f052006-03-30 15:15:30 +02001617
1618 if (unlikely(!len))
1619 return 0;
1620
1621 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001622 in = fdget(fd_in);
1623 if (in.file) {
1624 if (in.file->f_mode & FMODE_READ) {
1625 out = fdget(fd_out);
1626 if (out.file) {
1627 if (out.file->f_mode & FMODE_WRITE)
1628 error = do_splice(in.file, off_in,
1629 out.file, off_out,
Ingo Molnar529565dc2006-04-10 15:18:58 +02001630 len, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001631 fdput(out);
Jens Axboe5274f052006-03-30 15:15:30 +02001632 }
1633 }
Al Viro2903ff02012-08-28 12:52:22 -04001634 fdput(in);
Jens Axboe5274f052006-03-30 15:15:30 +02001635 }
Jens Axboe5274f052006-03-30 15:15:30 +02001636 return error;
1637}
Jens Axboe70524492006-04-11 15:51:17 +02001638
1639/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001640 * Make sure there's data to read. Wait for input if we can, otherwise
1641 * return an appropriate error.
1642 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001643static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001644{
1645 int ret;
1646
1647 /*
1648 * Check ->nrbufs without the inode lock first. This function
1649 * is speculative anyways, so missing one is ok.
1650 */
1651 if (pipe->nrbufs)
1652 return 0;
1653
1654 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001655 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001656
1657 while (!pipe->nrbufs) {
1658 if (signal_pending(current)) {
1659 ret = -ERESTARTSYS;
1660 break;
1661 }
1662 if (!pipe->writers)
1663 break;
1664 if (!pipe->waiting_writers) {
1665 if (flags & SPLICE_F_NONBLOCK) {
1666 ret = -EAGAIN;
1667 break;
1668 }
1669 }
1670 pipe_wait(pipe);
1671 }
1672
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001673 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001674 return ret;
1675}
1676
1677/*
1678 * Make sure there's writeable room. Wait for room if we can, otherwise
1679 * return an appropriate error.
1680 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001681static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001682{
1683 int ret;
1684
1685 /*
1686 * Check ->nrbufs without the inode lock first. This function
1687 * is speculative anyways, so missing one is ok.
1688 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001689 if (pipe->nrbufs < pipe->buffers)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001690 return 0;
1691
1692 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001693 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001694
Jens Axboe35f3d142010-05-20 10:43:18 +02001695 while (pipe->nrbufs >= pipe->buffers) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001696 if (!pipe->readers) {
1697 send_sig(SIGPIPE, current, 0);
1698 ret = -EPIPE;
1699 break;
1700 }
1701 if (flags & SPLICE_F_NONBLOCK) {
1702 ret = -EAGAIN;
1703 break;
1704 }
1705 if (signal_pending(current)) {
1706 ret = -ERESTARTSYS;
1707 break;
1708 }
1709 pipe->waiting_writers++;
1710 pipe_wait(pipe);
1711 pipe->waiting_writers--;
1712 }
1713
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001714 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001715 return ret;
1716}
1717
1718/*
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001719 * Splice contents of ipipe to opipe.
1720 */
1721static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1722 struct pipe_inode_info *opipe,
1723 size_t len, unsigned int flags)
1724{
1725 struct pipe_buffer *ibuf, *obuf;
1726 int ret = 0, nbuf;
1727 bool input_wakeup = false;
1728
1729
1730retry:
1731 ret = ipipe_prep(ipipe, flags);
1732 if (ret)
1733 return ret;
1734
1735 ret = opipe_prep(opipe, flags);
1736 if (ret)
1737 return ret;
1738
1739 /*
1740 * Potential ABBA deadlock, work around it by ordering lock
1741 * grabbing by pipe info address. Otherwise two different processes
1742 * could deadlock (one doing tee from A -> B, the other from B -> A).
1743 */
1744 pipe_double_lock(ipipe, opipe);
1745
1746 do {
1747 if (!opipe->readers) {
1748 send_sig(SIGPIPE, current, 0);
1749 if (!ret)
1750 ret = -EPIPE;
1751 break;
1752 }
1753
1754 if (!ipipe->nrbufs && !ipipe->writers)
1755 break;
1756
1757 /*
1758 * Cannot make any progress, because either the input
1759 * pipe is empty or the output pipe is full.
1760 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001761 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001762 /* Already processed some buffers, break */
1763 if (ret)
1764 break;
1765
1766 if (flags & SPLICE_F_NONBLOCK) {
1767 ret = -EAGAIN;
1768 break;
1769 }
1770
1771 /*
1772 * We raced with another reader/writer and haven't
1773 * managed to process any buffers. A zero return
1774 * value means EOF, so retry instead.
1775 */
1776 pipe_unlock(ipipe);
1777 pipe_unlock(opipe);
1778 goto retry;
1779 }
1780
1781 ibuf = ipipe->bufs + ipipe->curbuf;
Jens Axboe35f3d142010-05-20 10:43:18 +02001782 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001783 obuf = opipe->bufs + nbuf;
1784
1785 if (len >= ibuf->len) {
1786 /*
1787 * Simply move the whole buffer from ipipe to opipe
1788 */
1789 *obuf = *ibuf;
1790 ibuf->ops = NULL;
1791 opipe->nrbufs++;
Jens Axboe35f3d142010-05-20 10:43:18 +02001792 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001793 ipipe->nrbufs--;
1794 input_wakeup = true;
1795 } else {
1796 /*
1797 * Get a reference to this pipe buffer,
1798 * so we can copy the contents over.
1799 */
1800 ibuf->ops->get(ipipe, ibuf);
1801 *obuf = *ibuf;
1802
1803 /*
1804 * Don't inherit the gift flag, we need to
1805 * prevent multiple steals of this page.
1806 */
1807 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1808
1809 obuf->len = len;
1810 opipe->nrbufs++;
1811 ibuf->offset += obuf->len;
1812 ibuf->len -= obuf->len;
1813 }
1814 ret += obuf->len;
1815 len -= obuf->len;
1816 } while (len);
1817
1818 pipe_unlock(ipipe);
1819 pipe_unlock(opipe);
1820
1821 /*
1822 * If we put data in the output pipe, wakeup any potential readers.
1823 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001824 if (ret > 0)
1825 wakeup_pipe_readers(opipe);
1826
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001827 if (input_wakeup)
1828 wakeup_pipe_writers(ipipe);
1829
1830 return ret;
1831}
1832
1833/*
Jens Axboe70524492006-04-11 15:51:17 +02001834 * Link contents of ipipe to opipe.
1835 */
1836static int link_pipe(struct pipe_inode_info *ipipe,
1837 struct pipe_inode_info *opipe,
1838 size_t len, unsigned int flags)
1839{
1840 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001841 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001842
1843 /*
1844 * Potential ABBA deadlock, work around it by ordering lock
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001845 * grabbing by pipe info address. Otherwise two different processes
Jens Axboe70524492006-04-11 15:51:17 +02001846 * could deadlock (one doing tee from A -> B, the other from B -> A).
1847 */
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001848 pipe_double_lock(ipipe, opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001849
Jens Axboeaadd06e2006-07-10 11:00:01 +02001850 do {
Jens Axboe70524492006-04-11 15:51:17 +02001851 if (!opipe->readers) {
1852 send_sig(SIGPIPE, current, 0);
1853 if (!ret)
1854 ret = -EPIPE;
1855 break;
1856 }
Jens Axboe70524492006-04-11 15:51:17 +02001857
1858 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001859 * If we have iterated all input buffers or ran out of
1860 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001861 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001862 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
Jens Axboe70524492006-04-11 15:51:17 +02001863 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001864
Jens Axboe35f3d142010-05-20 10:43:18 +02001865 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1866 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001867
Jens Axboe2a27250e2006-04-19 15:56:40 +02001868 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001869 * Get a reference to this pipe buffer,
1870 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001871 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001872 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001873
Jens Axboeaadd06e2006-07-10 11:00:01 +02001874 obuf = opipe->bufs + nbuf;
1875 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001876
Jens Axboeaadd06e2006-07-10 11:00:01 +02001877 /*
1878 * Don't inherit the gift flag, we need to
1879 * prevent multiple steals of this page.
1880 */
1881 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1882
1883 if (obuf->len > len)
1884 obuf->len = len;
1885
1886 opipe->nrbufs++;
1887 ret += obuf->len;
1888 len -= obuf->len;
1889 i++;
1890 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001891
Jens Axboe02cf01a2008-02-20 10:34:51 +01001892 /*
1893 * return EAGAIN if we have the potential of some data in the
1894 * future, otherwise just return 0
1895 */
1896 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1897 ret = -EAGAIN;
1898
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001899 pipe_unlock(ipipe);
1900 pipe_unlock(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001901
Jens Axboeaadd06e2006-07-10 11:00:01 +02001902 /*
1903 * If we put data in the output pipe, wakeup any potential readers.
1904 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001905 if (ret > 0)
1906 wakeup_pipe_readers(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001907
1908 return ret;
1909}
1910
1911/*
1912 * This is a tee(1) implementation that works on pipes. It doesn't copy
1913 * any data, it simply references the 'in' pages on the 'out' pipe.
1914 * The 'flags' used are the SPLICE_F_* variants, currently the only
1915 * applicable one is SPLICE_F_NONBLOCK.
1916 */
1917static long do_tee(struct file *in, struct file *out, size_t len,
1918 unsigned int flags)
1919{
Linus Torvalds71993e62010-11-28 13:56:09 -08001920 struct pipe_inode_info *ipipe = get_pipe_info(in);
1921 struct pipe_inode_info *opipe = get_pipe_info(out);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001922 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001923
1924 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001925 * Duplicate the contents of ipipe to opipe without actually
1926 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001927 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001928 if (ipipe && opipe && ipipe != opipe) {
1929 /*
1930 * Keep going, unless we encounter an error. The ipipe/opipe
1931 * ordering doesn't really matter.
1932 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001933 ret = ipipe_prep(ipipe, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001934 if (!ret) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001935 ret = opipe_prep(opipe, flags);
Jens Axboe02cf01a2008-02-20 10:34:51 +01001936 if (!ret)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001937 ret = link_pipe(ipipe, opipe, len, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001938 }
1939 }
Jens Axboe70524492006-04-11 15:51:17 +02001940
Jens Axboeaadd06e2006-07-10 11:00:01 +02001941 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001942}
1943
Heiko Carstens836f92a2009-01-14 14:14:33 +01001944SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
Jens Axboe70524492006-04-11 15:51:17 +02001945{
Al Viro2903ff02012-08-28 12:52:22 -04001946 struct fd in;
1947 int error;
Jens Axboe70524492006-04-11 15:51:17 +02001948
1949 if (unlikely(!len))
1950 return 0;
1951
1952 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001953 in = fdget(fdin);
1954 if (in.file) {
1955 if (in.file->f_mode & FMODE_READ) {
1956 struct fd out = fdget(fdout);
1957 if (out.file) {
1958 if (out.file->f_mode & FMODE_WRITE)
1959 error = do_tee(in.file, out.file,
1960 len, flags);
1961 fdput(out);
Jens Axboe70524492006-04-11 15:51:17 +02001962 }
1963 }
Al Viro2903ff02012-08-28 12:52:22 -04001964 fdput(in);
Jens Axboe70524492006-04-11 15:51:17 +02001965 }
1966
1967 return error;
1968}