blob: 58c322a87f44274b10c3d99f6a0efb0b8248b1fb [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;
Jens Axboe5274f052006-03-30 15:15:30 +0200206
Al Viro8924fef2016-09-17 20:44:45 -0400207 pipe->nrbufs++;
208 page_nr++;
209 ret += buf->len;
210
211 if (!--spd->nr_pages)
212 break;
213 }
214
215 if (!ret)
216 ret = -EAGAIN;
217
218out:
Jens Axboe00de00b2007-06-15 13:14:22 +0200219 while (page_nr < spd_pages)
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800220 spd->spd_release(spd, page_nr++);
Jens Axboe5274f052006-03-30 15:15:30 +0200221
222 return ret;
223}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200224EXPORT_SYMBOL_GPL(splice_to_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200225
Al Viro79fddc42016-09-17 22:38:20 -0400226ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
227{
228 int ret;
229
230 if (unlikely(!pipe->readers)) {
231 send_sig(SIGPIPE, current, 0);
232 ret = -EPIPE;
233 } else if (pipe->nrbufs == pipe->buffers) {
234 ret = -EAGAIN;
235 } else {
236 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
237 pipe->bufs[newbuf] = *buf;
238 pipe->nrbufs++;
239 return buf->len;
240 }
241 buf->ops->release(pipe, buf);
242 buf->ops = NULL;
243 return ret;
244}
245EXPORT_SYMBOL(add_to_pipe);
246
Hugh Dickins708e3502011-07-25 17:12:32 -0700247void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800248{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 put_page(spd->pages[i]);
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800250}
251
Jens Axboe35f3d142010-05-20 10:43:18 +0200252/*
253 * Check if we need to grow the arrays holding pages and partial page
254 * descriptions.
255 */
Eric Dumazet047fe362012-06-12 15:24:40 +0200256int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200257{
Eric Dumazet047fe362012-06-12 15:24:40 +0200258 unsigned int buffers = ACCESS_ONCE(pipe->buffers);
259
260 spd->nr_pages_max = buffers;
261 if (buffers <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200262 return 0;
263
Eric Dumazet047fe362012-06-12 15:24:40 +0200264 spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
265 spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
Jens Axboe35f3d142010-05-20 10:43:18 +0200266
267 if (spd->pages && spd->partial)
268 return 0;
269
270 kfree(spd->pages);
271 kfree(spd->partial);
272 return -ENOMEM;
273}
274
Eric Dumazet047fe362012-06-12 15:24:40 +0200275void splice_shrink_spd(struct splice_pipe_desc *spd)
Jens Axboe35f3d142010-05-20 10:43:18 +0200276{
Eric Dumazet047fe362012-06-12 15:24:40 +0200277 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
Jens Axboe35f3d142010-05-20 10:43:18 +0200278 return;
279
280 kfree(spd->pages);
281 kfree(spd->partial);
282}
283
Jens Axboe83f91352006-04-02 23:05:09 +0200284/**
285 * generic_file_splice_read - splice data from file to a pipe
286 * @in: file to splice from
Jens Axboe932cc6d2007-06-21 13:10:21 +0200287 * @ppos: position in @in
Jens Axboe83f91352006-04-02 23:05:09 +0200288 * @pipe: pipe to splice to
289 * @len: number of bytes to splice
290 * @flags: splice modifier flags
291 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200292 * Description:
293 * Will read pages from given file and fill them into a pipe. Can be
Al Viro82c156f2016-09-22 23:35:42 -0400294 * used as long as it has more or less sane ->read_iter().
Jens Axboe932cc6d2007-06-21 13:10:21 +0200295 *
Jens Axboe83f91352006-04-02 23:05:09 +0200296 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200297ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
298 struct pipe_inode_info *pipe, size_t len,
299 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200300{
Al Viro82c156f2016-09-22 23:35:42 -0400301 struct iov_iter to;
302 struct kiocb kiocb;
303 loff_t isize;
304 int idx, ret;
Boaz Harroshbe64f882015-04-15 16:15:17 -0700305
Jens Axboed366d3982007-06-01 14:54:11 +0200306 isize = i_size_read(in->f_mapping->host);
307 if (unlikely(*ppos >= isize))
308 return 0;
309
Al Viro82c156f2016-09-22 23:35:42 -0400310 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len);
311 idx = to.idx;
312 init_sync_kiocb(&kiocb, in);
313 kiocb.ki_pos = *ppos;
314 ret = in->f_op->read_iter(&kiocb, &to);
Miklos Szeredi723590e2009-08-15 08:43:22 +0200315 if (ret > 0) {
Al Viro82c156f2016-09-22 23:35:42 -0400316 *ppos = kiocb.ki_pos;
Miklos Szeredi723590e2009-08-15 08:43:22 +0200317 file_accessed(in);
Al Viro82c156f2016-09-22 23:35:42 -0400318 } else if (ret < 0) {
319 if (WARN_ON(to.idx != idx || to.iov_offset)) {
320 /*
321 * a bogus ->read_iter() has copied something and still
322 * returned an error instead of a short read.
323 */
324 to.idx = idx;
325 to.iov_offset = 0;
326 iov_iter_advance(&to, 0); /* to free what was emitted */
327 }
328 /*
329 * callers of ->splice_read() expect -EAGAIN on
330 * "can't put anything in there", rather than -EFAULT.
331 */
332 if (ret == -EFAULT)
333 ret = -EAGAIN;
Miklos Szeredi723590e2009-08-15 08:43:22 +0200334 }
Jens Axboe5274f052006-03-30 15:15:30 +0200335
336 return ret;
337}
Jens Axboe059a8f32006-04-02 23:06:05 +0200338EXPORT_SYMBOL(generic_file_splice_read);
339
Al Viro241699c2016-09-22 16:33:12 -0400340const struct pipe_buf_operations default_pipe_buf_ops = {
Miklos Szeredi68181732009-05-07 15:37:36 +0200341 .can_merge = 0,
Miklos Szeredi68181732009-05-07 15:37:36 +0200342 .confirm = generic_pipe_buf_confirm,
343 .release = generic_pipe_buf_release,
344 .steal = generic_pipe_buf_steal,
345 .get = generic_pipe_buf_get,
346};
347
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100348static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
349 struct pipe_buffer *buf)
350{
351 return 1;
352}
353
354/* Pipe buffer operations for a socket and similar. */
355const struct pipe_buf_operations nosteal_pipe_buf_ops = {
356 .can_merge = 0,
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100357 .confirm = generic_pipe_buf_confirm,
358 .release = generic_pipe_buf_release,
359 .steal = generic_pipe_buf_nosteal,
360 .get = generic_pipe_buf_get,
361};
362EXPORT_SYMBOL(nosteal_pipe_buf_ops);
363
Miklos Szeredi68181732009-05-07 15:37:36 +0200364static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
365 unsigned long vlen, loff_t offset)
366{
367 mm_segment_t old_fs;
368 loff_t pos = offset;
369 ssize_t res;
370
371 old_fs = get_fs();
372 set_fs(get_ds());
373 /* The cast to a user pointer is valid due to the set_fs() */
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100374 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
Miklos Szeredi68181732009-05-07 15:37:36 +0200375 set_fs(old_fs);
376
377 return res;
378}
379
Al Viro7bb307e2013-02-23 14:51:48 -0500380ssize_t kernel_write(struct file *file, const char *buf, size_t count,
Miklos Szeredib2858d72009-05-19 11:37:46 +0200381 loff_t pos)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200382{
383 mm_segment_t old_fs;
384 ssize_t res;
385
386 old_fs = get_fs();
387 set_fs(get_ds());
388 /* The cast to a user pointer is valid due to the set_fs() */
Al Viro7bb307e2013-02-23 14:51:48 -0500389 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200390 set_fs(old_fs);
391
392 return res;
393}
Al Viro7bb307e2013-02-23 14:51:48 -0500394EXPORT_SYMBOL(kernel_write);
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200395
Al Viro82c156f2016-09-22 23:35:42 -0400396static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
Miklos Szeredi68181732009-05-07 15:37:36 +0200397 struct pipe_inode_info *pipe, size_t len,
398 unsigned int flags)
399{
400 unsigned int nr_pages;
401 unsigned int nr_freed;
402 size_t offset;
Jens Axboe35f3d142010-05-20 10:43:18 +0200403 struct page *pages[PIPE_DEF_BUFFERS];
404 struct partial_page partial[PIPE_DEF_BUFFERS];
405 struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
Miklos Szeredi68181732009-05-07 15:37:36 +0200406 ssize_t res;
407 size_t this_len;
408 int error;
409 int i;
410 struct splice_pipe_desc spd = {
411 .pages = pages,
412 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +0200413 .nr_pages_max = PIPE_DEF_BUFFERS,
Miklos Szeredi68181732009-05-07 15:37:36 +0200414 .flags = flags,
415 .ops = &default_pipe_buf_ops,
416 .spd_release = spd_release_page,
417 };
418
Jens Axboe35f3d142010-05-20 10:43:18 +0200419 if (splice_grow_spd(pipe, &spd))
420 return -ENOMEM;
421
422 res = -ENOMEM;
423 vec = __vec;
Eric Dumazet047fe362012-06-12 15:24:40 +0200424 if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
425 vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
Jens Axboe35f3d142010-05-20 10:43:18 +0200426 if (!vec)
427 goto shrink_ret;
428 }
429
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300430 offset = *ppos & ~PAGE_MASK;
431 nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi68181732009-05-07 15:37:36 +0200432
Eric Dumazet047fe362012-06-12 15:24:40 +0200433 for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
Miklos Szeredi68181732009-05-07 15:37:36 +0200434 struct page *page;
435
Jens Axboe4f231222009-05-13 08:35:35 +0200436 page = alloc_page(GFP_USER);
Miklos Szeredi68181732009-05-07 15:37:36 +0200437 error = -ENOMEM;
438 if (!page)
439 goto err;
440
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300441 this_len = min_t(size_t, len, PAGE_SIZE - offset);
Jens Axboe4f231222009-05-13 08:35:35 +0200442 vec[i].iov_base = (void __user *) page_address(page);
Miklos Szeredi68181732009-05-07 15:37:36 +0200443 vec[i].iov_len = this_len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200444 spd.pages[i] = page;
Miklos Szeredi68181732009-05-07 15:37:36 +0200445 spd.nr_pages++;
446 len -= this_len;
447 offset = 0;
448 }
449
450 res = kernel_readv(in, vec, spd.nr_pages, *ppos);
Andrew Morton77f6bf52009-05-14 09:49:44 +0200451 if (res < 0) {
452 error = res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200453 goto err;
Andrew Morton77f6bf52009-05-14 09:49:44 +0200454 }
Miklos Szeredi68181732009-05-07 15:37:36 +0200455
456 error = 0;
457 if (!res)
458 goto err;
459
460 nr_freed = 0;
461 for (i = 0; i < spd.nr_pages; i++) {
Miklos Szeredi68181732009-05-07 15:37:36 +0200462 this_len = min_t(size_t, vec[i].iov_len, res);
Jens Axboe35f3d142010-05-20 10:43:18 +0200463 spd.partial[i].offset = 0;
464 spd.partial[i].len = this_len;
Miklos Szeredi68181732009-05-07 15:37:36 +0200465 if (!this_len) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200466 __free_page(spd.pages[i]);
467 spd.pages[i] = NULL;
Miklos Szeredi68181732009-05-07 15:37:36 +0200468 nr_freed++;
469 }
470 res -= this_len;
471 }
472 spd.nr_pages -= nr_freed;
473
474 res = splice_to_pipe(pipe, &spd);
475 if (res > 0)
476 *ppos += res;
477
Jens Axboe35f3d142010-05-20 10:43:18 +0200478shrink_ret:
479 if (vec != __vec)
480 kfree(vec);
Eric Dumazet047fe362012-06-12 15:24:40 +0200481 splice_shrink_spd(&spd);
Miklos Szeredi68181732009-05-07 15:37:36 +0200482 return res;
483
484err:
Jens Axboe4f231222009-05-13 08:35:35 +0200485 for (i = 0; i < spd.nr_pages; i++)
Jens Axboe35f3d142010-05-20 10:43:18 +0200486 __free_page(spd.pages[i]);
Jens Axboe4f231222009-05-13 08:35:35 +0200487
Jens Axboe35f3d142010-05-20 10:43:18 +0200488 res = error;
489 goto shrink_ret;
Miklos Szeredi68181732009-05-07 15:37:36 +0200490}
Miklos Szeredi68181732009-05-07 15:37:36 +0200491
Jens Axboe5274f052006-03-30 15:15:30 +0200492/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200493 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200494 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200495 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200496static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200497 struct pipe_buffer *buf, struct splice_desc *sd)
498{
Jens Axboe6a14b902007-06-14 13:08:55 +0200499 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200500 loff_t pos = sd->pos;
Michał Mirosława8adbe32010-12-17 08:56:44 +0100501 int more;
Jens Axboe5274f052006-03-30 15:15:30 +0200502
Al Viro72c2d532013-09-22 16:27:52 -0400503 if (!likely(file->f_op->sendpage))
Michał Mirosława8adbe32010-12-17 08:56:44 +0100504 return -EINVAL;
Jens Axboe5274f052006-03-30 15:15:30 +0200505
Eric Dumazet35f9c092012-04-05 03:05:35 +0000506 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000507
508 if (sd->len < sd->total_len && pipe->nrbufs > 1)
Eric Dumazet35f9c092012-04-05 03:05:35 +0000509 more |= MSG_SENDPAGE_NOTLAST;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000510
Michał Mirosława8adbe32010-12-17 08:56:44 +0100511 return file->f_op->sendpage(file, buf->page, buf->offset,
512 sd->len, &pos, more);
Jens Axboe5274f052006-03-30 15:15:30 +0200513}
514
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200515static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
516{
517 smp_mb();
518 if (waitqueue_active(&pipe->wait))
519 wake_up_interruptible(&pipe->wait);
520 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
521}
522
523/**
524 * splice_from_pipe_feed - feed available data from a pipe to a file
525 * @pipe: pipe to splice from
526 * @sd: information to @actor
527 * @actor: handler that splices the data
528 *
529 * Description:
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200530 * This function loops over the pipe and calls @actor to do the
531 * actual moving of a single struct pipe_buffer to the desired
532 * destination. It returns when there's no more buffers left in
533 * the pipe or if the requested number of bytes (@sd->total_len)
534 * have been copied. It returns a positive number (one) if the
535 * pipe needs to be filled with more data, zero if the required
536 * number of bytes have been copied and -errno on error.
537 *
538 * This, together with splice_from_pipe_{begin,end,next}, may be
539 * used to implement the functionality of __splice_from_pipe() when
540 * locking is required around copying the pipe buffers to the
541 * destination.
542 */
Al Viro96f9bc82014-04-05 04:35:49 -0400543static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200544 splice_actor *actor)
545{
546 int ret;
547
548 while (pipe->nrbufs) {
549 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
550 const struct pipe_buf_operations *ops = buf->ops;
551
552 sd->len = buf->len;
553 if (sd->len > sd->total_len)
554 sd->len = sd->total_len;
555
Michał Mirosława8adbe32010-12-17 08:56:44 +0100556 ret = buf->ops->confirm(pipe, buf);
557 if (unlikely(ret)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200558 if (ret == -ENODATA)
559 ret = 0;
560 return ret;
561 }
Michał Mirosława8adbe32010-12-17 08:56:44 +0100562
563 ret = actor(pipe, buf, sd);
564 if (ret <= 0)
565 return ret;
566
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200567 buf->offset += ret;
568 buf->len -= ret;
569
570 sd->num_spliced += ret;
571 sd->len -= ret;
572 sd->pos += ret;
573 sd->total_len -= ret;
574
575 if (!buf->len) {
576 buf->ops = NULL;
577 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200578 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200579 pipe->nrbufs--;
Al Viro6447a3c2013-03-21 11:01:38 -0400580 if (pipe->files)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200581 sd->need_wakeup = true;
582 }
583
584 if (!sd->total_len)
585 return 0;
586 }
587
588 return 1;
589}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200590
591/**
592 * splice_from_pipe_next - wait for some data to splice from
593 * @pipe: pipe to splice from
594 * @sd: information about the splice operation
595 *
596 * Description:
597 * This function will wait for some data and return a positive
598 * value (one) if pipe buffers are available. It will return zero
599 * or -errno if no more data needs to be spliced.
600 */
Al Viro96f9bc82014-04-05 04:35:49 -0400601static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200602{
Jan Karac725bfc2015-11-23 13:09:50 +0100603 /*
604 * Check for signal early to make process killable when there are
605 * always buffers available
606 */
607 if (signal_pending(current))
608 return -ERESTARTSYS;
609
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200610 while (!pipe->nrbufs) {
611 if (!pipe->writers)
612 return 0;
613
614 if (!pipe->waiting_writers && sd->num_spliced)
615 return 0;
616
617 if (sd->flags & SPLICE_F_NONBLOCK)
618 return -EAGAIN;
619
620 if (signal_pending(current))
621 return -ERESTARTSYS;
622
623 if (sd->need_wakeup) {
624 wakeup_pipe_writers(pipe);
625 sd->need_wakeup = false;
626 }
627
628 pipe_wait(pipe);
629 }
630
631 return 1;
632}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200633
634/**
635 * splice_from_pipe_begin - start splicing from pipe
Randy Dunlapb80901b2009-04-16 19:09:55 -0700636 * @sd: information about the splice operation
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200637 *
638 * Description:
639 * This function should be called before a loop containing
640 * splice_from_pipe_next() and splice_from_pipe_feed() to
641 * initialize the necessary fields of @sd.
642 */
Al Viro96f9bc82014-04-05 04:35:49 -0400643static void splice_from_pipe_begin(struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200644{
645 sd->num_spliced = 0;
646 sd->need_wakeup = false;
647}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200648
649/**
650 * splice_from_pipe_end - finish splicing from pipe
651 * @pipe: pipe to splice from
652 * @sd: information about the splice operation
653 *
654 * Description:
655 * This function will wake up pipe writers if necessary. It should
656 * be called after a loop containing splice_from_pipe_next() and
657 * splice_from_pipe_feed().
658 */
Al Viro96f9bc82014-04-05 04:35:49 -0400659static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200660{
661 if (sd->need_wakeup)
662 wakeup_pipe_writers(pipe);
663}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200664
Jens Axboe932cc6d2007-06-21 13:10:21 +0200665/**
666 * __splice_from_pipe - splice data from a pipe to given actor
667 * @pipe: pipe to splice from
668 * @sd: information to @actor
669 * @actor: handler that splices the data
670 *
671 * Description:
672 * This function does little more than loop over the pipe and call
673 * @actor to do the actual moving of a single struct pipe_buffer to
674 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
675 * pipe_to_user.
676 *
Jens Axboe83f91352006-04-02 23:05:09 +0200677 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200678ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
679 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200680{
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200681 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200682
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200683 splice_from_pipe_begin(sd);
684 do {
Jan Karac2489e02015-11-23 13:09:51 +0100685 cond_resched();
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200686 ret = splice_from_pipe_next(pipe, sd);
687 if (ret > 0)
688 ret = splice_from_pipe_feed(pipe, sd, actor);
689 } while (ret > 0);
690 splice_from_pipe_end(pipe, sd);
Jens Axboe5274f052006-03-30 15:15:30 +0200691
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200692 return sd->num_spliced ? sd->num_spliced : ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200693}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100694EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200695
Jens Axboe932cc6d2007-06-21 13:10:21 +0200696/**
697 * splice_from_pipe - splice data from a pipe to a file
698 * @pipe: pipe to splice from
699 * @out: file to splice to
700 * @ppos: position in @out
701 * @len: how many bytes to splice
702 * @flags: splice modifier flags
703 * @actor: handler that splices the data
704 *
705 * Description:
Miklos Szeredi29339702009-04-14 19:48:37 +0200706 * See __splice_from_pipe. This function locks the pipe inode,
Jens Axboe932cc6d2007-06-21 13:10:21 +0200707 * otherwise it's identical to __splice_from_pipe().
708 *
709 */
Mark Fasheh6da61802006-10-17 18:43:07 +0200710ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
711 loff_t *ppos, size_t len, unsigned int flags,
712 splice_actor *actor)
713{
714 ssize_t ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200715 struct splice_desc sd = {
716 .total_len = len,
717 .flags = flags,
718 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200719 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200720 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200721
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200722 pipe_lock(pipe);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200723 ret = __splice_from_pipe(pipe, &sd, actor);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200724 pipe_unlock(pipe);
Mark Fasheh6da61802006-10-17 18:43:07 +0200725
726 return ret;
727}
728
729/**
Al Viro8d020762014-04-05 04:27:08 -0400730 * iter_file_splice_write - splice data from a pipe to a file
731 * @pipe: pipe info
732 * @out: file to write to
733 * @ppos: position in @out
734 * @len: number of bytes to splice
735 * @flags: splice modifier flags
736 *
737 * Description:
738 * Will either move or copy pages (determined by @flags options) from
739 * the given pipe inode to the given file.
740 * This one is ->write_iter-based.
741 *
742 */
743ssize_t
744iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
745 loff_t *ppos, size_t len, unsigned int flags)
746{
747 struct splice_desc sd = {
748 .total_len = len,
749 .flags = flags,
750 .pos = *ppos,
751 .u.file = out,
752 };
753 int nbufs = pipe->buffers;
754 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
755 GFP_KERNEL);
756 ssize_t ret;
757
758 if (unlikely(!array))
759 return -ENOMEM;
760
761 pipe_lock(pipe);
762
763 splice_from_pipe_begin(&sd);
764 while (sd.total_len) {
765 struct iov_iter from;
Al Viro8d020762014-04-05 04:27:08 -0400766 size_t left;
767 int n, idx;
768
769 ret = splice_from_pipe_next(pipe, &sd);
770 if (ret <= 0)
771 break;
772
773 if (unlikely(nbufs < pipe->buffers)) {
774 kfree(array);
775 nbufs = pipe->buffers;
776 array = kcalloc(nbufs, sizeof(struct bio_vec),
777 GFP_KERNEL);
778 if (!array) {
779 ret = -ENOMEM;
780 break;
781 }
782 }
783
784 /* build the vector */
785 left = sd.total_len;
786 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
787 struct pipe_buffer *buf = pipe->bufs + idx;
788 size_t this_len = buf->len;
789
790 if (this_len > left)
791 this_len = left;
792
793 if (idx == pipe->buffers - 1)
794 idx = -1;
795
796 ret = buf->ops->confirm(pipe, buf);
797 if (unlikely(ret)) {
798 if (ret == -ENODATA)
799 ret = 0;
800 goto done;
801 }
802
803 array[n].bv_page = buf->page;
804 array[n].bv_len = this_len;
805 array[n].bv_offset = buf->offset;
806 left -= this_len;
807 }
808
Al Viro05afcb72015-01-23 01:08:07 -0500809 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
810 sd.total_len - left);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100811 ret = vfs_iter_write(out, &from, &sd.pos);
Al Viro8d020762014-04-05 04:27:08 -0400812 if (ret <= 0)
813 break;
814
815 sd.num_spliced += ret;
816 sd.total_len -= ret;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100817 *ppos = sd.pos;
Al Viro8d020762014-04-05 04:27:08 -0400818
819 /* dismiss the fully eaten buffers, adjust the partial one */
820 while (ret) {
821 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
822 if (ret >= buf->len) {
823 const struct pipe_buf_operations *ops = buf->ops;
824 ret -= buf->len;
825 buf->len = 0;
826 buf->ops = NULL;
827 ops->release(pipe, buf);
828 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
829 pipe->nrbufs--;
830 if (pipe->files)
831 sd.need_wakeup = true;
832 } else {
833 buf->offset += ret;
834 buf->len -= ret;
835 ret = 0;
836 }
837 }
838 }
839done:
840 kfree(array);
841 splice_from_pipe_end(pipe, &sd);
842
843 pipe_unlock(pipe);
844
845 if (sd.num_spliced)
846 ret = sd.num_spliced;
847
848 return ret;
849}
850
851EXPORT_SYMBOL(iter_file_splice_write);
852
Miklos Szeredib2858d72009-05-19 11:37:46 +0200853static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
854 struct splice_desc *sd)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200855{
Miklos Szeredib2858d72009-05-19 11:37:46 +0200856 int ret;
857 void *data;
Al Viro06ae43f2013-03-20 13:19:30 -0400858 loff_t tmp = sd->pos;
Miklos Szeredib2858d72009-05-19 11:37:46 +0200859
Al Virofbb32752014-02-02 21:09:54 -0500860 data = kmap(buf->page);
Al Viro06ae43f2013-03-20 13:19:30 -0400861 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
Al Virofbb32752014-02-02 21:09:54 -0500862 kunmap(buf->page);
Miklos Szeredib2858d72009-05-19 11:37:46 +0200863
864 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200865}
866
867static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
868 struct file *out, loff_t *ppos,
869 size_t len, unsigned int flags)
870{
Miklos Szeredib2858d72009-05-19 11:37:46 +0200871 ssize_t ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200872
Miklos Szeredib2858d72009-05-19 11:37:46 +0200873 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
874 if (ret > 0)
875 *ppos += ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200876
Miklos Szeredib2858d72009-05-19 11:37:46 +0200877 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200878}
879
Jens Axboe83f91352006-04-02 23:05:09 +0200880/**
881 * generic_splice_sendpage - splice data from a pipe to a socket
Jens Axboe932cc6d2007-06-21 13:10:21 +0200882 * @pipe: pipe to splice from
Jens Axboe83f91352006-04-02 23:05:09 +0200883 * @out: socket to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +0200884 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +0200885 * @len: number of bytes to splice
886 * @flags: splice modifier flags
887 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200888 * Description:
889 * Will send @len bytes from the pipe to a network socket. No data copying
890 * is involved.
Jens Axboe83f91352006-04-02 23:05:09 +0200891 *
892 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200893ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200894 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200895{
Jens Axboe00522fb2006-04-26 14:39:29 +0200896 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200897}
898
Jens Axboe059a8f32006-04-02 23:06:05 +0200899EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500900
Jens Axboe83f91352006-04-02 23:05:09 +0200901/*
902 * Attempt to initiate a splice from pipe to file.
903 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200904static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200905 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200906{
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200907 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
908 loff_t *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +0200909
Al Viro72c2d532013-09-22 16:27:52 -0400910 if (out->f_op->splice_write)
Changli Gaocc56f7d2009-11-04 09:09:52 +0100911 splice_write = out->f_op->splice_write;
912 else
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200913 splice_write = default_file_splice_write;
914
Al Viro500368f2013-05-23 20:07:11 -0400915 return splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200916}
917
Jens Axboe83f91352006-04-02 23:05:09 +0200918/*
919 * Attempt to initiate a splice from a file to a pipe.
920 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200921static long do_splice_to(struct file *in, loff_t *ppos,
922 struct pipe_inode_info *pipe, size_t len,
923 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200924{
Miklos Szeredi68181732009-05-07 15:37:36 +0200925 ssize_t (*splice_read)(struct file *, loff_t *,
926 struct pipe_inode_info *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +0200927 int ret;
928
Jens Axboe49570e92006-04-11 13:56:09 +0200929 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200930 return -EBADF;
931
Jens Axboecbb7e572006-04-11 14:57:50 +0200932 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200933 if (unlikely(ret < 0))
934 return ret;
935
Al Viro03cc0782016-04-02 14:56:58 -0400936 if (unlikely(len > MAX_RW_COUNT))
937 len = MAX_RW_COUNT;
938
Al Viro72c2d532013-09-22 16:27:52 -0400939 if (in->f_op->splice_read)
Changli Gaocc56f7d2009-11-04 09:09:52 +0100940 splice_read = in->f_op->splice_read;
941 else
Miklos Szeredi68181732009-05-07 15:37:36 +0200942 splice_read = default_file_splice_read;
943
944 return splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200945}
946
Jens Axboe932cc6d2007-06-21 13:10:21 +0200947/**
948 * splice_direct_to_actor - splices data directly between two non-pipes
949 * @in: file to splice from
950 * @sd: actor information on where to splice to
951 * @actor: handles the data splicing
952 *
953 * Description:
954 * This is a special case helper to splice directly between two
955 * points, without requiring an explicit pipe. Internally an allocated
Randy Dunlap79685b82007-07-27 08:08:51 +0200956 * pipe is cached in the process, and reused during the lifetime of
Jens Axboe932cc6d2007-06-21 13:10:21 +0200957 * that process.
958 *
Jens Axboec66ab6f2007-06-12 21:17:17 +0200959 */
960ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
961 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +0200962{
963 struct pipe_inode_info *pipe;
964 long ret, bytes;
965 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200966 size_t len;
Christophe Leroy0ff28d92015-05-06 17:26:47 +0200967 int i, flags, more;
Jens Axboeb92ce552006-04-11 13:52:07 +0200968
969 /*
970 * We require the input being a regular file, as we don't want to
971 * randomly drop data for eg socket -> socket splicing. Use the
972 * piped splicing for that!
973 */
Al Viro496ad9a2013-01-23 17:07:38 -0500974 i_mode = file_inode(in)->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +0200975 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
976 return -EINVAL;
977
978 /*
979 * neither in nor out is a pipe, setup an internal pipe attached to
980 * 'out' and transfer the wanted data from 'in' to 'out' through that
981 */
982 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200983 if (unlikely(!pipe)) {
Al Viro7bee1302013-03-21 11:04:15 -0400984 pipe = alloc_pipe_info();
Jens Axboeb92ce552006-04-11 13:52:07 +0200985 if (!pipe)
986 return -ENOMEM;
987
988 /*
989 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200990 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200991 * PIPE_READERS appropriately.
992 */
993 pipe->readers = 1;
994
995 current->splice_pipe = pipe;
996 }
997
998 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200999 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +02001000 */
1001 ret = 0;
1002 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001003 len = sd->total_len;
1004 flags = sd->flags;
1005
1006 /*
1007 * Don't block on output, we have to drain the direct pipe.
1008 */
1009 sd->flags &= ~SPLICE_F_NONBLOCK;
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001010 more = sd->flags & SPLICE_F_MORE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001011
1012 while (len) {
Jens Axboe51a92c02007-07-13 14:11:43 +02001013 size_t read_len;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001014 loff_t pos = sd->pos, prev_pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001015
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001016 ret = do_splice_to(in, &pos, pipe, len, flags);
Jens Axboe51a92c02007-07-13 14:11:43 +02001017 if (unlikely(ret <= 0))
Jens Axboeb92ce552006-04-11 13:52:07 +02001018 goto out_release;
1019
1020 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001021 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +02001022
1023 /*
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001024 * If more data is pending, set SPLICE_F_MORE
1025 * If this is the last data and SPLICE_F_MORE was not set
1026 * initially, clears it.
1027 */
1028 if (read_len < len)
1029 sd->flags |= SPLICE_F_MORE;
1030 else if (!more)
1031 sd->flags &= ~SPLICE_F_MORE;
1032 /*
Jens Axboeb92ce552006-04-11 13:52:07 +02001033 * NOTE: nonblocking mode only applies to the input. We
1034 * must not do the output in nonblocking mode as then we
1035 * could get stuck data in the internal pipe:
1036 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001037 ret = actor(pipe, sd);
Tom Zanussia82c53a2008-05-09 13:28:36 +02001038 if (unlikely(ret <= 0)) {
1039 sd->pos = prev_pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001040 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001041 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001042
1043 bytes += ret;
1044 len -= ret;
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001045 sd->pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001046
Tom Zanussia82c53a2008-05-09 13:28:36 +02001047 if (ret < read_len) {
1048 sd->pos = prev_pos + ret;
Jens Axboe51a92c02007-07-13 14:11:43 +02001049 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001050 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001051 }
1052
Jens Axboe9e971982008-01-29 21:05:57 +01001053done:
Jens Axboeb92ce552006-04-11 13:52:07 +02001054 pipe->nrbufs = pipe->curbuf = 0;
Jens Axboe80848702008-01-30 12:24:48 +01001055 file_accessed(in);
Jens Axboeb92ce552006-04-11 13:52:07 +02001056 return bytes;
1057
1058out_release:
1059 /*
1060 * If we did an incomplete transfer we must release
1061 * the pipe buffers in question:
1062 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001063 for (i = 0; i < pipe->buffers; i++) {
Jens Axboeb92ce552006-04-11 13:52:07 +02001064 struct pipe_buffer *buf = pipe->bufs + i;
1065
1066 if (buf->ops) {
1067 buf->ops->release(pipe, buf);
1068 buf->ops = NULL;
1069 }
1070 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001071
Jens Axboe9e971982008-01-29 21:05:57 +01001072 if (!bytes)
1073 bytes = ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001074
Jens Axboe9e971982008-01-29 21:05:57 +01001075 goto done;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001076}
1077EXPORT_SYMBOL(splice_direct_to_actor);
1078
1079static int direct_splice_actor(struct pipe_inode_info *pipe,
1080 struct splice_desc *sd)
1081{
Jens Axboe6a14b902007-06-14 13:08:55 +02001082 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001083
Al Viro7995bd22013-06-20 18:58:36 +04001084 return do_splice_from(pipe, file, sd->opos, sd->total_len,
Changli Gao2cb4b052010-06-29 13:09:18 +02001085 sd->flags);
Jens Axboec66ab6f2007-06-12 21:17:17 +02001086}
1087
Jens Axboe932cc6d2007-06-21 13:10:21 +02001088/**
1089 * do_splice_direct - splices data directly between two files
1090 * @in: file to splice from
1091 * @ppos: input file offset
1092 * @out: file to splice to
Randy Dunlapacdb37c2013-06-22 19:44:08 -07001093 * @opos: output file offset
Jens Axboe932cc6d2007-06-21 13:10:21 +02001094 * @len: number of bytes to splice
1095 * @flags: splice modifier flags
1096 *
1097 * Description:
1098 * For use by do_sendfile(). splice can easily emulate sendfile, but
1099 * doing it in the application would incur an extra system call
1100 * (splice in + splice out, as compared to just sendfile()). So this helper
1101 * can splice directly through a process-private pipe.
1102 *
1103 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001104long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
Al Viro7995bd22013-06-20 18:58:36 +04001105 loff_t *opos, size_t len, unsigned int flags)
Jens Axboec66ab6f2007-06-12 21:17:17 +02001106{
1107 struct splice_desc sd = {
1108 .len = len,
1109 .total_len = len,
1110 .flags = flags,
1111 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001112 .u.file = out,
Al Viro7995bd22013-06-20 18:58:36 +04001113 .opos = opos,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001114 };
Jens Axboe51a92c02007-07-13 14:11:43 +02001115 long ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001116
Al Viro18c67cb2013-06-19 15:41:54 +04001117 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1118 return -EBADF;
1119
1120 if (unlikely(out->f_flags & O_APPEND))
1121 return -EINVAL;
1122
1123 ret = rw_verify_area(WRITE, out, opos, len);
1124 if (unlikely(ret < 0))
1125 return ret;
1126
Jens Axboec66ab6f2007-06-12 21:17:17 +02001127 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
Jens Axboe51a92c02007-07-13 14:11:43 +02001128 if (ret > 0)
Tom Zanussia82c53a2008-05-09 13:28:36 +02001129 *ppos = sd.pos;
Jens Axboe51a92c02007-07-13 14:11:43 +02001130
Jens Axboec66ab6f2007-06-12 21:17:17 +02001131 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001132}
Miklos Szeredi1c118592014-10-24 00:14:35 +02001133EXPORT_SYMBOL(do_splice_direct);
Jens Axboeb92ce552006-04-11 13:52:07 +02001134
Al Viro8924fef2016-09-17 20:44:45 -04001135static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1136{
1137 while (pipe->nrbufs == pipe->buffers) {
1138 if (flags & SPLICE_F_NONBLOCK)
1139 return -EAGAIN;
1140 if (signal_pending(current))
1141 return -ERESTARTSYS;
1142 pipe->waiting_writers++;
1143 pipe_wait(pipe);
1144 pipe->waiting_writers--;
1145 }
1146 return 0;
1147}
1148
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001149static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1150 struct pipe_inode_info *opipe,
1151 size_t len, unsigned int flags);
Jens Axboeddac0d32006-11-04 12:49:32 +01001152
1153/*
Jens Axboe83f91352006-04-02 23:05:09 +02001154 * Determine where to splice to/from.
1155 */
Ingo Molnar529565dc2006-04-10 15:18:58 +02001156static long do_splice(struct file *in, loff_t __user *off_in,
1157 struct file *out, loff_t __user *off_out,
1158 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001159{
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001160 struct pipe_inode_info *ipipe;
1161 struct pipe_inode_info *opipe;
Al Viro7995bd22013-06-20 18:58:36 +04001162 loff_t offset;
Jens Axboea4514eb2006-04-19 15:57:05 +02001163 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001164
Linus Torvalds71993e62010-11-28 13:56:09 -08001165 ipipe = get_pipe_info(in);
1166 opipe = get_pipe_info(out);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001167
1168 if (ipipe && opipe) {
1169 if (off_in || off_out)
1170 return -ESPIPE;
1171
1172 if (!(in->f_mode & FMODE_READ))
1173 return -EBADF;
1174
1175 if (!(out->f_mode & FMODE_WRITE))
1176 return -EBADF;
1177
1178 /* Splicing to self would be fun, but... */
1179 if (ipipe == opipe)
1180 return -EINVAL;
1181
1182 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1183 }
1184
1185 if (ipipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001186 if (off_in)
1187 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001188 if (off_out) {
Changli Gao19c9a492010-06-29 13:10:36 +02001189 if (!(out->f_mode & FMODE_PWRITE))
Jens Axboeb92ce552006-04-11 13:52:07 +02001190 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001191 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001192 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001193 } else {
1194 offset = out->f_pos;
1195 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001196
Al Viro18c67cb2013-06-19 15:41:54 +04001197 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1198 return -EBADF;
1199
1200 if (unlikely(out->f_flags & O_APPEND))
1201 return -EINVAL;
1202
1203 ret = rw_verify_area(WRITE, out, &offset, len);
1204 if (unlikely(ret < 0))
1205 return ret;
1206
Al Viro500368f2013-05-23 20:07:11 -04001207 file_start_write(out);
Al Viro7995bd22013-06-20 18:58:36 +04001208 ret = do_splice_from(ipipe, out, &offset, len, flags);
Al Viro500368f2013-05-23 20:07:11 -04001209 file_end_write(out);
Jens Axboea4514eb2006-04-19 15:57:05 +02001210
Al Viro7995bd22013-06-20 18:58:36 +04001211 if (!off_out)
1212 out->f_pos = offset;
1213 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001214 ret = -EFAULT;
1215
1216 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001217 }
Jens Axboe5274f052006-03-30 15:15:30 +02001218
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001219 if (opipe) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001220 if (off_out)
1221 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001222 if (off_in) {
Changli Gao19c9a492010-06-29 13:10:36 +02001223 if (!(in->f_mode & FMODE_PREAD))
Jens Axboeb92ce552006-04-11 13:52:07 +02001224 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001225 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001226 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001227 } else {
1228 offset = in->f_pos;
1229 }
Ingo Molnar529565dc2006-04-10 15:18:58 +02001230
Al Viro8924fef2016-09-17 20:44:45 -04001231 pipe_lock(opipe);
1232 ret = wait_for_space(opipe, flags);
1233 if (!ret)
1234 ret = do_splice_to(in, &offset, opipe, len, flags);
1235 pipe_unlock(opipe);
1236 if (ret > 0)
1237 wakeup_pipe_readers(opipe);
Al Viro7995bd22013-06-20 18:58:36 +04001238 if (!off_in)
1239 in->f_pos = offset;
1240 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001241 ret = -EFAULT;
1242
1243 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001244 }
Jens Axboe5274f052006-03-30 15:15:30 +02001245
1246 return -EINVAL;
1247}
1248
Al Viro79fddc42016-09-17 22:38:20 -04001249static int iter_to_pipe(struct iov_iter *from,
1250 struct pipe_inode_info *pipe,
1251 unsigned flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001252{
Al Viro79fddc42016-09-17 22:38:20 -04001253 struct pipe_buffer buf = {
1254 .ops = &user_page_pipe_buf_ops,
1255 .flags = flags
1256 };
1257 size_t total = 0;
1258 int ret = 0;
1259 bool failed = false;
1260
1261 while (iov_iter_count(from) && !failed) {
1262 struct page *pages[16];
Al Virodb85a9e2016-09-17 20:25:06 -04001263 ssize_t copied;
1264 size_t start;
Al Viro79fddc42016-09-17 22:38:20 -04001265 int n;
Jens Axboe912d35f2006-04-26 10:59:21 +02001266
Al Viro79fddc42016-09-17 22:38:20 -04001267 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1268 if (copied <= 0) {
1269 ret = copied;
1270 break;
1271 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001272
Al Viro79fddc42016-09-17 22:38:20 -04001273 for (n = 0; copied; n++, start = 0) {
Al Virodb85a9e2016-09-17 20:25:06 -04001274 int size = min_t(int, copied, PAGE_SIZE - start);
Al Viro79fddc42016-09-17 22:38:20 -04001275 if (!failed) {
1276 buf.page = pages[n];
1277 buf.offset = start;
1278 buf.len = size;
1279 ret = add_to_pipe(pipe, &buf);
1280 if (unlikely(ret < 0)) {
1281 failed = true;
1282 } else {
1283 iov_iter_advance(from, ret);
1284 total += ret;
1285 }
1286 } else {
1287 put_page(pages[n]);
1288 }
Al Virodb85a9e2016-09-17 20:25:06 -04001289 copied -= size;
Jens Axboe912d35f2006-04-26 10:59:21 +02001290 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001291 }
Al Viro79fddc42016-09-17 22:38:20 -04001292 return total ? total : ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001293}
1294
Jens Axboe6a14b902007-06-14 13:08:55 +02001295static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1296 struct splice_desc *sd)
1297{
Al Viro6130f532014-02-03 18:19:51 -05001298 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1299 return n == sd->len ? n : -EFAULT;
Jens Axboe6a14b902007-06-14 13:08:55 +02001300}
1301
1302/*
1303 * For lack of a better implementation, implement vmsplice() to userspace
1304 * as a simple copy of the pipes pages to the user iov.
1305 */
Al Viro6130f532014-02-03 18:19:51 -05001306static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001307 unsigned long nr_segs, unsigned int flags)
1308{
1309 struct pipe_inode_info *pipe;
1310 struct splice_desc sd;
Jens Axboe6a14b902007-06-14 13:08:55 +02001311 long ret;
Al Viro6130f532014-02-03 18:19:51 -05001312 struct iovec iovstack[UIO_FASTIOV];
1313 struct iovec *iov = iovstack;
1314 struct iov_iter iter;
Jens Axboe6a14b902007-06-14 13:08:55 +02001315
Linus Torvalds71993e62010-11-28 13:56:09 -08001316 pipe = get_pipe_info(file);
Jens Axboe6a14b902007-06-14 13:08:55 +02001317 if (!pipe)
1318 return -EBADF;
1319
Al Viro345995f2015-03-21 19:17:55 -04001320 ret = import_iovec(READ, uiov, nr_segs,
1321 ARRAY_SIZE(iovstack), &iov, &iter);
1322 if (ret < 0)
1323 return ret;
Al Viro6130f532014-02-03 18:19:51 -05001324
Al Viro345995f2015-03-21 19:17:55 -04001325 sd.total_len = iov_iter_count(&iter);
Al Viro6130f532014-02-03 18:19:51 -05001326 sd.len = 0;
Al Viro6130f532014-02-03 18:19:51 -05001327 sd.flags = flags;
1328 sd.u.data = &iter;
1329 sd.pos = 0;
1330
Al Viro345995f2015-03-21 19:17:55 -04001331 if (sd.total_len) {
1332 pipe_lock(pipe);
1333 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1334 pipe_unlock(pipe);
1335 }
Jens Axboe6a14b902007-06-14 13:08:55 +02001336
Al Viro345995f2015-03-21 19:17:55 -04001337 kfree(iov);
Jens Axboe6a14b902007-06-14 13:08:55 +02001338 return ret;
1339}
1340
Jens Axboe912d35f2006-04-26 10:59:21 +02001341/*
1342 * vmsplice splices a user address range into a pipe. It can be thought of
1343 * as splice-from-memory, where the regular splice is splice-from-file (or
1344 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001345 */
Al Virodb85a9e2016-09-17 20:25:06 -04001346static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001347 unsigned long nr_segs, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001348{
Jens Axboeddac0d32006-11-04 12:49:32 +01001349 struct pipe_inode_info *pipe;
Al Virodb85a9e2016-09-17 20:25:06 -04001350 struct iovec iovstack[UIO_FASTIOV];
1351 struct iovec *iov = iovstack;
1352 struct iov_iter from;
Jens Axboe35f3d142010-05-20 10:43:18 +02001353 long ret;
Al Viro79fddc42016-09-17 22:38:20 -04001354 unsigned buf_flag = 0;
1355
1356 if (flags & SPLICE_F_GIFT)
1357 buf_flag = PIPE_BUF_FLAG_GIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +02001358
Linus Torvalds71993e62010-11-28 13:56:09 -08001359 pipe = get_pipe_info(file);
Jens Axboeddac0d32006-11-04 12:49:32 +01001360 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001361 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001362
Al Virodb85a9e2016-09-17 20:25:06 -04001363 ret = import_iovec(WRITE, uiov, nr_segs,
1364 ARRAY_SIZE(iovstack), &iov, &from);
1365 if (ret < 0)
1366 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001367
Al Viro8924fef2016-09-17 20:44:45 -04001368 pipe_lock(pipe);
1369 ret = wait_for_space(pipe, flags);
Al Viro79fddc42016-09-17 22:38:20 -04001370 if (!ret)
1371 ret = iter_to_pipe(&from, pipe, buf_flag);
Al Viro8924fef2016-09-17 20:44:45 -04001372 pipe_unlock(pipe);
1373 if (ret > 0)
1374 wakeup_pipe_readers(pipe);
Al Virodb85a9e2016-09-17 20:25:06 -04001375 kfree(iov);
Jens Axboe35f3d142010-05-20 10:43:18 +02001376 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001377}
1378
Jens Axboe6a14b902007-06-14 13:08:55 +02001379/*
1380 * Note that vmsplice only really supports true splicing _from_ user memory
1381 * to a pipe, not the other way around. Splicing from user memory is a simple
1382 * operation that can be supported without any funky alignment restrictions
1383 * or nasty vm tricks. We simply map in the user memory and fill them into
1384 * a pipe. The reverse isn't quite as easy, though. There are two possible
1385 * solutions for that:
1386 *
1387 * - memcpy() the data internally, at which point we might as well just
1388 * do a regular read() on the buffer anyway.
1389 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1390 * has restriction limitations on both ends of the pipe).
1391 *
1392 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1393 *
1394 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01001395SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1396 unsigned long, nr_segs, unsigned int, flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001397{
Al Viro2903ff02012-08-28 12:52:22 -04001398 struct fd f;
Jens Axboe912d35f2006-04-26 10:59:21 +02001399 long error;
Jens Axboe912d35f2006-04-26 10:59:21 +02001400
Jens Axboe6a14b902007-06-14 13:08:55 +02001401 if (unlikely(nr_segs > UIO_MAXIOV))
1402 return -EINVAL;
1403 else if (unlikely(!nr_segs))
1404 return 0;
1405
Jens Axboe912d35f2006-04-26 10:59:21 +02001406 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001407 f = fdget(fd);
1408 if (f.file) {
1409 if (f.file->f_mode & FMODE_WRITE)
1410 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1411 else if (f.file->f_mode & FMODE_READ)
1412 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001413
Al Viro2903ff02012-08-28 12:52:22 -04001414 fdput(f);
Jens Axboe912d35f2006-04-26 10:59:21 +02001415 }
1416
1417 return error;
1418}
1419
Al Viro76b021d2013-03-02 10:19:56 -05001420#ifdef CONFIG_COMPAT
1421COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1422 unsigned int, nr_segs, unsigned int, flags)
1423{
1424 unsigned i;
1425 struct iovec __user *iov;
1426 if (nr_segs > UIO_MAXIOV)
1427 return -EINVAL;
1428 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1429 for (i = 0; i < nr_segs; i++) {
1430 struct compat_iovec v;
1431 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1432 get_user(v.iov_len, &iov32[i].iov_len) ||
1433 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1434 put_user(v.iov_len, &iov[i].iov_len))
1435 return -EFAULT;
1436 }
1437 return sys_vmsplice(fd, iov, nr_segs, flags);
1438}
1439#endif
1440
Heiko Carstens836f92a2009-01-14 14:14:33 +01001441SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1442 int, fd_out, loff_t __user *, off_out,
1443 size_t, len, unsigned int, flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001444{
Al Viro2903ff02012-08-28 12:52:22 -04001445 struct fd in, out;
Jens Axboe5274f052006-03-30 15:15:30 +02001446 long error;
Jens Axboe5274f052006-03-30 15:15:30 +02001447
1448 if (unlikely(!len))
1449 return 0;
1450
1451 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001452 in = fdget(fd_in);
1453 if (in.file) {
1454 if (in.file->f_mode & FMODE_READ) {
1455 out = fdget(fd_out);
1456 if (out.file) {
1457 if (out.file->f_mode & FMODE_WRITE)
1458 error = do_splice(in.file, off_in,
1459 out.file, off_out,
Ingo Molnar529565dc2006-04-10 15:18:58 +02001460 len, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001461 fdput(out);
Jens Axboe5274f052006-03-30 15:15:30 +02001462 }
1463 }
Al Viro2903ff02012-08-28 12:52:22 -04001464 fdput(in);
Jens Axboe5274f052006-03-30 15:15:30 +02001465 }
Jens Axboe5274f052006-03-30 15:15:30 +02001466 return error;
1467}
Jens Axboe70524492006-04-11 15:51:17 +02001468
1469/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001470 * Make sure there's data to read. Wait for input if we can, otherwise
1471 * return an appropriate error.
1472 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001473static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001474{
1475 int ret;
1476
1477 /*
1478 * Check ->nrbufs without the inode lock first. This function
1479 * is speculative anyways, so missing one is ok.
1480 */
1481 if (pipe->nrbufs)
1482 return 0;
1483
1484 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001485 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001486
1487 while (!pipe->nrbufs) {
1488 if (signal_pending(current)) {
1489 ret = -ERESTARTSYS;
1490 break;
1491 }
1492 if (!pipe->writers)
1493 break;
1494 if (!pipe->waiting_writers) {
1495 if (flags & SPLICE_F_NONBLOCK) {
1496 ret = -EAGAIN;
1497 break;
1498 }
1499 }
1500 pipe_wait(pipe);
1501 }
1502
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001503 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001504 return ret;
1505}
1506
1507/*
1508 * Make sure there's writeable room. Wait for room if we can, otherwise
1509 * return an appropriate error.
1510 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001511static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001512{
1513 int ret;
1514
1515 /*
1516 * Check ->nrbufs without the inode lock first. This function
1517 * is speculative anyways, so missing one is ok.
1518 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001519 if (pipe->nrbufs < pipe->buffers)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001520 return 0;
1521
1522 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001523 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001524
Jens Axboe35f3d142010-05-20 10:43:18 +02001525 while (pipe->nrbufs >= pipe->buffers) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001526 if (!pipe->readers) {
1527 send_sig(SIGPIPE, current, 0);
1528 ret = -EPIPE;
1529 break;
1530 }
1531 if (flags & SPLICE_F_NONBLOCK) {
1532 ret = -EAGAIN;
1533 break;
1534 }
1535 if (signal_pending(current)) {
1536 ret = -ERESTARTSYS;
1537 break;
1538 }
1539 pipe->waiting_writers++;
1540 pipe_wait(pipe);
1541 pipe->waiting_writers--;
1542 }
1543
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001544 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001545 return ret;
1546}
1547
1548/*
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001549 * Splice contents of ipipe to opipe.
1550 */
1551static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1552 struct pipe_inode_info *opipe,
1553 size_t len, unsigned int flags)
1554{
1555 struct pipe_buffer *ibuf, *obuf;
1556 int ret = 0, nbuf;
1557 bool input_wakeup = false;
1558
1559
1560retry:
1561 ret = ipipe_prep(ipipe, flags);
1562 if (ret)
1563 return ret;
1564
1565 ret = opipe_prep(opipe, flags);
1566 if (ret)
1567 return ret;
1568
1569 /*
1570 * Potential ABBA deadlock, work around it by ordering lock
1571 * grabbing by pipe info address. Otherwise two different processes
1572 * could deadlock (one doing tee from A -> B, the other from B -> A).
1573 */
1574 pipe_double_lock(ipipe, opipe);
1575
1576 do {
1577 if (!opipe->readers) {
1578 send_sig(SIGPIPE, current, 0);
1579 if (!ret)
1580 ret = -EPIPE;
1581 break;
1582 }
1583
1584 if (!ipipe->nrbufs && !ipipe->writers)
1585 break;
1586
1587 /*
1588 * Cannot make any progress, because either the input
1589 * pipe is empty or the output pipe is full.
1590 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001591 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001592 /* Already processed some buffers, break */
1593 if (ret)
1594 break;
1595
1596 if (flags & SPLICE_F_NONBLOCK) {
1597 ret = -EAGAIN;
1598 break;
1599 }
1600
1601 /*
1602 * We raced with another reader/writer and haven't
1603 * managed to process any buffers. A zero return
1604 * value means EOF, so retry instead.
1605 */
1606 pipe_unlock(ipipe);
1607 pipe_unlock(opipe);
1608 goto retry;
1609 }
1610
1611 ibuf = ipipe->bufs + ipipe->curbuf;
Jens Axboe35f3d142010-05-20 10:43:18 +02001612 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001613 obuf = opipe->bufs + nbuf;
1614
1615 if (len >= ibuf->len) {
1616 /*
1617 * Simply move the whole buffer from ipipe to opipe
1618 */
1619 *obuf = *ibuf;
1620 ibuf->ops = NULL;
1621 opipe->nrbufs++;
Jens Axboe35f3d142010-05-20 10:43:18 +02001622 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001623 ipipe->nrbufs--;
1624 input_wakeup = true;
1625 } else {
1626 /*
1627 * Get a reference to this pipe buffer,
1628 * so we can copy the contents over.
1629 */
1630 ibuf->ops->get(ipipe, ibuf);
1631 *obuf = *ibuf;
1632
1633 /*
1634 * Don't inherit the gift flag, we need to
1635 * prevent multiple steals of this page.
1636 */
1637 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1638
1639 obuf->len = len;
1640 opipe->nrbufs++;
1641 ibuf->offset += obuf->len;
1642 ibuf->len -= obuf->len;
1643 }
1644 ret += obuf->len;
1645 len -= obuf->len;
1646 } while (len);
1647
1648 pipe_unlock(ipipe);
1649 pipe_unlock(opipe);
1650
1651 /*
1652 * If we put data in the output pipe, wakeup any potential readers.
1653 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001654 if (ret > 0)
1655 wakeup_pipe_readers(opipe);
1656
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001657 if (input_wakeup)
1658 wakeup_pipe_writers(ipipe);
1659
1660 return ret;
1661}
1662
1663/*
Jens Axboe70524492006-04-11 15:51:17 +02001664 * Link contents of ipipe to opipe.
1665 */
1666static int link_pipe(struct pipe_inode_info *ipipe,
1667 struct pipe_inode_info *opipe,
1668 size_t len, unsigned int flags)
1669{
1670 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001671 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001672
1673 /*
1674 * Potential ABBA deadlock, work around it by ordering lock
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001675 * grabbing by pipe info address. Otherwise two different processes
Jens Axboe70524492006-04-11 15:51:17 +02001676 * could deadlock (one doing tee from A -> B, the other from B -> A).
1677 */
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001678 pipe_double_lock(ipipe, opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001679
Jens Axboeaadd06e2006-07-10 11:00:01 +02001680 do {
Jens Axboe70524492006-04-11 15:51:17 +02001681 if (!opipe->readers) {
1682 send_sig(SIGPIPE, current, 0);
1683 if (!ret)
1684 ret = -EPIPE;
1685 break;
1686 }
Jens Axboe70524492006-04-11 15:51:17 +02001687
1688 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001689 * If we have iterated all input buffers or ran out of
1690 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001691 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001692 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
Jens Axboe70524492006-04-11 15:51:17 +02001693 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001694
Jens Axboe35f3d142010-05-20 10:43:18 +02001695 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1696 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001697
Jens Axboe2a27250e2006-04-19 15:56:40 +02001698 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001699 * Get a reference to this pipe buffer,
1700 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001701 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001702 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001703
Jens Axboeaadd06e2006-07-10 11:00:01 +02001704 obuf = opipe->bufs + nbuf;
1705 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001706
Jens Axboeaadd06e2006-07-10 11:00:01 +02001707 /*
1708 * Don't inherit the gift flag, we need to
1709 * prevent multiple steals of this page.
1710 */
1711 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1712
1713 if (obuf->len > len)
1714 obuf->len = len;
1715
1716 opipe->nrbufs++;
1717 ret += obuf->len;
1718 len -= obuf->len;
1719 i++;
1720 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001721
Jens Axboe02cf01a2008-02-20 10:34:51 +01001722 /*
1723 * return EAGAIN if we have the potential of some data in the
1724 * future, otherwise just return 0
1725 */
1726 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1727 ret = -EAGAIN;
1728
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001729 pipe_unlock(ipipe);
1730 pipe_unlock(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001731
Jens Axboeaadd06e2006-07-10 11:00:01 +02001732 /*
1733 * If we put data in the output pipe, wakeup any potential readers.
1734 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001735 if (ret > 0)
1736 wakeup_pipe_readers(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001737
1738 return ret;
1739}
1740
1741/*
1742 * This is a tee(1) implementation that works on pipes. It doesn't copy
1743 * any data, it simply references the 'in' pages on the 'out' pipe.
1744 * The 'flags' used are the SPLICE_F_* variants, currently the only
1745 * applicable one is SPLICE_F_NONBLOCK.
1746 */
1747static long do_tee(struct file *in, struct file *out, size_t len,
1748 unsigned int flags)
1749{
Linus Torvalds71993e62010-11-28 13:56:09 -08001750 struct pipe_inode_info *ipipe = get_pipe_info(in);
1751 struct pipe_inode_info *opipe = get_pipe_info(out);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001752 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001753
1754 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001755 * Duplicate the contents of ipipe to opipe without actually
1756 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001757 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001758 if (ipipe && opipe && ipipe != opipe) {
1759 /*
1760 * Keep going, unless we encounter an error. The ipipe/opipe
1761 * ordering doesn't really matter.
1762 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001763 ret = ipipe_prep(ipipe, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001764 if (!ret) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001765 ret = opipe_prep(opipe, flags);
Jens Axboe02cf01a2008-02-20 10:34:51 +01001766 if (!ret)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001767 ret = link_pipe(ipipe, opipe, len, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001768 }
1769 }
Jens Axboe70524492006-04-11 15:51:17 +02001770
Jens Axboeaadd06e2006-07-10 11:00:01 +02001771 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001772}
1773
Heiko Carstens836f92a2009-01-14 14:14:33 +01001774SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
Jens Axboe70524492006-04-11 15:51:17 +02001775{
Al Viro2903ff02012-08-28 12:52:22 -04001776 struct fd in;
1777 int error;
Jens Axboe70524492006-04-11 15:51:17 +02001778
1779 if (unlikely(!len))
1780 return 0;
1781
1782 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001783 in = fdget(fdin);
1784 if (in.file) {
1785 if (in.file->f_mode & FMODE_READ) {
1786 struct fd out = fdget(fdout);
1787 if (out.file) {
1788 if (out.file->f_mode & FMODE_WRITE)
1789 error = do_tee(in.file, out.file,
1790 len, flags);
1791 fdput(out);
Jens Axboe70524492006-04-11 15:51:17 +02001792 }
1793 }
Al Viro2903ff02012-08-28 12:52:22 -04001794 fdput(in);
Jens Axboe70524492006-04-11 15:51:17 +02001795 }
1796
1797 return error;
1798}