blob: 7fb04970c72d853e53874e4ec03d9df48911dcb3 [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 Axboec2058e02006-04-11 13:56:34 +020015 * Copyright (C) 2005-2006 Jens Axboe <axboe@suse.de>
16 * 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>
23#include <linux/pipe_fs_i.h>
24#include <linux/mm_inline.h>
Jens Axboe5abc97a2006-03-30 15:16:46 +020025#include <linux/swap.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020026#include <linux/writeback.h>
27#include <linux/buffer_head.h>
Jeff Garzika0f06782006-03-30 23:06:13 -050028#include <linux/module.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>
Jens Axboe5274f052006-03-30 15:15:30 +020031
Jens Axboe912d35f2006-04-26 10:59:21 +020032struct partial_page {
33 unsigned int offset;
34 unsigned int len;
35};
36
37/*
Jens Axboe00522fb2006-04-26 14:39:29 +020038 * Passed to splice_to_pipe
Jens Axboe912d35f2006-04-26 10:59:21 +020039 */
40struct splice_pipe_desc {
41 struct page **pages; /* page map */
42 struct partial_page *partial; /* pages[] may not be contig */
43 int nr_pages; /* number of pages in map */
44 unsigned int flags; /* splice flags */
45 struct pipe_buf_operations *ops;/* ops associated with output pipe */
46};
47
Jens Axboe83f91352006-04-02 23:05:09 +020048/*
49 * Attempt to steal a page from a pipe buffer. This should perhaps go into
50 * a vm helper function, it's already simplified quite a bit by the
51 * addition of remove_mapping(). If success is returned, the caller may
52 * attempt to reuse this page for another destination.
53 */
Jens Axboe5abc97a2006-03-30 15:16:46 +020054static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
55 struct pipe_buffer *buf)
56{
57 struct page *page = buf->page;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020058 struct address_space *mapping = page_mapping(page);
Jens Axboe5abc97a2006-03-30 15:16:46 +020059
Jens Axboe9e0267c2006-04-19 15:57:31 +020060 lock_page(page);
61
Jens Axboe5abc97a2006-03-30 15:16:46 +020062 WARN_ON(!PageUptodate(page));
63
Jens Axboead8d6f02006-04-02 23:10:32 +020064 /*
65 * At least for ext2 with nobh option, we need to wait on writeback
66 * completing on this page, since we'll remove it from the pagecache.
67 * Otherwise truncate wont wait on the page, allowing the disk
68 * blocks to be reused by someone else before we actually wrote our
69 * data to them. fs corruption ensues.
70 */
71 wait_on_page_writeback(page);
72
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020073 if (PagePrivate(page))
74 try_to_release_page(page, mapping_gfp_mask(mapping));
75
Jens Axboe9e0267c2006-04-19 15:57:31 +020076 if (!remove_mapping(mapping, page)) {
77 unlock_page(page);
Jens Axboe5abc97a2006-03-30 15:16:46 +020078 return 1;
Jens Axboe9e0267c2006-04-19 15:57:31 +020079 }
Jens Axboe5abc97a2006-03-30 15:16:46 +020080
Jens Axboe5abc97a2006-03-30 15:16:46 +020081 return 0;
82}
83
Jens Axboe5274f052006-03-30 15:15:30 +020084static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
85 struct pipe_buffer *buf)
86{
87 page_cache_release(buf->page);
Jens Axboe5274f052006-03-30 15:15:30 +020088}
89
Jens Axboef84d7512006-05-01 19:59:03 +020090static int page_cache_pipe_buf_pin(struct pipe_inode_info *info,
91 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +020092{
93 struct page *page = buf->page;
Jens Axboe49d0b212006-04-10 09:04:41 +020094 int err;
Jens Axboe5274f052006-03-30 15:15:30 +020095
96 if (!PageUptodate(page)) {
Jens Axboe49d0b212006-04-10 09:04:41 +020097 lock_page(page);
98
99 /*
100 * Page got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +0200101 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200102 */
103 if (!page->mapping) {
104 err = -ENODATA;
105 goto error;
106 }
107
108 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200109 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200110 */
111 if (!PageUptodate(page)) {
112 err = -EIO;
113 goto error;
114 }
115
116 /*
Jens Axboef84d7512006-05-01 19:59:03 +0200117 * Page is ok afterall, we are done.
Jens Axboe49d0b212006-04-10 09:04:41 +0200118 */
Jens Axboe5274f052006-03-30 15:15:30 +0200119 unlock_page(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200120 }
121
Jens Axboef84d7512006-05-01 19:59:03 +0200122 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200123error:
124 unlock_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200125 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200126}
127
Jens Axboe5274f052006-03-30 15:15:30 +0200128static struct pipe_buf_operations page_cache_pipe_buf_ops = {
129 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200130 .map = generic_pipe_buf_map,
131 .unmap = generic_pipe_buf_unmap,
132 .pin = page_cache_pipe_buf_pin,
Jens Axboe5274f052006-03-30 15:15:30 +0200133 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200134 .steal = page_cache_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200135 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200136};
137
Jens Axboe912d35f2006-04-26 10:59:21 +0200138static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
139 struct pipe_buffer *buf)
140{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200141 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
142 return 1;
143
Jens Axboe330ab712006-05-02 15:29:57 +0200144 return generic_pipe_buf_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200145}
146
147static struct pipe_buf_operations user_page_pipe_buf_ops = {
148 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200149 .map = generic_pipe_buf_map,
150 .unmap = generic_pipe_buf_unmap,
151 .pin = generic_pipe_buf_pin,
Jens Axboe912d35f2006-04-26 10:59:21 +0200152 .release = page_cache_pipe_buf_release,
153 .steal = user_page_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200154 .get = generic_pipe_buf_get,
Jens Axboe912d35f2006-04-26 10:59:21 +0200155};
156
Jens Axboe83f91352006-04-02 23:05:09 +0200157/*
158 * Pipe output worker. This sets up our pipe format with the page cache
159 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
160 */
Jens Axboe00522fb2006-04-26 14:39:29 +0200161static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
162 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200163{
Jens Axboe912d35f2006-04-26 10:59:21 +0200164 int ret, do_wakeup, page_nr;
Jens Axboe5274f052006-03-30 15:15:30 +0200165
166 ret = 0;
167 do_wakeup = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +0200168 page_nr = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200169
Ingo Molnar3a326a22006-04-10 15:18:35 +0200170 if (pipe->inode)
171 mutex_lock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200172
Jens Axboe5274f052006-03-30 15:15:30 +0200173 for (;;) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200174 if (!pipe->readers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200175 send_sig(SIGPIPE, current, 0);
176 if (!ret)
177 ret = -EPIPE;
178 break;
179 }
180
Jens Axboe6f767b02006-04-11 13:53:56 +0200181 if (pipe->nrbufs < PIPE_BUFFERS) {
182 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200183 struct pipe_buffer *buf = pipe->bufs + newbuf;
Jens Axboe5274f052006-03-30 15:15:30 +0200184
Jens Axboe912d35f2006-04-26 10:59:21 +0200185 buf->page = spd->pages[page_nr];
186 buf->offset = spd->partial[page_nr].offset;
187 buf->len = spd->partial[page_nr].len;
188 buf->ops = spd->ops;
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200189 if (spd->flags & SPLICE_F_GIFT)
190 buf->flags |= PIPE_BUF_FLAG_GIFT;
191
Jens Axboe6f767b02006-04-11 13:53:56 +0200192 pipe->nrbufs++;
Jens Axboe912d35f2006-04-26 10:59:21 +0200193 page_nr++;
194 ret += buf->len;
195
Jens Axboe6f767b02006-04-11 13:53:56 +0200196 if (pipe->inode)
197 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200198
Jens Axboe912d35f2006-04-26 10:59:21 +0200199 if (!--spd->nr_pages)
Jens Axboe5274f052006-03-30 15:15:30 +0200200 break;
Jens Axboe6f767b02006-04-11 13:53:56 +0200201 if (pipe->nrbufs < PIPE_BUFFERS)
Jens Axboe5274f052006-03-30 15:15:30 +0200202 continue;
203
204 break;
205 }
206
Jens Axboe912d35f2006-04-26 10:59:21 +0200207 if (spd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700208 if (!ret)
209 ret = -EAGAIN;
210 break;
211 }
212
Jens Axboe5274f052006-03-30 15:15:30 +0200213 if (signal_pending(current)) {
214 if (!ret)
215 ret = -ERESTARTSYS;
216 break;
217 }
218
219 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200220 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200221 if (waitqueue_active(&pipe->wait))
222 wake_up_interruptible_sync(&pipe->wait);
223 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Jens Axboe5274f052006-03-30 15:15:30 +0200224 do_wakeup = 0;
225 }
226
Ingo Molnar3a326a22006-04-10 15:18:35 +0200227 pipe->waiting_writers++;
228 pipe_wait(pipe);
229 pipe->waiting_writers--;
Jens Axboe5274f052006-03-30 15:15:30 +0200230 }
231
Ingo Molnar3a326a22006-04-10 15:18:35 +0200232 if (pipe->inode)
233 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200234
235 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200236 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200237 if (waitqueue_active(&pipe->wait))
238 wake_up_interruptible(&pipe->wait);
239 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Jens Axboe5274f052006-03-30 15:15:30 +0200240 }
241
Jens Axboe912d35f2006-04-26 10:59:21 +0200242 while (page_nr < spd->nr_pages)
243 page_cache_release(spd->pages[page_nr++]);
Jens Axboe5274f052006-03-30 15:15:30 +0200244
245 return ret;
246}
247
Ingo Molnar3a326a22006-04-10 15:18:35 +0200248static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200249__generic_file_splice_read(struct file *in, loff_t *ppos,
250 struct pipe_inode_info *pipe, size_t len,
251 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200252{
253 struct address_space *mapping = in->f_mapping;
Jens Axboe912d35f2006-04-26 10:59:21 +0200254 unsigned int loff, nr_pages;
Jens Axboe16c523d2006-04-10 09:03:58 +0200255 struct page *pages[PIPE_BUFFERS];
Jens Axboe912d35f2006-04-26 10:59:21 +0200256 struct partial_page partial[PIPE_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200257 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200258 pgoff_t index, end_index;
259 loff_t isize;
Jens Axboe912d35f2006-04-26 10:59:21 +0200260 size_t total_len;
Jens Axboeeb207962006-04-27 11:05:22 +0200261 int error, page_nr;
Jens Axboe912d35f2006-04-26 10:59:21 +0200262 struct splice_pipe_desc spd = {
263 .pages = pages,
264 .partial = partial,
265 .flags = flags,
266 .ops = &page_cache_pipe_buf_ops,
267 };
Jens Axboe5274f052006-03-30 15:15:30 +0200268
Jens Axboecbb7e572006-04-11 14:57:50 +0200269 index = *ppos >> PAGE_CACHE_SHIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +0200270 loff = *ppos & ~PAGE_CACHE_MASK;
271 nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe5274f052006-03-30 15:15:30 +0200272
273 if (nr_pages > PIPE_BUFFERS)
274 nr_pages = PIPE_BUFFERS;
275
276 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200277 * Initiate read-ahead on this page range. however, don't call into
Jens Axboe0b749ce2006-04-10 09:05:04 +0200278 * read-ahead if this is a non-zero offset (we are likely doing small
279 * chunk splice and the page is already there) for a single page.
Jens Axboe5274f052006-03-30 15:15:30 +0200280 */
Jens Axboeeb645a22006-04-27 08:44:27 +0200281 if (!loff || nr_pages > 1)
282 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
Jens Axboe5274f052006-03-30 15:15:30 +0200283
284 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200285 * Now fill in the holes:
Jens Axboe5274f052006-03-30 15:15:30 +0200286 */
Jens Axboe7480a902006-04-11 13:52:47 +0200287 error = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +0200288 total_len = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200289
290 /*
291 * Lookup the (hopefully) full range of pages we need.
292 */
293 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
294
295 /*
296 * If find_get_pages_contig() returned fewer pages than we needed,
297 * allocate the rest.
298 */
299 index += spd.nr_pages;
300 while (spd.nr_pages < nr_pages) {
301 /*
302 * Page could be there, find_get_pages_contig() breaks on
303 * the first hole.
304 */
305 page = find_get_page(mapping, index);
306 if (!page) {
307 /*
Jens Axboee27dedd2006-05-01 19:59:54 +0200308 * Make sure the read-ahead engine is notified
309 * about this failure.
310 */
311 handle_ra_miss(mapping, &in->f_ra, index);
312
313 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200314 * page didn't exist, allocate one.
315 */
316 page = page_cache_alloc_cold(mapping);
317 if (!page)
318 break;
319
320 error = add_to_page_cache_lru(page, mapping, index,
321 mapping_gfp_mask(mapping));
322 if (unlikely(error)) {
323 page_cache_release(page);
324 break;
325 }
326 /*
327 * add_to_page_cache() locks the page, unlock it
328 * to avoid convoluting the logic below even more.
329 */
330 unlock_page(page);
331 }
332
333 pages[spd.nr_pages++] = page;
334 index++;
335 }
336
337 /*
338 * Now loop over the map and see if we need to start IO on any
339 * pages, fill in the partial map, etc.
340 */
341 index = *ppos >> PAGE_CACHE_SHIFT;
342 nr_pages = spd.nr_pages;
343 spd.nr_pages = 0;
344 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200345 unsigned int this_len;
346
347 if (!len)
348 break;
349
350 /*
351 * this_len is the max we'll use from this page
352 */
Andrew Mortonba5f5d92006-04-25 15:33:34 +0200353 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
Jens Axboeeb207962006-04-27 11:05:22 +0200354 page = pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200355
356 /*
357 * If the page isn't uptodate, we may need to start io on it
358 */
359 if (!PageUptodate(page)) {
Jens Axboec4f895c2006-04-19 15:56:12 +0200360 /*
361 * If in nonblock mode then dont block on waiting
362 * for an in-flight io page
363 */
364 if (flags & SPLICE_F_NONBLOCK)
365 break;
366
Jens Axboe7480a902006-04-11 13:52:47 +0200367 lock_page(page);
368
369 /*
370 * page was truncated, stop here. if this isn't the
371 * first page, we'll just complete what we already
372 * added
373 */
374 if (!page->mapping) {
375 unlock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200376 break;
377 }
378 /*
379 * page was already under io and is now done, great
380 */
381 if (PageUptodate(page)) {
382 unlock_page(page);
383 goto fill_it;
384 }
385
Jens Axboe7480a902006-04-11 13:52:47 +0200386 /*
387 * need to read in the page
388 */
389 error = mapping->a_ops->readpage(in, page);
Jens Axboe7480a902006-04-11 13:52:47 +0200390 if (unlikely(error)) {
Jens Axboeeb207962006-04-27 11:05:22 +0200391 /*
392 * We really should re-lookup the page here,
393 * but it complicates things a lot. Instead
394 * lets just do what we already stored, and
395 * we'll get it the next time we are called.
396 */
Jens Axboe7480a902006-04-11 13:52:47 +0200397 if (error == AOP_TRUNCATED_PAGE)
Jens Axboeeb207962006-04-27 11:05:22 +0200398 error = 0;
399
Jens Axboe7480a902006-04-11 13:52:47 +0200400 break;
401 }
Jens Axboe91ad66e2006-04-19 15:55:10 +0200402
403 /*
404 * i_size must be checked after ->readpage().
405 */
406 isize = i_size_read(mapping->host);
407 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Jens Axboeeb207962006-04-27 11:05:22 +0200408 if (unlikely(!isize || index > end_index))
Jens Axboe91ad66e2006-04-19 15:55:10 +0200409 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200410
411 /*
412 * if this is the last page, see if we need to shrink
413 * the length and stop
414 */
415 if (end_index == index) {
416 loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
Jens Axboeeb207962006-04-27 11:05:22 +0200417 if (total_len + loff > isize)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200418 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200419 /*
420 * force quit after adding this page
421 */
Jens Axboeeb207962006-04-27 11:05:22 +0200422 len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200423 this_len = min(this_len, loff);
Jens Axboe912d35f2006-04-26 10:59:21 +0200424 loff = 0;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200425 }
Jens Axboe7480a902006-04-11 13:52:47 +0200426 }
427fill_it:
Jens Axboeeb207962006-04-27 11:05:22 +0200428 partial[page_nr].offset = loff;
429 partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200430 len -= this_len;
Jens Axboe912d35f2006-04-26 10:59:21 +0200431 total_len += this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200432 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200433 spd.nr_pages++;
434 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200435 }
436
Jens Axboeeb207962006-04-27 11:05:22 +0200437 /*
438 * Release any pages at the end, if we quit early. 'i' is how far
439 * we got, 'nr_pages' is how many pages are in the map.
440 */
441 while (page_nr < nr_pages)
442 page_cache_release(pages[page_nr++]);
443
Jens Axboe912d35f2006-04-26 10:59:21 +0200444 if (spd.nr_pages)
Jens Axboe00522fb2006-04-26 14:39:29 +0200445 return splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200446
Jens Axboe7480a902006-04-11 13:52:47 +0200447 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200448}
449
Jens Axboe83f91352006-04-02 23:05:09 +0200450/**
451 * generic_file_splice_read - splice data from file to a pipe
452 * @in: file to splice from
453 * @pipe: pipe to splice to
454 * @len: number of bytes to splice
455 * @flags: splice modifier flags
456 *
457 * Will read pages from given file and fill them into a pipe.
Jens Axboe83f91352006-04-02 23:05:09 +0200458 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200459ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
460 struct pipe_inode_info *pipe, size_t len,
461 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200462{
463 ssize_t spliced;
464 int ret;
465
466 ret = 0;
467 spliced = 0;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200468
Jens Axboe5274f052006-03-30 15:15:30 +0200469 while (len) {
Jens Axboecbb7e572006-04-11 14:57:50 +0200470 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200471
Jens Axboec4f895c2006-04-19 15:56:12 +0200472 if (ret < 0)
Jens Axboe5274f052006-03-30 15:15:30 +0200473 break;
Jens Axboec4f895c2006-04-19 15:56:12 +0200474 else if (!ret) {
475 if (spliced)
476 break;
477 if (flags & SPLICE_F_NONBLOCK) {
478 ret = -EAGAIN;
479 break;
480 }
481 }
Jens Axboe5274f052006-03-30 15:15:30 +0200482
Jens Axboecbb7e572006-04-11 14:57:50 +0200483 *ppos += ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200484 len -= ret;
485 spliced += ret;
486 }
487
488 if (spliced)
489 return spliced;
490
491 return ret;
492}
493
Jens Axboe059a8f32006-04-02 23:06:05 +0200494EXPORT_SYMBOL(generic_file_splice_read);
495
Jens Axboe5274f052006-03-30 15:15:30 +0200496/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200497 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200498 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200499 */
500static int pipe_to_sendpage(struct pipe_inode_info *info,
501 struct pipe_buffer *buf, struct splice_desc *sd)
502{
503 struct file *file = sd->file;
504 loff_t pos = sd->pos;
Jens Axboef84d7512006-05-01 19:59:03 +0200505 int ret, more;
Jens Axboe5274f052006-03-30 15:15:30 +0200506
Jens Axboef84d7512006-05-01 19:59:03 +0200507 ret = buf->ops->pin(info, buf);
508 if (!ret) {
509 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200510
Jens Axboef84d7512006-05-01 19:59:03 +0200511 ret = file->f_op->sendpage(file, buf->page, buf->offset,
512 sd->len, &pos, more);
513 }
Jens Axboe5274f052006-03-30 15:15:30 +0200514
Jens Axboe016b6612006-04-25 15:42:00 +0200515 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200516}
517
518/*
519 * This is a little more tricky than the file -> pipe splicing. There are
520 * basically three cases:
521 *
522 * - Destination page already exists in the address space and there
523 * are users of it. For that case we have no other option that
524 * copying the data. Tough luck.
525 * - Destination page already exists in the address space, but there
526 * are no users of it. Make sure it's uptodate, then drop it. Fall
527 * through to last case.
528 * - Destination page does not exist, we can add the pipe page to
529 * the page cache and avoid the copy.
530 *
Jens Axboe83f91352006-04-02 23:05:09 +0200531 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
532 * sd->flags), we attempt to migrate pages from the pipe to the output
533 * file address space page cache. This is possible if no one else has
534 * the pipe page referenced outside of the pipe and page cache. If
535 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
536 * a new page in the output file page cache and fill/dirty that.
Jens Axboe5274f052006-03-30 15:15:30 +0200537 */
538static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
539 struct splice_desc *sd)
540{
541 struct file *file = sd->file;
542 struct address_space *mapping = file->f_mapping;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200543 gfp_t gfp_mask = mapping_gfp_mask(mapping);
Jens Axboe016b6612006-04-25 15:42:00 +0200544 unsigned int offset, this_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200545 struct page *page;
Jens Axboe5274f052006-03-30 15:15:30 +0200546 pgoff_t index;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200547 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200548
549 /*
Jens Axboe49d0b212006-04-10 09:04:41 +0200550 * make sure the data in this buffer is uptodate
Jens Axboe5274f052006-03-30 15:15:30 +0200551 */
Jens Axboef84d7512006-05-01 19:59:03 +0200552 ret = buf->ops->pin(info, buf);
553 if (unlikely(ret))
554 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200555
556 index = sd->pos >> PAGE_CACHE_SHIFT;
557 offset = sd->pos & ~PAGE_CACHE_MASK;
558
Jens Axboe016b6612006-04-25 15:42:00 +0200559 this_len = sd->len;
560 if (this_len + offset > PAGE_CACHE_SIZE)
561 this_len = PAGE_CACHE_SIZE - offset;
562
Jens Axboe5274f052006-03-30 15:15:30 +0200563 /*
Jens Axboe0568b402006-05-01 19:50:48 +0200564 * Reuse buf page, if SPLICE_F_MOVE is set and we are doing a full
565 * page.
Jens Axboe5274f052006-03-30 15:15:30 +0200566 */
Jens Axboe0568b402006-05-01 19:50:48 +0200567 if ((sd->flags & SPLICE_F_MOVE) && this_len == PAGE_CACHE_SIZE) {
Jens Axboe83f91352006-04-02 23:05:09 +0200568 /*
569 * If steal succeeds, buf->page is now pruned from the vm
Jens Axboea893b992006-05-02 15:03:27 +0200570 * side (page cache) and we can reuse it. The page will also
571 * be locked on successful return.
Jens Axboe83f91352006-04-02 23:05:09 +0200572 */
Jens Axboe5abc97a2006-03-30 15:16:46 +0200573 if (buf->ops->steal(info, buf))
574 goto find_page;
Jens Axboe5274f052006-03-30 15:15:30 +0200575
Jens Axboe5abc97a2006-03-30 15:16:46 +0200576 page = buf->page;
Jens Axboea893b992006-05-02 15:03:27 +0200577 page_cache_get(page);
578
579 /*
580 * page must be on the LRU for adding to the pagecache.
581 * Check this without grabbing the zone lock, if it isn't
582 * the do grab the zone lock, recheck, and add if necessary.
583 */
584 if (!PageLRU(page)) {
585 struct zone *zone = page_zone(page);
586
587 spin_lock_irq(&zone->lru_lock);
588 if (!PageLRU(page)) {
589 SetPageLRU(page);
590 add_page_to_inactive_list(zone, page);
591 }
592 spin_unlock_irq(&zone->lru_lock);
593 }
594
Jens Axboe46e678c2006-04-30 16:36:32 +0200595 if (add_to_page_cache(page, mapping, index, gfp_mask)) {
Jens Axboea893b992006-05-02 15:03:27 +0200596 page_cache_release(page);
Jens Axboe46e678c2006-04-30 16:36:32 +0200597 unlock_page(page);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200598 goto find_page;
Jens Axboe46e678c2006-04-30 16:36:32 +0200599 }
Jens Axboe5abc97a2006-03-30 15:16:46 +0200600 } else {
601find_page:
Jens Axboe9e0267c2006-04-19 15:57:31 +0200602 page = find_lock_page(mapping, index);
603 if (!page) {
604 ret = -ENOMEM;
605 page = page_cache_alloc_cold(mapping);
606 if (unlikely(!page))
607 goto out_nomem;
608
609 /*
610 * This will also lock the page
611 */
612 ret = add_to_page_cache_lru(page, mapping, index,
613 gfp_mask);
614 if (unlikely(ret))
615 goto out;
616 }
Jens Axboe5274f052006-03-30 15:15:30 +0200617
Jens Axboe5abc97a2006-03-30 15:16:46 +0200618 /*
Jens Axboe9e0267c2006-04-19 15:57:31 +0200619 * We get here with the page locked. If the page is also
620 * uptodate, we don't need to do more. If it isn't, we
621 * may need to bring it in if we are not going to overwrite
622 * the full page.
Jens Axboe5abc97a2006-03-30 15:16:46 +0200623 */
624 if (!PageUptodate(page)) {
Jens Axboe016b6612006-04-25 15:42:00 +0200625 if (this_len < PAGE_CACHE_SIZE) {
Jens Axboe5abc97a2006-03-30 15:16:46 +0200626 ret = mapping->a_ops->readpage(file, page);
627 if (unlikely(ret))
628 goto out;
629
630 lock_page(page);
631
632 if (!PageUptodate(page)) {
633 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200634 * Page got invalidated, repeat.
Jens Axboe5abc97a2006-03-30 15:16:46 +0200635 */
636 if (!page->mapping) {
637 unlock_page(page);
638 page_cache_release(page);
639 goto find_page;
640 }
641 ret = -EIO;
642 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200643 }
Jens Axboe9e0267c2006-04-19 15:57:31 +0200644 } else
Jens Axboe5abc97a2006-03-30 15:16:46 +0200645 SetPageUptodate(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200646 }
647 }
648
Jens Axboe016b6612006-04-25 15:42:00 +0200649 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200650 if (ret == AOP_TRUNCATED_PAGE) {
651 page_cache_release(page);
652 goto find_page;
653 } else if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200654 goto out;
655
Jens Axboe0568b402006-05-01 19:50:48 +0200656 if (buf->page != page) {
Jens Axboef84d7512006-05-01 19:59:03 +0200657 /*
658 * Careful, ->map() uses KM_USER0!
659 */
Jens Axboef6762b72006-05-01 20:02:05 +0200660 char *src = buf->ops->map(info, buf, 1);
Jens Axboef84d7512006-05-01 19:59:03 +0200661 char *dst = kmap_atomic(page, KM_USER1);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200662
Jens Axboe016b6612006-04-25 15:42:00 +0200663 memcpy(dst + offset, src + buf->offset, this_len);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200664 flush_dcache_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200665 kunmap_atomic(dst, KM_USER1);
Jens Axboef6762b72006-05-01 20:02:05 +0200666 buf->ops->unmap(info, buf, src);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200667 }
Jens Axboe5274f052006-03-30 15:15:30 +0200668
Jens Axboe016b6612006-04-25 15:42:00 +0200669 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
Jens Axboe0568b402006-05-01 19:50:48 +0200670 if (!ret) {
671 /*
672 * Return the number of bytes written and mark page as
673 * accessed, we are now done!
674 */
675 ret = this_len;
676 mark_page_accessed(page);
677 balance_dirty_pages_ratelimited(mapping);
678 } else if (ret == AOP_TRUNCATED_PAGE) {
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200679 page_cache_release(page);
680 goto find_page;
Jens Axboe0568b402006-05-01 19:50:48 +0200681 }
Jens Axboe5274f052006-03-30 15:15:30 +0200682out:
Jens Axboe0568b402006-05-01 19:50:48 +0200683 page_cache_release(page);
Jens Axboe9e0267c2006-04-19 15:57:31 +0200684 unlock_page(page);
Dave Jones9aefe432006-04-10 09:02:40 +0200685out_nomem:
Jens Axboe5274f052006-03-30 15:15:30 +0200686 return ret;
687}
688
Jens Axboe83f91352006-04-02 23:05:09 +0200689/*
690 * Pipe input worker. Most of this logic works like a regular pipe, the
691 * key here is the 'actor' worker passed in that actually moves the data
692 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
693 */
Jens Axboe00522fb2006-04-26 14:39:29 +0200694ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
695 loff_t *ppos, size_t len, unsigned int flags,
696 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200697{
Jens Axboe5274f052006-03-30 15:15:30 +0200698 int ret, do_wakeup, err;
699 struct splice_desc sd;
700
701 ret = 0;
702 do_wakeup = 0;
703
704 sd.total_len = len;
705 sd.flags = flags;
706 sd.file = out;
Jens Axboecbb7e572006-04-11 14:57:50 +0200707 sd.pos = *ppos;
Jens Axboe5274f052006-03-30 15:15:30 +0200708
Ingo Molnar3a326a22006-04-10 15:18:35 +0200709 if (pipe->inode)
710 mutex_lock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200711
Jens Axboe5274f052006-03-30 15:15:30 +0200712 for (;;) {
Jens Axboe6f767b02006-04-11 13:53:56 +0200713 if (pipe->nrbufs) {
714 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
Jens Axboe5274f052006-03-30 15:15:30 +0200715 struct pipe_buf_operations *ops = buf->ops;
716
717 sd.len = buf->len;
718 if (sd.len > sd.total_len)
719 sd.len = sd.total_len;
720
Ingo Molnar3a326a22006-04-10 15:18:35 +0200721 err = actor(pipe, buf, &sd);
Jens Axboe016b6612006-04-25 15:42:00 +0200722 if (err <= 0) {
Jens Axboe5274f052006-03-30 15:15:30 +0200723 if (!ret && err != -ENODATA)
724 ret = err;
725
726 break;
727 }
728
Jens Axboe016b6612006-04-25 15:42:00 +0200729 ret += err;
730 buf->offset += err;
731 buf->len -= err;
732
733 sd.len -= err;
734 sd.pos += err;
735 sd.total_len -= err;
736 if (sd.len)
737 continue;
Ingo Molnar73d62d82006-04-11 13:57:21 +0200738
Jens Axboe5274f052006-03-30 15:15:30 +0200739 if (!buf->len) {
740 buf->ops = NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200741 ops->release(pipe, buf);
Jens Axboe6f767b02006-04-11 13:53:56 +0200742 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
743 pipe->nrbufs--;
744 if (pipe->inode)
745 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200746 }
747
Jens Axboe5274f052006-03-30 15:15:30 +0200748 if (!sd.total_len)
749 break;
750 }
751
Jens Axboe6f767b02006-04-11 13:53:56 +0200752 if (pipe->nrbufs)
Jens Axboe5274f052006-03-30 15:15:30 +0200753 continue;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200754 if (!pipe->writers)
Jens Axboe5274f052006-03-30 15:15:30 +0200755 break;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200756 if (!pipe->waiting_writers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200757 if (ret)
758 break;
759 }
760
Linus Torvalds29e35092006-04-02 12:46:35 -0700761 if (flags & SPLICE_F_NONBLOCK) {
762 if (!ret)
763 ret = -EAGAIN;
764 break;
765 }
766
Jens Axboe5274f052006-03-30 15:15:30 +0200767 if (signal_pending(current)) {
768 if (!ret)
769 ret = -ERESTARTSYS;
770 break;
771 }
772
773 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200774 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200775 if (waitqueue_active(&pipe->wait))
776 wake_up_interruptible_sync(&pipe->wait);
777 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200778 do_wakeup = 0;
779 }
780
Ingo Molnar3a326a22006-04-10 15:18:35 +0200781 pipe_wait(pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200782 }
783
Ingo Molnar3a326a22006-04-10 15:18:35 +0200784 if (pipe->inode)
785 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200786
787 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200788 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200789 if (waitqueue_active(&pipe->wait))
790 wake_up_interruptible(&pipe->wait);
791 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200792 }
793
Jens Axboe5274f052006-03-30 15:15:30 +0200794 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200795}
796
Jens Axboe83f91352006-04-02 23:05:09 +0200797/**
798 * generic_file_splice_write - splice data from a pipe to a file
Ingo Molnar3a326a22006-04-10 15:18:35 +0200799 * @pipe: pipe info
Jens Axboe83f91352006-04-02 23:05:09 +0200800 * @out: file to write to
801 * @len: number of bytes to splice
802 * @flags: splice modifier flags
803 *
804 * Will either move or copy pages (determined by @flags options) from
805 * the given pipe inode to the given file.
806 *
807 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200808ssize_t
809generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200810 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200811{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200812 struct address_space *mapping = out->f_mapping;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200813 ssize_t ret;
814
Jens Axboe00522fb2006-04-26 14:39:29 +0200815 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
Jens Axboea4514eb2006-04-19 15:57:05 +0200816 if (ret > 0) {
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200817 struct inode *inode = mapping->host;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200818
Jens Axboea4514eb2006-04-19 15:57:05 +0200819 *ppos += ret;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200820
Jens Axboea4514eb2006-04-19 15:57:05 +0200821 /*
822 * If file or inode is SYNC and we actually wrote some data,
823 * sync it.
824 */
825 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
826 int err;
827
828 mutex_lock(&inode->i_mutex);
829 err = generic_osync_inode(inode, mapping,
830 OSYNC_METADATA|OSYNC_DATA);
831 mutex_unlock(&inode->i_mutex);
832
833 if (err)
834 ret = err;
835 }
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200836 }
837
838 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200839}
840
Jens Axboe059a8f32006-04-02 23:06:05 +0200841EXPORT_SYMBOL(generic_file_splice_write);
842
Jens Axboe83f91352006-04-02 23:05:09 +0200843/**
844 * generic_splice_sendpage - splice data from a pipe to a socket
845 * @inode: pipe inode
846 * @out: socket to write to
847 * @len: number of bytes to splice
848 * @flags: splice modifier flags
849 *
850 * Will send @len bytes from the pipe to a network socket. No data copying
851 * is involved.
852 *
853 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200854ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200855 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200856{
Jens Axboe00522fb2006-04-26 14:39:29 +0200857 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200858}
859
Jens Axboe059a8f32006-04-02 23:06:05 +0200860EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500861
Jens Axboe83f91352006-04-02 23:05:09 +0200862/*
863 * Attempt to initiate a splice from pipe to file.
864 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200865static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200866 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200867{
Jens Axboe5274f052006-03-30 15:15:30 +0200868 int ret;
869
Jens Axboe49570e92006-04-11 13:56:09 +0200870 if (unlikely(!out->f_op || !out->f_op->splice_write))
Jens Axboe5274f052006-03-30 15:15:30 +0200871 return -EINVAL;
872
Jens Axboe49570e92006-04-11 13:56:09 +0200873 if (unlikely(!(out->f_mode & FMODE_WRITE)))
Jens Axboe5274f052006-03-30 15:15:30 +0200874 return -EBADF;
875
Jens Axboecbb7e572006-04-11 14:57:50 +0200876 ret = rw_verify_area(WRITE, out, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200877 if (unlikely(ret < 0))
878 return ret;
879
Jens Axboecbb7e572006-04-11 14:57:50 +0200880 return out->f_op->splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200881}
882
Jens Axboe83f91352006-04-02 23:05:09 +0200883/*
884 * Attempt to initiate a splice from a file to a pipe.
885 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200886static long do_splice_to(struct file *in, loff_t *ppos,
887 struct pipe_inode_info *pipe, size_t len,
888 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200889{
Jens Axboecbb7e572006-04-11 14:57:50 +0200890 loff_t isize, left;
Jens Axboe5274f052006-03-30 15:15:30 +0200891 int ret;
892
Jens Axboe49570e92006-04-11 13:56:09 +0200893 if (unlikely(!in->f_op || !in->f_op->splice_read))
Jens Axboe5274f052006-03-30 15:15:30 +0200894 return -EINVAL;
895
Jens Axboe49570e92006-04-11 13:56:09 +0200896 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200897 return -EBADF;
898
Jens Axboecbb7e572006-04-11 14:57:50 +0200899 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200900 if (unlikely(ret < 0))
901 return ret;
902
903 isize = i_size_read(in->f_mapping->host);
Jens Axboecbb7e572006-04-11 14:57:50 +0200904 if (unlikely(*ppos >= isize))
Jens Axboe5274f052006-03-30 15:15:30 +0200905 return 0;
906
Jens Axboecbb7e572006-04-11 14:57:50 +0200907 left = isize - *ppos;
Jens Axboe49570e92006-04-11 13:56:09 +0200908 if (unlikely(left < len))
Jens Axboe5274f052006-03-30 15:15:30 +0200909 len = left;
910
Jens Axboecbb7e572006-04-11 14:57:50 +0200911 return in->f_op->splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200912}
913
Jens Axboecbb7e572006-04-11 14:57:50 +0200914long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
915 size_t len, unsigned int flags)
Jens Axboeb92ce552006-04-11 13:52:07 +0200916{
917 struct pipe_inode_info *pipe;
918 long ret, bytes;
Jens Axboecbb7e572006-04-11 14:57:50 +0200919 loff_t out_off;
Jens Axboeb92ce552006-04-11 13:52:07 +0200920 umode_t i_mode;
921 int i;
922
923 /*
924 * We require the input being a regular file, as we don't want to
925 * randomly drop data for eg socket -> socket splicing. Use the
926 * piped splicing for that!
927 */
928 i_mode = in->f_dentry->d_inode->i_mode;
929 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
930 return -EINVAL;
931
932 /*
933 * neither in nor out is a pipe, setup an internal pipe attached to
934 * 'out' and transfer the wanted data from 'in' to 'out' through that
935 */
936 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200937 if (unlikely(!pipe)) {
Jens Axboeb92ce552006-04-11 13:52:07 +0200938 pipe = alloc_pipe_info(NULL);
939 if (!pipe)
940 return -ENOMEM;
941
942 /*
943 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200944 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200945 * PIPE_READERS appropriately.
946 */
947 pipe->readers = 1;
948
949 current->splice_pipe = pipe;
950 }
951
952 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200953 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200954 */
955 ret = 0;
956 bytes = 0;
Jens Axboecbb7e572006-04-11 14:57:50 +0200957 out_off = 0;
Jens Axboeb92ce552006-04-11 13:52:07 +0200958
959 while (len) {
960 size_t read_len, max_read_len;
961
962 /*
963 * Do at most PIPE_BUFFERS pages worth of transfer:
964 */
965 max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
966
Jens Axboecbb7e572006-04-11 14:57:50 +0200967 ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
Jens Axboeb92ce552006-04-11 13:52:07 +0200968 if (unlikely(ret < 0))
969 goto out_release;
970
971 read_len = ret;
972
973 /*
974 * NOTE: nonblocking mode only applies to the input. We
975 * must not do the output in nonblocking mode as then we
976 * could get stuck data in the internal pipe:
977 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200978 ret = do_splice_from(pipe, out, &out_off, read_len,
Jens Axboeb92ce552006-04-11 13:52:07 +0200979 flags & ~SPLICE_F_NONBLOCK);
980 if (unlikely(ret < 0))
981 goto out_release;
982
983 bytes += ret;
984 len -= ret;
985
986 /*
987 * In nonblocking mode, if we got back a short read then
988 * that was due to either an IO error or due to the
989 * pagecache entry not being there. In the IO error case
990 * the _next_ splice attempt will produce a clean IO error
991 * return value (not a short read), so in both cases it's
992 * correct to break out of the loop here:
993 */
994 if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
995 break;
996 }
997
998 pipe->nrbufs = pipe->curbuf = 0;
999
1000 return bytes;
1001
1002out_release:
1003 /*
1004 * If we did an incomplete transfer we must release
1005 * the pipe buffers in question:
1006 */
1007 for (i = 0; i < PIPE_BUFFERS; i++) {
1008 struct pipe_buffer *buf = pipe->bufs + i;
1009
1010 if (buf->ops) {
1011 buf->ops->release(pipe, buf);
1012 buf->ops = NULL;
1013 }
1014 }
1015 pipe->nrbufs = pipe->curbuf = 0;
1016
1017 /*
1018 * If we transferred some data, return the number of bytes:
1019 */
1020 if (bytes > 0)
1021 return bytes;
1022
1023 return ret;
1024}
1025
1026EXPORT_SYMBOL(do_splice_direct);
1027
Jens Axboe83f91352006-04-02 23:05:09 +02001028/*
1029 * Determine where to splice to/from.
1030 */
Ingo Molnar529565d2006-04-10 15:18:58 +02001031static long do_splice(struct file *in, loff_t __user *off_in,
1032 struct file *out, loff_t __user *off_out,
1033 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001034{
Ingo Molnar3a326a22006-04-10 15:18:35 +02001035 struct pipe_inode_info *pipe;
Jens Axboecbb7e572006-04-11 14:57:50 +02001036 loff_t offset, *off;
Jens Axboea4514eb2006-04-19 15:57:05 +02001037 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001038
Ingo Molnar3a326a22006-04-10 15:18:35 +02001039 pipe = in->f_dentry->d_inode->i_pipe;
Ingo Molnar529565d2006-04-10 15:18:58 +02001040 if (pipe) {
1041 if (off_in)
1042 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001043 if (off_out) {
1044 if (out->f_op->llseek == no_llseek)
1045 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001046 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001047 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001048 off = &offset;
1049 } else
1050 off = &out->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001051
Jens Axboea4514eb2006-04-19 15:57:05 +02001052 ret = do_splice_from(pipe, out, off, len, flags);
1053
1054 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1055 ret = -EFAULT;
1056
1057 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001058 }
Jens Axboe5274f052006-03-30 15:15:30 +02001059
Ingo Molnar3a326a22006-04-10 15:18:35 +02001060 pipe = out->f_dentry->d_inode->i_pipe;
Ingo Molnar529565d2006-04-10 15:18:58 +02001061 if (pipe) {
1062 if (off_out)
1063 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001064 if (off_in) {
1065 if (in->f_op->llseek == no_llseek)
1066 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001067 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001068 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001069 off = &offset;
1070 } else
1071 off = &in->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001072
Jens Axboea4514eb2006-04-19 15:57:05 +02001073 ret = do_splice_to(in, off, pipe, len, flags);
1074
1075 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1076 ret = -EFAULT;
1077
1078 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001079 }
Jens Axboe5274f052006-03-30 15:15:30 +02001080
1081 return -EINVAL;
1082}
1083
Jens Axboe912d35f2006-04-26 10:59:21 +02001084/*
1085 * Map an iov into an array of pages and offset/length tupples. With the
1086 * partial_page structure, we can map several non-contiguous ranges into
1087 * our ones pages[] map instead of splitting that operation into pieces.
1088 * Could easily be exported as a generic helper for other users, in which
1089 * case one would probably want to add a 'max_nr_pages' parameter as well.
1090 */
1091static int get_iovec_page_array(const struct iovec __user *iov,
1092 unsigned int nr_vecs, struct page **pages,
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001093 struct partial_page *partial, int aligned)
Jens Axboe912d35f2006-04-26 10:59:21 +02001094{
1095 int buffers = 0, error = 0;
1096
1097 /*
1098 * It's ok to take the mmap_sem for reading, even
1099 * across a "get_user()".
1100 */
1101 down_read(&current->mm->mmap_sem);
1102
1103 while (nr_vecs) {
1104 unsigned long off, npages;
1105 void __user *base;
1106 size_t len;
1107 int i;
1108
1109 /*
1110 * Get user address base and length for this iovec.
1111 */
1112 error = get_user(base, &iov->iov_base);
1113 if (unlikely(error))
1114 break;
1115 error = get_user(len, &iov->iov_len);
1116 if (unlikely(error))
1117 break;
1118
1119 /*
1120 * Sanity check this iovec. 0 read succeeds.
1121 */
1122 if (unlikely(!len))
1123 break;
1124 error = -EFAULT;
1125 if (unlikely(!base))
1126 break;
1127
1128 /*
1129 * Get this base offset and number of pages, then map
1130 * in the user pages.
1131 */
1132 off = (unsigned long) base & ~PAGE_MASK;
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001133
1134 /*
1135 * If asked for alignment, the offset must be zero and the
1136 * length a multiple of the PAGE_SIZE.
1137 */
1138 error = -EINVAL;
1139 if (aligned && (off || len & ~PAGE_MASK))
1140 break;
1141
Jens Axboe912d35f2006-04-26 10:59:21 +02001142 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1143 if (npages > PIPE_BUFFERS - buffers)
1144 npages = PIPE_BUFFERS - buffers;
1145
1146 error = get_user_pages(current, current->mm,
1147 (unsigned long) base, npages, 0, 0,
1148 &pages[buffers], NULL);
1149
1150 if (unlikely(error <= 0))
1151 break;
1152
1153 /*
1154 * Fill this contiguous range into the partial page map.
1155 */
1156 for (i = 0; i < error; i++) {
Jens Axboe75914892006-05-02 12:57:18 +02001157 const int plen = min_t(size_t, len, PAGE_SIZE - off);
Jens Axboe912d35f2006-04-26 10:59:21 +02001158
1159 partial[buffers].offset = off;
1160 partial[buffers].len = plen;
1161
1162 off = 0;
1163 len -= plen;
1164 buffers++;
1165 }
1166
1167 /*
1168 * We didn't complete this iov, stop here since it probably
1169 * means we have to move some of this into a pipe to
1170 * be able to continue.
1171 */
1172 if (len)
1173 break;
1174
1175 /*
1176 * Don't continue if we mapped fewer pages than we asked for,
1177 * or if we mapped the max number of pages that we have
1178 * room for.
1179 */
1180 if (error < npages || buffers == PIPE_BUFFERS)
1181 break;
1182
1183 nr_vecs--;
1184 iov++;
1185 }
1186
1187 up_read(&current->mm->mmap_sem);
1188
1189 if (buffers)
1190 return buffers;
1191
1192 return error;
1193}
1194
1195/*
1196 * vmsplice splices a user address range into a pipe. It can be thought of
1197 * as splice-from-memory, where the regular splice is splice-from-file (or
1198 * to file). In both cases the output is a pipe, naturally.
1199 *
1200 * Note that vmsplice only supports splicing _from_ user memory to a pipe,
1201 * not the other way around. Splicing from user memory is a simple operation
1202 * that can be supported without any funky alignment restrictions or nasty
1203 * vm tricks. We simply map in the user memory and fill them into a pipe.
1204 * The reverse isn't quite as easy, though. There are two possible solutions
1205 * for that:
1206 *
1207 * - memcpy() the data internally, at which point we might as well just
1208 * do a regular read() on the buffer anyway.
1209 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1210 * has restriction limitations on both ends of the pipe).
1211 *
1212 * Alas, it isn't here.
1213 *
1214 */
1215static long do_vmsplice(struct file *file, const struct iovec __user *iov,
1216 unsigned long nr_segs, unsigned int flags)
1217{
1218 struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
1219 struct page *pages[PIPE_BUFFERS];
1220 struct partial_page partial[PIPE_BUFFERS];
1221 struct splice_pipe_desc spd = {
1222 .pages = pages,
1223 .partial = partial,
1224 .flags = flags,
1225 .ops = &user_page_pipe_buf_ops,
1226 };
1227
1228 if (unlikely(!pipe))
1229 return -EBADF;
1230 if (unlikely(nr_segs > UIO_MAXIOV))
1231 return -EINVAL;
1232 else if (unlikely(!nr_segs))
1233 return 0;
1234
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001235 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1236 flags & SPLICE_F_GIFT);
Jens Axboe912d35f2006-04-26 10:59:21 +02001237 if (spd.nr_pages <= 0)
1238 return spd.nr_pages;
1239
Jens Axboe00522fb2006-04-26 14:39:29 +02001240 return splice_to_pipe(pipe, &spd);
Jens Axboe912d35f2006-04-26 10:59:21 +02001241}
1242
1243asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1244 unsigned long nr_segs, unsigned int flags)
1245{
1246 struct file *file;
1247 long error;
1248 int fput;
1249
1250 error = -EBADF;
1251 file = fget_light(fd, &fput);
1252 if (file) {
1253 if (file->f_mode & FMODE_WRITE)
1254 error = do_vmsplice(file, iov, nr_segs, flags);
1255
1256 fput_light(file, fput);
1257 }
1258
1259 return error;
1260}
1261
Ingo Molnar529565d2006-04-10 15:18:58 +02001262asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1263 int fd_out, loff_t __user *off_out,
1264 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001265{
1266 long error;
1267 struct file *in, *out;
1268 int fput_in, fput_out;
1269
1270 if (unlikely(!len))
1271 return 0;
1272
1273 error = -EBADF;
Ingo Molnar529565d2006-04-10 15:18:58 +02001274 in = fget_light(fd_in, &fput_in);
Jens Axboe5274f052006-03-30 15:15:30 +02001275 if (in) {
1276 if (in->f_mode & FMODE_READ) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001277 out = fget_light(fd_out, &fput_out);
Jens Axboe5274f052006-03-30 15:15:30 +02001278 if (out) {
1279 if (out->f_mode & FMODE_WRITE)
Ingo Molnar529565d2006-04-10 15:18:58 +02001280 error = do_splice(in, off_in,
1281 out, off_out,
1282 len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001283 fput_light(out, fput_out);
1284 }
1285 }
1286
1287 fput_light(in, fput_in);
1288 }
1289
1290 return error;
1291}
Jens Axboe70524492006-04-11 15:51:17 +02001292
1293/*
1294 * Link contents of ipipe to opipe.
1295 */
1296static int link_pipe(struct pipe_inode_info *ipipe,
1297 struct pipe_inode_info *opipe,
1298 size_t len, unsigned int flags)
1299{
1300 struct pipe_buffer *ibuf, *obuf;
Jens Axboe2a27250e2006-04-19 15:56:40 +02001301 int ret, do_wakeup, i, ipipe_first;
1302
1303 ret = do_wakeup = ipipe_first = 0;
Jens Axboe70524492006-04-11 15:51:17 +02001304
1305 /*
1306 * Potential ABBA deadlock, work around it by ordering lock
1307 * grabbing by inode address. Otherwise two different processes
1308 * could deadlock (one doing tee from A -> B, the other from B -> A).
1309 */
1310 if (ipipe->inode < opipe->inode) {
Jens Axboe2a27250e2006-04-19 15:56:40 +02001311 ipipe_first = 1;
Jens Axboe70524492006-04-11 15:51:17 +02001312 mutex_lock(&ipipe->inode->i_mutex);
1313 mutex_lock(&opipe->inode->i_mutex);
1314 } else {
1315 mutex_lock(&opipe->inode->i_mutex);
1316 mutex_lock(&ipipe->inode->i_mutex);
1317 }
1318
1319 for (i = 0;; i++) {
1320 if (!opipe->readers) {
1321 send_sig(SIGPIPE, current, 0);
1322 if (!ret)
1323 ret = -EPIPE;
1324 break;
1325 }
1326 if (ipipe->nrbufs - i) {
1327 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1328
1329 /*
1330 * If we have room, fill this buffer
1331 */
1332 if (opipe->nrbufs < PIPE_BUFFERS) {
1333 int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1334
1335 /*
1336 * Get a reference to this pipe buffer,
1337 * so we can copy the contents over.
1338 */
1339 ibuf->ops->get(ipipe, ibuf);
1340
1341 obuf = opipe->bufs + nbuf;
1342 *obuf = *ibuf;
1343
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001344 /*
1345 * Don't inherit the gift flag, we need to
1346 * prevent multiple steals of this page.
1347 */
1348 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1349
Jens Axboe70524492006-04-11 15:51:17 +02001350 if (obuf->len > len)
1351 obuf->len = len;
1352
1353 opipe->nrbufs++;
1354 do_wakeup = 1;
1355 ret += obuf->len;
1356 len -= obuf->len;
1357
1358 if (!len)
1359 break;
1360 if (opipe->nrbufs < PIPE_BUFFERS)
1361 continue;
1362 }
1363
1364 /*
1365 * We have input available, but no output room.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001366 * If we already copied data, return that. If we
1367 * need to drop the opipe lock, it must be ordered
1368 * last to avoid deadlocks.
Jens Axboe70524492006-04-11 15:51:17 +02001369 */
Jens Axboe2a27250e2006-04-19 15:56:40 +02001370 if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
Jens Axboe70524492006-04-11 15:51:17 +02001371 if (!ret)
1372 ret = -EAGAIN;
1373 break;
1374 }
1375 if (signal_pending(current)) {
1376 if (!ret)
1377 ret = -ERESTARTSYS;
1378 break;
1379 }
1380 if (do_wakeup) {
1381 smp_mb();
1382 if (waitqueue_active(&opipe->wait))
1383 wake_up_interruptible(&opipe->wait);
1384 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1385 do_wakeup = 0;
1386 }
1387
1388 opipe->waiting_writers++;
1389 pipe_wait(opipe);
1390 opipe->waiting_writers--;
1391 continue;
1392 }
1393
1394 /*
1395 * No input buffers, do the usual checks for available
1396 * writers and blocking and wait if necessary
1397 */
1398 if (!ipipe->writers)
1399 break;
1400 if (!ipipe->waiting_writers) {
1401 if (ret)
1402 break;
1403 }
Jens Axboe2a27250e2006-04-19 15:56:40 +02001404 /*
1405 * pipe_wait() drops the ipipe mutex. To avoid deadlocks
1406 * with another process, we can only safely do that if
1407 * the ipipe lock is ordered last.
1408 */
1409 if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
Jens Axboe70524492006-04-11 15:51:17 +02001410 if (!ret)
1411 ret = -EAGAIN;
1412 break;
1413 }
1414 if (signal_pending(current)) {
1415 if (!ret)
1416 ret = -ERESTARTSYS;
1417 break;
1418 }
1419
1420 if (waitqueue_active(&ipipe->wait))
1421 wake_up_interruptible_sync(&ipipe->wait);
1422 kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
1423
1424 pipe_wait(ipipe);
1425 }
1426
1427 mutex_unlock(&ipipe->inode->i_mutex);
1428 mutex_unlock(&opipe->inode->i_mutex);
1429
1430 if (do_wakeup) {
1431 smp_mb();
1432 if (waitqueue_active(&opipe->wait))
1433 wake_up_interruptible(&opipe->wait);
1434 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1435 }
1436
1437 return ret;
1438}
1439
1440/*
1441 * This is a tee(1) implementation that works on pipes. It doesn't copy
1442 * any data, it simply references the 'in' pages on the 'out' pipe.
1443 * The 'flags' used are the SPLICE_F_* variants, currently the only
1444 * applicable one is SPLICE_F_NONBLOCK.
1445 */
1446static long do_tee(struct file *in, struct file *out, size_t len,
1447 unsigned int flags)
1448{
1449 struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
1450 struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
1451
1452 /*
1453 * Link ipipe to the two output pipes, consuming as we go along.
1454 */
1455 if (ipipe && opipe)
1456 return link_pipe(ipipe, opipe, len, flags);
1457
1458 return -EINVAL;
1459}
1460
1461asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1462{
1463 struct file *in;
1464 int error, fput_in;
1465
1466 if (unlikely(!len))
1467 return 0;
1468
1469 error = -EBADF;
1470 in = fget_light(fdin, &fput_in);
1471 if (in) {
1472 if (in->f_mode & FMODE_READ) {
1473 int fput_out;
1474 struct file *out = fget_light(fdout, &fput_out);
1475
1476 if (out) {
1477 if (out->f_mode & FMODE_WRITE)
1478 error = do_tee(in, out, len, flags);
1479 fput_light(out, fput_out);
1480 }
1481 }
1482 fput_light(in, fput_in);
1483 }
1484
1485 return error;
1486}