blob: 0b202425b0b5c6e92d0cb1a4fd5f92deab3df3c9 [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 Axboe14328732006-05-03 10:35:26 +020081 buf->flags |= PIPE_BUF_FLAG_LRU;
Jens Axboe5abc97a2006-03-30 15:16:46 +020082 return 0;
83}
84
Jens Axboe5274f052006-03-30 15:15:30 +020085static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
86 struct pipe_buffer *buf)
87{
88 page_cache_release(buf->page);
Jens Axboe14328732006-05-03 10:35:26 +020089 buf->flags &= ~PIPE_BUF_FLAG_LRU;
Jens Axboe5274f052006-03-30 15:15:30 +020090}
91
Jens Axboef84d7512006-05-01 19:59:03 +020092static int page_cache_pipe_buf_pin(struct pipe_inode_info *info,
93 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +020094{
95 struct page *page = buf->page;
Jens Axboe49d0b212006-04-10 09:04:41 +020096 int err;
Jens Axboe5274f052006-03-30 15:15:30 +020097
98 if (!PageUptodate(page)) {
Jens Axboe49d0b212006-04-10 09:04:41 +020099 lock_page(page);
100
101 /*
102 * Page got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +0200103 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200104 */
105 if (!page->mapping) {
106 err = -ENODATA;
107 goto error;
108 }
109
110 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200111 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200112 */
113 if (!PageUptodate(page)) {
114 err = -EIO;
115 goto error;
116 }
117
118 /*
Jens Axboef84d7512006-05-01 19:59:03 +0200119 * Page is ok afterall, we are done.
Jens Axboe49d0b212006-04-10 09:04:41 +0200120 */
Jens Axboe5274f052006-03-30 15:15:30 +0200121 unlock_page(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200122 }
123
Jens Axboef84d7512006-05-01 19:59:03 +0200124 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200125error:
126 unlock_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200127 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200128}
129
Jens Axboe5274f052006-03-30 15:15:30 +0200130static struct pipe_buf_operations page_cache_pipe_buf_ops = {
131 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200132 .map = generic_pipe_buf_map,
133 .unmap = generic_pipe_buf_unmap,
134 .pin = page_cache_pipe_buf_pin,
Jens Axboe5274f052006-03-30 15:15:30 +0200135 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200136 .steal = page_cache_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200137 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200138};
139
Jens Axboe912d35f2006-04-26 10:59:21 +0200140static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
141 struct pipe_buffer *buf)
142{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200143 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
144 return 1;
145
Jens Axboe14328732006-05-03 10:35:26 +0200146 buf->flags |= PIPE_BUF_FLAG_LRU;
Jens Axboe330ab712006-05-02 15:29:57 +0200147 return generic_pipe_buf_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200148}
149
150static struct pipe_buf_operations user_page_pipe_buf_ops = {
151 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200152 .map = generic_pipe_buf_map,
153 .unmap = generic_pipe_buf_unmap,
154 .pin = generic_pipe_buf_pin,
Jens Axboe912d35f2006-04-26 10:59:21 +0200155 .release = page_cache_pipe_buf_release,
156 .steal = user_page_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200157 .get = generic_pipe_buf_get,
Jens Axboe912d35f2006-04-26 10:59:21 +0200158};
159
Jens Axboe83f91352006-04-02 23:05:09 +0200160/*
161 * Pipe output worker. This sets up our pipe format with the page cache
162 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
163 */
Jens Axboe00522fb2006-04-26 14:39:29 +0200164static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
165 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200166{
Jens Axboe912d35f2006-04-26 10:59:21 +0200167 int ret, do_wakeup, page_nr;
Jens Axboe5274f052006-03-30 15:15:30 +0200168
169 ret = 0;
170 do_wakeup = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +0200171 page_nr = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200172
Ingo Molnar3a326a22006-04-10 15:18:35 +0200173 if (pipe->inode)
174 mutex_lock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200175
Jens Axboe5274f052006-03-30 15:15:30 +0200176 for (;;) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200177 if (!pipe->readers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200178 send_sig(SIGPIPE, current, 0);
179 if (!ret)
180 ret = -EPIPE;
181 break;
182 }
183
Jens Axboe6f767b02006-04-11 13:53:56 +0200184 if (pipe->nrbufs < PIPE_BUFFERS) {
185 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200186 struct pipe_buffer *buf = pipe->bufs + newbuf;
Jens Axboe5274f052006-03-30 15:15:30 +0200187
Jens Axboe912d35f2006-04-26 10:59:21 +0200188 buf->page = spd->pages[page_nr];
189 buf->offset = spd->partial[page_nr].offset;
190 buf->len = spd->partial[page_nr].len;
191 buf->ops = spd->ops;
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200192 if (spd->flags & SPLICE_F_GIFT)
193 buf->flags |= PIPE_BUF_FLAG_GIFT;
194
Jens Axboe6f767b02006-04-11 13:53:56 +0200195 pipe->nrbufs++;
Jens Axboe912d35f2006-04-26 10:59:21 +0200196 page_nr++;
197 ret += buf->len;
198
Jens Axboe6f767b02006-04-11 13:53:56 +0200199 if (pipe->inode)
200 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200201
Jens Axboe912d35f2006-04-26 10:59:21 +0200202 if (!--spd->nr_pages)
Jens Axboe5274f052006-03-30 15:15:30 +0200203 break;
Jens Axboe6f767b02006-04-11 13:53:56 +0200204 if (pipe->nrbufs < PIPE_BUFFERS)
Jens Axboe5274f052006-03-30 15:15:30 +0200205 continue;
206
207 break;
208 }
209
Jens Axboe912d35f2006-04-26 10:59:21 +0200210 if (spd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700211 if (!ret)
212 ret = -EAGAIN;
213 break;
214 }
215
Jens Axboe5274f052006-03-30 15:15:30 +0200216 if (signal_pending(current)) {
217 if (!ret)
218 ret = -ERESTARTSYS;
219 break;
220 }
221
222 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200223 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200224 if (waitqueue_active(&pipe->wait))
225 wake_up_interruptible_sync(&pipe->wait);
226 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Jens Axboe5274f052006-03-30 15:15:30 +0200227 do_wakeup = 0;
228 }
229
Ingo Molnar3a326a22006-04-10 15:18:35 +0200230 pipe->waiting_writers++;
231 pipe_wait(pipe);
232 pipe->waiting_writers--;
Jens Axboe5274f052006-03-30 15:15:30 +0200233 }
234
Ingo Molnar3a326a22006-04-10 15:18:35 +0200235 if (pipe->inode)
236 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200237
238 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200239 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200240 if (waitqueue_active(&pipe->wait))
241 wake_up_interruptible(&pipe->wait);
242 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Jens Axboe5274f052006-03-30 15:15:30 +0200243 }
244
Jens Axboe912d35f2006-04-26 10:59:21 +0200245 while (page_nr < spd->nr_pages)
246 page_cache_release(spd->pages[page_nr++]);
Jens Axboe5274f052006-03-30 15:15:30 +0200247
248 return ret;
249}
250
Ingo Molnar3a326a22006-04-10 15:18:35 +0200251static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200252__generic_file_splice_read(struct file *in, loff_t *ppos,
253 struct pipe_inode_info *pipe, size_t len,
254 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200255{
256 struct address_space *mapping = in->f_mapping;
Jens Axboe912d35f2006-04-26 10:59:21 +0200257 unsigned int loff, nr_pages;
Jens Axboe16c523d2006-04-10 09:03:58 +0200258 struct page *pages[PIPE_BUFFERS];
Jens Axboe912d35f2006-04-26 10:59:21 +0200259 struct partial_page partial[PIPE_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200260 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200261 pgoff_t index, end_index;
262 loff_t isize;
Jens Axboe912d35f2006-04-26 10:59:21 +0200263 size_t total_len;
Jens Axboeeb207962006-04-27 11:05:22 +0200264 int error, page_nr;
Jens Axboe912d35f2006-04-26 10:59:21 +0200265 struct splice_pipe_desc spd = {
266 .pages = pages,
267 .partial = partial,
268 .flags = flags,
269 .ops = &page_cache_pipe_buf_ops,
270 };
Jens Axboe5274f052006-03-30 15:15:30 +0200271
Jens Axboecbb7e572006-04-11 14:57:50 +0200272 index = *ppos >> PAGE_CACHE_SHIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +0200273 loff = *ppos & ~PAGE_CACHE_MASK;
274 nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe5274f052006-03-30 15:15:30 +0200275
276 if (nr_pages > PIPE_BUFFERS)
277 nr_pages = PIPE_BUFFERS;
278
279 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200280 * Initiate read-ahead on this page range. however, don't call into
Jens Axboe0b749ce2006-04-10 09:05:04 +0200281 * read-ahead if this is a non-zero offset (we are likely doing small
282 * chunk splice and the page is already there) for a single page.
Jens Axboe5274f052006-03-30 15:15:30 +0200283 */
Jens Axboeeb645a22006-04-27 08:44:27 +0200284 if (!loff || nr_pages > 1)
285 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
Jens Axboe5274f052006-03-30 15:15:30 +0200286
287 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200288 * Now fill in the holes:
Jens Axboe5274f052006-03-30 15:15:30 +0200289 */
Jens Axboe7480a902006-04-11 13:52:47 +0200290 error = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +0200291 total_len = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200292
293 /*
294 * Lookup the (hopefully) full range of pages we need.
295 */
296 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
297
298 /*
299 * If find_get_pages_contig() returned fewer pages than we needed,
300 * allocate the rest.
301 */
302 index += spd.nr_pages;
303 while (spd.nr_pages < nr_pages) {
304 /*
305 * Page could be there, find_get_pages_contig() breaks on
306 * the first hole.
307 */
308 page = find_get_page(mapping, index);
309 if (!page) {
310 /*
Jens Axboee27dedd2006-05-01 19:59:54 +0200311 * Make sure the read-ahead engine is notified
312 * about this failure.
313 */
314 handle_ra_miss(mapping, &in->f_ra, index);
315
316 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200317 * page didn't exist, allocate one.
318 */
319 page = page_cache_alloc_cold(mapping);
320 if (!page)
321 break;
322
323 error = add_to_page_cache_lru(page, mapping, index,
324 mapping_gfp_mask(mapping));
325 if (unlikely(error)) {
326 page_cache_release(page);
327 break;
328 }
329 /*
330 * add_to_page_cache() locks the page, unlock it
331 * to avoid convoluting the logic below even more.
332 */
333 unlock_page(page);
334 }
335
336 pages[spd.nr_pages++] = page;
337 index++;
338 }
339
340 /*
341 * Now loop over the map and see if we need to start IO on any
342 * pages, fill in the partial map, etc.
343 */
344 index = *ppos >> PAGE_CACHE_SHIFT;
345 nr_pages = spd.nr_pages;
346 spd.nr_pages = 0;
347 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200348 unsigned int this_len;
349
350 if (!len)
351 break;
352
353 /*
354 * this_len is the max we'll use from this page
355 */
Andrew Mortonba5f5d92006-04-25 15:33:34 +0200356 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
Jens Axboeeb207962006-04-27 11:05:22 +0200357 page = pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200358
359 /*
360 * If the page isn't uptodate, we may need to start io on it
361 */
362 if (!PageUptodate(page)) {
Jens Axboec4f895c2006-04-19 15:56:12 +0200363 /*
364 * If in nonblock mode then dont block on waiting
365 * for an in-flight io page
366 */
367 if (flags & SPLICE_F_NONBLOCK)
368 break;
369
Jens Axboe7480a902006-04-11 13:52:47 +0200370 lock_page(page);
371
372 /*
373 * page was truncated, stop here. if this isn't the
374 * first page, we'll just complete what we already
375 * added
376 */
377 if (!page->mapping) {
378 unlock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200379 break;
380 }
381 /*
382 * page was already under io and is now done, great
383 */
384 if (PageUptodate(page)) {
385 unlock_page(page);
386 goto fill_it;
387 }
388
Jens Axboe7480a902006-04-11 13:52:47 +0200389 /*
390 * need to read in the page
391 */
392 error = mapping->a_ops->readpage(in, page);
Jens Axboe7480a902006-04-11 13:52:47 +0200393 if (unlikely(error)) {
Jens Axboeeb207962006-04-27 11:05:22 +0200394 /*
395 * We really should re-lookup the page here,
396 * but it complicates things a lot. Instead
397 * lets just do what we already stored, and
398 * we'll get it the next time we are called.
399 */
Jens Axboe7480a902006-04-11 13:52:47 +0200400 if (error == AOP_TRUNCATED_PAGE)
Jens Axboeeb207962006-04-27 11:05:22 +0200401 error = 0;
402
Jens Axboe7480a902006-04-11 13:52:47 +0200403 break;
404 }
Jens Axboe91ad66e2006-04-19 15:55:10 +0200405
406 /*
407 * i_size must be checked after ->readpage().
408 */
409 isize = i_size_read(mapping->host);
410 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Jens Axboeeb207962006-04-27 11:05:22 +0200411 if (unlikely(!isize || index > end_index))
Jens Axboe91ad66e2006-04-19 15:55:10 +0200412 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200413
414 /*
415 * if this is the last page, see if we need to shrink
416 * the length and stop
417 */
418 if (end_index == index) {
419 loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
Jens Axboeeb207962006-04-27 11:05:22 +0200420 if (total_len + loff > isize)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200421 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200422 /*
423 * force quit after adding this page
424 */
Jens Axboeeb207962006-04-27 11:05:22 +0200425 len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200426 this_len = min(this_len, loff);
Jens Axboe912d35f2006-04-26 10:59:21 +0200427 loff = 0;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200428 }
Jens Axboe7480a902006-04-11 13:52:47 +0200429 }
430fill_it:
Jens Axboeeb207962006-04-27 11:05:22 +0200431 partial[page_nr].offset = loff;
432 partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200433 len -= this_len;
Jens Axboe912d35f2006-04-26 10:59:21 +0200434 total_len += this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200435 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200436 spd.nr_pages++;
437 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200438 }
439
Jens Axboeeb207962006-04-27 11:05:22 +0200440 /*
441 * Release any pages at the end, if we quit early. 'i' is how far
442 * we got, 'nr_pages' is how many pages are in the map.
443 */
444 while (page_nr < nr_pages)
445 page_cache_release(pages[page_nr++]);
446
Jens Axboe912d35f2006-04-26 10:59:21 +0200447 if (spd.nr_pages)
Jens Axboe00522fb2006-04-26 14:39:29 +0200448 return splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200449
Jens Axboe7480a902006-04-11 13:52:47 +0200450 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200451}
452
Jens Axboe83f91352006-04-02 23:05:09 +0200453/**
454 * generic_file_splice_read - splice data from file to a pipe
455 * @in: file to splice from
456 * @pipe: pipe to splice to
457 * @len: number of bytes to splice
458 * @flags: splice modifier flags
459 *
460 * Will read pages from given file and fill them into a pipe.
Jens Axboe83f91352006-04-02 23:05:09 +0200461 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200462ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
463 struct pipe_inode_info *pipe, size_t len,
464 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200465{
466 ssize_t spliced;
467 int ret;
468
469 ret = 0;
470 spliced = 0;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200471
Jens Axboe5274f052006-03-30 15:15:30 +0200472 while (len) {
Jens Axboecbb7e572006-04-11 14:57:50 +0200473 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200474
Jens Axboec4f895c2006-04-19 15:56:12 +0200475 if (ret < 0)
Jens Axboe5274f052006-03-30 15:15:30 +0200476 break;
Jens Axboec4f895c2006-04-19 15:56:12 +0200477 else if (!ret) {
478 if (spliced)
479 break;
480 if (flags & SPLICE_F_NONBLOCK) {
481 ret = -EAGAIN;
482 break;
483 }
484 }
Jens Axboe5274f052006-03-30 15:15:30 +0200485
Jens Axboecbb7e572006-04-11 14:57:50 +0200486 *ppos += ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200487 len -= ret;
488 spliced += ret;
489 }
490
491 if (spliced)
492 return spliced;
493
494 return ret;
495}
496
Jens Axboe059a8f32006-04-02 23:06:05 +0200497EXPORT_SYMBOL(generic_file_splice_read);
498
Jens Axboe5274f052006-03-30 15:15:30 +0200499/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200500 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200501 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200502 */
503static int pipe_to_sendpage(struct pipe_inode_info *info,
504 struct pipe_buffer *buf, struct splice_desc *sd)
505{
506 struct file *file = sd->file;
507 loff_t pos = sd->pos;
Jens Axboef84d7512006-05-01 19:59:03 +0200508 int ret, more;
Jens Axboe5274f052006-03-30 15:15:30 +0200509
Jens Axboef84d7512006-05-01 19:59:03 +0200510 ret = buf->ops->pin(info, buf);
511 if (!ret) {
512 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200513
Jens Axboef84d7512006-05-01 19:59:03 +0200514 ret = file->f_op->sendpage(file, buf->page, buf->offset,
515 sd->len, &pos, more);
516 }
Jens Axboe5274f052006-03-30 15:15:30 +0200517
Jens Axboe016b6612006-04-25 15:42:00 +0200518 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200519}
520
521/*
522 * This is a little more tricky than the file -> pipe splicing. There are
523 * basically three cases:
524 *
525 * - Destination page already exists in the address space and there
526 * are users of it. For that case we have no other option that
527 * copying the data. Tough luck.
528 * - Destination page already exists in the address space, but there
529 * are no users of it. Make sure it's uptodate, then drop it. Fall
530 * through to last case.
531 * - Destination page does not exist, we can add the pipe page to
532 * the page cache and avoid the copy.
533 *
Jens Axboe83f91352006-04-02 23:05:09 +0200534 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
535 * sd->flags), we attempt to migrate pages from the pipe to the output
536 * file address space page cache. This is possible if no one else has
537 * the pipe page referenced outside of the pipe and page cache. If
538 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
539 * a new page in the output file page cache and fill/dirty that.
Jens Axboe5274f052006-03-30 15:15:30 +0200540 */
541static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
542 struct splice_desc *sd)
543{
544 struct file *file = sd->file;
545 struct address_space *mapping = file->f_mapping;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200546 gfp_t gfp_mask = mapping_gfp_mask(mapping);
Jens Axboe016b6612006-04-25 15:42:00 +0200547 unsigned int offset, this_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200548 struct page *page;
Jens Axboe5274f052006-03-30 15:15:30 +0200549 pgoff_t index;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200550 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200551
552 /*
Jens Axboe49d0b212006-04-10 09:04:41 +0200553 * make sure the data in this buffer is uptodate
Jens Axboe5274f052006-03-30 15:15:30 +0200554 */
Jens Axboef84d7512006-05-01 19:59:03 +0200555 ret = buf->ops->pin(info, buf);
556 if (unlikely(ret))
557 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200558
559 index = sd->pos >> PAGE_CACHE_SHIFT;
560 offset = sd->pos & ~PAGE_CACHE_MASK;
561
Jens Axboe016b6612006-04-25 15:42:00 +0200562 this_len = sd->len;
563 if (this_len + offset > PAGE_CACHE_SIZE)
564 this_len = PAGE_CACHE_SIZE - offset;
565
Jens Axboe5274f052006-03-30 15:15:30 +0200566 /*
Jens Axboe0568b402006-05-01 19:50:48 +0200567 * Reuse buf page, if SPLICE_F_MOVE is set and we are doing a full
568 * page.
Jens Axboe5274f052006-03-30 15:15:30 +0200569 */
Jens Axboe0568b402006-05-01 19:50:48 +0200570 if ((sd->flags & SPLICE_F_MOVE) && this_len == PAGE_CACHE_SIZE) {
Jens Axboe83f91352006-04-02 23:05:09 +0200571 /*
Jens Axboe14328732006-05-03 10:35:26 +0200572 * If steal succeeds, buf->page is now pruned from the
573 * pagecache and we can reuse it. The page will also be
574 * locked on successful return.
Jens Axboe83f91352006-04-02 23:05:09 +0200575 */
Jens Axboe5abc97a2006-03-30 15:16:46 +0200576 if (buf->ops->steal(info, buf))
577 goto find_page;
Jens Axboe5274f052006-03-30 15:15:30 +0200578
Jens Axboe5abc97a2006-03-30 15:16:46 +0200579 page = buf->page;
Jens Axboe46e678c2006-04-30 16:36:32 +0200580 if (add_to_page_cache(page, mapping, index, gfp_mask)) {
581 unlock_page(page);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200582 goto find_page;
Jens Axboe46e678c2006-04-30 16:36:32 +0200583 }
Jens Axboe14328732006-05-03 10:35:26 +0200584
585 page_cache_get(page);
586
587 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
588 lru_cache_add(page);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200589 } else {
590find_page:
Jens Axboe9e0267c2006-04-19 15:57:31 +0200591 page = find_lock_page(mapping, index);
592 if (!page) {
593 ret = -ENOMEM;
594 page = page_cache_alloc_cold(mapping);
595 if (unlikely(!page))
596 goto out_nomem;
597
598 /*
599 * This will also lock the page
600 */
601 ret = add_to_page_cache_lru(page, mapping, index,
602 gfp_mask);
603 if (unlikely(ret))
604 goto out;
605 }
Jens Axboe5274f052006-03-30 15:15:30 +0200606
Jens Axboe5abc97a2006-03-30 15:16:46 +0200607 /*
Jens Axboe9e0267c2006-04-19 15:57:31 +0200608 * We get here with the page locked. If the page is also
609 * uptodate, we don't need to do more. If it isn't, we
610 * may need to bring it in if we are not going to overwrite
611 * the full page.
Jens Axboe5abc97a2006-03-30 15:16:46 +0200612 */
613 if (!PageUptodate(page)) {
Jens Axboe016b6612006-04-25 15:42:00 +0200614 if (this_len < PAGE_CACHE_SIZE) {
Jens Axboe5abc97a2006-03-30 15:16:46 +0200615 ret = mapping->a_ops->readpage(file, page);
616 if (unlikely(ret))
617 goto out;
618
619 lock_page(page);
620
621 if (!PageUptodate(page)) {
622 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200623 * Page got invalidated, repeat.
Jens Axboe5abc97a2006-03-30 15:16:46 +0200624 */
625 if (!page->mapping) {
626 unlock_page(page);
627 page_cache_release(page);
628 goto find_page;
629 }
630 ret = -EIO;
631 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200632 }
Jens Axboe9e0267c2006-04-19 15:57:31 +0200633 } else
Jens Axboe5abc97a2006-03-30 15:16:46 +0200634 SetPageUptodate(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200635 }
636 }
637
Jens Axboe016b6612006-04-25 15:42:00 +0200638 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200639 if (unlikely(ret)) {
640 loff_t isize = i_size_read(mapping->host);
641
642 if (ret != AOP_TRUNCATED_PAGE)
643 unlock_page(page);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200644 page_cache_release(page);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200645 if (ret == AOP_TRUNCATED_PAGE)
646 goto find_page;
647
648 /*
649 * prepare_write() may have instantiated a few blocks
650 * outside i_size. Trim these off again.
651 */
652 if (sd->pos + this_len > isize)
653 vmtruncate(mapping->host, isize);
654
Jens Axboe5274f052006-03-30 15:15:30 +0200655 goto out;
Jens Axboebfc4ee32006-05-03 10:35:10 +0200656 }
Jens Axboe5274f052006-03-30 15:15:30 +0200657
Jens Axboe0568b402006-05-01 19:50:48 +0200658 if (buf->page != page) {
Jens Axboef84d7512006-05-01 19:59:03 +0200659 /*
660 * Careful, ->map() uses KM_USER0!
661 */
Jens Axboef6762b72006-05-01 20:02:05 +0200662 char *src = buf->ops->map(info, buf, 1);
Jens Axboef84d7512006-05-01 19:59:03 +0200663 char *dst = kmap_atomic(page, KM_USER1);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200664
Jens Axboe016b6612006-04-25 15:42:00 +0200665 memcpy(dst + offset, src + buf->offset, this_len);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200666 flush_dcache_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200667 kunmap_atomic(dst, KM_USER1);
Jens Axboef6762b72006-05-01 20:02:05 +0200668 buf->ops->unmap(info, buf, src);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200669 }
Jens Axboe5274f052006-03-30 15:15:30 +0200670
Jens Axboe016b6612006-04-25 15:42:00 +0200671 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
Jens Axboe0568b402006-05-01 19:50:48 +0200672 if (!ret) {
673 /*
674 * Return the number of bytes written and mark page as
675 * accessed, we are now done!
676 */
677 ret = this_len;
678 mark_page_accessed(page);
679 balance_dirty_pages_ratelimited(mapping);
680 } else if (ret == AOP_TRUNCATED_PAGE) {
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200681 page_cache_release(page);
682 goto find_page;
Jens Axboe0568b402006-05-01 19:50:48 +0200683 }
Jens Axboe5274f052006-03-30 15:15:30 +0200684out:
Jens Axboe0568b402006-05-01 19:50:48 +0200685 page_cache_release(page);
Jens Axboe9e0267c2006-04-19 15:57:31 +0200686 unlock_page(page);
Dave Jones9aefe432006-04-10 09:02:40 +0200687out_nomem:
Jens Axboe5274f052006-03-30 15:15:30 +0200688 return ret;
689}
690
Jens Axboe83f91352006-04-02 23:05:09 +0200691/*
692 * Pipe input worker. Most of this logic works like a regular pipe, the
693 * key here is the 'actor' worker passed in that actually moves the data
694 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
695 */
Jens Axboe00522fb2006-04-26 14:39:29 +0200696ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
697 loff_t *ppos, size_t len, unsigned int flags,
698 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200699{
Jens Axboe5274f052006-03-30 15:15:30 +0200700 int ret, do_wakeup, err;
701 struct splice_desc sd;
702
703 ret = 0;
704 do_wakeup = 0;
705
706 sd.total_len = len;
707 sd.flags = flags;
708 sd.file = out;
Jens Axboecbb7e572006-04-11 14:57:50 +0200709 sd.pos = *ppos;
Jens Axboe5274f052006-03-30 15:15:30 +0200710
Ingo Molnar3a326a22006-04-10 15:18:35 +0200711 if (pipe->inode)
712 mutex_lock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200713
Jens Axboe5274f052006-03-30 15:15:30 +0200714 for (;;) {
Jens Axboe6f767b02006-04-11 13:53:56 +0200715 if (pipe->nrbufs) {
716 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
Jens Axboe5274f052006-03-30 15:15:30 +0200717 struct pipe_buf_operations *ops = buf->ops;
718
719 sd.len = buf->len;
720 if (sd.len > sd.total_len)
721 sd.len = sd.total_len;
722
Ingo Molnar3a326a22006-04-10 15:18:35 +0200723 err = actor(pipe, buf, &sd);
Jens Axboe016b6612006-04-25 15:42:00 +0200724 if (err <= 0) {
Jens Axboe5274f052006-03-30 15:15:30 +0200725 if (!ret && err != -ENODATA)
726 ret = err;
727
728 break;
729 }
730
Jens Axboe016b6612006-04-25 15:42:00 +0200731 ret += err;
732 buf->offset += err;
733 buf->len -= err;
734
735 sd.len -= err;
736 sd.pos += err;
737 sd.total_len -= err;
738 if (sd.len)
739 continue;
Ingo Molnar73d62d82006-04-11 13:57:21 +0200740
Jens Axboe5274f052006-03-30 15:15:30 +0200741 if (!buf->len) {
742 buf->ops = NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200743 ops->release(pipe, buf);
Jens Axboe6f767b02006-04-11 13:53:56 +0200744 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
745 pipe->nrbufs--;
746 if (pipe->inode)
747 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200748 }
749
Jens Axboe5274f052006-03-30 15:15:30 +0200750 if (!sd.total_len)
751 break;
752 }
753
Jens Axboe6f767b02006-04-11 13:53:56 +0200754 if (pipe->nrbufs)
Jens Axboe5274f052006-03-30 15:15:30 +0200755 continue;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200756 if (!pipe->writers)
Jens Axboe5274f052006-03-30 15:15:30 +0200757 break;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200758 if (!pipe->waiting_writers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200759 if (ret)
760 break;
761 }
762
Linus Torvalds29e35092006-04-02 12:46:35 -0700763 if (flags & SPLICE_F_NONBLOCK) {
764 if (!ret)
765 ret = -EAGAIN;
766 break;
767 }
768
Jens Axboe5274f052006-03-30 15:15:30 +0200769 if (signal_pending(current)) {
770 if (!ret)
771 ret = -ERESTARTSYS;
772 break;
773 }
774
775 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200776 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200777 if (waitqueue_active(&pipe->wait))
778 wake_up_interruptible_sync(&pipe->wait);
779 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200780 do_wakeup = 0;
781 }
782
Ingo Molnar3a326a22006-04-10 15:18:35 +0200783 pipe_wait(pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200784 }
785
Ingo Molnar3a326a22006-04-10 15:18:35 +0200786 if (pipe->inode)
787 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200788
789 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200790 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200791 if (waitqueue_active(&pipe->wait))
792 wake_up_interruptible(&pipe->wait);
793 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200794 }
795
Jens Axboe5274f052006-03-30 15:15:30 +0200796 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200797}
798
Jens Axboe83f91352006-04-02 23:05:09 +0200799/**
800 * generic_file_splice_write - splice data from a pipe to a file
Ingo Molnar3a326a22006-04-10 15:18:35 +0200801 * @pipe: pipe info
Jens Axboe83f91352006-04-02 23:05:09 +0200802 * @out: file to write to
803 * @len: number of bytes to splice
804 * @flags: splice modifier flags
805 *
806 * Will either move or copy pages (determined by @flags options) from
807 * the given pipe inode to the given file.
808 *
809 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200810ssize_t
811generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200812 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200813{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200814 struct address_space *mapping = out->f_mapping;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200815 ssize_t ret;
816
Jens Axboe00522fb2006-04-26 14:39:29 +0200817 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
Jens Axboea4514eb2006-04-19 15:57:05 +0200818 if (ret > 0) {
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200819 struct inode *inode = mapping->host;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200820
Jens Axboea4514eb2006-04-19 15:57:05 +0200821 *ppos += ret;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200822
Jens Axboea4514eb2006-04-19 15:57:05 +0200823 /*
824 * If file or inode is SYNC and we actually wrote some data,
825 * sync it.
826 */
827 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
828 int err;
829
830 mutex_lock(&inode->i_mutex);
831 err = generic_osync_inode(inode, mapping,
832 OSYNC_METADATA|OSYNC_DATA);
833 mutex_unlock(&inode->i_mutex);
834
835 if (err)
836 ret = err;
837 }
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200838 }
839
840 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200841}
842
Jens Axboe059a8f32006-04-02 23:06:05 +0200843EXPORT_SYMBOL(generic_file_splice_write);
844
Jens Axboe83f91352006-04-02 23:05:09 +0200845/**
846 * generic_splice_sendpage - splice data from a pipe to a socket
847 * @inode: pipe inode
848 * @out: socket to write to
849 * @len: number of bytes to splice
850 * @flags: splice modifier flags
851 *
852 * Will send @len bytes from the pipe to a network socket. No data copying
853 * is involved.
854 *
855 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200856ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200857 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200858{
Jens Axboe00522fb2006-04-26 14:39:29 +0200859 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200860}
861
Jens Axboe059a8f32006-04-02 23:06:05 +0200862EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500863
Jens Axboe83f91352006-04-02 23:05:09 +0200864/*
865 * Attempt to initiate a splice from pipe to file.
866 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200867static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200868 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200869{
Jens Axboe5274f052006-03-30 15:15:30 +0200870 int ret;
871
Jens Axboe49570e92006-04-11 13:56:09 +0200872 if (unlikely(!out->f_op || !out->f_op->splice_write))
Jens Axboe5274f052006-03-30 15:15:30 +0200873 return -EINVAL;
874
Jens Axboe49570e92006-04-11 13:56:09 +0200875 if (unlikely(!(out->f_mode & FMODE_WRITE)))
Jens Axboe5274f052006-03-30 15:15:30 +0200876 return -EBADF;
877
Jens Axboecbb7e572006-04-11 14:57:50 +0200878 ret = rw_verify_area(WRITE, out, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200879 if (unlikely(ret < 0))
880 return ret;
881
Jens Axboecbb7e572006-04-11 14:57:50 +0200882 return out->f_op->splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200883}
884
Jens Axboe83f91352006-04-02 23:05:09 +0200885/*
886 * Attempt to initiate a splice from a file to a pipe.
887 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200888static long do_splice_to(struct file *in, loff_t *ppos,
889 struct pipe_inode_info *pipe, size_t len,
890 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200891{
Jens Axboecbb7e572006-04-11 14:57:50 +0200892 loff_t isize, left;
Jens Axboe5274f052006-03-30 15:15:30 +0200893 int ret;
894
Jens Axboe49570e92006-04-11 13:56:09 +0200895 if (unlikely(!in->f_op || !in->f_op->splice_read))
Jens Axboe5274f052006-03-30 15:15:30 +0200896 return -EINVAL;
897
Jens Axboe49570e92006-04-11 13:56:09 +0200898 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200899 return -EBADF;
900
Jens Axboecbb7e572006-04-11 14:57:50 +0200901 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200902 if (unlikely(ret < 0))
903 return ret;
904
905 isize = i_size_read(in->f_mapping->host);
Jens Axboecbb7e572006-04-11 14:57:50 +0200906 if (unlikely(*ppos >= isize))
Jens Axboe5274f052006-03-30 15:15:30 +0200907 return 0;
908
Jens Axboecbb7e572006-04-11 14:57:50 +0200909 left = isize - *ppos;
Jens Axboe49570e92006-04-11 13:56:09 +0200910 if (unlikely(left < len))
Jens Axboe5274f052006-03-30 15:15:30 +0200911 len = left;
912
Jens Axboecbb7e572006-04-11 14:57:50 +0200913 return in->f_op->splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200914}
915
Jens Axboecbb7e572006-04-11 14:57:50 +0200916long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
917 size_t len, unsigned int flags)
Jens Axboeb92ce552006-04-11 13:52:07 +0200918{
919 struct pipe_inode_info *pipe;
920 long ret, bytes;
Jens Axboecbb7e572006-04-11 14:57:50 +0200921 loff_t out_off;
Jens Axboeb92ce552006-04-11 13:52:07 +0200922 umode_t i_mode;
923 int i;
924
925 /*
926 * We require the input being a regular file, as we don't want to
927 * randomly drop data for eg socket -> socket splicing. Use the
928 * piped splicing for that!
929 */
930 i_mode = in->f_dentry->d_inode->i_mode;
931 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
932 return -EINVAL;
933
934 /*
935 * neither in nor out is a pipe, setup an internal pipe attached to
936 * 'out' and transfer the wanted data from 'in' to 'out' through that
937 */
938 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200939 if (unlikely(!pipe)) {
Jens Axboeb92ce552006-04-11 13:52:07 +0200940 pipe = alloc_pipe_info(NULL);
941 if (!pipe)
942 return -ENOMEM;
943
944 /*
945 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200946 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200947 * PIPE_READERS appropriately.
948 */
949 pipe->readers = 1;
950
951 current->splice_pipe = pipe;
952 }
953
954 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200955 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200956 */
957 ret = 0;
958 bytes = 0;
Jens Axboecbb7e572006-04-11 14:57:50 +0200959 out_off = 0;
Jens Axboeb92ce552006-04-11 13:52:07 +0200960
961 while (len) {
962 size_t read_len, max_read_len;
963
964 /*
965 * Do at most PIPE_BUFFERS pages worth of transfer:
966 */
967 max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
968
Jens Axboecbb7e572006-04-11 14:57:50 +0200969 ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
Jens Axboeb92ce552006-04-11 13:52:07 +0200970 if (unlikely(ret < 0))
971 goto out_release;
972
973 read_len = ret;
974
975 /*
976 * NOTE: nonblocking mode only applies to the input. We
977 * must not do the output in nonblocking mode as then we
978 * could get stuck data in the internal pipe:
979 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200980 ret = do_splice_from(pipe, out, &out_off, read_len,
Jens Axboeb92ce552006-04-11 13:52:07 +0200981 flags & ~SPLICE_F_NONBLOCK);
982 if (unlikely(ret < 0))
983 goto out_release;
984
985 bytes += ret;
986 len -= ret;
987
988 /*
989 * In nonblocking mode, if we got back a short read then
990 * that was due to either an IO error or due to the
991 * pagecache entry not being there. In the IO error case
992 * the _next_ splice attempt will produce a clean IO error
993 * return value (not a short read), so in both cases it's
994 * correct to break out of the loop here:
995 */
996 if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
997 break;
998 }
999
1000 pipe->nrbufs = pipe->curbuf = 0;
1001
1002 return bytes;
1003
1004out_release:
1005 /*
1006 * If we did an incomplete transfer we must release
1007 * the pipe buffers in question:
1008 */
1009 for (i = 0; i < PIPE_BUFFERS; i++) {
1010 struct pipe_buffer *buf = pipe->bufs + i;
1011
1012 if (buf->ops) {
1013 buf->ops->release(pipe, buf);
1014 buf->ops = NULL;
1015 }
1016 }
1017 pipe->nrbufs = pipe->curbuf = 0;
1018
1019 /*
1020 * If we transferred some data, return the number of bytes:
1021 */
1022 if (bytes > 0)
1023 return bytes;
1024
1025 return ret;
1026}
1027
1028EXPORT_SYMBOL(do_splice_direct);
1029
Jens Axboe83f91352006-04-02 23:05:09 +02001030/*
1031 * Determine where to splice to/from.
1032 */
Ingo Molnar529565dc2006-04-10 15:18:58 +02001033static long do_splice(struct file *in, loff_t __user *off_in,
1034 struct file *out, loff_t __user *off_out,
1035 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001036{
Ingo Molnar3a326a22006-04-10 15:18:35 +02001037 struct pipe_inode_info *pipe;
Jens Axboecbb7e572006-04-11 14:57:50 +02001038 loff_t offset, *off;
Jens Axboea4514eb2006-04-19 15:57:05 +02001039 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001040
Ingo Molnar3a326a22006-04-10 15:18:35 +02001041 pipe = in->f_dentry->d_inode->i_pipe;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001042 if (pipe) {
1043 if (off_in)
1044 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001045 if (off_out) {
1046 if (out->f_op->llseek == no_llseek)
1047 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001048 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001049 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001050 off = &offset;
1051 } else
1052 off = &out->f_pos;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001053
Jens Axboea4514eb2006-04-19 15:57:05 +02001054 ret = do_splice_from(pipe, out, off, len, flags);
1055
1056 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1057 ret = -EFAULT;
1058
1059 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001060 }
Jens Axboe5274f052006-03-30 15:15:30 +02001061
Ingo Molnar3a326a22006-04-10 15:18:35 +02001062 pipe = out->f_dentry->d_inode->i_pipe;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001063 if (pipe) {
1064 if (off_out)
1065 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001066 if (off_in) {
1067 if (in->f_op->llseek == no_llseek)
1068 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001069 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001070 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001071 off = &offset;
1072 } else
1073 off = &in->f_pos;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001074
Jens Axboea4514eb2006-04-19 15:57:05 +02001075 ret = do_splice_to(in, off, pipe, len, flags);
1076
1077 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1078 ret = -EFAULT;
1079
1080 return ret;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001081 }
Jens Axboe5274f052006-03-30 15:15:30 +02001082
1083 return -EINVAL;
1084}
1085
Jens Axboe912d35f2006-04-26 10:59:21 +02001086/*
1087 * Map an iov into an array of pages and offset/length tupples. With the
1088 * partial_page structure, we can map several non-contiguous ranges into
1089 * our ones pages[] map instead of splitting that operation into pieces.
1090 * Could easily be exported as a generic helper for other users, in which
1091 * case one would probably want to add a 'max_nr_pages' parameter as well.
1092 */
1093static int get_iovec_page_array(const struct iovec __user *iov,
1094 unsigned int nr_vecs, struct page **pages,
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001095 struct partial_page *partial, int aligned)
Jens Axboe912d35f2006-04-26 10:59:21 +02001096{
1097 int buffers = 0, error = 0;
1098
1099 /*
1100 * It's ok to take the mmap_sem for reading, even
1101 * across a "get_user()".
1102 */
1103 down_read(&current->mm->mmap_sem);
1104
1105 while (nr_vecs) {
1106 unsigned long off, npages;
1107 void __user *base;
1108 size_t len;
1109 int i;
1110
1111 /*
1112 * Get user address base and length for this iovec.
1113 */
1114 error = get_user(base, &iov->iov_base);
1115 if (unlikely(error))
1116 break;
1117 error = get_user(len, &iov->iov_len);
1118 if (unlikely(error))
1119 break;
1120
1121 /*
1122 * Sanity check this iovec. 0 read succeeds.
1123 */
1124 if (unlikely(!len))
1125 break;
1126 error = -EFAULT;
1127 if (unlikely(!base))
1128 break;
1129
1130 /*
1131 * Get this base offset and number of pages, then map
1132 * in the user pages.
1133 */
1134 off = (unsigned long) base & ~PAGE_MASK;
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001135
1136 /*
1137 * If asked for alignment, the offset must be zero and the
1138 * length a multiple of the PAGE_SIZE.
1139 */
1140 error = -EINVAL;
1141 if (aligned && (off || len & ~PAGE_MASK))
1142 break;
1143
Jens Axboe912d35f2006-04-26 10:59:21 +02001144 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1145 if (npages > PIPE_BUFFERS - buffers)
1146 npages = PIPE_BUFFERS - buffers;
1147
1148 error = get_user_pages(current, current->mm,
1149 (unsigned long) base, npages, 0, 0,
1150 &pages[buffers], NULL);
1151
1152 if (unlikely(error <= 0))
1153 break;
1154
1155 /*
1156 * Fill this contiguous range into the partial page map.
1157 */
1158 for (i = 0; i < error; i++) {
Jens Axboe75914892006-05-02 12:57:18 +02001159 const int plen = min_t(size_t, len, PAGE_SIZE - off);
Jens Axboe912d35f2006-04-26 10:59:21 +02001160
1161 partial[buffers].offset = off;
1162 partial[buffers].len = plen;
1163
1164 off = 0;
1165 len -= plen;
1166 buffers++;
1167 }
1168
1169 /*
1170 * We didn't complete this iov, stop here since it probably
1171 * means we have to move some of this into a pipe to
1172 * be able to continue.
1173 */
1174 if (len)
1175 break;
1176
1177 /*
1178 * Don't continue if we mapped fewer pages than we asked for,
1179 * or if we mapped the max number of pages that we have
1180 * room for.
1181 */
1182 if (error < npages || buffers == PIPE_BUFFERS)
1183 break;
1184
1185 nr_vecs--;
1186 iov++;
1187 }
1188
1189 up_read(&current->mm->mmap_sem);
1190
1191 if (buffers)
1192 return buffers;
1193
1194 return error;
1195}
1196
1197/*
1198 * vmsplice splices a user address range into a pipe. It can be thought of
1199 * as splice-from-memory, where the regular splice is splice-from-file (or
1200 * to file). In both cases the output is a pipe, naturally.
1201 *
1202 * Note that vmsplice only supports splicing _from_ user memory to a pipe,
1203 * not the other way around. Splicing from user memory is a simple operation
1204 * that can be supported without any funky alignment restrictions or nasty
1205 * vm tricks. We simply map in the user memory and fill them into a pipe.
1206 * The reverse isn't quite as easy, though. There are two possible solutions
1207 * for that:
1208 *
1209 * - memcpy() the data internally, at which point we might as well just
1210 * do a regular read() on the buffer anyway.
1211 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1212 * has restriction limitations on both ends of the pipe).
1213 *
1214 * Alas, it isn't here.
1215 *
1216 */
1217static long do_vmsplice(struct file *file, const struct iovec __user *iov,
1218 unsigned long nr_segs, unsigned int flags)
1219{
1220 struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
1221 struct page *pages[PIPE_BUFFERS];
1222 struct partial_page partial[PIPE_BUFFERS];
1223 struct splice_pipe_desc spd = {
1224 .pages = pages,
1225 .partial = partial,
1226 .flags = flags,
1227 .ops = &user_page_pipe_buf_ops,
1228 };
1229
1230 if (unlikely(!pipe))
1231 return -EBADF;
1232 if (unlikely(nr_segs > UIO_MAXIOV))
1233 return -EINVAL;
1234 else if (unlikely(!nr_segs))
1235 return 0;
1236
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001237 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1238 flags & SPLICE_F_GIFT);
Jens Axboe912d35f2006-04-26 10:59:21 +02001239 if (spd.nr_pages <= 0)
1240 return spd.nr_pages;
1241
Jens Axboe00522fb2006-04-26 14:39:29 +02001242 return splice_to_pipe(pipe, &spd);
Jens Axboe912d35f2006-04-26 10:59:21 +02001243}
1244
1245asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1246 unsigned long nr_segs, unsigned int flags)
1247{
1248 struct file *file;
1249 long error;
1250 int fput;
1251
1252 error = -EBADF;
1253 file = fget_light(fd, &fput);
1254 if (file) {
1255 if (file->f_mode & FMODE_WRITE)
1256 error = do_vmsplice(file, iov, nr_segs, flags);
1257
1258 fput_light(file, fput);
1259 }
1260
1261 return error;
1262}
1263
Ingo Molnar529565dc2006-04-10 15:18:58 +02001264asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1265 int fd_out, loff_t __user *off_out,
1266 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001267{
1268 long error;
1269 struct file *in, *out;
1270 int fput_in, fput_out;
1271
1272 if (unlikely(!len))
1273 return 0;
1274
1275 error = -EBADF;
Ingo Molnar529565dc2006-04-10 15:18:58 +02001276 in = fget_light(fd_in, &fput_in);
Jens Axboe5274f052006-03-30 15:15:30 +02001277 if (in) {
1278 if (in->f_mode & FMODE_READ) {
Ingo Molnar529565dc2006-04-10 15:18:58 +02001279 out = fget_light(fd_out, &fput_out);
Jens Axboe5274f052006-03-30 15:15:30 +02001280 if (out) {
1281 if (out->f_mode & FMODE_WRITE)
Ingo Molnar529565dc2006-04-10 15:18:58 +02001282 error = do_splice(in, off_in,
1283 out, off_out,
1284 len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001285 fput_light(out, fput_out);
1286 }
1287 }
1288
1289 fput_light(in, fput_in);
1290 }
1291
1292 return error;
1293}
Jens Axboe70524492006-04-11 15:51:17 +02001294
1295/*
1296 * Link contents of ipipe to opipe.
1297 */
1298static int link_pipe(struct pipe_inode_info *ipipe,
1299 struct pipe_inode_info *opipe,
1300 size_t len, unsigned int flags)
1301{
1302 struct pipe_buffer *ibuf, *obuf;
Jens Axboe2a27250e2006-04-19 15:56:40 +02001303 int ret, do_wakeup, i, ipipe_first;
1304
1305 ret = do_wakeup = ipipe_first = 0;
Jens Axboe70524492006-04-11 15:51:17 +02001306
1307 /*
1308 * Potential ABBA deadlock, work around it by ordering lock
1309 * grabbing by inode address. Otherwise two different processes
1310 * could deadlock (one doing tee from A -> B, the other from B -> A).
1311 */
1312 if (ipipe->inode < opipe->inode) {
Jens Axboe2a27250e2006-04-19 15:56:40 +02001313 ipipe_first = 1;
Jens Axboe70524492006-04-11 15:51:17 +02001314 mutex_lock(&ipipe->inode->i_mutex);
1315 mutex_lock(&opipe->inode->i_mutex);
1316 } else {
1317 mutex_lock(&opipe->inode->i_mutex);
1318 mutex_lock(&ipipe->inode->i_mutex);
1319 }
1320
1321 for (i = 0;; i++) {
1322 if (!opipe->readers) {
1323 send_sig(SIGPIPE, current, 0);
1324 if (!ret)
1325 ret = -EPIPE;
1326 break;
1327 }
1328 if (ipipe->nrbufs - i) {
1329 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1330
1331 /*
1332 * If we have room, fill this buffer
1333 */
1334 if (opipe->nrbufs < PIPE_BUFFERS) {
1335 int nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1336
1337 /*
1338 * Get a reference to this pipe buffer,
1339 * so we can copy the contents over.
1340 */
1341 ibuf->ops->get(ipipe, ibuf);
1342
1343 obuf = opipe->bufs + nbuf;
1344 *obuf = *ibuf;
1345
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001346 /*
1347 * Don't inherit the gift flag, we need to
1348 * prevent multiple steals of this page.
1349 */
1350 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1351
Jens Axboe70524492006-04-11 15:51:17 +02001352 if (obuf->len > len)
1353 obuf->len = len;
1354
1355 opipe->nrbufs++;
1356 do_wakeup = 1;
1357 ret += obuf->len;
1358 len -= obuf->len;
1359
1360 if (!len)
1361 break;
1362 if (opipe->nrbufs < PIPE_BUFFERS)
1363 continue;
1364 }
1365
1366 /*
1367 * We have input available, but no output room.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001368 * If we already copied data, return that. If we
1369 * need to drop the opipe lock, it must be ordered
1370 * last to avoid deadlocks.
Jens Axboe70524492006-04-11 15:51:17 +02001371 */
Jens Axboe2a27250e2006-04-19 15:56:40 +02001372 if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
Jens Axboe70524492006-04-11 15:51:17 +02001373 if (!ret)
1374 ret = -EAGAIN;
1375 break;
1376 }
1377 if (signal_pending(current)) {
1378 if (!ret)
1379 ret = -ERESTARTSYS;
1380 break;
1381 }
1382 if (do_wakeup) {
1383 smp_mb();
1384 if (waitqueue_active(&opipe->wait))
1385 wake_up_interruptible(&opipe->wait);
1386 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1387 do_wakeup = 0;
1388 }
1389
1390 opipe->waiting_writers++;
1391 pipe_wait(opipe);
1392 opipe->waiting_writers--;
1393 continue;
1394 }
1395
1396 /*
1397 * No input buffers, do the usual checks for available
1398 * writers and blocking and wait if necessary
1399 */
1400 if (!ipipe->writers)
1401 break;
1402 if (!ipipe->waiting_writers) {
1403 if (ret)
1404 break;
1405 }
Jens Axboe2a27250e2006-04-19 15:56:40 +02001406 /*
1407 * pipe_wait() drops the ipipe mutex. To avoid deadlocks
1408 * with another process, we can only safely do that if
1409 * the ipipe lock is ordered last.
1410 */
1411 if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
Jens Axboe70524492006-04-11 15:51:17 +02001412 if (!ret)
1413 ret = -EAGAIN;
1414 break;
1415 }
1416 if (signal_pending(current)) {
1417 if (!ret)
1418 ret = -ERESTARTSYS;
1419 break;
1420 }
1421
1422 if (waitqueue_active(&ipipe->wait))
1423 wake_up_interruptible_sync(&ipipe->wait);
1424 kill_fasync(&ipipe->fasync_writers, SIGIO, POLL_OUT);
1425
1426 pipe_wait(ipipe);
1427 }
1428
1429 mutex_unlock(&ipipe->inode->i_mutex);
1430 mutex_unlock(&opipe->inode->i_mutex);
1431
1432 if (do_wakeup) {
1433 smp_mb();
1434 if (waitqueue_active(&opipe->wait))
1435 wake_up_interruptible(&opipe->wait);
1436 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1437 }
1438
1439 return ret;
1440}
1441
1442/*
1443 * This is a tee(1) implementation that works on pipes. It doesn't copy
1444 * any data, it simply references the 'in' pages on the 'out' pipe.
1445 * The 'flags' used are the SPLICE_F_* variants, currently the only
1446 * applicable one is SPLICE_F_NONBLOCK.
1447 */
1448static long do_tee(struct file *in, struct file *out, size_t len,
1449 unsigned int flags)
1450{
1451 struct pipe_inode_info *ipipe = in->f_dentry->d_inode->i_pipe;
1452 struct pipe_inode_info *opipe = out->f_dentry->d_inode->i_pipe;
1453
1454 /*
1455 * Link ipipe to the two output pipes, consuming as we go along.
1456 */
1457 if (ipipe && opipe)
1458 return link_pipe(ipipe, opipe, len, flags);
1459
1460 return -EINVAL;
1461}
1462
1463asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1464{
1465 struct file *in;
1466 int error, fput_in;
1467
1468 if (unlikely(!len))
1469 return 0;
1470
1471 error = -EBADF;
1472 in = fget_light(fdin, &fput_in);
1473 if (in) {
1474 if (in->f_mode & FMODE_READ) {
1475 int fput_out;
1476 struct file *out = fget_light(fdout, &fput_out);
1477
1478 if (out) {
1479 if (out->f_mode & FMODE_WRITE)
1480 error = do_tee(in, out, len, flags);
1481 fput_light(out, fput_out);
1482 }
1483 }
1484 fput_light(in, fput_in);
1485 }
1486
1487 return error;
1488}