blob: 0df907ba46ed7eb6c74a351f2eb924f830c10d40 [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
Al Viro523ac9a2016-09-23 15:34:57 -0400364static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
Miklos Szeredi68181732009-05-07 15:37:36 +0200365 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{
Al Viro523ac9a2016-09-23 15:34:57 -0400400 struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
401 struct iov_iter to;
402 struct page **pages;
Miklos Szeredi68181732009-05-07 15:37:36 +0200403 unsigned int nr_pages;
Al Viro523ac9a2016-09-23 15:34:57 -0400404 size_t offset, dummy, copied = 0;
Miklos Szeredi68181732009-05-07 15:37:36 +0200405 ssize_t res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200406 int i;
Miklos Szeredi68181732009-05-07 15:37:36 +0200407
Al Viro523ac9a2016-09-23 15:34:57 -0400408 if (pipe->nrbufs == pipe->buffers)
409 return -EAGAIN;
410
411 /*
412 * Try to keep page boundaries matching to source pagecache ones -
413 * it probably won't be much help, but...
414 */
415 offset = *ppos & ~PAGE_MASK;
416
417 iov_iter_pipe(&to, ITER_PIPE | READ, pipe, len + offset);
418
419 res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &dummy);
420 if (res <= 0)
Jens Axboe35f3d142010-05-20 10:43:18 +0200421 return -ENOMEM;
422
Al Viro523ac9a2016-09-23 15:34:57 -0400423 nr_pages = res / PAGE_SIZE;
424
Jens Axboe35f3d142010-05-20 10:43:18 +0200425 vec = __vec;
Al Viro523ac9a2016-09-23 15:34:57 -0400426 if (nr_pages > PIPE_DEF_BUFFERS) {
427 vec = kmalloc(nr_pages * sizeof(struct kvec), GFP_KERNEL);
428 if (unlikely(!vec)) {
429 res = -ENOMEM;
430 goto out;
431 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200432 }
433
Al Viro523ac9a2016-09-23 15:34:57 -0400434 pipe->bufs[to.idx].offset = offset;
435 pipe->bufs[to.idx].len -= offset;
Miklos Szeredi68181732009-05-07 15:37:36 +0200436
Al Viro523ac9a2016-09-23 15:34:57 -0400437 for (i = 0; i < nr_pages; i++) {
438 size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
439 vec[i].iov_base = page_address(pages[i]) + offset;
Miklos Szeredi68181732009-05-07 15:37:36 +0200440 vec[i].iov_len = this_len;
Miklos Szeredi68181732009-05-07 15:37:36 +0200441 len -= this_len;
442 offset = 0;
443 }
444
Al Viro523ac9a2016-09-23 15:34:57 -0400445 res = kernel_readv(in, vec, nr_pages, *ppos);
446 if (res > 0) {
447 copied = res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200448 *ppos += res;
Al Viro523ac9a2016-09-23 15:34:57 -0400449 }
Miklos Szeredi68181732009-05-07 15:37:36 +0200450
Jens Axboe35f3d142010-05-20 10:43:18 +0200451 if (vec != __vec)
452 kfree(vec);
Al Viro523ac9a2016-09-23 15:34:57 -0400453out:
454 for (i = 0; i < nr_pages; i++)
455 put_page(pages[i]);
456 kvfree(pages);
457 iov_iter_advance(&to, copied); /* truncates and discards */
Miklos Szeredi68181732009-05-07 15:37:36 +0200458 return res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200459}
Miklos Szeredi68181732009-05-07 15:37:36 +0200460
Jens Axboe5274f052006-03-30 15:15:30 +0200461/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200462 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200463 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200464 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200465static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200466 struct pipe_buffer *buf, struct splice_desc *sd)
467{
Jens Axboe6a14b902007-06-14 13:08:55 +0200468 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200469 loff_t pos = sd->pos;
Michał Mirosława8adbe32010-12-17 08:56:44 +0100470 int more;
Jens Axboe5274f052006-03-30 15:15:30 +0200471
Al Viro72c2d532013-09-22 16:27:52 -0400472 if (!likely(file->f_op->sendpage))
Michał Mirosława8adbe32010-12-17 08:56:44 +0100473 return -EINVAL;
Jens Axboe5274f052006-03-30 15:15:30 +0200474
Eric Dumazet35f9c092012-04-05 03:05:35 +0000475 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000476
477 if (sd->len < sd->total_len && pipe->nrbufs > 1)
Eric Dumazet35f9c092012-04-05 03:05:35 +0000478 more |= MSG_SENDPAGE_NOTLAST;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000479
Michał Mirosława8adbe32010-12-17 08:56:44 +0100480 return file->f_op->sendpage(file, buf->page, buf->offset,
481 sd->len, &pos, more);
Jens Axboe5274f052006-03-30 15:15:30 +0200482}
483
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200484static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
485{
486 smp_mb();
487 if (waitqueue_active(&pipe->wait))
488 wake_up_interruptible(&pipe->wait);
489 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
490}
491
492/**
493 * splice_from_pipe_feed - feed available data from a pipe to a file
494 * @pipe: pipe to splice from
495 * @sd: information to @actor
496 * @actor: handler that splices the data
497 *
498 * Description:
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200499 * This function loops over the pipe and calls @actor to do the
500 * actual moving of a single struct pipe_buffer to the desired
501 * destination. It returns when there's no more buffers left in
502 * the pipe or if the requested number of bytes (@sd->total_len)
503 * have been copied. It returns a positive number (one) if the
504 * pipe needs to be filled with more data, zero if the required
505 * number of bytes have been copied and -errno on error.
506 *
507 * This, together with splice_from_pipe_{begin,end,next}, may be
508 * used to implement the functionality of __splice_from_pipe() when
509 * locking is required around copying the pipe buffers to the
510 * destination.
511 */
Al Viro96f9bc82014-04-05 04:35:49 -0400512static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200513 splice_actor *actor)
514{
515 int ret;
516
517 while (pipe->nrbufs) {
518 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
519 const struct pipe_buf_operations *ops = buf->ops;
520
521 sd->len = buf->len;
522 if (sd->len > sd->total_len)
523 sd->len = sd->total_len;
524
Michał Mirosława8adbe32010-12-17 08:56:44 +0100525 ret = buf->ops->confirm(pipe, buf);
526 if (unlikely(ret)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200527 if (ret == -ENODATA)
528 ret = 0;
529 return ret;
530 }
Michał Mirosława8adbe32010-12-17 08:56:44 +0100531
532 ret = actor(pipe, buf, sd);
533 if (ret <= 0)
534 return ret;
535
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200536 buf->offset += ret;
537 buf->len -= ret;
538
539 sd->num_spliced += ret;
540 sd->len -= ret;
541 sd->pos += ret;
542 sd->total_len -= ret;
543
544 if (!buf->len) {
545 buf->ops = NULL;
546 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200547 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200548 pipe->nrbufs--;
Al Viro6447a3c2013-03-21 11:01:38 -0400549 if (pipe->files)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200550 sd->need_wakeup = true;
551 }
552
553 if (!sd->total_len)
554 return 0;
555 }
556
557 return 1;
558}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200559
560/**
561 * splice_from_pipe_next - wait for some data to splice from
562 * @pipe: pipe to splice from
563 * @sd: information about the splice operation
564 *
565 * Description:
566 * This function will wait for some data and return a positive
567 * value (one) if pipe buffers are available. It will return zero
568 * or -errno if no more data needs to be spliced.
569 */
Al Viro96f9bc82014-04-05 04:35:49 -0400570static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200571{
Jan Karac725bfc2015-11-23 13:09:50 +0100572 /*
573 * Check for signal early to make process killable when there are
574 * always buffers available
575 */
576 if (signal_pending(current))
577 return -ERESTARTSYS;
578
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200579 while (!pipe->nrbufs) {
580 if (!pipe->writers)
581 return 0;
582
583 if (!pipe->waiting_writers && sd->num_spliced)
584 return 0;
585
586 if (sd->flags & SPLICE_F_NONBLOCK)
587 return -EAGAIN;
588
589 if (signal_pending(current))
590 return -ERESTARTSYS;
591
592 if (sd->need_wakeup) {
593 wakeup_pipe_writers(pipe);
594 sd->need_wakeup = false;
595 }
596
597 pipe_wait(pipe);
598 }
599
600 return 1;
601}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200602
603/**
604 * splice_from_pipe_begin - start splicing from pipe
Randy Dunlapb80901b2009-04-16 19:09:55 -0700605 * @sd: information about the splice operation
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200606 *
607 * Description:
608 * This function should be called before a loop containing
609 * splice_from_pipe_next() and splice_from_pipe_feed() to
610 * initialize the necessary fields of @sd.
611 */
Al Viro96f9bc82014-04-05 04:35:49 -0400612static void splice_from_pipe_begin(struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200613{
614 sd->num_spliced = 0;
615 sd->need_wakeup = false;
616}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200617
618/**
619 * splice_from_pipe_end - finish splicing from pipe
620 * @pipe: pipe to splice from
621 * @sd: information about the splice operation
622 *
623 * Description:
624 * This function will wake up pipe writers if necessary. It should
625 * be called after a loop containing splice_from_pipe_next() and
626 * splice_from_pipe_feed().
627 */
Al Viro96f9bc82014-04-05 04:35:49 -0400628static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200629{
630 if (sd->need_wakeup)
631 wakeup_pipe_writers(pipe);
632}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200633
Jens Axboe932cc6d2007-06-21 13:10:21 +0200634/**
635 * __splice_from_pipe - splice data from a pipe to given actor
636 * @pipe: pipe to splice from
637 * @sd: information to @actor
638 * @actor: handler that splices the data
639 *
640 * Description:
641 * This function does little more than loop over the pipe and call
642 * @actor to do the actual moving of a single struct pipe_buffer to
643 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
644 * pipe_to_user.
645 *
Jens Axboe83f91352006-04-02 23:05:09 +0200646 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200647ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
648 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200649{
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200650 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200651
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200652 splice_from_pipe_begin(sd);
653 do {
Jan Karac2489e02015-11-23 13:09:51 +0100654 cond_resched();
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200655 ret = splice_from_pipe_next(pipe, sd);
656 if (ret > 0)
657 ret = splice_from_pipe_feed(pipe, sd, actor);
658 } while (ret > 0);
659 splice_from_pipe_end(pipe, sd);
Jens Axboe5274f052006-03-30 15:15:30 +0200660
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200661 return sd->num_spliced ? sd->num_spliced : ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200662}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100663EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200664
Jens Axboe932cc6d2007-06-21 13:10:21 +0200665/**
666 * splice_from_pipe - splice data from a pipe to a file
667 * @pipe: pipe to splice from
668 * @out: file to splice to
669 * @ppos: position in @out
670 * @len: how many bytes to splice
671 * @flags: splice modifier flags
672 * @actor: handler that splices the data
673 *
674 * Description:
Miklos Szeredi29339702009-04-14 19:48:37 +0200675 * See __splice_from_pipe. This function locks the pipe inode,
Jens Axboe932cc6d2007-06-21 13:10:21 +0200676 * otherwise it's identical to __splice_from_pipe().
677 *
678 */
Mark Fasheh6da61802006-10-17 18:43:07 +0200679ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
680 loff_t *ppos, size_t len, unsigned int flags,
681 splice_actor *actor)
682{
683 ssize_t ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200684 struct splice_desc sd = {
685 .total_len = len,
686 .flags = flags,
687 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200688 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200689 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200690
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200691 pipe_lock(pipe);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200692 ret = __splice_from_pipe(pipe, &sd, actor);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200693 pipe_unlock(pipe);
Mark Fasheh6da61802006-10-17 18:43:07 +0200694
695 return ret;
696}
697
698/**
Al Viro8d020762014-04-05 04:27:08 -0400699 * iter_file_splice_write - splice data from a pipe to a file
700 * @pipe: pipe info
701 * @out: file to write to
702 * @ppos: position in @out
703 * @len: number of bytes to splice
704 * @flags: splice modifier flags
705 *
706 * Description:
707 * Will either move or copy pages (determined by @flags options) from
708 * the given pipe inode to the given file.
709 * This one is ->write_iter-based.
710 *
711 */
712ssize_t
713iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
714 loff_t *ppos, size_t len, unsigned int flags)
715{
716 struct splice_desc sd = {
717 .total_len = len,
718 .flags = flags,
719 .pos = *ppos,
720 .u.file = out,
721 };
722 int nbufs = pipe->buffers;
723 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
724 GFP_KERNEL);
725 ssize_t ret;
726
727 if (unlikely(!array))
728 return -ENOMEM;
729
730 pipe_lock(pipe);
731
732 splice_from_pipe_begin(&sd);
733 while (sd.total_len) {
734 struct iov_iter from;
Al Viro8d020762014-04-05 04:27:08 -0400735 size_t left;
736 int n, idx;
737
738 ret = splice_from_pipe_next(pipe, &sd);
739 if (ret <= 0)
740 break;
741
742 if (unlikely(nbufs < pipe->buffers)) {
743 kfree(array);
744 nbufs = pipe->buffers;
745 array = kcalloc(nbufs, sizeof(struct bio_vec),
746 GFP_KERNEL);
747 if (!array) {
748 ret = -ENOMEM;
749 break;
750 }
751 }
752
753 /* build the vector */
754 left = sd.total_len;
755 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
756 struct pipe_buffer *buf = pipe->bufs + idx;
757 size_t this_len = buf->len;
758
759 if (this_len > left)
760 this_len = left;
761
762 if (idx == pipe->buffers - 1)
763 idx = -1;
764
765 ret = buf->ops->confirm(pipe, buf);
766 if (unlikely(ret)) {
767 if (ret == -ENODATA)
768 ret = 0;
769 goto done;
770 }
771
772 array[n].bv_page = buf->page;
773 array[n].bv_len = this_len;
774 array[n].bv_offset = buf->offset;
775 left -= this_len;
776 }
777
Al Viro05afcb72015-01-23 01:08:07 -0500778 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
779 sd.total_len - left);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100780 ret = vfs_iter_write(out, &from, &sd.pos);
Al Viro8d020762014-04-05 04:27:08 -0400781 if (ret <= 0)
782 break;
783
784 sd.num_spliced += ret;
785 sd.total_len -= ret;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100786 *ppos = sd.pos;
Al Viro8d020762014-04-05 04:27:08 -0400787
788 /* dismiss the fully eaten buffers, adjust the partial one */
789 while (ret) {
790 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
791 if (ret >= buf->len) {
792 const struct pipe_buf_operations *ops = buf->ops;
793 ret -= buf->len;
794 buf->len = 0;
795 buf->ops = NULL;
796 ops->release(pipe, buf);
797 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
798 pipe->nrbufs--;
799 if (pipe->files)
800 sd.need_wakeup = true;
801 } else {
802 buf->offset += ret;
803 buf->len -= ret;
804 ret = 0;
805 }
806 }
807 }
808done:
809 kfree(array);
810 splice_from_pipe_end(pipe, &sd);
811
812 pipe_unlock(pipe);
813
814 if (sd.num_spliced)
815 ret = sd.num_spliced;
816
817 return ret;
818}
819
820EXPORT_SYMBOL(iter_file_splice_write);
821
Miklos Szeredib2858d72009-05-19 11:37:46 +0200822static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
823 struct splice_desc *sd)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200824{
Miklos Szeredib2858d72009-05-19 11:37:46 +0200825 int ret;
826 void *data;
Al Viro06ae43f2013-03-20 13:19:30 -0400827 loff_t tmp = sd->pos;
Miklos Szeredib2858d72009-05-19 11:37:46 +0200828
Al Virofbb32752014-02-02 21:09:54 -0500829 data = kmap(buf->page);
Al Viro06ae43f2013-03-20 13:19:30 -0400830 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
Al Virofbb32752014-02-02 21:09:54 -0500831 kunmap(buf->page);
Miklos Szeredib2858d72009-05-19 11:37:46 +0200832
833 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200834}
835
836static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
837 struct file *out, loff_t *ppos,
838 size_t len, unsigned int flags)
839{
Miklos Szeredib2858d72009-05-19 11:37:46 +0200840 ssize_t ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200841
Miklos Szeredib2858d72009-05-19 11:37:46 +0200842 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
843 if (ret > 0)
844 *ppos += ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200845
Miklos Szeredib2858d72009-05-19 11:37:46 +0200846 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200847}
848
Jens Axboe83f91352006-04-02 23:05:09 +0200849/**
850 * generic_splice_sendpage - splice data from a pipe to a socket
Jens Axboe932cc6d2007-06-21 13:10:21 +0200851 * @pipe: pipe to splice from
Jens Axboe83f91352006-04-02 23:05:09 +0200852 * @out: socket to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +0200853 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +0200854 * @len: number of bytes to splice
855 * @flags: splice modifier flags
856 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200857 * Description:
858 * Will send @len bytes from the pipe to a network socket. No data copying
859 * is involved.
Jens Axboe83f91352006-04-02 23:05:09 +0200860 *
861 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200862ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200863 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200864{
Jens Axboe00522fb2006-04-26 14:39:29 +0200865 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200866}
867
Jens Axboe059a8f32006-04-02 23:06:05 +0200868EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500869
Jens Axboe83f91352006-04-02 23:05:09 +0200870/*
871 * Attempt to initiate a splice from pipe to file.
872 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200873static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200874 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200875{
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200876 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
877 loff_t *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +0200878
Al Viro72c2d532013-09-22 16:27:52 -0400879 if (out->f_op->splice_write)
Changli Gaocc56f7d2009-11-04 09:09:52 +0100880 splice_write = out->f_op->splice_write;
881 else
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200882 splice_write = default_file_splice_write;
883
Al Viro500368f2013-05-23 20:07:11 -0400884 return splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200885}
886
Jens Axboe83f91352006-04-02 23:05:09 +0200887/*
888 * Attempt to initiate a splice from a file to a pipe.
889 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200890static long do_splice_to(struct file *in, loff_t *ppos,
891 struct pipe_inode_info *pipe, size_t len,
892 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200893{
Miklos Szeredi68181732009-05-07 15:37:36 +0200894 ssize_t (*splice_read)(struct file *, loff_t *,
895 struct pipe_inode_info *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +0200896 int ret;
897
Jens Axboe49570e92006-04-11 13:56:09 +0200898 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200899 return -EBADF;
900
Jens Axboecbb7e572006-04-11 14:57:50 +0200901 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200902 if (unlikely(ret < 0))
903 return ret;
904
Al Viro03cc0782016-04-02 14:56:58 -0400905 if (unlikely(len > MAX_RW_COUNT))
906 len = MAX_RW_COUNT;
907
Al Viro72c2d532013-09-22 16:27:52 -0400908 if (in->f_op->splice_read)
Changli Gaocc56f7d2009-11-04 09:09:52 +0100909 splice_read = in->f_op->splice_read;
910 else
Miklos Szeredi68181732009-05-07 15:37:36 +0200911 splice_read = default_file_splice_read;
912
913 return splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200914}
915
Jens Axboe932cc6d2007-06-21 13:10:21 +0200916/**
917 * splice_direct_to_actor - splices data directly between two non-pipes
918 * @in: file to splice from
919 * @sd: actor information on where to splice to
920 * @actor: handles the data splicing
921 *
922 * Description:
923 * This is a special case helper to splice directly between two
924 * points, without requiring an explicit pipe. Internally an allocated
Randy Dunlap79685b82007-07-27 08:08:51 +0200925 * pipe is cached in the process, and reused during the lifetime of
Jens Axboe932cc6d2007-06-21 13:10:21 +0200926 * that process.
927 *
Jens Axboec66ab6f2007-06-12 21:17:17 +0200928 */
929ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
930 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +0200931{
932 struct pipe_inode_info *pipe;
933 long ret, bytes;
934 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200935 size_t len;
Christophe Leroy0ff28d92015-05-06 17:26:47 +0200936 int i, flags, more;
Jens Axboeb92ce552006-04-11 13:52:07 +0200937
938 /*
939 * We require the input being a regular file, as we don't want to
940 * randomly drop data for eg socket -> socket splicing. Use the
941 * piped splicing for that!
942 */
Al Viro496ad9a2013-01-23 17:07:38 -0500943 i_mode = file_inode(in)->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +0200944 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
945 return -EINVAL;
946
947 /*
948 * neither in nor out is a pipe, setup an internal pipe attached to
949 * 'out' and transfer the wanted data from 'in' to 'out' through that
950 */
951 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200952 if (unlikely(!pipe)) {
Al Viro7bee1302013-03-21 11:04:15 -0400953 pipe = alloc_pipe_info();
Jens Axboeb92ce552006-04-11 13:52:07 +0200954 if (!pipe)
955 return -ENOMEM;
956
957 /*
958 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200959 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200960 * PIPE_READERS appropriately.
961 */
962 pipe->readers = 1;
963
964 current->splice_pipe = pipe;
965 }
966
967 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200968 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200969 */
970 ret = 0;
971 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200972 len = sd->total_len;
973 flags = sd->flags;
974
975 /*
976 * Don't block on output, we have to drain the direct pipe.
977 */
978 sd->flags &= ~SPLICE_F_NONBLOCK;
Christophe Leroy0ff28d92015-05-06 17:26:47 +0200979 more = sd->flags & SPLICE_F_MORE;
Jens Axboeb92ce552006-04-11 13:52:07 +0200980
981 while (len) {
Jens Axboe51a92c02007-07-13 14:11:43 +0200982 size_t read_len;
Tom Zanussia82c53a2008-05-09 13:28:36 +0200983 loff_t pos = sd->pos, prev_pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +0200984
Jens Axboebcd4f3a2007-07-16 14:41:49 +0200985 ret = do_splice_to(in, &pos, pipe, len, flags);
Jens Axboe51a92c02007-07-13 14:11:43 +0200986 if (unlikely(ret <= 0))
Jens Axboeb92ce552006-04-11 13:52:07 +0200987 goto out_release;
988
989 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200990 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +0200991
992 /*
Christophe Leroy0ff28d92015-05-06 17:26:47 +0200993 * If more data is pending, set SPLICE_F_MORE
994 * If this is the last data and SPLICE_F_MORE was not set
995 * initially, clears it.
996 */
997 if (read_len < len)
998 sd->flags |= SPLICE_F_MORE;
999 else if (!more)
1000 sd->flags &= ~SPLICE_F_MORE;
1001 /*
Jens Axboeb92ce552006-04-11 13:52:07 +02001002 * NOTE: nonblocking mode only applies to the input. We
1003 * must not do the output in nonblocking mode as then we
1004 * could get stuck data in the internal pipe:
1005 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001006 ret = actor(pipe, sd);
Tom Zanussia82c53a2008-05-09 13:28:36 +02001007 if (unlikely(ret <= 0)) {
1008 sd->pos = prev_pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001009 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001010 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001011
1012 bytes += ret;
1013 len -= ret;
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001014 sd->pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001015
Tom Zanussia82c53a2008-05-09 13:28:36 +02001016 if (ret < read_len) {
1017 sd->pos = prev_pos + ret;
Jens Axboe51a92c02007-07-13 14:11:43 +02001018 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001019 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001020 }
1021
Jens Axboe9e971982008-01-29 21:05:57 +01001022done:
Jens Axboeb92ce552006-04-11 13:52:07 +02001023 pipe->nrbufs = pipe->curbuf = 0;
Jens Axboe80848702008-01-30 12:24:48 +01001024 file_accessed(in);
Jens Axboeb92ce552006-04-11 13:52:07 +02001025 return bytes;
1026
1027out_release:
1028 /*
1029 * If we did an incomplete transfer we must release
1030 * the pipe buffers in question:
1031 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001032 for (i = 0; i < pipe->buffers; i++) {
Jens Axboeb92ce552006-04-11 13:52:07 +02001033 struct pipe_buffer *buf = pipe->bufs + i;
1034
1035 if (buf->ops) {
1036 buf->ops->release(pipe, buf);
1037 buf->ops = NULL;
1038 }
1039 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001040
Jens Axboe9e971982008-01-29 21:05:57 +01001041 if (!bytes)
1042 bytes = ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001043
Jens Axboe9e971982008-01-29 21:05:57 +01001044 goto done;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001045}
1046EXPORT_SYMBOL(splice_direct_to_actor);
1047
1048static int direct_splice_actor(struct pipe_inode_info *pipe,
1049 struct splice_desc *sd)
1050{
Jens Axboe6a14b902007-06-14 13:08:55 +02001051 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001052
Al Viro7995bd22013-06-20 18:58:36 +04001053 return do_splice_from(pipe, file, sd->opos, sd->total_len,
Changli Gao2cb4b052010-06-29 13:09:18 +02001054 sd->flags);
Jens Axboec66ab6f2007-06-12 21:17:17 +02001055}
1056
Jens Axboe932cc6d2007-06-21 13:10:21 +02001057/**
1058 * do_splice_direct - splices data directly between two files
1059 * @in: file to splice from
1060 * @ppos: input file offset
1061 * @out: file to splice to
Randy Dunlapacdb37c2013-06-22 19:44:08 -07001062 * @opos: output file offset
Jens Axboe932cc6d2007-06-21 13:10:21 +02001063 * @len: number of bytes to splice
1064 * @flags: splice modifier flags
1065 *
1066 * Description:
1067 * For use by do_sendfile(). splice can easily emulate sendfile, but
1068 * doing it in the application would incur an extra system call
1069 * (splice in + splice out, as compared to just sendfile()). So this helper
1070 * can splice directly through a process-private pipe.
1071 *
1072 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001073long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
Al Viro7995bd22013-06-20 18:58:36 +04001074 loff_t *opos, size_t len, unsigned int flags)
Jens Axboec66ab6f2007-06-12 21:17:17 +02001075{
1076 struct splice_desc sd = {
1077 .len = len,
1078 .total_len = len,
1079 .flags = flags,
1080 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001081 .u.file = out,
Al Viro7995bd22013-06-20 18:58:36 +04001082 .opos = opos,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001083 };
Jens Axboe51a92c02007-07-13 14:11:43 +02001084 long ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001085
Al Viro18c67cb2013-06-19 15:41:54 +04001086 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1087 return -EBADF;
1088
1089 if (unlikely(out->f_flags & O_APPEND))
1090 return -EINVAL;
1091
1092 ret = rw_verify_area(WRITE, out, opos, len);
1093 if (unlikely(ret < 0))
1094 return ret;
1095
Jens Axboec66ab6f2007-06-12 21:17:17 +02001096 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
Jens Axboe51a92c02007-07-13 14:11:43 +02001097 if (ret > 0)
Tom Zanussia82c53a2008-05-09 13:28:36 +02001098 *ppos = sd.pos;
Jens Axboe51a92c02007-07-13 14:11:43 +02001099
Jens Axboec66ab6f2007-06-12 21:17:17 +02001100 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001101}
Miklos Szeredi1c118592014-10-24 00:14:35 +02001102EXPORT_SYMBOL(do_splice_direct);
Jens Axboeb92ce552006-04-11 13:52:07 +02001103
Al Viro8924fef2016-09-17 20:44:45 -04001104static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1105{
1106 while (pipe->nrbufs == pipe->buffers) {
1107 if (flags & SPLICE_F_NONBLOCK)
1108 return -EAGAIN;
1109 if (signal_pending(current))
1110 return -ERESTARTSYS;
1111 pipe->waiting_writers++;
1112 pipe_wait(pipe);
1113 pipe->waiting_writers--;
1114 }
1115 return 0;
1116}
1117
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001118static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1119 struct pipe_inode_info *opipe,
1120 size_t len, unsigned int flags);
Jens Axboeddac0d32006-11-04 12:49:32 +01001121
1122/*
Jens Axboe83f91352006-04-02 23:05:09 +02001123 * Determine where to splice to/from.
1124 */
Ingo Molnar529565d2006-04-10 15:18:58 +02001125static long do_splice(struct file *in, loff_t __user *off_in,
1126 struct file *out, loff_t __user *off_out,
1127 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001128{
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001129 struct pipe_inode_info *ipipe;
1130 struct pipe_inode_info *opipe;
Al Viro7995bd22013-06-20 18:58:36 +04001131 loff_t offset;
Jens Axboea4514eb2006-04-19 15:57:05 +02001132 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001133
Linus Torvalds71993e62010-11-28 13:56:09 -08001134 ipipe = get_pipe_info(in);
1135 opipe = get_pipe_info(out);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001136
1137 if (ipipe && opipe) {
1138 if (off_in || off_out)
1139 return -ESPIPE;
1140
1141 if (!(in->f_mode & FMODE_READ))
1142 return -EBADF;
1143
1144 if (!(out->f_mode & FMODE_WRITE))
1145 return -EBADF;
1146
1147 /* Splicing to self would be fun, but... */
1148 if (ipipe == opipe)
1149 return -EINVAL;
1150
1151 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1152 }
1153
1154 if (ipipe) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001155 if (off_in)
1156 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001157 if (off_out) {
Changli Gao19c9a492010-06-29 13:10:36 +02001158 if (!(out->f_mode & FMODE_PWRITE))
Jens Axboeb92ce552006-04-11 13:52:07 +02001159 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001160 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001161 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001162 } else {
1163 offset = out->f_pos;
1164 }
Ingo Molnar529565d2006-04-10 15:18:58 +02001165
Al Viro18c67cb2013-06-19 15:41:54 +04001166 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1167 return -EBADF;
1168
1169 if (unlikely(out->f_flags & O_APPEND))
1170 return -EINVAL;
1171
1172 ret = rw_verify_area(WRITE, out, &offset, len);
1173 if (unlikely(ret < 0))
1174 return ret;
1175
Al Viro500368f2013-05-23 20:07:11 -04001176 file_start_write(out);
Al Viro7995bd22013-06-20 18:58:36 +04001177 ret = do_splice_from(ipipe, out, &offset, len, flags);
Al Viro500368f2013-05-23 20:07:11 -04001178 file_end_write(out);
Jens Axboea4514eb2006-04-19 15:57:05 +02001179
Al Viro7995bd22013-06-20 18:58:36 +04001180 if (!off_out)
1181 out->f_pos = offset;
1182 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001183 ret = -EFAULT;
1184
1185 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001186 }
Jens Axboe5274f052006-03-30 15:15:30 +02001187
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001188 if (opipe) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001189 if (off_out)
1190 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001191 if (off_in) {
Changli Gao19c9a492010-06-29 13:10:36 +02001192 if (!(in->f_mode & FMODE_PREAD))
Jens Axboeb92ce552006-04-11 13:52:07 +02001193 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001194 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001195 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001196 } else {
1197 offset = in->f_pos;
1198 }
Ingo Molnar529565d2006-04-10 15:18:58 +02001199
Al Viro8924fef2016-09-17 20:44:45 -04001200 pipe_lock(opipe);
1201 ret = wait_for_space(opipe, flags);
1202 if (!ret)
1203 ret = do_splice_to(in, &offset, opipe, len, flags);
1204 pipe_unlock(opipe);
1205 if (ret > 0)
1206 wakeup_pipe_readers(opipe);
Al Viro7995bd22013-06-20 18:58:36 +04001207 if (!off_in)
1208 in->f_pos = offset;
1209 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001210 ret = -EFAULT;
1211
1212 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001213 }
Jens Axboe5274f052006-03-30 15:15:30 +02001214
1215 return -EINVAL;
1216}
1217
Al Viro79fddc42016-09-17 22:38:20 -04001218static int iter_to_pipe(struct iov_iter *from,
1219 struct pipe_inode_info *pipe,
1220 unsigned flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001221{
Al Viro79fddc42016-09-17 22:38:20 -04001222 struct pipe_buffer buf = {
1223 .ops = &user_page_pipe_buf_ops,
1224 .flags = flags
1225 };
1226 size_t total = 0;
1227 int ret = 0;
1228 bool failed = false;
1229
1230 while (iov_iter_count(from) && !failed) {
1231 struct page *pages[16];
Al Virodb85a9e2016-09-17 20:25:06 -04001232 ssize_t copied;
1233 size_t start;
Al Viro79fddc42016-09-17 22:38:20 -04001234 int n;
Jens Axboe912d35f2006-04-26 10:59:21 +02001235
Al Viro79fddc42016-09-17 22:38:20 -04001236 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1237 if (copied <= 0) {
1238 ret = copied;
1239 break;
1240 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001241
Al Viro79fddc42016-09-17 22:38:20 -04001242 for (n = 0; copied; n++, start = 0) {
Al Virodb85a9e2016-09-17 20:25:06 -04001243 int size = min_t(int, copied, PAGE_SIZE - start);
Al Viro79fddc42016-09-17 22:38:20 -04001244 if (!failed) {
1245 buf.page = pages[n];
1246 buf.offset = start;
1247 buf.len = size;
1248 ret = add_to_pipe(pipe, &buf);
1249 if (unlikely(ret < 0)) {
1250 failed = true;
1251 } else {
1252 iov_iter_advance(from, ret);
1253 total += ret;
1254 }
1255 } else {
1256 put_page(pages[n]);
1257 }
Al Virodb85a9e2016-09-17 20:25:06 -04001258 copied -= size;
Jens Axboe912d35f2006-04-26 10:59:21 +02001259 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001260 }
Al Viro79fddc42016-09-17 22:38:20 -04001261 return total ? total : ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001262}
1263
Jens Axboe6a14b902007-06-14 13:08:55 +02001264static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1265 struct splice_desc *sd)
1266{
Al Viro6130f532014-02-03 18:19:51 -05001267 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1268 return n == sd->len ? n : -EFAULT;
Jens Axboe6a14b902007-06-14 13:08:55 +02001269}
1270
1271/*
1272 * For lack of a better implementation, implement vmsplice() to userspace
1273 * as a simple copy of the pipes pages to the user iov.
1274 */
Al Viro6130f532014-02-03 18:19:51 -05001275static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001276 unsigned long nr_segs, unsigned int flags)
1277{
1278 struct pipe_inode_info *pipe;
1279 struct splice_desc sd;
Jens Axboe6a14b902007-06-14 13:08:55 +02001280 long ret;
Al Viro6130f532014-02-03 18:19:51 -05001281 struct iovec iovstack[UIO_FASTIOV];
1282 struct iovec *iov = iovstack;
1283 struct iov_iter iter;
Jens Axboe6a14b902007-06-14 13:08:55 +02001284
Linus Torvalds71993e62010-11-28 13:56:09 -08001285 pipe = get_pipe_info(file);
Jens Axboe6a14b902007-06-14 13:08:55 +02001286 if (!pipe)
1287 return -EBADF;
1288
Al Viro345995f2015-03-21 19:17:55 -04001289 ret = import_iovec(READ, uiov, nr_segs,
1290 ARRAY_SIZE(iovstack), &iov, &iter);
1291 if (ret < 0)
1292 return ret;
Al Viro6130f532014-02-03 18:19:51 -05001293
Al Viro345995f2015-03-21 19:17:55 -04001294 sd.total_len = iov_iter_count(&iter);
Al Viro6130f532014-02-03 18:19:51 -05001295 sd.len = 0;
Al Viro6130f532014-02-03 18:19:51 -05001296 sd.flags = flags;
1297 sd.u.data = &iter;
1298 sd.pos = 0;
1299
Al Viro345995f2015-03-21 19:17:55 -04001300 if (sd.total_len) {
1301 pipe_lock(pipe);
1302 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1303 pipe_unlock(pipe);
1304 }
Jens Axboe6a14b902007-06-14 13:08:55 +02001305
Al Viro345995f2015-03-21 19:17:55 -04001306 kfree(iov);
Jens Axboe6a14b902007-06-14 13:08:55 +02001307 return ret;
1308}
1309
Jens Axboe912d35f2006-04-26 10:59:21 +02001310/*
1311 * vmsplice splices a user address range into a pipe. It can be thought of
1312 * as splice-from-memory, where the regular splice is splice-from-file (or
1313 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001314 */
Al Virodb85a9e2016-09-17 20:25:06 -04001315static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001316 unsigned long nr_segs, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001317{
Jens Axboeddac0d32006-11-04 12:49:32 +01001318 struct pipe_inode_info *pipe;
Al Virodb85a9e2016-09-17 20:25:06 -04001319 struct iovec iovstack[UIO_FASTIOV];
1320 struct iovec *iov = iovstack;
1321 struct iov_iter from;
Jens Axboe35f3d142010-05-20 10:43:18 +02001322 long ret;
Al Viro79fddc42016-09-17 22:38:20 -04001323 unsigned buf_flag = 0;
1324
1325 if (flags & SPLICE_F_GIFT)
1326 buf_flag = PIPE_BUF_FLAG_GIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +02001327
Linus Torvalds71993e62010-11-28 13:56:09 -08001328 pipe = get_pipe_info(file);
Jens Axboeddac0d32006-11-04 12:49:32 +01001329 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001330 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001331
Al Virodb85a9e2016-09-17 20:25:06 -04001332 ret = import_iovec(WRITE, uiov, nr_segs,
1333 ARRAY_SIZE(iovstack), &iov, &from);
1334 if (ret < 0)
1335 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001336
Al Viro8924fef2016-09-17 20:44:45 -04001337 pipe_lock(pipe);
1338 ret = wait_for_space(pipe, flags);
Al Viro79fddc42016-09-17 22:38:20 -04001339 if (!ret)
1340 ret = iter_to_pipe(&from, pipe, buf_flag);
Al Viro8924fef2016-09-17 20:44:45 -04001341 pipe_unlock(pipe);
1342 if (ret > 0)
1343 wakeup_pipe_readers(pipe);
Al Virodb85a9e2016-09-17 20:25:06 -04001344 kfree(iov);
Jens Axboe35f3d142010-05-20 10:43:18 +02001345 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001346}
1347
Jens Axboe6a14b902007-06-14 13:08:55 +02001348/*
1349 * Note that vmsplice only really supports true splicing _from_ user memory
1350 * to a pipe, not the other way around. Splicing from user memory is a simple
1351 * operation that can be supported without any funky alignment restrictions
1352 * or nasty vm tricks. We simply map in the user memory and fill them into
1353 * a pipe. The reverse isn't quite as easy, though. There are two possible
1354 * solutions for that:
1355 *
1356 * - memcpy() the data internally, at which point we might as well just
1357 * do a regular read() on the buffer anyway.
1358 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1359 * has restriction limitations on both ends of the pipe).
1360 *
1361 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1362 *
1363 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01001364SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1365 unsigned long, nr_segs, unsigned int, flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001366{
Al Viro2903ff02012-08-28 12:52:22 -04001367 struct fd f;
Jens Axboe912d35f2006-04-26 10:59:21 +02001368 long error;
Jens Axboe912d35f2006-04-26 10:59:21 +02001369
Jens Axboe6a14b902007-06-14 13:08:55 +02001370 if (unlikely(nr_segs > UIO_MAXIOV))
1371 return -EINVAL;
1372 else if (unlikely(!nr_segs))
1373 return 0;
1374
Jens Axboe912d35f2006-04-26 10:59:21 +02001375 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001376 f = fdget(fd);
1377 if (f.file) {
1378 if (f.file->f_mode & FMODE_WRITE)
1379 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1380 else if (f.file->f_mode & FMODE_READ)
1381 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001382
Al Viro2903ff02012-08-28 12:52:22 -04001383 fdput(f);
Jens Axboe912d35f2006-04-26 10:59:21 +02001384 }
1385
1386 return error;
1387}
1388
Al Viro76b021d2013-03-02 10:19:56 -05001389#ifdef CONFIG_COMPAT
1390COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1391 unsigned int, nr_segs, unsigned int, flags)
1392{
1393 unsigned i;
1394 struct iovec __user *iov;
1395 if (nr_segs > UIO_MAXIOV)
1396 return -EINVAL;
1397 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1398 for (i = 0; i < nr_segs; i++) {
1399 struct compat_iovec v;
1400 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1401 get_user(v.iov_len, &iov32[i].iov_len) ||
1402 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1403 put_user(v.iov_len, &iov[i].iov_len))
1404 return -EFAULT;
1405 }
1406 return sys_vmsplice(fd, iov, nr_segs, flags);
1407}
1408#endif
1409
Heiko Carstens836f92a2009-01-14 14:14:33 +01001410SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1411 int, fd_out, loff_t __user *, off_out,
1412 size_t, len, unsigned int, flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001413{
Al Viro2903ff02012-08-28 12:52:22 -04001414 struct fd in, out;
Jens Axboe5274f052006-03-30 15:15:30 +02001415 long error;
Jens Axboe5274f052006-03-30 15:15:30 +02001416
1417 if (unlikely(!len))
1418 return 0;
1419
1420 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001421 in = fdget(fd_in);
1422 if (in.file) {
1423 if (in.file->f_mode & FMODE_READ) {
1424 out = fdget(fd_out);
1425 if (out.file) {
1426 if (out.file->f_mode & FMODE_WRITE)
1427 error = do_splice(in.file, off_in,
1428 out.file, off_out,
Ingo Molnar529565d2006-04-10 15:18:58 +02001429 len, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001430 fdput(out);
Jens Axboe5274f052006-03-30 15:15:30 +02001431 }
1432 }
Al Viro2903ff02012-08-28 12:52:22 -04001433 fdput(in);
Jens Axboe5274f052006-03-30 15:15:30 +02001434 }
Jens Axboe5274f052006-03-30 15:15:30 +02001435 return error;
1436}
Jens Axboe70524492006-04-11 15:51:17 +02001437
1438/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001439 * Make sure there's data to read. Wait for input if we can, otherwise
1440 * return an appropriate error.
1441 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001442static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001443{
1444 int ret;
1445
1446 /*
1447 * Check ->nrbufs without the inode lock first. This function
1448 * is speculative anyways, so missing one is ok.
1449 */
1450 if (pipe->nrbufs)
1451 return 0;
1452
1453 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001454 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001455
1456 while (!pipe->nrbufs) {
1457 if (signal_pending(current)) {
1458 ret = -ERESTARTSYS;
1459 break;
1460 }
1461 if (!pipe->writers)
1462 break;
1463 if (!pipe->waiting_writers) {
1464 if (flags & SPLICE_F_NONBLOCK) {
1465 ret = -EAGAIN;
1466 break;
1467 }
1468 }
1469 pipe_wait(pipe);
1470 }
1471
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001472 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001473 return ret;
1474}
1475
1476/*
1477 * Make sure there's writeable room. Wait for room if we can, otherwise
1478 * return an appropriate error.
1479 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001480static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001481{
1482 int ret;
1483
1484 /*
1485 * Check ->nrbufs without the inode lock first. This function
1486 * is speculative anyways, so missing one is ok.
1487 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001488 if (pipe->nrbufs < pipe->buffers)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001489 return 0;
1490
1491 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001492 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001493
Jens Axboe35f3d142010-05-20 10:43:18 +02001494 while (pipe->nrbufs >= pipe->buffers) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001495 if (!pipe->readers) {
1496 send_sig(SIGPIPE, current, 0);
1497 ret = -EPIPE;
1498 break;
1499 }
1500 if (flags & SPLICE_F_NONBLOCK) {
1501 ret = -EAGAIN;
1502 break;
1503 }
1504 if (signal_pending(current)) {
1505 ret = -ERESTARTSYS;
1506 break;
1507 }
1508 pipe->waiting_writers++;
1509 pipe_wait(pipe);
1510 pipe->waiting_writers--;
1511 }
1512
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001513 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001514 return ret;
1515}
1516
1517/*
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001518 * Splice contents of ipipe to opipe.
1519 */
1520static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1521 struct pipe_inode_info *opipe,
1522 size_t len, unsigned int flags)
1523{
1524 struct pipe_buffer *ibuf, *obuf;
1525 int ret = 0, nbuf;
1526 bool input_wakeup = false;
1527
1528
1529retry:
1530 ret = ipipe_prep(ipipe, flags);
1531 if (ret)
1532 return ret;
1533
1534 ret = opipe_prep(opipe, flags);
1535 if (ret)
1536 return ret;
1537
1538 /*
1539 * Potential ABBA deadlock, work around it by ordering lock
1540 * grabbing by pipe info address. Otherwise two different processes
1541 * could deadlock (one doing tee from A -> B, the other from B -> A).
1542 */
1543 pipe_double_lock(ipipe, opipe);
1544
1545 do {
1546 if (!opipe->readers) {
1547 send_sig(SIGPIPE, current, 0);
1548 if (!ret)
1549 ret = -EPIPE;
1550 break;
1551 }
1552
1553 if (!ipipe->nrbufs && !ipipe->writers)
1554 break;
1555
1556 /*
1557 * Cannot make any progress, because either the input
1558 * pipe is empty or the output pipe is full.
1559 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001560 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001561 /* Already processed some buffers, break */
1562 if (ret)
1563 break;
1564
1565 if (flags & SPLICE_F_NONBLOCK) {
1566 ret = -EAGAIN;
1567 break;
1568 }
1569
1570 /*
1571 * We raced with another reader/writer and haven't
1572 * managed to process any buffers. A zero return
1573 * value means EOF, so retry instead.
1574 */
1575 pipe_unlock(ipipe);
1576 pipe_unlock(opipe);
1577 goto retry;
1578 }
1579
1580 ibuf = ipipe->bufs + ipipe->curbuf;
Jens Axboe35f3d142010-05-20 10:43:18 +02001581 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001582 obuf = opipe->bufs + nbuf;
1583
1584 if (len >= ibuf->len) {
1585 /*
1586 * Simply move the whole buffer from ipipe to opipe
1587 */
1588 *obuf = *ibuf;
1589 ibuf->ops = NULL;
1590 opipe->nrbufs++;
Jens Axboe35f3d142010-05-20 10:43:18 +02001591 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001592 ipipe->nrbufs--;
1593 input_wakeup = true;
1594 } else {
1595 /*
1596 * Get a reference to this pipe buffer,
1597 * so we can copy the contents over.
1598 */
1599 ibuf->ops->get(ipipe, ibuf);
1600 *obuf = *ibuf;
1601
1602 /*
1603 * Don't inherit the gift flag, we need to
1604 * prevent multiple steals of this page.
1605 */
1606 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1607
1608 obuf->len = len;
1609 opipe->nrbufs++;
1610 ibuf->offset += obuf->len;
1611 ibuf->len -= obuf->len;
1612 }
1613 ret += obuf->len;
1614 len -= obuf->len;
1615 } while (len);
1616
1617 pipe_unlock(ipipe);
1618 pipe_unlock(opipe);
1619
1620 /*
1621 * If we put data in the output pipe, wakeup any potential readers.
1622 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001623 if (ret > 0)
1624 wakeup_pipe_readers(opipe);
1625
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001626 if (input_wakeup)
1627 wakeup_pipe_writers(ipipe);
1628
1629 return ret;
1630}
1631
1632/*
Jens Axboe70524492006-04-11 15:51:17 +02001633 * Link contents of ipipe to opipe.
1634 */
1635static int link_pipe(struct pipe_inode_info *ipipe,
1636 struct pipe_inode_info *opipe,
1637 size_t len, unsigned int flags)
1638{
1639 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001640 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001641
1642 /*
1643 * Potential ABBA deadlock, work around it by ordering lock
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001644 * grabbing by pipe info address. Otherwise two different processes
Jens Axboe70524492006-04-11 15:51:17 +02001645 * could deadlock (one doing tee from A -> B, the other from B -> A).
1646 */
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001647 pipe_double_lock(ipipe, opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001648
Jens Axboeaadd06e2006-07-10 11:00:01 +02001649 do {
Jens Axboe70524492006-04-11 15:51:17 +02001650 if (!opipe->readers) {
1651 send_sig(SIGPIPE, current, 0);
1652 if (!ret)
1653 ret = -EPIPE;
1654 break;
1655 }
Jens Axboe70524492006-04-11 15:51:17 +02001656
1657 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001658 * If we have iterated all input buffers or ran out of
1659 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001660 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001661 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
Jens Axboe70524492006-04-11 15:51:17 +02001662 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001663
Jens Axboe35f3d142010-05-20 10:43:18 +02001664 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1665 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001666
Jens Axboe2a27250e2006-04-19 15:56:40 +02001667 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001668 * Get a reference to this pipe buffer,
1669 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001670 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001671 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001672
Jens Axboeaadd06e2006-07-10 11:00:01 +02001673 obuf = opipe->bufs + nbuf;
1674 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001675
Jens Axboeaadd06e2006-07-10 11:00:01 +02001676 /*
1677 * Don't inherit the gift flag, we need to
1678 * prevent multiple steals of this page.
1679 */
1680 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1681
1682 if (obuf->len > len)
1683 obuf->len = len;
1684
1685 opipe->nrbufs++;
1686 ret += obuf->len;
1687 len -= obuf->len;
1688 i++;
1689 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001690
Jens Axboe02cf01a2008-02-20 10:34:51 +01001691 /*
1692 * return EAGAIN if we have the potential of some data in the
1693 * future, otherwise just return 0
1694 */
1695 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1696 ret = -EAGAIN;
1697
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001698 pipe_unlock(ipipe);
1699 pipe_unlock(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001700
Jens Axboeaadd06e2006-07-10 11:00:01 +02001701 /*
1702 * If we put data in the output pipe, wakeup any potential readers.
1703 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001704 if (ret > 0)
1705 wakeup_pipe_readers(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001706
1707 return ret;
1708}
1709
1710/*
1711 * This is a tee(1) implementation that works on pipes. It doesn't copy
1712 * any data, it simply references the 'in' pages on the 'out' pipe.
1713 * The 'flags' used are the SPLICE_F_* variants, currently the only
1714 * applicable one is SPLICE_F_NONBLOCK.
1715 */
1716static long do_tee(struct file *in, struct file *out, size_t len,
1717 unsigned int flags)
1718{
Linus Torvalds71993e62010-11-28 13:56:09 -08001719 struct pipe_inode_info *ipipe = get_pipe_info(in);
1720 struct pipe_inode_info *opipe = get_pipe_info(out);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001721 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001722
1723 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001724 * Duplicate the contents of ipipe to opipe without actually
1725 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001726 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001727 if (ipipe && opipe && ipipe != opipe) {
1728 /*
1729 * Keep going, unless we encounter an error. The ipipe/opipe
1730 * ordering doesn't really matter.
1731 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001732 ret = ipipe_prep(ipipe, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001733 if (!ret) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001734 ret = opipe_prep(opipe, flags);
Jens Axboe02cf01a2008-02-20 10:34:51 +01001735 if (!ret)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001736 ret = link_pipe(ipipe, opipe, len, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001737 }
1738 }
Jens Axboe70524492006-04-11 15:51:17 +02001739
Jens Axboeaadd06e2006-07-10 11:00:01 +02001740 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001741}
1742
Heiko Carstens836f92a2009-01-14 14:14:33 +01001743SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
Jens Axboe70524492006-04-11 15:51:17 +02001744{
Al Viro2903ff02012-08-28 12:52:22 -04001745 struct fd in;
1746 int error;
Jens Axboe70524492006-04-11 15:51:17 +02001747
1748 if (unlikely(!len))
1749 return 0;
1750
1751 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001752 in = fdget(fdin);
1753 if (in.file) {
1754 if (in.file->f_mode & FMODE_READ) {
1755 struct fd out = fdget(fdout);
1756 if (out.file) {
1757 if (out.file->f_mode & FMODE_WRITE)
1758 error = do_tee(in.file, out.file,
1759 len, flags);
1760 fdput(out);
Jens Axboe70524492006-04-11 15:51:17 +02001761 }
1762 }
Al Viro2903ff02012-08-28 12:52:22 -04001763 fdput(in);
Jens Axboe70524492006-04-11 15:51:17 +02001764 }
1765
1766 return error;
1767}