blob: e13d93531554cd38ef4839faa1a0ce662ab00048 [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
Ingo Molnar3a326a22006-04-10 15:18:35 +0200284static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200285__generic_file_splice_read(struct file *in, loff_t *ppos,
286 struct pipe_inode_info *pipe, size_t len,
287 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200288{
289 struct address_space *mapping = in->f_mapping;
Fengguang Wud8983912007-07-19 01:48:06 -0700290 unsigned int loff, nr_pages, req_pages;
Jens Axboe35f3d142010-05-20 10:43:18 +0200291 struct page *pages[PIPE_DEF_BUFFERS];
292 struct partial_page partial[PIPE_DEF_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200293 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200294 pgoff_t index, end_index;
295 loff_t isize;
Jens Axboeeb207962006-04-27 11:05:22 +0200296 int error, page_nr;
Jens Axboe912d35f2006-04-26 10:59:21 +0200297 struct splice_pipe_desc spd = {
298 .pages = pages,
299 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +0200300 .nr_pages_max = PIPE_DEF_BUFFERS,
Jens Axboe912d35f2006-04-26 10:59:21 +0200301 .flags = flags,
302 .ops = &page_cache_pipe_buf_ops,
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800303 .spd_release = spd_release_page,
Jens Axboe912d35f2006-04-26 10:59:21 +0200304 };
Jens Axboe5274f052006-03-30 15:15:30 +0200305
Jens Axboe35f3d142010-05-20 10:43:18 +0200306 if (splice_grow_spd(pipe, &spd))
307 return -ENOMEM;
308
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300309 index = *ppos >> PAGE_SHIFT;
310 loff = *ppos & ~PAGE_MASK;
311 req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
Eric Dumazet047fe362012-06-12 15:24:40 +0200312 nr_pages = min(req_pages, spd.nr_pages_max);
Jens Axboe5274f052006-03-30 15:15:30 +0200313
314 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200315 * Lookup the (hopefully) full range of pages we need.
316 */
Jens Axboe35f3d142010-05-20 10:43:18 +0200317 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
Fengguang Wu431a48202007-07-19 01:48:05 -0700318 index += spd.nr_pages;
Jens Axboeeb207962006-04-27 11:05:22 +0200319
320 /*
321 * If find_get_pages_contig() returned fewer pages than we needed,
Fengguang Wu431a48202007-07-19 01:48:05 -0700322 * readahead/allocate the rest and fill in the holes.
Jens Axboeeb207962006-04-27 11:05:22 +0200323 */
Fengguang Wu431a48202007-07-19 01:48:05 -0700324 if (spd.nr_pages < nr_pages)
Rusty Russellcf914a72007-07-19 01:48:08 -0700325 page_cache_sync_readahead(mapping, &in->f_ra, in,
326 index, req_pages - spd.nr_pages);
Fengguang Wu431a48202007-07-19 01:48:05 -0700327
Jens Axboe932cc6d2007-06-21 13:10:21 +0200328 error = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200329 while (spd.nr_pages < nr_pages) {
330 /*
331 * Page could be there, find_get_pages_contig() breaks on
332 * the first hole.
333 */
334 page = find_get_page(mapping, index);
335 if (!page) {
Jens Axboee27dedd2006-05-01 19:59:54 +0200336 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200337 * page didn't exist, allocate one.
338 */
339 page = page_cache_alloc_cold(mapping);
340 if (!page)
341 break;
342
343 error = add_to_page_cache_lru(page, mapping, index,
Michal Hockoc62d2552015-11-06 16:28:49 -0800344 mapping_gfp_constraint(mapping, GFP_KERNEL));
Jens Axboeeb207962006-04-27 11:05:22 +0200345 if (unlikely(error)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300346 put_page(page);
Jens Axboea0548872006-05-03 10:58:22 +0200347 if (error == -EEXIST)
348 continue;
Jens Axboeeb207962006-04-27 11:05:22 +0200349 break;
350 }
351 /*
352 * add_to_page_cache() locks the page, unlock it
353 * to avoid convoluting the logic below even more.
354 */
355 unlock_page(page);
356 }
357
Jens Axboe35f3d142010-05-20 10:43:18 +0200358 spd.pages[spd.nr_pages++] = page;
Jens Axboeeb207962006-04-27 11:05:22 +0200359 index++;
360 }
361
362 /*
363 * Now loop over the map and see if we need to start IO on any
364 * pages, fill in the partial map, etc.
365 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300366 index = *ppos >> PAGE_SHIFT;
Jens Axboeeb207962006-04-27 11:05:22 +0200367 nr_pages = spd.nr_pages;
368 spd.nr_pages = 0;
369 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200370 unsigned int this_len;
371
372 if (!len)
373 break;
374
375 /*
376 * this_len is the max we'll use from this page
377 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300378 this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
Jens Axboe35f3d142010-05-20 10:43:18 +0200379 page = spd.pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200380
Fengguang Wua08a1662007-07-19 01:48:03 -0700381 if (PageReadahead(page))
Rusty Russellcf914a72007-07-19 01:48:08 -0700382 page_cache_async_readahead(mapping, &in->f_ra, in,
Fengguang Wud8983912007-07-19 01:48:06 -0700383 page, index, req_pages - page_nr);
Fengguang Wua08a1662007-07-19 01:48:03 -0700384
Jens Axboe7480a902006-04-11 13:52:47 +0200385 /*
386 * If the page isn't uptodate, we may need to start io on it
387 */
388 if (!PageUptodate(page)) {
Miklos Szeredi69650312010-08-03 12:48:50 +0200389 lock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200390
391 /*
Miklos Szeredi32502b82008-07-04 09:35:17 +0200392 * Page was truncated, or invalidated by the
393 * filesystem. Redo the find/create, but this time the
394 * page is kept locked, so there's no chance of another
395 * race with truncate/invalidate.
Jens Axboe7480a902006-04-11 13:52:47 +0200396 */
397 if (!page->mapping) {
398 unlock_page(page);
Abhi Das90330e62015-12-18 14:11:36 -0600399retry_lookup:
Miklos Szeredi32502b82008-07-04 09:35:17 +0200400 page = find_or_create_page(mapping, index,
401 mapping_gfp_mask(mapping));
402
403 if (!page) {
404 error = -ENOMEM;
405 break;
406 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300407 put_page(spd.pages[page_nr]);
Jens Axboe35f3d142010-05-20 10:43:18 +0200408 spd.pages[page_nr] = page;
Jens Axboe7480a902006-04-11 13:52:47 +0200409 }
410 /*
411 * page was already under io and is now done, great
412 */
413 if (PageUptodate(page)) {
414 unlock_page(page);
415 goto fill_it;
416 }
417
Jens Axboe7480a902006-04-11 13:52:47 +0200418 /*
419 * need to read in the page
420 */
421 error = mapping->a_ops->readpage(in, page);
Jens Axboe7480a902006-04-11 13:52:47 +0200422 if (unlikely(error)) {
Jens Axboeeb207962006-04-27 11:05:22 +0200423 /*
Abhi Das90330e62015-12-18 14:11:36 -0600424 * Re-lookup the page
Jens Axboeeb207962006-04-27 11:05:22 +0200425 */
Jens Axboe7480a902006-04-11 13:52:47 +0200426 if (error == AOP_TRUNCATED_PAGE)
Abhi Das90330e62015-12-18 14:11:36 -0600427 goto retry_lookup;
Jens Axboeeb207962006-04-27 11:05:22 +0200428
Jens Axboe7480a902006-04-11 13:52:47 +0200429 break;
430 }
Jens Axboe620a3242007-06-07 09:39:42 +0200431 }
432fill_it:
433 /*
434 * i_size must be checked after PageUptodate.
435 */
436 isize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300437 end_index = (isize - 1) >> PAGE_SHIFT;
Jens Axboe620a3242007-06-07 09:39:42 +0200438 if (unlikely(!isize || index > end_index))
439 break;
440
441 /*
442 * if this is the last page, see if we need to shrink
443 * the length and stop
444 */
445 if (end_index == index) {
446 unsigned int plen;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200447
448 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200449 * max good bytes in this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200450 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300451 plen = ((isize - 1) & ~PAGE_MASK) + 1;
Jens Axboe620a3242007-06-07 09:39:42 +0200452 if (plen <= loff)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200453 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200454
455 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200456 * force quit after adding this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200457 */
Jens Axboe620a3242007-06-07 09:39:42 +0200458 this_len = min(this_len, plen - loff);
459 len = this_len;
Jens Axboe7480a902006-04-11 13:52:47 +0200460 }
Jens Axboe620a3242007-06-07 09:39:42 +0200461
Jens Axboe35f3d142010-05-20 10:43:18 +0200462 spd.partial[page_nr].offset = loff;
463 spd.partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200464 len -= this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200465 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200466 spd.nr_pages++;
467 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200468 }
469
Jens Axboeeb207962006-04-27 11:05:22 +0200470 /*
Hugh Dickins475ecad2007-06-07 09:36:00 +0200471 * Release any pages at the end, if we quit early. 'page_nr' is how far
Jens Axboeeb207962006-04-27 11:05:22 +0200472 * we got, 'nr_pages' is how many pages are in the map.
473 */
474 while (page_nr < nr_pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300475 put_page(spd.pages[page_nr++]);
476 in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
Jens Axboeeb207962006-04-27 11:05:22 +0200477
Jens Axboe912d35f2006-04-26 10:59:21 +0200478 if (spd.nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +0200479 error = splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200480
Eric Dumazet047fe362012-06-12 15:24:40 +0200481 splice_shrink_spd(&spd);
Jens Axboe7480a902006-04-11 13:52:47 +0200482 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200483}
484
Jens Axboe83f91352006-04-02 23:05:09 +0200485/**
486 * generic_file_splice_read - splice data from file to a pipe
487 * @in: file to splice from
Jens Axboe932cc6d2007-06-21 13:10:21 +0200488 * @ppos: position in @in
Jens Axboe83f91352006-04-02 23:05:09 +0200489 * @pipe: pipe to splice to
490 * @len: number of bytes to splice
491 * @flags: splice modifier flags
492 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200493 * Description:
494 * Will read pages from given file and fill them into a pipe. Can be
495 * used as long as the address_space operations for the source implements
496 * a readpage() hook.
497 *
Jens Axboe83f91352006-04-02 23:05:09 +0200498 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200499ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
500 struct pipe_inode_info *pipe, size_t len,
501 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200502{
Jens Axboed366d3982007-06-01 14:54:11 +0200503 loff_t isize, left;
Jens Axboe8191ecd2008-04-10 08:24:25 +0200504 int ret;
Jens Axboed366d3982007-06-01 14:54:11 +0200505
Boaz Harroshbe64f882015-04-15 16:15:17 -0700506 if (IS_DAX(in->f_mapping->host))
507 return default_file_splice_read(in, ppos, pipe, len, flags);
508
Jens Axboed366d3982007-06-01 14:54:11 +0200509 isize = i_size_read(in->f_mapping->host);
510 if (unlikely(*ppos >= isize))
511 return 0;
512
513 left = isize - *ppos;
514 if (unlikely(left < len))
515 len = left;
Jens Axboe5274f052006-03-30 15:15:30 +0200516
Jens Axboe8191ecd2008-04-10 08:24:25 +0200517 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
Miklos Szeredi723590e2009-08-15 08:43:22 +0200518 if (ret > 0) {
Jens Axboecbb7e572006-04-11 14:57:50 +0200519 *ppos += ret;
Miklos Szeredi723590e2009-08-15 08:43:22 +0200520 file_accessed(in);
521 }
Jens Axboe5274f052006-03-30 15:15:30 +0200522
523 return ret;
524}
Jens Axboe059a8f32006-04-02 23:06:05 +0200525EXPORT_SYMBOL(generic_file_splice_read);
526
Miklos Szeredi68181732009-05-07 15:37:36 +0200527static const struct pipe_buf_operations default_pipe_buf_ops = {
528 .can_merge = 0,
Miklos Szeredi68181732009-05-07 15:37:36 +0200529 .confirm = generic_pipe_buf_confirm,
530 .release = generic_pipe_buf_release,
531 .steal = generic_pipe_buf_steal,
532 .get = generic_pipe_buf_get,
533};
534
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100535static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
536 struct pipe_buffer *buf)
537{
538 return 1;
539}
540
541/* Pipe buffer operations for a socket and similar. */
542const struct pipe_buf_operations nosteal_pipe_buf_ops = {
543 .can_merge = 0,
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100544 .confirm = generic_pipe_buf_confirm,
545 .release = generic_pipe_buf_release,
546 .steal = generic_pipe_buf_nosteal,
547 .get = generic_pipe_buf_get,
548};
549EXPORT_SYMBOL(nosteal_pipe_buf_ops);
550
Miklos Szeredi68181732009-05-07 15:37:36 +0200551static ssize_t kernel_readv(struct file *file, const struct iovec *vec,
552 unsigned long vlen, loff_t offset)
553{
554 mm_segment_t old_fs;
555 loff_t pos = offset;
556 ssize_t res;
557
558 old_fs = get_fs();
559 set_fs(get_ds());
560 /* The cast to a user pointer is valid due to the set_fs() */
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100561 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
Miklos Szeredi68181732009-05-07 15:37:36 +0200562 set_fs(old_fs);
563
564 return res;
565}
566
Al Viro7bb307e2013-02-23 14:51:48 -0500567ssize_t kernel_write(struct file *file, const char *buf, size_t count,
Miklos Szeredib2858d72009-05-19 11:37:46 +0200568 loff_t pos)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200569{
570 mm_segment_t old_fs;
571 ssize_t res;
572
573 old_fs = get_fs();
574 set_fs(get_ds());
575 /* The cast to a user pointer is valid due to the set_fs() */
Al Viro7bb307e2013-02-23 14:51:48 -0500576 res = vfs_write(file, (__force const char __user *)buf, count, &pos);
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200577 set_fs(old_fs);
578
579 return res;
580}
Al Viro7bb307e2013-02-23 14:51:48 -0500581EXPORT_SYMBOL(kernel_write);
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +0200582
Miklos Szeredi68181732009-05-07 15:37:36 +0200583ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
584 struct pipe_inode_info *pipe, size_t len,
585 unsigned int flags)
586{
587 unsigned int nr_pages;
588 unsigned int nr_freed;
589 size_t offset;
Jens Axboe35f3d142010-05-20 10:43:18 +0200590 struct page *pages[PIPE_DEF_BUFFERS];
591 struct partial_page partial[PIPE_DEF_BUFFERS];
592 struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
Miklos Szeredi68181732009-05-07 15:37:36 +0200593 ssize_t res;
594 size_t this_len;
595 int error;
596 int i;
597 struct splice_pipe_desc spd = {
598 .pages = pages,
599 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +0200600 .nr_pages_max = PIPE_DEF_BUFFERS,
Miklos Szeredi68181732009-05-07 15:37:36 +0200601 .flags = flags,
602 .ops = &default_pipe_buf_ops,
603 .spd_release = spd_release_page,
604 };
605
Jens Axboe35f3d142010-05-20 10:43:18 +0200606 if (splice_grow_spd(pipe, &spd))
607 return -ENOMEM;
608
609 res = -ENOMEM;
610 vec = __vec;
Eric Dumazet047fe362012-06-12 15:24:40 +0200611 if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
612 vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
Jens Axboe35f3d142010-05-20 10:43:18 +0200613 if (!vec)
614 goto shrink_ret;
615 }
616
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300617 offset = *ppos & ~PAGE_MASK;
618 nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi68181732009-05-07 15:37:36 +0200619
Eric Dumazet047fe362012-06-12 15:24:40 +0200620 for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
Miklos Szeredi68181732009-05-07 15:37:36 +0200621 struct page *page;
622
Jens Axboe4f231222009-05-13 08:35:35 +0200623 page = alloc_page(GFP_USER);
Miklos Szeredi68181732009-05-07 15:37:36 +0200624 error = -ENOMEM;
625 if (!page)
626 goto err;
627
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300628 this_len = min_t(size_t, len, PAGE_SIZE - offset);
Jens Axboe4f231222009-05-13 08:35:35 +0200629 vec[i].iov_base = (void __user *) page_address(page);
Miklos Szeredi68181732009-05-07 15:37:36 +0200630 vec[i].iov_len = this_len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200631 spd.pages[i] = page;
Miklos Szeredi68181732009-05-07 15:37:36 +0200632 spd.nr_pages++;
633 len -= this_len;
634 offset = 0;
635 }
636
637 res = kernel_readv(in, vec, spd.nr_pages, *ppos);
Andrew Morton77f6bf52009-05-14 09:49:44 +0200638 if (res < 0) {
639 error = res;
Miklos Szeredi68181732009-05-07 15:37:36 +0200640 goto err;
Andrew Morton77f6bf52009-05-14 09:49:44 +0200641 }
Miklos Szeredi68181732009-05-07 15:37:36 +0200642
643 error = 0;
644 if (!res)
645 goto err;
646
647 nr_freed = 0;
648 for (i = 0; i < spd.nr_pages; i++) {
Miklos Szeredi68181732009-05-07 15:37:36 +0200649 this_len = min_t(size_t, vec[i].iov_len, res);
Jens Axboe35f3d142010-05-20 10:43:18 +0200650 spd.partial[i].offset = 0;
651 spd.partial[i].len = this_len;
Miklos Szeredi68181732009-05-07 15:37:36 +0200652 if (!this_len) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200653 __free_page(spd.pages[i]);
654 spd.pages[i] = NULL;
Miklos Szeredi68181732009-05-07 15:37:36 +0200655 nr_freed++;
656 }
657 res -= this_len;
658 }
659 spd.nr_pages -= nr_freed;
660
661 res = splice_to_pipe(pipe, &spd);
662 if (res > 0)
663 *ppos += res;
664
Jens Axboe35f3d142010-05-20 10:43:18 +0200665shrink_ret:
666 if (vec != __vec)
667 kfree(vec);
Eric Dumazet047fe362012-06-12 15:24:40 +0200668 splice_shrink_spd(&spd);
Miklos Szeredi68181732009-05-07 15:37:36 +0200669 return res;
670
671err:
Jens Axboe4f231222009-05-13 08:35:35 +0200672 for (i = 0; i < spd.nr_pages; i++)
Jens Axboe35f3d142010-05-20 10:43:18 +0200673 __free_page(spd.pages[i]);
Jens Axboe4f231222009-05-13 08:35:35 +0200674
Jens Axboe35f3d142010-05-20 10:43:18 +0200675 res = error;
676 goto shrink_ret;
Miklos Szeredi68181732009-05-07 15:37:36 +0200677}
678EXPORT_SYMBOL(default_file_splice_read);
679
Jens Axboe5274f052006-03-30 15:15:30 +0200680/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200681 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200682 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200683 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200684static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200685 struct pipe_buffer *buf, struct splice_desc *sd)
686{
Jens Axboe6a14b902007-06-14 13:08:55 +0200687 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200688 loff_t pos = sd->pos;
Michał Mirosława8adbe32010-12-17 08:56:44 +0100689 int more;
Jens Axboe5274f052006-03-30 15:15:30 +0200690
Al Viro72c2d532013-09-22 16:27:52 -0400691 if (!likely(file->f_op->sendpage))
Michał Mirosława8adbe32010-12-17 08:56:44 +0100692 return -EINVAL;
Jens Axboe5274f052006-03-30 15:15:30 +0200693
Eric Dumazet35f9c092012-04-05 03:05:35 +0000694 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000695
696 if (sd->len < sd->total_len && pipe->nrbufs > 1)
Eric Dumazet35f9c092012-04-05 03:05:35 +0000697 more |= MSG_SENDPAGE_NOTLAST;
Eric Dumazetae62ca72013-01-06 18:21:49 +0000698
Michał Mirosława8adbe32010-12-17 08:56:44 +0100699 return file->f_op->sendpage(file, buf->page, buf->offset,
700 sd->len, &pos, more);
Jens Axboe5274f052006-03-30 15:15:30 +0200701}
702
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200703static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
704{
705 smp_mb();
706 if (waitqueue_active(&pipe->wait))
707 wake_up_interruptible(&pipe->wait);
708 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
709}
710
711/**
712 * splice_from_pipe_feed - feed available data from a pipe to a file
713 * @pipe: pipe to splice from
714 * @sd: information to @actor
715 * @actor: handler that splices the data
716 *
717 * Description:
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200718 * This function loops over the pipe and calls @actor to do the
719 * actual moving of a single struct pipe_buffer to the desired
720 * destination. It returns when there's no more buffers left in
721 * the pipe or if the requested number of bytes (@sd->total_len)
722 * have been copied. It returns a positive number (one) if the
723 * pipe needs to be filled with more data, zero if the required
724 * number of bytes have been copied and -errno on error.
725 *
726 * This, together with splice_from_pipe_{begin,end,next}, may be
727 * used to implement the functionality of __splice_from_pipe() when
728 * locking is required around copying the pipe buffers to the
729 * destination.
730 */
Al Viro96f9bc82014-04-05 04:35:49 -0400731static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200732 splice_actor *actor)
733{
734 int ret;
735
736 while (pipe->nrbufs) {
737 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
738 const struct pipe_buf_operations *ops = buf->ops;
739
740 sd->len = buf->len;
741 if (sd->len > sd->total_len)
742 sd->len = sd->total_len;
743
Michał Mirosława8adbe32010-12-17 08:56:44 +0100744 ret = buf->ops->confirm(pipe, buf);
745 if (unlikely(ret)) {
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200746 if (ret == -ENODATA)
747 ret = 0;
748 return ret;
749 }
Michał Mirosława8adbe32010-12-17 08:56:44 +0100750
751 ret = actor(pipe, buf, sd);
752 if (ret <= 0)
753 return ret;
754
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200755 buf->offset += ret;
756 buf->len -= ret;
757
758 sd->num_spliced += ret;
759 sd->len -= ret;
760 sd->pos += ret;
761 sd->total_len -= ret;
762
763 if (!buf->len) {
764 buf->ops = NULL;
765 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200766 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200767 pipe->nrbufs--;
Al Viro6447a3c2013-03-21 11:01:38 -0400768 if (pipe->files)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200769 sd->need_wakeup = true;
770 }
771
772 if (!sd->total_len)
773 return 0;
774 }
775
776 return 1;
777}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200778
779/**
780 * splice_from_pipe_next - wait for some data to splice from
781 * @pipe: pipe to splice from
782 * @sd: information about the splice operation
783 *
784 * Description:
785 * This function will wait for some data and return a positive
786 * value (one) if pipe buffers are available. It will return zero
787 * or -errno if no more data needs to be spliced.
788 */
Al Viro96f9bc82014-04-05 04:35:49 -0400789static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200790{
Jan Karac725bfc2015-11-23 13:09:50 +0100791 /*
792 * Check for signal early to make process killable when there are
793 * always buffers available
794 */
795 if (signal_pending(current))
796 return -ERESTARTSYS;
797
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200798 while (!pipe->nrbufs) {
799 if (!pipe->writers)
800 return 0;
801
802 if (!pipe->waiting_writers && sd->num_spliced)
803 return 0;
804
805 if (sd->flags & SPLICE_F_NONBLOCK)
806 return -EAGAIN;
807
808 if (signal_pending(current))
809 return -ERESTARTSYS;
810
811 if (sd->need_wakeup) {
812 wakeup_pipe_writers(pipe);
813 sd->need_wakeup = false;
814 }
815
816 pipe_wait(pipe);
817 }
818
819 return 1;
820}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200821
822/**
823 * splice_from_pipe_begin - start splicing from pipe
Randy Dunlapb80901b2009-04-16 19:09:55 -0700824 * @sd: information about the splice operation
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200825 *
826 * Description:
827 * This function should be called before a loop containing
828 * splice_from_pipe_next() and splice_from_pipe_feed() to
829 * initialize the necessary fields of @sd.
830 */
Al Viro96f9bc82014-04-05 04:35:49 -0400831static void splice_from_pipe_begin(struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200832{
833 sd->num_spliced = 0;
834 sd->need_wakeup = false;
835}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200836
837/**
838 * splice_from_pipe_end - finish splicing from pipe
839 * @pipe: pipe to splice from
840 * @sd: information about the splice operation
841 *
842 * Description:
843 * This function will wake up pipe writers if necessary. It should
844 * be called after a loop containing splice_from_pipe_next() and
845 * splice_from_pipe_feed().
846 */
Al Viro96f9bc82014-04-05 04:35:49 -0400847static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200848{
849 if (sd->need_wakeup)
850 wakeup_pipe_writers(pipe);
851}
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200852
Jens Axboe932cc6d2007-06-21 13:10:21 +0200853/**
854 * __splice_from_pipe - splice data from a pipe to given actor
855 * @pipe: pipe to splice from
856 * @sd: information to @actor
857 * @actor: handler that splices the data
858 *
859 * Description:
860 * This function does little more than loop over the pipe and call
861 * @actor to do the actual moving of a single struct pipe_buffer to
862 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
863 * pipe_to_user.
864 *
Jens Axboe83f91352006-04-02 23:05:09 +0200865 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200866ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
867 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200868{
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200869 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200870
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200871 splice_from_pipe_begin(sd);
872 do {
Jan Karac2489e02015-11-23 13:09:51 +0100873 cond_resched();
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200874 ret = splice_from_pipe_next(pipe, sd);
875 if (ret > 0)
876 ret = splice_from_pipe_feed(pipe, sd, actor);
877 } while (ret > 0);
878 splice_from_pipe_end(pipe, sd);
Jens Axboe5274f052006-03-30 15:15:30 +0200879
Miklos Szeredib3c2d2d2009-04-14 19:48:36 +0200880 return sd->num_spliced ? sd->num_spliced : ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200881}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100882EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200883
Jens Axboe932cc6d2007-06-21 13:10:21 +0200884/**
885 * splice_from_pipe - splice data from a pipe to a file
886 * @pipe: pipe to splice from
887 * @out: file to splice to
888 * @ppos: position in @out
889 * @len: how many bytes to splice
890 * @flags: splice modifier flags
891 * @actor: handler that splices the data
892 *
893 * Description:
Miklos Szeredi29339702009-04-14 19:48:37 +0200894 * See __splice_from_pipe. This function locks the pipe inode,
Jens Axboe932cc6d2007-06-21 13:10:21 +0200895 * otherwise it's identical to __splice_from_pipe().
896 *
897 */
Mark Fasheh6da61802006-10-17 18:43:07 +0200898ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
899 loff_t *ppos, size_t len, unsigned int flags,
900 splice_actor *actor)
901{
902 ssize_t ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200903 struct splice_desc sd = {
904 .total_len = len,
905 .flags = flags,
906 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200907 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200908 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200909
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200910 pipe_lock(pipe);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200911 ret = __splice_from_pipe(pipe, &sd, actor);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200912 pipe_unlock(pipe);
Mark Fasheh6da61802006-10-17 18:43:07 +0200913
914 return ret;
915}
916
917/**
Al Viro8d020762014-04-05 04:27:08 -0400918 * iter_file_splice_write - splice data from a pipe to a file
919 * @pipe: pipe info
920 * @out: file to write to
921 * @ppos: position in @out
922 * @len: number of bytes to splice
923 * @flags: splice modifier flags
924 *
925 * Description:
926 * Will either move or copy pages (determined by @flags options) from
927 * the given pipe inode to the given file.
928 * This one is ->write_iter-based.
929 *
930 */
931ssize_t
932iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
933 loff_t *ppos, size_t len, unsigned int flags)
934{
935 struct splice_desc sd = {
936 .total_len = len,
937 .flags = flags,
938 .pos = *ppos,
939 .u.file = out,
940 };
941 int nbufs = pipe->buffers;
942 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
943 GFP_KERNEL);
944 ssize_t ret;
945
946 if (unlikely(!array))
947 return -ENOMEM;
948
949 pipe_lock(pipe);
950
951 splice_from_pipe_begin(&sd);
952 while (sd.total_len) {
953 struct iov_iter from;
Al Viro8d020762014-04-05 04:27:08 -0400954 size_t left;
955 int n, idx;
956
957 ret = splice_from_pipe_next(pipe, &sd);
958 if (ret <= 0)
959 break;
960
961 if (unlikely(nbufs < pipe->buffers)) {
962 kfree(array);
963 nbufs = pipe->buffers;
964 array = kcalloc(nbufs, sizeof(struct bio_vec),
965 GFP_KERNEL);
966 if (!array) {
967 ret = -ENOMEM;
968 break;
969 }
970 }
971
972 /* build the vector */
973 left = sd.total_len;
974 for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
975 struct pipe_buffer *buf = pipe->bufs + idx;
976 size_t this_len = buf->len;
977
978 if (this_len > left)
979 this_len = left;
980
981 if (idx == pipe->buffers - 1)
982 idx = -1;
983
984 ret = buf->ops->confirm(pipe, buf);
985 if (unlikely(ret)) {
986 if (ret == -ENODATA)
987 ret = 0;
988 goto done;
989 }
990
991 array[n].bv_page = buf->page;
992 array[n].bv_len = this_len;
993 array[n].bv_offset = buf->offset;
994 left -= this_len;
995 }
996
Al Viro05afcb72015-01-23 01:08:07 -0500997 iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
998 sd.total_len - left);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100999 ret = vfs_iter_write(out, &from, &sd.pos);
Al Viro8d020762014-04-05 04:27:08 -04001000 if (ret <= 0)
1001 break;
1002
1003 sd.num_spliced += ret;
1004 sd.total_len -= ret;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +01001005 *ppos = sd.pos;
Al Viro8d020762014-04-05 04:27:08 -04001006
1007 /* dismiss the fully eaten buffers, adjust the partial one */
1008 while (ret) {
1009 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
1010 if (ret >= buf->len) {
1011 const struct pipe_buf_operations *ops = buf->ops;
1012 ret -= buf->len;
1013 buf->len = 0;
1014 buf->ops = NULL;
1015 ops->release(pipe, buf);
1016 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1017 pipe->nrbufs--;
1018 if (pipe->files)
1019 sd.need_wakeup = true;
1020 } else {
1021 buf->offset += ret;
1022 buf->len -= ret;
1023 ret = 0;
1024 }
1025 }
1026 }
1027done:
1028 kfree(array);
1029 splice_from_pipe_end(pipe, &sd);
1030
1031 pipe_unlock(pipe);
1032
1033 if (sd.num_spliced)
1034 ret = sd.num_spliced;
1035
1036 return ret;
1037}
1038
1039EXPORT_SYMBOL(iter_file_splice_write);
1040
Miklos Szeredib2858d72009-05-19 11:37:46 +02001041static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1042 struct splice_desc *sd)
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001043{
Miklos Szeredib2858d72009-05-19 11:37:46 +02001044 int ret;
1045 void *data;
Al Viro06ae43f2013-03-20 13:19:30 -04001046 loff_t tmp = sd->pos;
Miklos Szeredib2858d72009-05-19 11:37:46 +02001047
Al Virofbb32752014-02-02 21:09:54 -05001048 data = kmap(buf->page);
Al Viro06ae43f2013-03-20 13:19:30 -04001049 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
Al Virofbb32752014-02-02 21:09:54 -05001050 kunmap(buf->page);
Miklos Szeredib2858d72009-05-19 11:37:46 +02001051
1052 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001053}
1054
1055static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
1056 struct file *out, loff_t *ppos,
1057 size_t len, unsigned int flags)
1058{
Miklos Szeredib2858d72009-05-19 11:37:46 +02001059 ssize_t ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001060
Miklos Szeredib2858d72009-05-19 11:37:46 +02001061 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
1062 if (ret > 0)
1063 *ppos += ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001064
Miklos Szeredib2858d72009-05-19 11:37:46 +02001065 return ret;
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001066}
1067
Jens Axboe83f91352006-04-02 23:05:09 +02001068/**
1069 * generic_splice_sendpage - splice data from a pipe to a socket
Jens Axboe932cc6d2007-06-21 13:10:21 +02001070 * @pipe: pipe to splice from
Jens Axboe83f91352006-04-02 23:05:09 +02001071 * @out: socket to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +02001072 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +02001073 * @len: number of bytes to splice
1074 * @flags: splice modifier flags
1075 *
Jens Axboe932cc6d2007-06-21 13:10:21 +02001076 * Description:
1077 * Will send @len bytes from the pipe to a network socket. No data copying
1078 * is involved.
Jens Axboe83f91352006-04-02 23:05:09 +02001079 *
1080 */
Ingo Molnar3a326a22006-04-10 15:18:35 +02001081ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +02001082 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001083{
Jens Axboe00522fb2006-04-26 14:39:29 +02001084 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +02001085}
1086
Jens Axboe059a8f32006-04-02 23:06:05 +02001087EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -05001088
Jens Axboe83f91352006-04-02 23:05:09 +02001089/*
1090 * Attempt to initiate a splice from pipe to file.
1091 */
Ingo Molnar3a326a22006-04-10 15:18:35 +02001092static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +02001093 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001094{
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001095 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
1096 loff_t *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +02001097
Al Viro72c2d532013-09-22 16:27:52 -04001098 if (out->f_op->splice_write)
Changli Gaocc56f7d2009-11-04 09:09:52 +01001099 splice_write = out->f_op->splice_write;
1100 else
Miklos Szeredi0b0a47f2009-05-07 15:37:37 +02001101 splice_write = default_file_splice_write;
1102
Al Viro500368f2013-05-23 20:07:11 -04001103 return splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001104}
1105
Jens Axboe83f91352006-04-02 23:05:09 +02001106/*
1107 * Attempt to initiate a splice from a file to a pipe.
1108 */
Jens Axboecbb7e572006-04-11 14:57:50 +02001109static long do_splice_to(struct file *in, loff_t *ppos,
1110 struct pipe_inode_info *pipe, size_t len,
1111 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001112{
Miklos Szeredi68181732009-05-07 15:37:36 +02001113 ssize_t (*splice_read)(struct file *, loff_t *,
1114 struct pipe_inode_info *, size_t, unsigned int);
Jens Axboe5274f052006-03-30 15:15:30 +02001115 int ret;
1116
Jens Axboe49570e92006-04-11 13:56:09 +02001117 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +02001118 return -EBADF;
1119
Jens Axboecbb7e572006-04-11 14:57:50 +02001120 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +02001121 if (unlikely(ret < 0))
1122 return ret;
1123
Al Viro03cc0782016-04-02 14:56:58 -04001124 if (unlikely(len > MAX_RW_COUNT))
1125 len = MAX_RW_COUNT;
1126
Al Viro72c2d532013-09-22 16:27:52 -04001127 if (in->f_op->splice_read)
Changli Gaocc56f7d2009-11-04 09:09:52 +01001128 splice_read = in->f_op->splice_read;
1129 else
Miklos Szeredi68181732009-05-07 15:37:36 +02001130 splice_read = default_file_splice_read;
1131
1132 return splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001133}
1134
Jens Axboe932cc6d2007-06-21 13:10:21 +02001135/**
1136 * splice_direct_to_actor - splices data directly between two non-pipes
1137 * @in: file to splice from
1138 * @sd: actor information on where to splice to
1139 * @actor: handles the data splicing
1140 *
1141 * Description:
1142 * This is a special case helper to splice directly between two
1143 * points, without requiring an explicit pipe. Internally an allocated
Randy Dunlap79685b82007-07-27 08:08:51 +02001144 * pipe is cached in the process, and reused during the lifetime of
Jens Axboe932cc6d2007-06-21 13:10:21 +02001145 * that process.
1146 *
Jens Axboec66ab6f2007-06-12 21:17:17 +02001147 */
1148ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
1149 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +02001150{
1151 struct pipe_inode_info *pipe;
1152 long ret, bytes;
1153 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001154 size_t len;
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001155 int i, flags, more;
Jens Axboeb92ce552006-04-11 13:52:07 +02001156
1157 /*
1158 * We require the input being a regular file, as we don't want to
1159 * randomly drop data for eg socket -> socket splicing. Use the
1160 * piped splicing for that!
1161 */
Al Viro496ad9a2013-01-23 17:07:38 -05001162 i_mode = file_inode(in)->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +02001163 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
1164 return -EINVAL;
1165
1166 /*
1167 * neither in nor out is a pipe, setup an internal pipe attached to
1168 * 'out' and transfer the wanted data from 'in' to 'out' through that
1169 */
1170 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +02001171 if (unlikely(!pipe)) {
Al Viro7bee1302013-03-21 11:04:15 -04001172 pipe = alloc_pipe_info();
Jens Axboeb92ce552006-04-11 13:52:07 +02001173 if (!pipe)
1174 return -ENOMEM;
1175
1176 /*
1177 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +02001178 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +02001179 * PIPE_READERS appropriately.
1180 */
1181 pipe->readers = 1;
1182
1183 current->splice_pipe = pipe;
1184 }
1185
1186 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +02001187 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +02001188 */
1189 ret = 0;
1190 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001191 len = sd->total_len;
1192 flags = sd->flags;
1193
1194 /*
1195 * Don't block on output, we have to drain the direct pipe.
1196 */
1197 sd->flags &= ~SPLICE_F_NONBLOCK;
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001198 more = sd->flags & SPLICE_F_MORE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001199
1200 while (len) {
Jens Axboe51a92c02007-07-13 14:11:43 +02001201 size_t read_len;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001202 loff_t pos = sd->pos, prev_pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001203
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001204 ret = do_splice_to(in, &pos, pipe, len, flags);
Jens Axboe51a92c02007-07-13 14:11:43 +02001205 if (unlikely(ret <= 0))
Jens Axboeb92ce552006-04-11 13:52:07 +02001206 goto out_release;
1207
1208 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001209 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +02001210
1211 /*
Christophe Leroy0ff28d92015-05-06 17:26:47 +02001212 * If more data is pending, set SPLICE_F_MORE
1213 * If this is the last data and SPLICE_F_MORE was not set
1214 * initially, clears it.
1215 */
1216 if (read_len < len)
1217 sd->flags |= SPLICE_F_MORE;
1218 else if (!more)
1219 sd->flags &= ~SPLICE_F_MORE;
1220 /*
Jens Axboeb92ce552006-04-11 13:52:07 +02001221 * NOTE: nonblocking mode only applies to the input. We
1222 * must not do the output in nonblocking mode as then we
1223 * could get stuck data in the internal pipe:
1224 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001225 ret = actor(pipe, sd);
Tom Zanussia82c53a2008-05-09 13:28:36 +02001226 if (unlikely(ret <= 0)) {
1227 sd->pos = prev_pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001228 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001229 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001230
1231 bytes += ret;
1232 len -= ret;
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001233 sd->pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001234
Tom Zanussia82c53a2008-05-09 13:28:36 +02001235 if (ret < read_len) {
1236 sd->pos = prev_pos + ret;
Jens Axboe51a92c02007-07-13 14:11:43 +02001237 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001238 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001239 }
1240
Jens Axboe9e971982008-01-29 21:05:57 +01001241done:
Jens Axboeb92ce552006-04-11 13:52:07 +02001242 pipe->nrbufs = pipe->curbuf = 0;
Jens Axboe80848702008-01-30 12:24:48 +01001243 file_accessed(in);
Jens Axboeb92ce552006-04-11 13:52:07 +02001244 return bytes;
1245
1246out_release:
1247 /*
1248 * If we did an incomplete transfer we must release
1249 * the pipe buffers in question:
1250 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001251 for (i = 0; i < pipe->buffers; i++) {
Jens Axboeb92ce552006-04-11 13:52:07 +02001252 struct pipe_buffer *buf = pipe->bufs + i;
1253
1254 if (buf->ops) {
1255 buf->ops->release(pipe, buf);
1256 buf->ops = NULL;
1257 }
1258 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001259
Jens Axboe9e971982008-01-29 21:05:57 +01001260 if (!bytes)
1261 bytes = ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001262
Jens Axboe9e971982008-01-29 21:05:57 +01001263 goto done;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001264}
1265EXPORT_SYMBOL(splice_direct_to_actor);
1266
1267static int direct_splice_actor(struct pipe_inode_info *pipe,
1268 struct splice_desc *sd)
1269{
Jens Axboe6a14b902007-06-14 13:08:55 +02001270 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001271
Al Viro7995bd22013-06-20 18:58:36 +04001272 return do_splice_from(pipe, file, sd->opos, sd->total_len,
Changli Gao2cb4b052010-06-29 13:09:18 +02001273 sd->flags);
Jens Axboec66ab6f2007-06-12 21:17:17 +02001274}
1275
Jens Axboe932cc6d2007-06-21 13:10:21 +02001276/**
1277 * do_splice_direct - splices data directly between two files
1278 * @in: file to splice from
1279 * @ppos: input file offset
1280 * @out: file to splice to
Randy Dunlapacdb37c2013-06-22 19:44:08 -07001281 * @opos: output file offset
Jens Axboe932cc6d2007-06-21 13:10:21 +02001282 * @len: number of bytes to splice
1283 * @flags: splice modifier flags
1284 *
1285 * Description:
1286 * For use by do_sendfile(). splice can easily emulate sendfile, but
1287 * doing it in the application would incur an extra system call
1288 * (splice in + splice out, as compared to just sendfile()). So this helper
1289 * can splice directly through a process-private pipe.
1290 *
1291 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001292long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
Al Viro7995bd22013-06-20 18:58:36 +04001293 loff_t *opos, size_t len, unsigned int flags)
Jens Axboec66ab6f2007-06-12 21:17:17 +02001294{
1295 struct splice_desc sd = {
1296 .len = len,
1297 .total_len = len,
1298 .flags = flags,
1299 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001300 .u.file = out,
Al Viro7995bd22013-06-20 18:58:36 +04001301 .opos = opos,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001302 };
Jens Axboe51a92c02007-07-13 14:11:43 +02001303 long ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001304
Al Viro18c67cb2013-06-19 15:41:54 +04001305 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1306 return -EBADF;
1307
1308 if (unlikely(out->f_flags & O_APPEND))
1309 return -EINVAL;
1310
1311 ret = rw_verify_area(WRITE, out, opos, len);
1312 if (unlikely(ret < 0))
1313 return ret;
1314
Jens Axboec66ab6f2007-06-12 21:17:17 +02001315 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
Jens Axboe51a92c02007-07-13 14:11:43 +02001316 if (ret > 0)
Tom Zanussia82c53a2008-05-09 13:28:36 +02001317 *ppos = sd.pos;
Jens Axboe51a92c02007-07-13 14:11:43 +02001318
Jens Axboec66ab6f2007-06-12 21:17:17 +02001319 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001320}
Miklos Szeredi1c118592014-10-24 00:14:35 +02001321EXPORT_SYMBOL(do_splice_direct);
Jens Axboeb92ce552006-04-11 13:52:07 +02001322
Al Viro8924fef2016-09-17 20:44:45 -04001323static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1324{
1325 while (pipe->nrbufs == pipe->buffers) {
1326 if (flags & SPLICE_F_NONBLOCK)
1327 return -EAGAIN;
1328 if (signal_pending(current))
1329 return -ERESTARTSYS;
1330 pipe->waiting_writers++;
1331 pipe_wait(pipe);
1332 pipe->waiting_writers--;
1333 }
1334 return 0;
1335}
1336
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001337static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1338 struct pipe_inode_info *opipe,
1339 size_t len, unsigned int flags);
Jens Axboeddac0d32006-11-04 12:49:32 +01001340
1341/*
Jens Axboe83f91352006-04-02 23:05:09 +02001342 * Determine where to splice to/from.
1343 */
Ingo Molnar529565d2006-04-10 15:18:58 +02001344static long do_splice(struct file *in, loff_t __user *off_in,
1345 struct file *out, loff_t __user *off_out,
1346 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001347{
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001348 struct pipe_inode_info *ipipe;
1349 struct pipe_inode_info *opipe;
Al Viro7995bd22013-06-20 18:58:36 +04001350 loff_t offset;
Jens Axboea4514eb2006-04-19 15:57:05 +02001351 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001352
Linus Torvalds71993e62010-11-28 13:56:09 -08001353 ipipe = get_pipe_info(in);
1354 opipe = get_pipe_info(out);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001355
1356 if (ipipe && opipe) {
1357 if (off_in || off_out)
1358 return -ESPIPE;
1359
1360 if (!(in->f_mode & FMODE_READ))
1361 return -EBADF;
1362
1363 if (!(out->f_mode & FMODE_WRITE))
1364 return -EBADF;
1365
1366 /* Splicing to self would be fun, but... */
1367 if (ipipe == opipe)
1368 return -EINVAL;
1369
1370 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1371 }
1372
1373 if (ipipe) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001374 if (off_in)
1375 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001376 if (off_out) {
Changli Gao19c9a492010-06-29 13:10:36 +02001377 if (!(out->f_mode & FMODE_PWRITE))
Jens Axboeb92ce552006-04-11 13:52:07 +02001378 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001379 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001380 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001381 } else {
1382 offset = out->f_pos;
1383 }
Ingo Molnar529565d2006-04-10 15:18:58 +02001384
Al Viro18c67cb2013-06-19 15:41:54 +04001385 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1386 return -EBADF;
1387
1388 if (unlikely(out->f_flags & O_APPEND))
1389 return -EINVAL;
1390
1391 ret = rw_verify_area(WRITE, out, &offset, len);
1392 if (unlikely(ret < 0))
1393 return ret;
1394
Al Viro500368f2013-05-23 20:07:11 -04001395 file_start_write(out);
Al Viro7995bd22013-06-20 18:58:36 +04001396 ret = do_splice_from(ipipe, out, &offset, len, flags);
Al Viro500368f2013-05-23 20:07:11 -04001397 file_end_write(out);
Jens Axboea4514eb2006-04-19 15:57:05 +02001398
Al Viro7995bd22013-06-20 18:58:36 +04001399 if (!off_out)
1400 out->f_pos = offset;
1401 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001402 ret = -EFAULT;
1403
1404 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001405 }
Jens Axboe5274f052006-03-30 15:15:30 +02001406
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001407 if (opipe) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001408 if (off_out)
1409 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001410 if (off_in) {
Changli Gao19c9a492010-06-29 13:10:36 +02001411 if (!(in->f_mode & FMODE_PREAD))
Jens Axboeb92ce552006-04-11 13:52:07 +02001412 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001413 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001414 return -EFAULT;
Al Viro7995bd22013-06-20 18:58:36 +04001415 } else {
1416 offset = in->f_pos;
1417 }
Ingo Molnar529565d2006-04-10 15:18:58 +02001418
Al Viro8924fef2016-09-17 20:44:45 -04001419 pipe_lock(opipe);
1420 ret = wait_for_space(opipe, flags);
1421 if (!ret)
1422 ret = do_splice_to(in, &offset, opipe, len, flags);
1423 pipe_unlock(opipe);
1424 if (ret > 0)
1425 wakeup_pipe_readers(opipe);
Al Viro7995bd22013-06-20 18:58:36 +04001426 if (!off_in)
1427 in->f_pos = offset;
1428 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
Jens Axboea4514eb2006-04-19 15:57:05 +02001429 ret = -EFAULT;
1430
1431 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001432 }
Jens Axboe5274f052006-03-30 15:15:30 +02001433
1434 return -EINVAL;
1435}
1436
Al Viro79fddc42016-09-17 22:38:20 -04001437static int iter_to_pipe(struct iov_iter *from,
1438 struct pipe_inode_info *pipe,
1439 unsigned flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001440{
Al Viro79fddc42016-09-17 22:38:20 -04001441 struct pipe_buffer buf = {
1442 .ops = &user_page_pipe_buf_ops,
1443 .flags = flags
1444 };
1445 size_t total = 0;
1446 int ret = 0;
1447 bool failed = false;
1448
1449 while (iov_iter_count(from) && !failed) {
1450 struct page *pages[16];
Al Virodb85a9e2016-09-17 20:25:06 -04001451 ssize_t copied;
1452 size_t start;
Al Viro79fddc42016-09-17 22:38:20 -04001453 int n;
Jens Axboe912d35f2006-04-26 10:59:21 +02001454
Al Viro79fddc42016-09-17 22:38:20 -04001455 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1456 if (copied <= 0) {
1457 ret = copied;
1458 break;
1459 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001460
Al Viro79fddc42016-09-17 22:38:20 -04001461 for (n = 0; copied; n++, start = 0) {
Al Virodb85a9e2016-09-17 20:25:06 -04001462 int size = min_t(int, copied, PAGE_SIZE - start);
Al Viro79fddc42016-09-17 22:38:20 -04001463 if (!failed) {
1464 buf.page = pages[n];
1465 buf.offset = start;
1466 buf.len = size;
1467 ret = add_to_pipe(pipe, &buf);
1468 if (unlikely(ret < 0)) {
1469 failed = true;
1470 } else {
1471 iov_iter_advance(from, ret);
1472 total += ret;
1473 }
1474 } else {
1475 put_page(pages[n]);
1476 }
Al Virodb85a9e2016-09-17 20:25:06 -04001477 copied -= size;
Jens Axboe912d35f2006-04-26 10:59:21 +02001478 }
Jens Axboe912d35f2006-04-26 10:59:21 +02001479 }
Al Viro79fddc42016-09-17 22:38:20 -04001480 return total ? total : ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001481}
1482
Jens Axboe6a14b902007-06-14 13:08:55 +02001483static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1484 struct splice_desc *sd)
1485{
Al Viro6130f532014-02-03 18:19:51 -05001486 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1487 return n == sd->len ? n : -EFAULT;
Jens Axboe6a14b902007-06-14 13:08:55 +02001488}
1489
1490/*
1491 * For lack of a better implementation, implement vmsplice() to userspace
1492 * as a simple copy of the pipes pages to the user iov.
1493 */
Al Viro6130f532014-02-03 18:19:51 -05001494static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001495 unsigned long nr_segs, unsigned int flags)
1496{
1497 struct pipe_inode_info *pipe;
1498 struct splice_desc sd;
Jens Axboe6a14b902007-06-14 13:08:55 +02001499 long ret;
Al Viro6130f532014-02-03 18:19:51 -05001500 struct iovec iovstack[UIO_FASTIOV];
1501 struct iovec *iov = iovstack;
1502 struct iov_iter iter;
Jens Axboe6a14b902007-06-14 13:08:55 +02001503
Linus Torvalds71993e62010-11-28 13:56:09 -08001504 pipe = get_pipe_info(file);
Jens Axboe6a14b902007-06-14 13:08:55 +02001505 if (!pipe)
1506 return -EBADF;
1507
Al Viro345995f2015-03-21 19:17:55 -04001508 ret = import_iovec(READ, uiov, nr_segs,
1509 ARRAY_SIZE(iovstack), &iov, &iter);
1510 if (ret < 0)
1511 return ret;
Al Viro6130f532014-02-03 18:19:51 -05001512
Al Viro345995f2015-03-21 19:17:55 -04001513 sd.total_len = iov_iter_count(&iter);
Al Viro6130f532014-02-03 18:19:51 -05001514 sd.len = 0;
Al Viro6130f532014-02-03 18:19:51 -05001515 sd.flags = flags;
1516 sd.u.data = &iter;
1517 sd.pos = 0;
1518
Al Viro345995f2015-03-21 19:17:55 -04001519 if (sd.total_len) {
1520 pipe_lock(pipe);
1521 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1522 pipe_unlock(pipe);
1523 }
Jens Axboe6a14b902007-06-14 13:08:55 +02001524
Al Viro345995f2015-03-21 19:17:55 -04001525 kfree(iov);
Jens Axboe6a14b902007-06-14 13:08:55 +02001526 return ret;
1527}
1528
Jens Axboe912d35f2006-04-26 10:59:21 +02001529/*
1530 * vmsplice splices a user address range into a pipe. It can be thought of
1531 * as splice-from-memory, where the regular splice is splice-from-file (or
1532 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001533 */
Al Virodb85a9e2016-09-17 20:25:06 -04001534static long vmsplice_to_pipe(struct file *file, const struct iovec __user *uiov,
Jens Axboe6a14b902007-06-14 13:08:55 +02001535 unsigned long nr_segs, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001536{
Jens Axboeddac0d32006-11-04 12:49:32 +01001537 struct pipe_inode_info *pipe;
Al Virodb85a9e2016-09-17 20:25:06 -04001538 struct iovec iovstack[UIO_FASTIOV];
1539 struct iovec *iov = iovstack;
1540 struct iov_iter from;
Jens Axboe35f3d142010-05-20 10:43:18 +02001541 long ret;
Al Viro79fddc42016-09-17 22:38:20 -04001542 unsigned buf_flag = 0;
1543
1544 if (flags & SPLICE_F_GIFT)
1545 buf_flag = PIPE_BUF_FLAG_GIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +02001546
Linus Torvalds71993e62010-11-28 13:56:09 -08001547 pipe = get_pipe_info(file);
Jens Axboeddac0d32006-11-04 12:49:32 +01001548 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001549 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001550
Al Virodb85a9e2016-09-17 20:25:06 -04001551 ret = import_iovec(WRITE, uiov, nr_segs,
1552 ARRAY_SIZE(iovstack), &iov, &from);
1553 if (ret < 0)
1554 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001555
Al Viro8924fef2016-09-17 20:44:45 -04001556 pipe_lock(pipe);
1557 ret = wait_for_space(pipe, flags);
Al Viro79fddc42016-09-17 22:38:20 -04001558 if (!ret)
1559 ret = iter_to_pipe(&from, pipe, buf_flag);
Al Viro8924fef2016-09-17 20:44:45 -04001560 pipe_unlock(pipe);
1561 if (ret > 0)
1562 wakeup_pipe_readers(pipe);
Al Virodb85a9e2016-09-17 20:25:06 -04001563 kfree(iov);
Jens Axboe35f3d142010-05-20 10:43:18 +02001564 return ret;
Jens Axboe912d35f2006-04-26 10:59:21 +02001565}
1566
Jens Axboe6a14b902007-06-14 13:08:55 +02001567/*
1568 * Note that vmsplice only really supports true splicing _from_ user memory
1569 * to a pipe, not the other way around. Splicing from user memory is a simple
1570 * operation that can be supported without any funky alignment restrictions
1571 * or nasty vm tricks. We simply map in the user memory and fill them into
1572 * a pipe. The reverse isn't quite as easy, though. There are two possible
1573 * solutions for that:
1574 *
1575 * - memcpy() the data internally, at which point we might as well just
1576 * do a regular read() on the buffer anyway.
1577 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1578 * has restriction limitations on both ends of the pipe).
1579 *
1580 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1581 *
1582 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01001583SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1584 unsigned long, nr_segs, unsigned int, flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001585{
Al Viro2903ff02012-08-28 12:52:22 -04001586 struct fd f;
Jens Axboe912d35f2006-04-26 10:59:21 +02001587 long error;
Jens Axboe912d35f2006-04-26 10:59:21 +02001588
Jens Axboe6a14b902007-06-14 13:08:55 +02001589 if (unlikely(nr_segs > UIO_MAXIOV))
1590 return -EINVAL;
1591 else if (unlikely(!nr_segs))
1592 return 0;
1593
Jens Axboe912d35f2006-04-26 10:59:21 +02001594 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001595 f = fdget(fd);
1596 if (f.file) {
1597 if (f.file->f_mode & FMODE_WRITE)
1598 error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
1599 else if (f.file->f_mode & FMODE_READ)
1600 error = vmsplice_to_user(f.file, iov, nr_segs, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001601
Al Viro2903ff02012-08-28 12:52:22 -04001602 fdput(f);
Jens Axboe912d35f2006-04-26 10:59:21 +02001603 }
1604
1605 return error;
1606}
1607
Al Viro76b021d2013-03-02 10:19:56 -05001608#ifdef CONFIG_COMPAT
1609COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1610 unsigned int, nr_segs, unsigned int, flags)
1611{
1612 unsigned i;
1613 struct iovec __user *iov;
1614 if (nr_segs > UIO_MAXIOV)
1615 return -EINVAL;
1616 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1617 for (i = 0; i < nr_segs; i++) {
1618 struct compat_iovec v;
1619 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1620 get_user(v.iov_len, &iov32[i].iov_len) ||
1621 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1622 put_user(v.iov_len, &iov[i].iov_len))
1623 return -EFAULT;
1624 }
1625 return sys_vmsplice(fd, iov, nr_segs, flags);
1626}
1627#endif
1628
Heiko Carstens836f92a2009-01-14 14:14:33 +01001629SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1630 int, fd_out, loff_t __user *, off_out,
1631 size_t, len, unsigned int, flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001632{
Al Viro2903ff02012-08-28 12:52:22 -04001633 struct fd in, out;
Jens Axboe5274f052006-03-30 15:15:30 +02001634 long error;
Jens Axboe5274f052006-03-30 15:15:30 +02001635
1636 if (unlikely(!len))
1637 return 0;
1638
1639 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001640 in = fdget(fd_in);
1641 if (in.file) {
1642 if (in.file->f_mode & FMODE_READ) {
1643 out = fdget(fd_out);
1644 if (out.file) {
1645 if (out.file->f_mode & FMODE_WRITE)
1646 error = do_splice(in.file, off_in,
1647 out.file, off_out,
Ingo Molnar529565d2006-04-10 15:18:58 +02001648 len, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001649 fdput(out);
Jens Axboe5274f052006-03-30 15:15:30 +02001650 }
1651 }
Al Viro2903ff02012-08-28 12:52:22 -04001652 fdput(in);
Jens Axboe5274f052006-03-30 15:15:30 +02001653 }
Jens Axboe5274f052006-03-30 15:15:30 +02001654 return error;
1655}
Jens Axboe70524492006-04-11 15:51:17 +02001656
1657/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001658 * Make sure there's data to read. Wait for input if we can, otherwise
1659 * return an appropriate error.
1660 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001661static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001662{
1663 int ret;
1664
1665 /*
1666 * Check ->nrbufs without the inode lock first. This function
1667 * is speculative anyways, so missing one is ok.
1668 */
1669 if (pipe->nrbufs)
1670 return 0;
1671
1672 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001673 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001674
1675 while (!pipe->nrbufs) {
1676 if (signal_pending(current)) {
1677 ret = -ERESTARTSYS;
1678 break;
1679 }
1680 if (!pipe->writers)
1681 break;
1682 if (!pipe->waiting_writers) {
1683 if (flags & SPLICE_F_NONBLOCK) {
1684 ret = -EAGAIN;
1685 break;
1686 }
1687 }
1688 pipe_wait(pipe);
1689 }
1690
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001691 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001692 return ret;
1693}
1694
1695/*
1696 * Make sure there's writeable room. Wait for room if we can, otherwise
1697 * return an appropriate error.
1698 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001699static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001700{
1701 int ret;
1702
1703 /*
1704 * Check ->nrbufs without the inode lock first. This function
1705 * is speculative anyways, so missing one is ok.
1706 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001707 if (pipe->nrbufs < pipe->buffers)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001708 return 0;
1709
1710 ret = 0;
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001711 pipe_lock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001712
Jens Axboe35f3d142010-05-20 10:43:18 +02001713 while (pipe->nrbufs >= pipe->buffers) {
Jens Axboeaadd06e2006-07-10 11:00:01 +02001714 if (!pipe->readers) {
1715 send_sig(SIGPIPE, current, 0);
1716 ret = -EPIPE;
1717 break;
1718 }
1719 if (flags & SPLICE_F_NONBLOCK) {
1720 ret = -EAGAIN;
1721 break;
1722 }
1723 if (signal_pending(current)) {
1724 ret = -ERESTARTSYS;
1725 break;
1726 }
1727 pipe->waiting_writers++;
1728 pipe_wait(pipe);
1729 pipe->waiting_writers--;
1730 }
1731
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001732 pipe_unlock(pipe);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001733 return ret;
1734}
1735
1736/*
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001737 * Splice contents of ipipe to opipe.
1738 */
1739static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1740 struct pipe_inode_info *opipe,
1741 size_t len, unsigned int flags)
1742{
1743 struct pipe_buffer *ibuf, *obuf;
1744 int ret = 0, nbuf;
1745 bool input_wakeup = false;
1746
1747
1748retry:
1749 ret = ipipe_prep(ipipe, flags);
1750 if (ret)
1751 return ret;
1752
1753 ret = opipe_prep(opipe, flags);
1754 if (ret)
1755 return ret;
1756
1757 /*
1758 * Potential ABBA deadlock, work around it by ordering lock
1759 * grabbing by pipe info address. Otherwise two different processes
1760 * could deadlock (one doing tee from A -> B, the other from B -> A).
1761 */
1762 pipe_double_lock(ipipe, opipe);
1763
1764 do {
1765 if (!opipe->readers) {
1766 send_sig(SIGPIPE, current, 0);
1767 if (!ret)
1768 ret = -EPIPE;
1769 break;
1770 }
1771
1772 if (!ipipe->nrbufs && !ipipe->writers)
1773 break;
1774
1775 /*
1776 * Cannot make any progress, because either the input
1777 * pipe is empty or the output pipe is full.
1778 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001779 if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001780 /* Already processed some buffers, break */
1781 if (ret)
1782 break;
1783
1784 if (flags & SPLICE_F_NONBLOCK) {
1785 ret = -EAGAIN;
1786 break;
1787 }
1788
1789 /*
1790 * We raced with another reader/writer and haven't
1791 * managed to process any buffers. A zero return
1792 * value means EOF, so retry instead.
1793 */
1794 pipe_unlock(ipipe);
1795 pipe_unlock(opipe);
1796 goto retry;
1797 }
1798
1799 ibuf = ipipe->bufs + ipipe->curbuf;
Jens Axboe35f3d142010-05-20 10:43:18 +02001800 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001801 obuf = opipe->bufs + nbuf;
1802
1803 if (len >= ibuf->len) {
1804 /*
1805 * Simply move the whole buffer from ipipe to opipe
1806 */
1807 *obuf = *ibuf;
1808 ibuf->ops = NULL;
1809 opipe->nrbufs++;
Jens Axboe35f3d142010-05-20 10:43:18 +02001810 ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001811 ipipe->nrbufs--;
1812 input_wakeup = true;
1813 } else {
1814 /*
1815 * Get a reference to this pipe buffer,
1816 * so we can copy the contents over.
1817 */
1818 ibuf->ops->get(ipipe, ibuf);
1819 *obuf = *ibuf;
1820
1821 /*
1822 * Don't inherit the gift flag, we need to
1823 * prevent multiple steals of this page.
1824 */
1825 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1826
1827 obuf->len = len;
1828 opipe->nrbufs++;
1829 ibuf->offset += obuf->len;
1830 ibuf->len -= obuf->len;
1831 }
1832 ret += obuf->len;
1833 len -= obuf->len;
1834 } while (len);
1835
1836 pipe_unlock(ipipe);
1837 pipe_unlock(opipe);
1838
1839 /*
1840 * If we put data in the output pipe, wakeup any potential readers.
1841 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001842 if (ret > 0)
1843 wakeup_pipe_readers(opipe);
1844
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001845 if (input_wakeup)
1846 wakeup_pipe_writers(ipipe);
1847
1848 return ret;
1849}
1850
1851/*
Jens Axboe70524492006-04-11 15:51:17 +02001852 * Link contents of ipipe to opipe.
1853 */
1854static int link_pipe(struct pipe_inode_info *ipipe,
1855 struct pipe_inode_info *opipe,
1856 size_t len, unsigned int flags)
1857{
1858 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001859 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001860
1861 /*
1862 * Potential ABBA deadlock, work around it by ordering lock
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001863 * grabbing by pipe info address. Otherwise two different processes
Jens Axboe70524492006-04-11 15:51:17 +02001864 * could deadlock (one doing tee from A -> B, the other from B -> A).
1865 */
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001866 pipe_double_lock(ipipe, opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001867
Jens Axboeaadd06e2006-07-10 11:00:01 +02001868 do {
Jens Axboe70524492006-04-11 15:51:17 +02001869 if (!opipe->readers) {
1870 send_sig(SIGPIPE, current, 0);
1871 if (!ret)
1872 ret = -EPIPE;
1873 break;
1874 }
Jens Axboe70524492006-04-11 15:51:17 +02001875
1876 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001877 * If we have iterated all input buffers or ran out of
1878 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001879 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001880 if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
Jens Axboe70524492006-04-11 15:51:17 +02001881 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001882
Jens Axboe35f3d142010-05-20 10:43:18 +02001883 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
1884 nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001885
Jens Axboe2a27250e2006-04-19 15:56:40 +02001886 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001887 * Get a reference to this pipe buffer,
1888 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001889 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001890 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001891
Jens Axboeaadd06e2006-07-10 11:00:01 +02001892 obuf = opipe->bufs + nbuf;
1893 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001894
Jens Axboeaadd06e2006-07-10 11:00:01 +02001895 /*
1896 * Don't inherit the gift flag, we need to
1897 * prevent multiple steals of this page.
1898 */
1899 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1900
1901 if (obuf->len > len)
1902 obuf->len = len;
1903
1904 opipe->nrbufs++;
1905 ret += obuf->len;
1906 len -= obuf->len;
1907 i++;
1908 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001909
Jens Axboe02cf01a2008-02-20 10:34:51 +01001910 /*
1911 * return EAGAIN if we have the potential of some data in the
1912 * future, otherwise just return 0
1913 */
1914 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1915 ret = -EAGAIN;
1916
Miklos Szeredi61e0d472009-04-14 19:48:41 +02001917 pipe_unlock(ipipe);
1918 pipe_unlock(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001919
Jens Axboeaadd06e2006-07-10 11:00:01 +02001920 /*
1921 * If we put data in the output pipe, wakeup any potential readers.
1922 */
Namhyung Kim825cdcb2011-05-23 19:58:53 +02001923 if (ret > 0)
1924 wakeup_pipe_readers(opipe);
Jens Axboe70524492006-04-11 15:51:17 +02001925
1926 return ret;
1927}
1928
1929/*
1930 * This is a tee(1) implementation that works on pipes. It doesn't copy
1931 * any data, it simply references the 'in' pages on the 'out' pipe.
1932 * The 'flags' used are the SPLICE_F_* variants, currently the only
1933 * applicable one is SPLICE_F_NONBLOCK.
1934 */
1935static long do_tee(struct file *in, struct file *out, size_t len,
1936 unsigned int flags)
1937{
Linus Torvalds71993e62010-11-28 13:56:09 -08001938 struct pipe_inode_info *ipipe = get_pipe_info(in);
1939 struct pipe_inode_info *opipe = get_pipe_info(out);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001940 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001941
1942 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001943 * Duplicate the contents of ipipe to opipe without actually
1944 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001945 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001946 if (ipipe && opipe && ipipe != opipe) {
1947 /*
1948 * Keep going, unless we encounter an error. The ipipe/opipe
1949 * ordering doesn't really matter.
1950 */
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001951 ret = ipipe_prep(ipipe, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001952 if (!ret) {
Miklos Szeredi7c77f0b2009-05-07 15:37:35 +02001953 ret = opipe_prep(opipe, flags);
Jens Axboe02cf01a2008-02-20 10:34:51 +01001954 if (!ret)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001955 ret = link_pipe(ipipe, opipe, len, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001956 }
1957 }
Jens Axboe70524492006-04-11 15:51:17 +02001958
Jens Axboeaadd06e2006-07-10 11:00:01 +02001959 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001960}
1961
Heiko Carstens836f92a2009-01-14 14:14:33 +01001962SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
Jens Axboe70524492006-04-11 15:51:17 +02001963{
Al Viro2903ff02012-08-28 12:52:22 -04001964 struct fd in;
1965 int error;
Jens Axboe70524492006-04-11 15:51:17 +02001966
1967 if (unlikely(!len))
1968 return 0;
1969
1970 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001971 in = fdget(fdin);
1972 if (in.file) {
1973 if (in.file->f_mode & FMODE_READ) {
1974 struct fd out = fdget(fdout);
1975 if (out.file) {
1976 if (out.file->f_mode & FMODE_WRITE)
1977 error = do_tee(in.file, out.file,
1978 len, flags);
1979 fdput(out);
Jens Axboe70524492006-04-11 15:51:17 +02001980 }
1981 }
Al Viro2903ff02012-08-28 12:52:22 -04001982 fdput(in);
Jens Axboe70524492006-04-11 15:51:17 +02001983 }
1984
1985 return error;
1986}