blob: a54b3e3f10a716cb63ab56311745716a70086d0f [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>
28#include <linux/buffer_head.h>
Jeff Garzika0f06782006-03-30 23:06:13 -050029#include <linux/module.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020030#include <linux/syscalls.h>
Jens Axboe912d35f2006-04-26 10:59:21 +020031#include <linux/uio.h>
James Morris29ce2052007-07-13 11:44:32 +020032#include <linux/security.h>
Jens Axboe5274f052006-03-30 15:15:30 +020033
Jens Axboe83f91352006-04-02 23:05:09 +020034/*
35 * Attempt to steal a page from a pipe buffer. This should perhaps go into
36 * a vm helper function, it's already simplified quite a bit by the
37 * addition of remove_mapping(). If success is returned, the caller may
38 * attempt to reuse this page for another destination.
39 */
Jens Axboe76ad4d12006-05-03 10:41:33 +020040static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
Jens Axboe5abc97a2006-03-30 15:16:46 +020041 struct pipe_buffer *buf)
42{
43 struct page *page = buf->page;
Jens Axboe9e94cd42006-06-20 15:01:12 +020044 struct address_space *mapping;
Jens Axboe5abc97a2006-03-30 15:16:46 +020045
Jens Axboe9e0267c2006-04-19 15:57:31 +020046 lock_page(page);
47
Jens Axboe9e94cd42006-06-20 15:01:12 +020048 mapping = page_mapping(page);
49 if (mapping) {
50 WARN_ON(!PageUptodate(page));
Jens Axboe5abc97a2006-03-30 15:16:46 +020051
Jens Axboe9e94cd42006-06-20 15:01:12 +020052 /*
53 * At least for ext2 with nobh option, we need to wait on
54 * writeback completing on this page, since we'll remove it
55 * from the pagecache. Otherwise truncate wont wait on the
56 * page, allowing the disk blocks to be reused by someone else
57 * before we actually wrote our data to them. fs corruption
58 * ensues.
59 */
60 wait_on_page_writeback(page);
Jens Axboead8d6f02006-04-02 23:10:32 +020061
Jens Axboeca39d652008-05-20 21:27:41 +020062 if (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
63 goto out_unlock;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020064
Jens Axboe9e94cd42006-06-20 15:01:12 +020065 /*
66 * If we succeeded in removing the mapping, set LRU flag
67 * and return good.
68 */
69 if (remove_mapping(mapping, page)) {
70 buf->flags |= PIPE_BUF_FLAG_LRU;
71 return 0;
72 }
Jens Axboe9e0267c2006-04-19 15:57:31 +020073 }
Jens Axboe5abc97a2006-03-30 15:16:46 +020074
Jens Axboe9e94cd42006-06-20 15:01:12 +020075 /*
76 * Raced with truncate or failed to remove page from current
77 * address space, unlock and return failure.
78 */
Jens Axboeca39d652008-05-20 21:27:41 +020079out_unlock:
Jens Axboe9e94cd42006-06-20 15:01:12 +020080 unlock_page(page);
81 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +020082}
83
Jens Axboe76ad4d12006-05-03 10:41:33 +020084static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +020085 struct pipe_buffer *buf)
86{
87 page_cache_release(buf->page);
Jens Axboe14328732006-05-03 10:35:26 +020088 buf->flags &= ~PIPE_BUF_FLAG_LRU;
Jens Axboe5274f052006-03-30 15:15:30 +020089}
90
Jens Axboe08457182007-06-12 20:51:32 +020091/*
92 * Check whether the contents of buf is OK to access. Since the content
93 * is a page cache page, IO may be in flight.
94 */
Jens Axboecac36bb02007-06-14 13:10:48 +020095static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
96 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +020097{
98 struct page *page = buf->page;
Jens Axboe49d0b212006-04-10 09:04:41 +020099 int err;
Jens Axboe5274f052006-03-30 15:15:30 +0200100
101 if (!PageUptodate(page)) {
Jens Axboe49d0b212006-04-10 09:04:41 +0200102 lock_page(page);
103
104 /*
105 * Page got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +0200106 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200107 */
108 if (!page->mapping) {
109 err = -ENODATA;
110 goto error;
111 }
112
113 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200114 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200115 */
116 if (!PageUptodate(page)) {
117 err = -EIO;
118 goto error;
119 }
120
121 /*
Jens Axboef84d7512006-05-01 19:59:03 +0200122 * Page is ok afterall, we are done.
Jens Axboe49d0b212006-04-10 09:04:41 +0200123 */
Jens Axboe5274f052006-03-30 15:15:30 +0200124 unlock_page(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200125 }
126
Jens Axboef84d7512006-05-01 19:59:03 +0200127 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200128error:
129 unlock_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200130 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200131}
132
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800133static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
Jens Axboe5274f052006-03-30 15:15:30 +0200134 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200135 .map = generic_pipe_buf_map,
136 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb02007-06-14 13:10:48 +0200137 .confirm = page_cache_pipe_buf_confirm,
Jens Axboe5274f052006-03-30 15:15:30 +0200138 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200139 .steal = page_cache_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200140 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200141};
142
Jens Axboe912d35f2006-04-26 10:59:21 +0200143static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
144 struct pipe_buffer *buf)
145{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200146 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
147 return 1;
148
Jens Axboe14328732006-05-03 10:35:26 +0200149 buf->flags |= PIPE_BUF_FLAG_LRU;
Jens Axboe330ab712006-05-02 15:29:57 +0200150 return generic_pipe_buf_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200151}
152
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800153static const struct pipe_buf_operations user_page_pipe_buf_ops = {
Jens Axboe912d35f2006-04-26 10:59:21 +0200154 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200155 .map = generic_pipe_buf_map,
156 .unmap = generic_pipe_buf_unmap,
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
Jens Axboe932cc6d2007-06-21 13:10:21 +0200163/**
164 * splice_to_pipe - fill passed data into a pipe
165 * @pipe: pipe to fill
166 * @spd: data to fill
167 *
168 * Description:
Randy Dunlap79685b82007-07-27 08:08:51 +0200169 * @spd contains a map of pages and len/offset tuples, along with
Jens Axboe932cc6d2007-06-21 13:10:21 +0200170 * the struct pipe_buf_operations associated with these pages. This
171 * function will link that data to the pipe.
172 *
Jens Axboe83f91352006-04-02 23:05:09 +0200173 */
Jens Axboed6b29d72007-06-04 09:59:47 +0200174ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
175 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200176{
Jens Axboe00de00b2007-06-15 13:14:22 +0200177 unsigned int spd_pages = spd->nr_pages;
Jens Axboe912d35f2006-04-26 10:59:21 +0200178 int ret, do_wakeup, page_nr;
Jens Axboe5274f052006-03-30 15:15:30 +0200179
180 ret = 0;
181 do_wakeup = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +0200182 page_nr = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200183
Ingo Molnar3a326a22006-04-10 15:18:35 +0200184 if (pipe->inode)
185 mutex_lock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200186
Jens Axboe5274f052006-03-30 15:15:30 +0200187 for (;;) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200188 if (!pipe->readers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200189 send_sig(SIGPIPE, current, 0);
190 if (!ret)
191 ret = -EPIPE;
192 break;
193 }
194
Jens Axboe6f767b02006-04-11 13:53:56 +0200195 if (pipe->nrbufs < PIPE_BUFFERS) {
196 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200197 struct pipe_buffer *buf = pipe->bufs + newbuf;
Jens Axboe5274f052006-03-30 15:15:30 +0200198
Jens Axboe912d35f2006-04-26 10:59:21 +0200199 buf->page = spd->pages[page_nr];
200 buf->offset = spd->partial[page_nr].offset;
201 buf->len = spd->partial[page_nr].len;
Jens Axboe497f9622007-06-11 12:00:45 +0200202 buf->private = spd->partial[page_nr].private;
Jens Axboe912d35f2006-04-26 10:59:21 +0200203 buf->ops = spd->ops;
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200204 if (spd->flags & SPLICE_F_GIFT)
205 buf->flags |= PIPE_BUF_FLAG_GIFT;
206
Jens Axboe6f767b02006-04-11 13:53:56 +0200207 pipe->nrbufs++;
Jens Axboe912d35f2006-04-26 10:59:21 +0200208 page_nr++;
209 ret += buf->len;
210
Jens Axboe6f767b02006-04-11 13:53:56 +0200211 if (pipe->inode)
212 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200213
Jens Axboe912d35f2006-04-26 10:59:21 +0200214 if (!--spd->nr_pages)
Jens Axboe5274f052006-03-30 15:15:30 +0200215 break;
Jens Axboe6f767b02006-04-11 13:53:56 +0200216 if (pipe->nrbufs < PIPE_BUFFERS)
Jens Axboe5274f052006-03-30 15:15:30 +0200217 continue;
218
219 break;
220 }
221
Jens Axboe912d35f2006-04-26 10:59:21 +0200222 if (spd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700223 if (!ret)
224 ret = -EAGAIN;
225 break;
226 }
227
Jens Axboe5274f052006-03-30 15:15:30 +0200228 if (signal_pending(current)) {
229 if (!ret)
230 ret = -ERESTARTSYS;
231 break;
232 }
233
234 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200235 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200236 if (waitqueue_active(&pipe->wait))
237 wake_up_interruptible_sync(&pipe->wait);
238 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Jens Axboe5274f052006-03-30 15:15:30 +0200239 do_wakeup = 0;
240 }
241
Ingo Molnar3a326a22006-04-10 15:18:35 +0200242 pipe->waiting_writers++;
243 pipe_wait(pipe);
244 pipe->waiting_writers--;
Jens Axboe5274f052006-03-30 15:15:30 +0200245 }
246
Jens Axboe02676e52007-06-15 13:16:13 +0200247 if (pipe->inode) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200248 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200249
Jens Axboe02676e52007-06-15 13:16:13 +0200250 if (do_wakeup) {
251 smp_mb();
252 if (waitqueue_active(&pipe->wait))
253 wake_up_interruptible(&pipe->wait);
254 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
255 }
Jens Axboe5274f052006-03-30 15:15:30 +0200256 }
257
Jens Axboe00de00b2007-06-15 13:14:22 +0200258 while (page_nr < spd_pages)
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800259 spd->spd_release(spd, page_nr++);
Jens Axboe5274f052006-03-30 15:15:30 +0200260
261 return ret;
262}
263
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800264static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
265{
266 page_cache_release(spd->pages[i]);
267}
268
Ingo Molnar3a326a22006-04-10 15:18:35 +0200269static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200270__generic_file_splice_read(struct file *in, loff_t *ppos,
271 struct pipe_inode_info *pipe, size_t len,
272 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200273{
274 struct address_space *mapping = in->f_mapping;
Fengguang Wud8983912007-07-19 01:48:06 -0700275 unsigned int loff, nr_pages, req_pages;
Jens Axboe16c523d2006-04-10 09:03:58 +0200276 struct page *pages[PIPE_BUFFERS];
Jens Axboe912d35f2006-04-26 10:59:21 +0200277 struct partial_page partial[PIPE_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200278 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200279 pgoff_t index, end_index;
280 loff_t isize;
Jens Axboeeb207962006-04-27 11:05:22 +0200281 int error, page_nr;
Jens Axboe912d35f2006-04-26 10:59:21 +0200282 struct splice_pipe_desc spd = {
283 .pages = pages,
284 .partial = partial,
285 .flags = flags,
286 .ops = &page_cache_pipe_buf_ops,
Jens Axboebbdfc2f2007-11-06 23:29:47 -0800287 .spd_release = spd_release_page,
Jens Axboe912d35f2006-04-26 10:59:21 +0200288 };
Jens Axboe5274f052006-03-30 15:15:30 +0200289
Jens Axboecbb7e572006-04-11 14:57:50 +0200290 index = *ppos >> PAGE_CACHE_SHIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +0200291 loff = *ppos & ~PAGE_CACHE_MASK;
Fengguang Wud8983912007-07-19 01:48:06 -0700292 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
293 nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS);
Jens Axboe5274f052006-03-30 15:15:30 +0200294
295 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200296 * Lookup the (hopefully) full range of pages we need.
297 */
298 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
Fengguang Wu431a48202007-07-19 01:48:05 -0700299 index += spd.nr_pages;
Jens Axboeeb207962006-04-27 11:05:22 +0200300
301 /*
302 * If find_get_pages_contig() returned fewer pages than we needed,
Fengguang Wu431a48202007-07-19 01:48:05 -0700303 * readahead/allocate the rest and fill in the holes.
Jens Axboeeb207962006-04-27 11:05:22 +0200304 */
Fengguang Wu431a48202007-07-19 01:48:05 -0700305 if (spd.nr_pages < nr_pages)
Rusty Russellcf914a72007-07-19 01:48:08 -0700306 page_cache_sync_readahead(mapping, &in->f_ra, in,
307 index, req_pages - spd.nr_pages);
Fengguang Wu431a48202007-07-19 01:48:05 -0700308
Jens Axboe932cc6d2007-06-21 13:10:21 +0200309 error = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200310 while (spd.nr_pages < nr_pages) {
311 /*
312 * Page could be there, find_get_pages_contig() breaks on
313 * the first hole.
314 */
315 page = find_get_page(mapping, index);
316 if (!page) {
Jens Axboee27dedd2006-05-01 19:59:54 +0200317 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200318 * page didn't exist, allocate one.
319 */
320 page = page_cache_alloc_cold(mapping);
321 if (!page)
322 break;
323
324 error = add_to_page_cache_lru(page, mapping, index,
Hugh Dickins4cd13502008-04-03 23:35:22 +0100325 mapping_gfp_mask(mapping));
Jens Axboeeb207962006-04-27 11:05:22 +0200326 if (unlikely(error)) {
327 page_cache_release(page);
Jens Axboea0548872006-05-03 10:58:22 +0200328 if (error == -EEXIST)
329 continue;
Jens Axboeeb207962006-04-27 11:05:22 +0200330 break;
331 }
332 /*
333 * add_to_page_cache() locks the page, unlock it
334 * to avoid convoluting the logic below even more.
335 */
336 unlock_page(page);
337 }
338
339 pages[spd.nr_pages++] = page;
340 index++;
341 }
342
343 /*
344 * Now loop over the map and see if we need to start IO on any
345 * pages, fill in the partial map, etc.
346 */
347 index = *ppos >> PAGE_CACHE_SHIFT;
348 nr_pages = spd.nr_pages;
349 spd.nr_pages = 0;
350 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200351 unsigned int this_len;
352
353 if (!len)
354 break;
355
356 /*
357 * this_len is the max we'll use from this page
358 */
Andrew Mortonba5f5d92006-04-25 15:33:34 +0200359 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
Jens Axboeeb207962006-04-27 11:05:22 +0200360 page = pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200361
Fengguang Wua08a1662007-07-19 01:48:03 -0700362 if (PageReadahead(page))
Rusty Russellcf914a72007-07-19 01:48:08 -0700363 page_cache_async_readahead(mapping, &in->f_ra, in,
Fengguang Wud8983912007-07-19 01:48:06 -0700364 page, index, req_pages - page_nr);
Fengguang Wua08a1662007-07-19 01:48:03 -0700365
Jens Axboe7480a902006-04-11 13:52:47 +0200366 /*
367 * If the page isn't uptodate, we may need to start io on it
368 */
369 if (!PageUptodate(page)) {
Jens Axboec4f895c2006-04-19 15:56:12 +0200370 /*
371 * If in nonblock mode then dont block on waiting
372 * for an in-flight io page
373 */
Fengguang Wu9ae9d682007-05-08 08:44:36 +0200374 if (flags & SPLICE_F_NONBLOCK) {
Nick Piggin529ae9a2008-08-02 12:01:03 +0200375 if (!trylock_page(page)) {
Jens Axboe8191ecd2008-04-10 08:24:25 +0200376 error = -EAGAIN;
Fengguang Wu9ae9d682007-05-08 08:44:36 +0200377 break;
Jens Axboe8191ecd2008-04-10 08:24:25 +0200378 }
Fengguang Wu9ae9d682007-05-08 08:44:36 +0200379 } else
380 lock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200381
382 /*
Miklos Szeredi32502b82008-07-04 09:35:17 +0200383 * Page was truncated, or invalidated by the
384 * filesystem. Redo the find/create, but this time the
385 * page is kept locked, so there's no chance of another
386 * race with truncate/invalidate.
Jens Axboe7480a902006-04-11 13:52:47 +0200387 */
388 if (!page->mapping) {
389 unlock_page(page);
Miklos Szeredi32502b82008-07-04 09:35:17 +0200390 page = find_or_create_page(mapping, index,
391 mapping_gfp_mask(mapping));
392
393 if (!page) {
394 error = -ENOMEM;
395 break;
396 }
397 page_cache_release(pages[page_nr]);
398 pages[page_nr] = page;
Jens Axboe7480a902006-04-11 13:52:47 +0200399 }
400 /*
401 * page was already under io and is now done, great
402 */
403 if (PageUptodate(page)) {
404 unlock_page(page);
405 goto fill_it;
406 }
407
Jens Axboe7480a902006-04-11 13:52:47 +0200408 /*
409 * need to read in the page
410 */
411 error = mapping->a_ops->readpage(in, page);
Jens Axboe7480a902006-04-11 13:52:47 +0200412 if (unlikely(error)) {
Jens Axboeeb207962006-04-27 11:05:22 +0200413 /*
414 * We really should re-lookup the page here,
415 * but it complicates things a lot. Instead
416 * lets just do what we already stored, and
417 * we'll get it the next time we are called.
418 */
Jens Axboe7480a902006-04-11 13:52:47 +0200419 if (error == AOP_TRUNCATED_PAGE)
Jens Axboeeb207962006-04-27 11:05:22 +0200420 error = 0;
421
Jens Axboe7480a902006-04-11 13:52:47 +0200422 break;
423 }
Jens Axboe620a3242007-06-07 09:39:42 +0200424 }
425fill_it:
426 /*
427 * i_size must be checked after PageUptodate.
428 */
429 isize = i_size_read(mapping->host);
430 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
431 if (unlikely(!isize || index > end_index))
432 break;
433
434 /*
435 * if this is the last page, see if we need to shrink
436 * the length and stop
437 */
438 if (end_index == index) {
439 unsigned int plen;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200440
441 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200442 * max good bytes in this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200443 */
Jens Axboe620a3242007-06-07 09:39:42 +0200444 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
445 if (plen <= loff)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200446 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200447
448 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200449 * force quit after adding this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200450 */
Jens Axboe620a3242007-06-07 09:39:42 +0200451 this_len = min(this_len, plen - loff);
452 len = this_len;
Jens Axboe7480a902006-04-11 13:52:47 +0200453 }
Jens Axboe620a3242007-06-07 09:39:42 +0200454
Jens Axboeeb207962006-04-27 11:05:22 +0200455 partial[page_nr].offset = loff;
456 partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200457 len -= this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200458 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200459 spd.nr_pages++;
460 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200461 }
462
Jens Axboeeb207962006-04-27 11:05:22 +0200463 /*
Hugh Dickins475ecad2007-06-07 09:36:00 +0200464 * Release any pages at the end, if we quit early. 'page_nr' is how far
Jens Axboeeb207962006-04-27 11:05:22 +0200465 * we got, 'nr_pages' is how many pages are in the map.
466 */
467 while (page_nr < nr_pages)
468 page_cache_release(pages[page_nr++]);
Fengguang Wuf4e6b492007-10-16 01:24:33 -0700469 in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
Jens Axboeeb207962006-04-27 11:05:22 +0200470
Jens Axboe912d35f2006-04-26 10:59:21 +0200471 if (spd.nr_pages)
Jens Axboe00522fb2006-04-26 14:39:29 +0200472 return splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200473
Jens Axboe7480a902006-04-11 13:52:47 +0200474 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200475}
476
Jens Axboe83f91352006-04-02 23:05:09 +0200477/**
478 * generic_file_splice_read - splice data from file to a pipe
479 * @in: file to splice from
Jens Axboe932cc6d2007-06-21 13:10:21 +0200480 * @ppos: position in @in
Jens Axboe83f91352006-04-02 23:05:09 +0200481 * @pipe: pipe to splice to
482 * @len: number of bytes to splice
483 * @flags: splice modifier flags
484 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200485 * Description:
486 * Will read pages from given file and fill them into a pipe. Can be
487 * used as long as the address_space operations for the source implements
488 * a readpage() hook.
489 *
Jens Axboe83f91352006-04-02 23:05:09 +0200490 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200491ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
492 struct pipe_inode_info *pipe, size_t len,
493 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200494{
Jens Axboed366d3982007-06-01 14:54:11 +0200495 loff_t isize, left;
Jens Axboe8191ecd2008-04-10 08:24:25 +0200496 int ret;
Jens Axboed366d3982007-06-01 14:54:11 +0200497
498 isize = i_size_read(in->f_mapping->host);
499 if (unlikely(*ppos >= isize))
500 return 0;
501
502 left = isize - *ppos;
503 if (unlikely(left < len))
504 len = left;
Jens Axboe5274f052006-03-30 15:15:30 +0200505
Jens Axboe8191ecd2008-04-10 08:24:25 +0200506 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
507 if (ret > 0)
Jens Axboecbb7e572006-04-11 14:57:50 +0200508 *ppos += ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200509
510 return ret;
511}
512
Jens Axboe059a8f32006-04-02 23:06:05 +0200513EXPORT_SYMBOL(generic_file_splice_read);
514
Jens Axboe5274f052006-03-30 15:15:30 +0200515/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200516 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200517 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200518 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200519static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200520 struct pipe_buffer *buf, struct splice_desc *sd)
521{
Jens Axboe6a14b902007-06-14 13:08:55 +0200522 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200523 loff_t pos = sd->pos;
Jens Axboef84d7512006-05-01 19:59:03 +0200524 int ret, more;
Jens Axboe5274f052006-03-30 15:15:30 +0200525
Jens Axboecac36bb02007-06-14 13:10:48 +0200526 ret = buf->ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200527 if (!ret) {
528 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200529
Jens Axboef84d7512006-05-01 19:59:03 +0200530 ret = file->f_op->sendpage(file, buf->page, buf->offset,
531 sd->len, &pos, more);
532 }
Jens Axboe5274f052006-03-30 15:15:30 +0200533
Jens Axboe016b6612006-04-25 15:42:00 +0200534 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200535}
536
537/*
538 * This is a little more tricky than the file -> pipe splicing. There are
539 * basically three cases:
540 *
541 * - Destination page already exists in the address space and there
542 * are users of it. For that case we have no other option that
543 * copying the data. Tough luck.
544 * - Destination page already exists in the address space, but there
545 * are no users of it. Make sure it's uptodate, then drop it. Fall
546 * through to last case.
547 * - Destination page does not exist, we can add the pipe page to
548 * the page cache and avoid the copy.
549 *
Jens Axboe83f91352006-04-02 23:05:09 +0200550 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
551 * sd->flags), we attempt to migrate pages from the pipe to the output
552 * file address space page cache. This is possible if no one else has
553 * the pipe page referenced outside of the pipe and page cache. If
554 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
555 * a new page in the output file page cache and fill/dirty that.
Jens Axboe5274f052006-03-30 15:15:30 +0200556 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200557static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
Jens Axboe5274f052006-03-30 15:15:30 +0200558 struct splice_desc *sd)
559{
Jens Axboe6a14b902007-06-14 13:08:55 +0200560 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200561 struct address_space *mapping = file->f_mapping;
Jens Axboe016b6612006-04-25 15:42:00 +0200562 unsigned int offset, this_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200563 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -0700564 void *fsdata;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200565 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200566
567 /*
Jens Axboe49d0b212006-04-10 09:04:41 +0200568 * make sure the data in this buffer is uptodate
Jens Axboe5274f052006-03-30 15:15:30 +0200569 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200570 ret = buf->ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200571 if (unlikely(ret))
572 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200573
Jens Axboe5274f052006-03-30 15:15:30 +0200574 offset = sd->pos & ~PAGE_CACHE_MASK;
575
Jens Axboe016b6612006-04-25 15:42:00 +0200576 this_len = sd->len;
577 if (this_len + offset > PAGE_CACHE_SIZE)
578 this_len = PAGE_CACHE_SIZE - offset;
579
Nick Pigginafddba42007-10-16 01:25:01 -0700580 ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
581 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
582 if (unlikely(ret))
583 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200584
Jens Axboe0568b402006-05-01 19:50:48 +0200585 if (buf->page != page) {
Jens Axboef84d7512006-05-01 19:59:03 +0200586 /*
587 * Careful, ->map() uses KM_USER0!
588 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200589 char *src = buf->ops->map(pipe, buf, 1);
Jens Axboef84d7512006-05-01 19:59:03 +0200590 char *dst = kmap_atomic(page, KM_USER1);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200591
Jens Axboe016b6612006-04-25 15:42:00 +0200592 memcpy(dst + offset, src + buf->offset, this_len);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200593 flush_dcache_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200594 kunmap_atomic(dst, KM_USER1);
Jens Axboe76ad4d12006-05-03 10:41:33 +0200595 buf->ops->unmap(pipe, buf, src);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200596 }
Nick Pigginafddba42007-10-16 01:25:01 -0700597 ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
598 page, fsdata);
Jens Axboe5274f052006-03-30 15:15:30 +0200599out:
Jens Axboe5274f052006-03-30 15:15:30 +0200600 return ret;
601}
602
Jens Axboe932cc6d2007-06-21 13:10:21 +0200603/**
604 * __splice_from_pipe - splice data from a pipe to given actor
605 * @pipe: pipe to splice from
606 * @sd: information to @actor
607 * @actor: handler that splices the data
608 *
609 * Description:
610 * This function does little more than loop over the pipe and call
611 * @actor to do the actual moving of a single struct pipe_buffer to
612 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
613 * pipe_to_user.
614 *
Jens Axboe83f91352006-04-02 23:05:09 +0200615 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200616ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
617 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200618{
Jens Axboe5274f052006-03-30 15:15:30 +0200619 int ret, do_wakeup, err;
Jens Axboe5274f052006-03-30 15:15:30 +0200620
621 ret = 0;
622 do_wakeup = 0;
623
Jens Axboe5274f052006-03-30 15:15:30 +0200624 for (;;) {
Jens Axboe6f767b02006-04-11 13:53:56 +0200625 if (pipe->nrbufs) {
626 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800627 const struct pipe_buf_operations *ops = buf->ops;
Jens Axboe5274f052006-03-30 15:15:30 +0200628
Jens Axboec66ab6f2007-06-12 21:17:17 +0200629 sd->len = buf->len;
630 if (sd->len > sd->total_len)
631 sd->len = sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200632
Jens Axboec66ab6f2007-06-12 21:17:17 +0200633 err = actor(pipe, buf, sd);
Jens Axboe016b6612006-04-25 15:42:00 +0200634 if (err <= 0) {
Jens Axboe5274f052006-03-30 15:15:30 +0200635 if (!ret && err != -ENODATA)
636 ret = err;
637
638 break;
639 }
640
Jens Axboe016b6612006-04-25 15:42:00 +0200641 ret += err;
642 buf->offset += err;
643 buf->len -= err;
644
Jens Axboec66ab6f2007-06-12 21:17:17 +0200645 sd->len -= err;
646 sd->pos += err;
647 sd->total_len -= err;
648 if (sd->len)
Jens Axboe016b6612006-04-25 15:42:00 +0200649 continue;
Ingo Molnar73d62d82006-04-11 13:57:21 +0200650
Jens Axboe5274f052006-03-30 15:15:30 +0200651 if (!buf->len) {
652 buf->ops = NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200653 ops->release(pipe, buf);
Jens Axboe6f767b02006-04-11 13:53:56 +0200654 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
655 pipe->nrbufs--;
656 if (pipe->inode)
657 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200658 }
659
Jens Axboec66ab6f2007-06-12 21:17:17 +0200660 if (!sd->total_len)
Jens Axboe5274f052006-03-30 15:15:30 +0200661 break;
662 }
663
Jens Axboe6f767b02006-04-11 13:53:56 +0200664 if (pipe->nrbufs)
Jens Axboe5274f052006-03-30 15:15:30 +0200665 continue;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200666 if (!pipe->writers)
Jens Axboe5274f052006-03-30 15:15:30 +0200667 break;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200668 if (!pipe->waiting_writers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200669 if (ret)
670 break;
671 }
672
Jens Axboec66ab6f2007-06-12 21:17:17 +0200673 if (sd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700674 if (!ret)
675 ret = -EAGAIN;
676 break;
677 }
678
Jens Axboe5274f052006-03-30 15:15:30 +0200679 if (signal_pending(current)) {
680 if (!ret)
681 ret = -ERESTARTSYS;
682 break;
683 }
684
685 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200686 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200687 if (waitqueue_active(&pipe->wait))
688 wake_up_interruptible_sync(&pipe->wait);
689 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200690 do_wakeup = 0;
691 }
692
Ingo Molnar3a326a22006-04-10 15:18:35 +0200693 pipe_wait(pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200694 }
695
Jens Axboe5274f052006-03-30 15:15:30 +0200696 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200697 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200698 if (waitqueue_active(&pipe->wait))
699 wake_up_interruptible(&pipe->wait);
700 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200701 }
702
Jens Axboe5274f052006-03-30 15:15:30 +0200703 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200704}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100705EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200706
Jens Axboe932cc6d2007-06-21 13:10:21 +0200707/**
708 * splice_from_pipe - splice data from a pipe to a file
709 * @pipe: pipe to splice from
710 * @out: file to splice to
711 * @ppos: position in @out
712 * @len: how many bytes to splice
713 * @flags: splice modifier flags
714 * @actor: handler that splices the data
715 *
716 * Description:
717 * See __splice_from_pipe. This function locks the input and output inodes,
718 * otherwise it's identical to __splice_from_pipe().
719 *
720 */
Mark Fasheh6da61802006-10-17 18:43:07 +0200721ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
722 loff_t *ppos, size_t len, unsigned int flags,
723 splice_actor *actor)
724{
725 ssize_t ret;
726 struct inode *inode = out->f_mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200727 struct splice_desc sd = {
728 .total_len = len,
729 .flags = flags,
730 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200731 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200732 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200733
734 /*
Nick Piggin4e02ed42008-10-29 14:00:55 -0700735 * The actor worker might be calling ->write_begin and
736 * ->write_end. Most of the time, these expect i_mutex to
Mark Fasheh6da61802006-10-17 18:43:07 +0200737 * be held. Since this may result in an ABBA deadlock with
738 * pipe->inode, we have to order lock acquiry here.
739 */
740 inode_double_lock(inode, pipe->inode);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200741 ret = __splice_from_pipe(pipe, &sd, actor);
Mark Fasheh6da61802006-10-17 18:43:07 +0200742 inode_double_unlock(inode, pipe->inode);
743
744 return ret;
745}
746
747/**
748 * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
749 * @pipe: pipe info
750 * @out: file to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +0200751 * @ppos: position in @out
Mark Fasheh6da61802006-10-17 18:43:07 +0200752 * @len: number of bytes to splice
753 * @flags: splice modifier flags
754 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200755 * Description:
756 * Will either move or copy pages (determined by @flags options) from
757 * the given pipe inode to the given file. The caller is responsible
758 * for acquiring i_mutex on both inodes.
Mark Fasheh6da61802006-10-17 18:43:07 +0200759 *
760 */
761ssize_t
762generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
763 loff_t *ppos, size_t len, unsigned int flags)
764{
765 struct address_space *mapping = out->f_mapping;
766 struct inode *inode = mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200767 struct splice_desc sd = {
768 .total_len = len,
769 .flags = flags,
770 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200771 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200772 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200773 ssize_t ret;
774 int err;
775
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200776 err = file_remove_suid(out);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200777 if (unlikely(err))
778 return err;
779
Jens Axboec66ab6f2007-06-12 21:17:17 +0200780 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
Mark Fasheh6da61802006-10-17 18:43:07 +0200781 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200782 unsigned long nr_pages;
783
Mark Fasheh6da61802006-10-17 18:43:07 +0200784 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200785 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Mark Fasheh6da61802006-10-17 18:43:07 +0200786
787 /*
788 * If file or inode is SYNC and we actually wrote some data,
789 * sync it.
790 */
791 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
792 err = generic_osync_inode(inode, mapping,
793 OSYNC_METADATA|OSYNC_DATA);
794
795 if (err)
796 ret = err;
797 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200798 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Mark Fasheh6da61802006-10-17 18:43:07 +0200799 }
800
801 return ret;
802}
803
804EXPORT_SYMBOL(generic_file_splice_write_nolock);
805
Jens Axboe83f91352006-04-02 23:05:09 +0200806/**
807 * generic_file_splice_write - splice data from a pipe to a file
Ingo Molnar3a326a22006-04-10 15:18:35 +0200808 * @pipe: pipe info
Jens Axboe83f91352006-04-02 23:05:09 +0200809 * @out: file to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +0200810 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +0200811 * @len: number of bytes to splice
812 * @flags: splice modifier flags
813 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200814 * Description:
815 * Will either move or copy pages (determined by @flags options) from
816 * the given pipe inode to the given file.
Jens Axboe83f91352006-04-02 23:05:09 +0200817 *
818 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200819ssize_t
820generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200821 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200822{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200823 struct address_space *mapping = out->f_mapping;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200824 struct inode *inode = mapping->host;
Miklos Szeredi7f3d4ee2008-05-07 09:22:39 +0200825 struct splice_desc sd = {
826 .total_len = len,
827 .flags = flags,
828 .pos = *ppos,
829 .u.file = out,
830 };
Ingo Molnar3a326a22006-04-10 15:18:35 +0200831 ssize_t ret;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200832
Miklos Szeredi7f3d4ee2008-05-07 09:22:39 +0200833 inode_double_lock(inode, pipe->inode);
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200834 ret = file_remove_suid(out);
Miklos Szeredi7f3d4ee2008-05-07 09:22:39 +0200835 if (likely(!ret))
836 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
837 inode_double_unlock(inode, pipe->inode);
Jens Axboea4514eb2006-04-19 15:57:05 +0200838 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200839 unsigned long nr_pages;
840
Jens Axboea4514eb2006-04-19 15:57:05 +0200841 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200842 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200843
Jens Axboea4514eb2006-04-19 15:57:05 +0200844 /*
845 * If file or inode is SYNC and we actually wrote some data,
846 * sync it.
847 */
848 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
Miklos Szeredi7f3d4ee2008-05-07 09:22:39 +0200849 int err;
850
Jens Axboea4514eb2006-04-19 15:57:05 +0200851 mutex_lock(&inode->i_mutex);
852 err = generic_osync_inode(inode, mapping,
853 OSYNC_METADATA|OSYNC_DATA);
854 mutex_unlock(&inode->i_mutex);
855
856 if (err)
857 ret = err;
858 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200859 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200860 }
861
862 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200863}
864
Jens Axboe059a8f32006-04-02 23:06:05 +0200865EXPORT_SYMBOL(generic_file_splice_write);
866
Jens Axboe83f91352006-04-02 23:05:09 +0200867/**
868 * generic_splice_sendpage - splice data from a pipe to a socket
Jens Axboe932cc6d2007-06-21 13:10:21 +0200869 * @pipe: pipe to splice from
Jens Axboe83f91352006-04-02 23:05:09 +0200870 * @out: socket to write to
Jens Axboe932cc6d2007-06-21 13:10:21 +0200871 * @ppos: position in @out
Jens Axboe83f91352006-04-02 23:05:09 +0200872 * @len: number of bytes to splice
873 * @flags: splice modifier flags
874 *
Jens Axboe932cc6d2007-06-21 13:10:21 +0200875 * Description:
876 * Will send @len bytes from the pipe to a network socket. No data copying
877 * is involved.
Jens Axboe83f91352006-04-02 23:05:09 +0200878 *
879 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200880ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200881 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200882{
Jens Axboe00522fb2006-04-26 14:39:29 +0200883 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200884}
885
Jens Axboe059a8f32006-04-02 23:06:05 +0200886EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500887
Jens Axboe83f91352006-04-02 23:05:09 +0200888/*
889 * Attempt to initiate a splice from pipe to file.
890 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200891static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200892 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200893{
Jens Axboe5274f052006-03-30 15:15:30 +0200894 int ret;
895
Jens Axboe49570e92006-04-11 13:56:09 +0200896 if (unlikely(!out->f_op || !out->f_op->splice_write))
Jens Axboe5274f052006-03-30 15:15:30 +0200897 return -EINVAL;
898
Jens Axboe49570e92006-04-11 13:56:09 +0200899 if (unlikely(!(out->f_mode & FMODE_WRITE)))
Jens Axboe5274f052006-03-30 15:15:30 +0200900 return -EBADF;
901
Linus Torvaldsefc968d2008-10-09 14:04:54 -0700902 if (unlikely(out->f_flags & O_APPEND))
903 return -EINVAL;
904
Jens Axboecbb7e572006-04-11 14:57:50 +0200905 ret = rw_verify_area(WRITE, out, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200906 if (unlikely(ret < 0))
907 return ret;
908
Jens Axboecbb7e572006-04-11 14:57:50 +0200909 return out->f_op->splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200910}
911
Jens Axboe83f91352006-04-02 23:05:09 +0200912/*
913 * Attempt to initiate a splice from a file to a pipe.
914 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200915static long do_splice_to(struct file *in, loff_t *ppos,
916 struct pipe_inode_info *pipe, size_t len,
917 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200918{
Jens Axboe5274f052006-03-30 15:15:30 +0200919 int ret;
920
Jens Axboe49570e92006-04-11 13:56:09 +0200921 if (unlikely(!in->f_op || !in->f_op->splice_read))
Jens Axboe5274f052006-03-30 15:15:30 +0200922 return -EINVAL;
923
Jens Axboe49570e92006-04-11 13:56:09 +0200924 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200925 return -EBADF;
926
Jens Axboecbb7e572006-04-11 14:57:50 +0200927 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200928 if (unlikely(ret < 0))
929 return ret;
930
Jens Axboecbb7e572006-04-11 14:57:50 +0200931 return in->f_op->splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200932}
933
Jens Axboe932cc6d2007-06-21 13:10:21 +0200934/**
935 * splice_direct_to_actor - splices data directly between two non-pipes
936 * @in: file to splice from
937 * @sd: actor information on where to splice to
938 * @actor: handles the data splicing
939 *
940 * Description:
941 * This is a special case helper to splice directly between two
942 * points, without requiring an explicit pipe. Internally an allocated
Randy Dunlap79685b82007-07-27 08:08:51 +0200943 * pipe is cached in the process, and reused during the lifetime of
Jens Axboe932cc6d2007-06-21 13:10:21 +0200944 * that process.
945 *
Jens Axboec66ab6f2007-06-12 21:17:17 +0200946 */
947ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
948 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +0200949{
950 struct pipe_inode_info *pipe;
951 long ret, bytes;
952 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200953 size_t len;
954 int i, flags;
Jens Axboeb92ce552006-04-11 13:52:07 +0200955
956 /*
957 * We require the input being a regular file, as we don't want to
958 * randomly drop data for eg socket -> socket splicing. Use the
959 * piped splicing for that!
960 */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800961 i_mode = in->f_path.dentry->d_inode->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +0200962 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
963 return -EINVAL;
964
965 /*
966 * neither in nor out is a pipe, setup an internal pipe attached to
967 * 'out' and transfer the wanted data from 'in' to 'out' through that
968 */
969 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200970 if (unlikely(!pipe)) {
Jens Axboeb92ce552006-04-11 13:52:07 +0200971 pipe = alloc_pipe_info(NULL);
972 if (!pipe)
973 return -ENOMEM;
974
975 /*
976 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200977 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200978 * PIPE_READERS appropriately.
979 */
980 pipe->readers = 1;
981
982 current->splice_pipe = pipe;
983 }
984
985 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200986 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200987 */
988 ret = 0;
989 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200990 len = sd->total_len;
991 flags = sd->flags;
992
993 /*
994 * Don't block on output, we have to drain the direct pipe.
995 */
996 sd->flags &= ~SPLICE_F_NONBLOCK;
Jens Axboeb92ce552006-04-11 13:52:07 +0200997
998 while (len) {
Jens Axboe51a92c02007-07-13 14:11:43 +0200999 size_t read_len;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001000 loff_t pos = sd->pos, prev_pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001001
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001002 ret = do_splice_to(in, &pos, pipe, len, flags);
Jens Axboe51a92c02007-07-13 14:11:43 +02001003 if (unlikely(ret <= 0))
Jens Axboeb92ce552006-04-11 13:52:07 +02001004 goto out_release;
1005
1006 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001007 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +02001008
1009 /*
1010 * NOTE: nonblocking mode only applies to the input. We
1011 * must not do the output in nonblocking mode as then we
1012 * could get stuck data in the internal pipe:
1013 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001014 ret = actor(pipe, sd);
Tom Zanussia82c53a2008-05-09 13:28:36 +02001015 if (unlikely(ret <= 0)) {
1016 sd->pos = prev_pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001017 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001018 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001019
1020 bytes += ret;
1021 len -= ret;
Jens Axboebcd4f3a2007-07-16 14:41:49 +02001022 sd->pos = pos;
Jens Axboeb92ce552006-04-11 13:52:07 +02001023
Tom Zanussia82c53a2008-05-09 13:28:36 +02001024 if (ret < read_len) {
1025 sd->pos = prev_pos + ret;
Jens Axboe51a92c02007-07-13 14:11:43 +02001026 goto out_release;
Tom Zanussia82c53a2008-05-09 13:28:36 +02001027 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001028 }
1029
Jens Axboe9e971982008-01-29 21:05:57 +01001030done:
Jens Axboeb92ce552006-04-11 13:52:07 +02001031 pipe->nrbufs = pipe->curbuf = 0;
Jens Axboe80848702008-01-30 12:24:48 +01001032 file_accessed(in);
Jens Axboeb92ce552006-04-11 13:52:07 +02001033 return bytes;
1034
1035out_release:
1036 /*
1037 * If we did an incomplete transfer we must release
1038 * the pipe buffers in question:
1039 */
1040 for (i = 0; i < PIPE_BUFFERS; i++) {
1041 struct pipe_buffer *buf = pipe->bufs + i;
1042
1043 if (buf->ops) {
1044 buf->ops->release(pipe, buf);
1045 buf->ops = NULL;
1046 }
1047 }
Jens Axboeb92ce552006-04-11 13:52:07 +02001048
Jens Axboe9e971982008-01-29 21:05:57 +01001049 if (!bytes)
1050 bytes = ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001051
Jens Axboe9e971982008-01-29 21:05:57 +01001052 goto done;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001053}
1054EXPORT_SYMBOL(splice_direct_to_actor);
1055
1056static int direct_splice_actor(struct pipe_inode_info *pipe,
1057 struct splice_desc *sd)
1058{
Jens Axboe6a14b902007-06-14 13:08:55 +02001059 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001060
1061 return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
1062}
1063
Jens Axboe932cc6d2007-06-21 13:10:21 +02001064/**
1065 * do_splice_direct - splices data directly between two files
1066 * @in: file to splice from
1067 * @ppos: input file offset
1068 * @out: file to splice to
1069 * @len: number of bytes to splice
1070 * @flags: splice modifier flags
1071 *
1072 * Description:
1073 * For use by do_sendfile(). splice can easily emulate sendfile, but
1074 * doing it in the application would incur an extra system call
1075 * (splice in + splice out, as compared to just sendfile()). So this helper
1076 * can splice directly through a process-private pipe.
1077 *
1078 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001079long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1080 size_t len, unsigned int flags)
1081{
1082 struct splice_desc sd = {
1083 .len = len,
1084 .total_len = len,
1085 .flags = flags,
1086 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001087 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001088 };
Jens Axboe51a92c02007-07-13 14:11:43 +02001089 long ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001090
1091 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
Jens Axboe51a92c02007-07-13 14:11:43 +02001092 if (ret > 0)
Tom Zanussia82c53a2008-05-09 13:28:36 +02001093 *ppos = sd.pos;
Jens Axboe51a92c02007-07-13 14:11:43 +02001094
Jens Axboec66ab6f2007-06-12 21:17:17 +02001095 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001096}
1097
Jens Axboe83f91352006-04-02 23:05:09 +02001098/*
Jens Axboeddac0d32006-11-04 12:49:32 +01001099 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1100 * location, so checking ->i_pipe is not enough to verify that this is a
1101 * pipe.
1102 */
1103static inline struct pipe_inode_info *pipe_info(struct inode *inode)
1104{
1105 if (S_ISFIFO(inode->i_mode))
1106 return inode->i_pipe;
1107
1108 return NULL;
1109}
1110
1111/*
Jens Axboe83f91352006-04-02 23:05:09 +02001112 * Determine where to splice to/from.
1113 */
Ingo Molnar529565d2006-04-10 15:18:58 +02001114static long do_splice(struct file *in, loff_t __user *off_in,
1115 struct file *out, loff_t __user *off_out,
1116 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001117{
Ingo Molnar3a326a22006-04-10 15:18:35 +02001118 struct pipe_inode_info *pipe;
Jens Axboecbb7e572006-04-11 14:57:50 +02001119 loff_t offset, *off;
Jens Axboea4514eb2006-04-19 15:57:05 +02001120 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001121
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001122 pipe = pipe_info(in->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001123 if (pipe) {
1124 if (off_in)
1125 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001126 if (off_out) {
1127 if (out->f_op->llseek == no_llseek)
1128 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001129 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001130 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001131 off = &offset;
1132 } else
1133 off = &out->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001134
Jens Axboea4514eb2006-04-19 15:57:05 +02001135 ret = do_splice_from(pipe, out, off, len, flags);
1136
1137 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1138 ret = -EFAULT;
1139
1140 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001141 }
Jens Axboe5274f052006-03-30 15:15:30 +02001142
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001143 pipe = pipe_info(out->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001144 if (pipe) {
1145 if (off_out)
1146 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001147 if (off_in) {
1148 if (in->f_op->llseek == no_llseek)
1149 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001150 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001151 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001152 off = &offset;
1153 } else
1154 off = &in->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001155
Jens Axboea4514eb2006-04-19 15:57:05 +02001156 ret = do_splice_to(in, off, pipe, len, flags);
1157
1158 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1159 ret = -EFAULT;
1160
1161 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001162 }
Jens Axboe5274f052006-03-30 15:15:30 +02001163
1164 return -EINVAL;
1165}
1166
Jens Axboe912d35f2006-04-26 10:59:21 +02001167/*
1168 * Map an iov into an array of pages and offset/length tupples. With the
1169 * partial_page structure, we can map several non-contiguous ranges into
1170 * our ones pages[] map instead of splitting that operation into pieces.
1171 * Could easily be exported as a generic helper for other users, in which
1172 * case one would probably want to add a 'max_nr_pages' parameter as well.
1173 */
1174static int get_iovec_page_array(const struct iovec __user *iov,
1175 unsigned int nr_vecs, struct page **pages,
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001176 struct partial_page *partial, int aligned)
Jens Axboe912d35f2006-04-26 10:59:21 +02001177{
1178 int buffers = 0, error = 0;
1179
Jens Axboe912d35f2006-04-26 10:59:21 +02001180 while (nr_vecs) {
1181 unsigned long off, npages;
Linus Torvalds75723952007-10-01 13:17:28 -07001182 struct iovec entry;
Jens Axboe912d35f2006-04-26 10:59:21 +02001183 void __user *base;
1184 size_t len;
1185 int i;
1186
Linus Torvalds75723952007-10-01 13:17:28 -07001187 error = -EFAULT;
Nick Pigginbc40d732008-07-25 19:45:26 -07001188 if (copy_from_user(&entry, iov, sizeof(entry)))
Jens Axboe912d35f2006-04-26 10:59:21 +02001189 break;
Linus Torvalds75723952007-10-01 13:17:28 -07001190
1191 base = entry.iov_base;
1192 len = entry.iov_len;
Jens Axboe912d35f2006-04-26 10:59:21 +02001193
1194 /*
1195 * Sanity check this iovec. 0 read succeeds.
1196 */
Linus Torvalds75723952007-10-01 13:17:28 -07001197 error = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +02001198 if (unlikely(!len))
1199 break;
1200 error = -EFAULT;
Bastian Blank712a30e2008-02-10 16:47:57 +02001201 if (!access_ok(VERIFY_READ, base, len))
Jens Axboe912d35f2006-04-26 10:59:21 +02001202 break;
1203
1204 /*
1205 * Get this base offset and number of pages, then map
1206 * in the user pages.
1207 */
1208 off = (unsigned long) base & ~PAGE_MASK;
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001209
1210 /*
1211 * If asked for alignment, the offset must be zero and the
1212 * length a multiple of the PAGE_SIZE.
1213 */
1214 error = -EINVAL;
1215 if (aligned && (off || len & ~PAGE_MASK))
1216 break;
1217
Jens Axboe912d35f2006-04-26 10:59:21 +02001218 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1219 if (npages > PIPE_BUFFERS - buffers)
1220 npages = PIPE_BUFFERS - buffers;
1221
Nick Pigginbc40d732008-07-25 19:45:26 -07001222 error = get_user_pages_fast((unsigned long)base, npages,
1223 0, &pages[buffers]);
Jens Axboe912d35f2006-04-26 10:59:21 +02001224
1225 if (unlikely(error <= 0))
1226 break;
1227
1228 /*
1229 * Fill this contiguous range into the partial page map.
1230 */
1231 for (i = 0; i < error; i++) {
Jens Axboe75914892006-05-02 12:57:18 +02001232 const int plen = min_t(size_t, len, PAGE_SIZE - off);
Jens Axboe912d35f2006-04-26 10:59:21 +02001233
1234 partial[buffers].offset = off;
1235 partial[buffers].len = plen;
1236
1237 off = 0;
1238 len -= plen;
1239 buffers++;
1240 }
1241
1242 /*
1243 * We didn't complete this iov, stop here since it probably
1244 * means we have to move some of this into a pipe to
1245 * be able to continue.
1246 */
1247 if (len)
1248 break;
1249
1250 /*
1251 * Don't continue if we mapped fewer pages than we asked for,
1252 * or if we mapped the max number of pages that we have
1253 * room for.
1254 */
1255 if (error < npages || buffers == PIPE_BUFFERS)
1256 break;
1257
1258 nr_vecs--;
1259 iov++;
1260 }
1261
Jens Axboe912d35f2006-04-26 10:59:21 +02001262 if (buffers)
1263 return buffers;
1264
1265 return error;
1266}
1267
Jens Axboe6a14b902007-06-14 13:08:55 +02001268static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1269 struct splice_desc *sd)
1270{
1271 char *src;
1272 int ret;
1273
Jens Axboecac36bb02007-06-14 13:10:48 +02001274 ret = buf->ops->confirm(pipe, buf);
Jens Axboe6a14b902007-06-14 13:08:55 +02001275 if (unlikely(ret))
1276 return ret;
1277
1278 /*
1279 * See if we can use the atomic maps, by prefaulting in the
1280 * pages and doing an atomic copy
1281 */
1282 if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
1283 src = buf->ops->map(pipe, buf, 1);
1284 ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
1285 sd->len);
1286 buf->ops->unmap(pipe, buf, src);
1287 if (!ret) {
1288 ret = sd->len;
1289 goto out;
1290 }
1291 }
1292
1293 /*
1294 * No dice, use slow non-atomic map and copy
1295 */
1296 src = buf->ops->map(pipe, buf, 0);
1297
1298 ret = sd->len;
1299 if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
1300 ret = -EFAULT;
1301
Jens Axboe6866bef2007-10-16 10:01:29 +02001302 buf->ops->unmap(pipe, buf, src);
Jens Axboe6a14b902007-06-14 13:08:55 +02001303out:
1304 if (ret > 0)
1305 sd->u.userptr += ret;
Jens Axboe6a14b902007-06-14 13:08:55 +02001306 return ret;
1307}
1308
1309/*
1310 * For lack of a better implementation, implement vmsplice() to userspace
1311 * as a simple copy of the pipes pages to the user iov.
1312 */
1313static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
1314 unsigned long nr_segs, unsigned int flags)
1315{
1316 struct pipe_inode_info *pipe;
1317 struct splice_desc sd;
1318 ssize_t size;
1319 int error;
1320 long ret;
1321
1322 pipe = pipe_info(file->f_path.dentry->d_inode);
1323 if (!pipe)
1324 return -EBADF;
1325
1326 if (pipe->inode)
1327 mutex_lock(&pipe->inode->i_mutex);
1328
1329 error = ret = 0;
1330 while (nr_segs) {
1331 void __user *base;
1332 size_t len;
1333
1334 /*
1335 * Get user address base and length for this iovec.
1336 */
1337 error = get_user(base, &iov->iov_base);
1338 if (unlikely(error))
1339 break;
1340 error = get_user(len, &iov->iov_len);
1341 if (unlikely(error))
1342 break;
1343
1344 /*
1345 * Sanity check this iovec. 0 read succeeds.
1346 */
1347 if (unlikely(!len))
1348 break;
1349 if (unlikely(!base)) {
1350 error = -EFAULT;
1351 break;
1352 }
1353
Jens Axboe88119302008-02-08 08:49:14 -08001354 if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
1355 error = -EFAULT;
1356 break;
1357 }
1358
Jens Axboe6a14b902007-06-14 13:08:55 +02001359 sd.len = 0;
1360 sd.total_len = len;
1361 sd.flags = flags;
1362 sd.u.userptr = base;
1363 sd.pos = 0;
1364
1365 size = __splice_from_pipe(pipe, &sd, pipe_to_user);
1366 if (size < 0) {
1367 if (!ret)
1368 ret = size;
1369
1370 break;
1371 }
1372
1373 ret += size;
1374
1375 if (size < len)
1376 break;
1377
1378 nr_segs--;
1379 iov++;
1380 }
1381
1382 if (pipe->inode)
1383 mutex_unlock(&pipe->inode->i_mutex);
1384
1385 if (!ret)
1386 ret = error;
1387
1388 return ret;
1389}
1390
Jens Axboe912d35f2006-04-26 10:59:21 +02001391/*
1392 * vmsplice splices a user address range into a pipe. It can be thought of
1393 * as splice-from-memory, where the regular splice is splice-from-file (or
1394 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001395 */
Jens Axboe6a14b902007-06-14 13:08:55 +02001396static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
1397 unsigned long nr_segs, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001398{
Jens Axboeddac0d32006-11-04 12:49:32 +01001399 struct pipe_inode_info *pipe;
Jens Axboe912d35f2006-04-26 10:59:21 +02001400 struct page *pages[PIPE_BUFFERS];
1401 struct partial_page partial[PIPE_BUFFERS];
1402 struct splice_pipe_desc spd = {
1403 .pages = pages,
1404 .partial = partial,
1405 .flags = flags,
1406 .ops = &user_page_pipe_buf_ops,
Jens Axboebbdfc2f2007-11-06 23:29:47 -08001407 .spd_release = spd_release_page,
Jens Axboe912d35f2006-04-26 10:59:21 +02001408 };
1409
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001410 pipe = pipe_info(file->f_path.dentry->d_inode);
Jens Axboeddac0d32006-11-04 12:49:32 +01001411 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001412 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001413
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001414 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1415 flags & SPLICE_F_GIFT);
Jens Axboe912d35f2006-04-26 10:59:21 +02001416 if (spd.nr_pages <= 0)
1417 return spd.nr_pages;
1418
Jens Axboe00522fb2006-04-26 14:39:29 +02001419 return splice_to_pipe(pipe, &spd);
Jens Axboe912d35f2006-04-26 10:59:21 +02001420}
1421
Jens Axboe6a14b902007-06-14 13:08:55 +02001422/*
1423 * Note that vmsplice only really supports true splicing _from_ user memory
1424 * to a pipe, not the other way around. Splicing from user memory is a simple
1425 * operation that can be supported without any funky alignment restrictions
1426 * or nasty vm tricks. We simply map in the user memory and fill them into
1427 * a pipe. The reverse isn't quite as easy, though. There are two possible
1428 * solutions for that:
1429 *
1430 * - memcpy() the data internally, at which point we might as well just
1431 * do a regular read() on the buffer anyway.
1432 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1433 * has restriction limitations on both ends of the pipe).
1434 *
1435 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1436 *
1437 */
Jens Axboe912d35f2006-04-26 10:59:21 +02001438asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1439 unsigned long nr_segs, unsigned int flags)
1440{
1441 struct file *file;
1442 long error;
1443 int fput;
1444
Jens Axboe6a14b902007-06-14 13:08:55 +02001445 if (unlikely(nr_segs > UIO_MAXIOV))
1446 return -EINVAL;
1447 else if (unlikely(!nr_segs))
1448 return 0;
1449
Jens Axboe912d35f2006-04-26 10:59:21 +02001450 error = -EBADF;
1451 file = fget_light(fd, &fput);
1452 if (file) {
1453 if (file->f_mode & FMODE_WRITE)
Jens Axboe6a14b902007-06-14 13:08:55 +02001454 error = vmsplice_to_pipe(file, iov, nr_segs, flags);
1455 else if (file->f_mode & FMODE_READ)
1456 error = vmsplice_to_user(file, iov, nr_segs, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001457
1458 fput_light(file, fput);
1459 }
1460
1461 return error;
1462}
1463
Ingo Molnar529565d2006-04-10 15:18:58 +02001464asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1465 int fd_out, loff_t __user *off_out,
1466 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001467{
1468 long error;
1469 struct file *in, *out;
1470 int fput_in, fput_out;
1471
1472 if (unlikely(!len))
1473 return 0;
1474
1475 error = -EBADF;
Ingo Molnar529565d2006-04-10 15:18:58 +02001476 in = fget_light(fd_in, &fput_in);
Jens Axboe5274f052006-03-30 15:15:30 +02001477 if (in) {
1478 if (in->f_mode & FMODE_READ) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001479 out = fget_light(fd_out, &fput_out);
Jens Axboe5274f052006-03-30 15:15:30 +02001480 if (out) {
1481 if (out->f_mode & FMODE_WRITE)
Ingo Molnar529565d2006-04-10 15:18:58 +02001482 error = do_splice(in, off_in,
1483 out, off_out,
1484 len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001485 fput_light(out, fput_out);
1486 }
1487 }
1488
1489 fput_light(in, fput_in);
1490 }
1491
1492 return error;
1493}
Jens Axboe70524492006-04-11 15:51:17 +02001494
1495/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001496 * Make sure there's data to read. Wait for input if we can, otherwise
1497 * return an appropriate error.
1498 */
1499static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1500{
1501 int ret;
1502
1503 /*
1504 * Check ->nrbufs without the inode lock first. This function
1505 * is speculative anyways, so missing one is ok.
1506 */
1507 if (pipe->nrbufs)
1508 return 0;
1509
1510 ret = 0;
1511 mutex_lock(&pipe->inode->i_mutex);
1512
1513 while (!pipe->nrbufs) {
1514 if (signal_pending(current)) {
1515 ret = -ERESTARTSYS;
1516 break;
1517 }
1518 if (!pipe->writers)
1519 break;
1520 if (!pipe->waiting_writers) {
1521 if (flags & SPLICE_F_NONBLOCK) {
1522 ret = -EAGAIN;
1523 break;
1524 }
1525 }
1526 pipe_wait(pipe);
1527 }
1528
1529 mutex_unlock(&pipe->inode->i_mutex);
1530 return ret;
1531}
1532
1533/*
1534 * Make sure there's writeable room. Wait for room if we can, otherwise
1535 * return an appropriate error.
1536 */
1537static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1538{
1539 int ret;
1540
1541 /*
1542 * Check ->nrbufs without the inode lock first. This function
1543 * is speculative anyways, so missing one is ok.
1544 */
1545 if (pipe->nrbufs < PIPE_BUFFERS)
1546 return 0;
1547
1548 ret = 0;
1549 mutex_lock(&pipe->inode->i_mutex);
1550
1551 while (pipe->nrbufs >= PIPE_BUFFERS) {
1552 if (!pipe->readers) {
1553 send_sig(SIGPIPE, current, 0);
1554 ret = -EPIPE;
1555 break;
1556 }
1557 if (flags & SPLICE_F_NONBLOCK) {
1558 ret = -EAGAIN;
1559 break;
1560 }
1561 if (signal_pending(current)) {
1562 ret = -ERESTARTSYS;
1563 break;
1564 }
1565 pipe->waiting_writers++;
1566 pipe_wait(pipe);
1567 pipe->waiting_writers--;
1568 }
1569
1570 mutex_unlock(&pipe->inode->i_mutex);
1571 return ret;
1572}
1573
1574/*
Jens Axboe70524492006-04-11 15:51:17 +02001575 * Link contents of ipipe to opipe.
1576 */
1577static int link_pipe(struct pipe_inode_info *ipipe,
1578 struct pipe_inode_info *opipe,
1579 size_t len, unsigned int flags)
1580{
1581 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001582 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001583
1584 /*
1585 * Potential ABBA deadlock, work around it by ordering lock
1586 * grabbing by inode address. Otherwise two different processes
1587 * could deadlock (one doing tee from A -> B, the other from B -> A).
1588 */
Mark Fasheh62752ee2006-10-17 10:31:38 +02001589 inode_double_lock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001590
Jens Axboeaadd06e2006-07-10 11:00:01 +02001591 do {
Jens Axboe70524492006-04-11 15:51:17 +02001592 if (!opipe->readers) {
1593 send_sig(SIGPIPE, current, 0);
1594 if (!ret)
1595 ret = -EPIPE;
1596 break;
1597 }
Jens Axboe70524492006-04-11 15:51:17 +02001598
1599 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001600 * If we have iterated all input buffers or ran out of
1601 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001602 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001603 if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
Jens Axboe70524492006-04-11 15:51:17 +02001604 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001605
1606 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1607 nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1608
Jens Axboe2a27250e2006-04-19 15:56:40 +02001609 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001610 * Get a reference to this pipe buffer,
1611 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001612 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001613 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001614
Jens Axboeaadd06e2006-07-10 11:00:01 +02001615 obuf = opipe->bufs + nbuf;
1616 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001617
Jens Axboeaadd06e2006-07-10 11:00:01 +02001618 /*
1619 * Don't inherit the gift flag, we need to
1620 * prevent multiple steals of this page.
1621 */
1622 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1623
1624 if (obuf->len > len)
1625 obuf->len = len;
1626
1627 opipe->nrbufs++;
1628 ret += obuf->len;
1629 len -= obuf->len;
1630 i++;
1631 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001632
Jens Axboe02cf01a2008-02-20 10:34:51 +01001633 /*
1634 * return EAGAIN if we have the potential of some data in the
1635 * future, otherwise just return 0
1636 */
1637 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1638 ret = -EAGAIN;
1639
Mark Fasheh62752ee2006-10-17 10:31:38 +02001640 inode_double_unlock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001641
Jens Axboeaadd06e2006-07-10 11:00:01 +02001642 /*
1643 * If we put data in the output pipe, wakeup any potential readers.
1644 */
1645 if (ret > 0) {
Jens Axboe70524492006-04-11 15:51:17 +02001646 smp_mb();
1647 if (waitqueue_active(&opipe->wait))
1648 wake_up_interruptible(&opipe->wait);
1649 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1650 }
1651
1652 return ret;
1653}
1654
1655/*
1656 * This is a tee(1) implementation that works on pipes. It doesn't copy
1657 * any data, it simply references the 'in' pages on the 'out' pipe.
1658 * The 'flags' used are the SPLICE_F_* variants, currently the only
1659 * applicable one is SPLICE_F_NONBLOCK.
1660 */
1661static long do_tee(struct file *in, struct file *out, size_t len,
1662 unsigned int flags)
1663{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001664 struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
1665 struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001666 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001667
1668 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001669 * Duplicate the contents of ipipe to opipe without actually
1670 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001671 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001672 if (ipipe && opipe && ipipe != opipe) {
1673 /*
1674 * Keep going, unless we encounter an error. The ipipe/opipe
1675 * ordering doesn't really matter.
1676 */
1677 ret = link_ipipe_prep(ipipe, flags);
1678 if (!ret) {
1679 ret = link_opipe_prep(opipe, flags);
Jens Axboe02cf01a2008-02-20 10:34:51 +01001680 if (!ret)
Jens Axboeaadd06e2006-07-10 11:00:01 +02001681 ret = link_pipe(ipipe, opipe, len, flags);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001682 }
1683 }
Jens Axboe70524492006-04-11 15:51:17 +02001684
Jens Axboeaadd06e2006-07-10 11:00:01 +02001685 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001686}
1687
1688asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1689{
1690 struct file *in;
1691 int error, fput_in;
1692
1693 if (unlikely(!len))
1694 return 0;
1695
1696 error = -EBADF;
1697 in = fget_light(fdin, &fput_in);
1698 if (in) {
1699 if (in->f_mode & FMODE_READ) {
1700 int fput_out;
1701 struct file *out = fget_light(fdout, &fput_out);
1702
1703 if (out) {
1704 if (out->f_mode & FMODE_WRITE)
1705 error = do_tee(in, out, len, flags);
1706 fput_light(out, fput_out);
1707 }
1708 }
1709 fput_light(in, fput_in);
1710 }
1711
1712 return error;
1713}