blob: bea9f1581ca054339fb02260088b2c2a3b0ad142 [file] [log] [blame]
Jens Axboe5274f052006-03-30 15:15:30 +02001/*
2 * "splice": joining two ropes together by interweaving their strands.
3 *
4 * This is the "extended pipe" functionality, where a pipe is used as
5 * an arbitrary in-memory buffer. Think of a pipe as a small kernel
6 * buffer that you can use to transfer data from one end to the other.
7 *
8 * The traditional unix read/write is extended with a "splice()" operation
9 * that transfers data buffers to or from a pipe buffer.
10 *
11 * Named by Larry McVoy, original implementation from Linus, extended by
Jens Axboec2058e02006-04-11 13:56:34 +020012 * Jens to support splicing to files, network, direct splicing, etc and
13 * fixing lots of bugs.
Jens Axboe5274f052006-03-30 15:15:30 +020014 *
Jens Axboe0fe23472006-09-04 15:41:16 +020015 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
Jens Axboec2058e02006-04-11 13:56:34 +020016 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
17 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
Jens Axboe5274f052006-03-30 15:15:30 +020018 *
19 */
20#include <linux/fs.h>
21#include <linux/file.h>
22#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020023#include <linux/splice.h>
Jens Axboe5274f052006-03-30 15:15:30 +020024#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 Axboe83f91352006-04-02 23:05:09 +020032/*
33 * Attempt to steal a page from a pipe buffer. This should perhaps go into
34 * a vm helper function, it's already simplified quite a bit by the
35 * addition of remove_mapping(). If success is returned, the caller may
36 * attempt to reuse this page for another destination.
37 */
Jens Axboe76ad4d12006-05-03 10:41:33 +020038static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
Jens Axboe5abc97a2006-03-30 15:16:46 +020039 struct pipe_buffer *buf)
40{
41 struct page *page = buf->page;
Jens Axboe9e94cd42006-06-20 15:01:12 +020042 struct address_space *mapping;
Jens Axboe5abc97a2006-03-30 15:16:46 +020043
Jens Axboe9e0267c2006-04-19 15:57:31 +020044 lock_page(page);
45
Jens Axboe9e94cd42006-06-20 15:01:12 +020046 mapping = page_mapping(page);
47 if (mapping) {
48 WARN_ON(!PageUptodate(page));
Jens Axboe5abc97a2006-03-30 15:16:46 +020049
Jens Axboe9e94cd42006-06-20 15:01:12 +020050 /*
51 * At least for ext2 with nobh option, we need to wait on
52 * writeback completing on this page, since we'll remove it
53 * from the pagecache. Otherwise truncate wont wait on the
54 * page, allowing the disk blocks to be reused by someone else
55 * before we actually wrote our data to them. fs corruption
56 * ensues.
57 */
58 wait_on_page_writeback(page);
Jens Axboead8d6f02006-04-02 23:10:32 +020059
Jens Axboe9e94cd42006-06-20 15:01:12 +020060 if (PagePrivate(page))
Nick Piggin2ae88142006-10-28 10:38:23 -070061 try_to_release_page(page, GFP_KERNEL);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020062
Jens Axboe9e94cd42006-06-20 15:01:12 +020063 /*
64 * If we succeeded in removing the mapping, set LRU flag
65 * and return good.
66 */
67 if (remove_mapping(mapping, page)) {
68 buf->flags |= PIPE_BUF_FLAG_LRU;
69 return 0;
70 }
Jens Axboe9e0267c2006-04-19 15:57:31 +020071 }
Jens Axboe5abc97a2006-03-30 15:16:46 +020072
Jens Axboe9e94cd42006-06-20 15:01:12 +020073 /*
74 * Raced with truncate or failed to remove page from current
75 * address space, unlock and return failure.
76 */
77 unlock_page(page);
78 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +020079}
80
Jens Axboe76ad4d12006-05-03 10:41:33 +020081static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +020082 struct pipe_buffer *buf)
83{
84 page_cache_release(buf->page);
Jens Axboe14328732006-05-03 10:35:26 +020085 buf->flags &= ~PIPE_BUF_FLAG_LRU;
Jens Axboe5274f052006-03-30 15:15:30 +020086}
87
Jens Axboe76ad4d12006-05-03 10:41:33 +020088static int page_cache_pipe_buf_pin(struct pipe_inode_info *pipe,
Jens Axboef84d7512006-05-01 19:59:03 +020089 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +020090{
91 struct page *page = buf->page;
Jens Axboe49d0b212006-04-10 09:04:41 +020092 int err;
Jens Axboe5274f052006-03-30 15:15:30 +020093
94 if (!PageUptodate(page)) {
Jens Axboe49d0b212006-04-10 09:04:41 +020095 lock_page(page);
96
97 /*
98 * Page got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +020099 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200100 */
101 if (!page->mapping) {
102 err = -ENODATA;
103 goto error;
104 }
105
106 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200107 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200108 */
109 if (!PageUptodate(page)) {
110 err = -EIO;
111 goto error;
112 }
113
114 /*
Jens Axboef84d7512006-05-01 19:59:03 +0200115 * Page is ok afterall, we are done.
Jens Axboe49d0b212006-04-10 09:04:41 +0200116 */
Jens Axboe5274f052006-03-30 15:15:30 +0200117 unlock_page(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200118 }
119
Jens Axboef84d7512006-05-01 19:59:03 +0200120 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200121error:
122 unlock_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200123 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200124}
125
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800126static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
Jens Axboe5274f052006-03-30 15:15:30 +0200127 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200128 .map = generic_pipe_buf_map,
129 .unmap = generic_pipe_buf_unmap,
130 .pin = page_cache_pipe_buf_pin,
Jens Axboe5274f052006-03-30 15:15:30 +0200131 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200132 .steal = page_cache_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200133 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200134};
135
Jens Axboe912d35f2006-04-26 10:59:21 +0200136static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
137 struct pipe_buffer *buf)
138{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200139 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
140 return 1;
141
Jens Axboe14328732006-05-03 10:35:26 +0200142 buf->flags |= PIPE_BUF_FLAG_LRU;
Jens Axboe330ab712006-05-02 15:29:57 +0200143 return generic_pipe_buf_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200144}
145
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800146static const struct pipe_buf_operations user_page_pipe_buf_ops = {
Jens Axboe912d35f2006-04-26 10:59:21 +0200147 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200148 .map = generic_pipe_buf_map,
149 .unmap = generic_pipe_buf_unmap,
150 .pin = generic_pipe_buf_pin,
Jens Axboe912d35f2006-04-26 10:59:21 +0200151 .release = page_cache_pipe_buf_release,
152 .steal = user_page_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200153 .get = generic_pipe_buf_get,
Jens Axboe912d35f2006-04-26 10:59:21 +0200154};
155
Jens Axboe83f91352006-04-02 23:05:09 +0200156/*
Jens Axboed6b29d72007-06-04 09:59:47 +0200157 * Pipe output worker. This fills a pipe with the information contained
158 * from splice_pipe_desc().
Jens Axboe83f91352006-04-02 23:05:09 +0200159 */
Jens Axboed6b29d72007-06-04 09:59:47 +0200160ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
161 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200162{
Jens Axboe00de00b2007-06-15 13:14:22 +0200163 unsigned int spd_pages = spd->nr_pages;
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
Jens Axboe02676e52007-06-15 13:16:13 +0200232 if (pipe->inode) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200233 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200234
Jens Axboe02676e52007-06-15 13:16:13 +0200235 if (do_wakeup) {
236 smp_mb();
237 if (waitqueue_active(&pipe->wait))
238 wake_up_interruptible(&pipe->wait);
239 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
240 }
Jens Axboe5274f052006-03-30 15:15:30 +0200241 }
242
Jens Axboe00de00b2007-06-15 13:14:22 +0200243 while (page_nr < spd_pages)
Jens Axboe912d35f2006-04-26 10:59:21 +0200244 page_cache_release(spd->pages[page_nr++]);
Jens Axboe5274f052006-03-30 15:15:30 +0200245
246 return ret;
247}
248
Ingo Molnar3a326a22006-04-10 15:18:35 +0200249static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200250__generic_file_splice_read(struct file *in, loff_t *ppos,
251 struct pipe_inode_info *pipe, size_t len,
252 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200253{
254 struct address_space *mapping = in->f_mapping;
Jens Axboe912d35f2006-04-26 10:59:21 +0200255 unsigned int loff, nr_pages;
Jens Axboe16c523d2006-04-10 09:03:58 +0200256 struct page *pages[PIPE_BUFFERS];
Jens Axboe912d35f2006-04-26 10:59:21 +0200257 struct partial_page partial[PIPE_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200258 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200259 pgoff_t index, end_index;
260 loff_t isize;
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 /*
Jens Axboe86aa5ac2007-05-08 08:46:19 +0200277 * Don't try to 2nd guess the read-ahead logic, call into
278 * page_cache_readahead() like the page cache reads would do.
Jens Axboe5274f052006-03-30 15:15:30 +0200279 */
Jens Axboe86aa5ac2007-05-08 08:46:19 +0200280 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
Jens Axboe5274f052006-03-30 15:15:30 +0200281
282 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200283 * Now fill in the holes:
Jens Axboe5274f052006-03-30 15:15:30 +0200284 */
Jens Axboe7480a902006-04-11 13:52:47 +0200285 error = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200286
287 /*
288 * Lookup the (hopefully) full range of pages we need.
289 */
290 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
291
292 /*
293 * If find_get_pages_contig() returned fewer pages than we needed,
294 * allocate the rest.
295 */
296 index += spd.nr_pages;
297 while (spd.nr_pages < nr_pages) {
298 /*
299 * Page could be there, find_get_pages_contig() breaks on
300 * the first hole.
301 */
302 page = find_get_page(mapping, index);
303 if (!page) {
304 /*
Jens Axboee27dedd2006-05-01 19:59:54 +0200305 * Make sure the read-ahead engine is notified
306 * about this failure.
307 */
308 handle_ra_miss(mapping, &in->f_ra, index);
309
310 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200311 * page didn't exist, allocate one.
312 */
313 page = page_cache_alloc_cold(mapping);
314 if (!page)
315 break;
316
317 error = add_to_page_cache_lru(page, mapping, index,
Nick Piggin2ae88142006-10-28 10:38:23 -0700318 GFP_KERNEL);
Jens Axboeeb207962006-04-27 11:05:22 +0200319 if (unlikely(error)) {
320 page_cache_release(page);
Jens Axboea0548872006-05-03 10:58:22 +0200321 if (error == -EEXIST)
322 continue;
Jens Axboeeb207962006-04-27 11:05:22 +0200323 break;
324 }
325 /*
326 * add_to_page_cache() locks the page, unlock it
327 * to avoid convoluting the logic below even more.
328 */
329 unlock_page(page);
330 }
331
332 pages[spd.nr_pages++] = page;
333 index++;
334 }
335
336 /*
337 * Now loop over the map and see if we need to start IO on any
338 * pages, fill in the partial map, etc.
339 */
340 index = *ppos >> PAGE_CACHE_SHIFT;
341 nr_pages = spd.nr_pages;
342 spd.nr_pages = 0;
343 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200344 unsigned int this_len;
345
346 if (!len)
347 break;
348
349 /*
350 * this_len is the max we'll use from this page
351 */
Andrew Mortonba5f5d92006-04-25 15:33:34 +0200352 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
Jens Axboeeb207962006-04-27 11:05:22 +0200353 page = pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200354
355 /*
356 * If the page isn't uptodate, we may need to start io on it
357 */
358 if (!PageUptodate(page)) {
Jens Axboec4f895c2006-04-19 15:56:12 +0200359 /*
360 * If in nonblock mode then dont block on waiting
361 * for an in-flight io page
362 */
Fengguang Wu9ae9d682007-05-08 08:44:36 +0200363 if (flags & SPLICE_F_NONBLOCK) {
364 if (TestSetPageLocked(page))
365 break;
366 } else
367 lock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200368
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 Axboe620a3242007-06-07 09:39:42 +0200402 }
403fill_it:
404 /*
405 * i_size must be checked after PageUptodate.
406 */
407 isize = i_size_read(mapping->host);
408 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
409 if (unlikely(!isize || index > end_index))
410 break;
411
412 /*
413 * if this is the last page, see if we need to shrink
414 * the length and stop
415 */
416 if (end_index == index) {
417 unsigned int plen;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200418
419 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200420 * max good bytes in this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200421 */
Jens Axboe620a3242007-06-07 09:39:42 +0200422 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
423 if (plen <= loff)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200424 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200425
426 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200427 * force quit after adding this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200428 */
Jens Axboe620a3242007-06-07 09:39:42 +0200429 this_len = min(this_len, plen - loff);
430 len = this_len;
Jens Axboe7480a902006-04-11 13:52:47 +0200431 }
Jens Axboe620a3242007-06-07 09:39:42 +0200432
Jens Axboeeb207962006-04-27 11:05:22 +0200433 partial[page_nr].offset = loff;
434 partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200435 len -= this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200436 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200437 spd.nr_pages++;
438 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200439 }
440
Jens Axboeeb207962006-04-27 11:05:22 +0200441 /*
Hugh Dickins475ecad2007-06-07 09:36:00 +0200442 * Release any pages at the end, if we quit early. 'page_nr' is how far
Jens Axboeeb207962006-04-27 11:05:22 +0200443 * we got, 'nr_pages' is how many pages are in the map.
444 */
445 while (page_nr < nr_pages)
446 page_cache_release(pages[page_nr++]);
447
Jens Axboe912d35f2006-04-26 10:59:21 +0200448 if (spd.nr_pages)
Jens Axboe00522fb2006-04-26 14:39:29 +0200449 return splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200450
Jens Axboe7480a902006-04-11 13:52:47 +0200451 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200452}
453
Jens Axboe83f91352006-04-02 23:05:09 +0200454/**
455 * generic_file_splice_read - splice data from file to a pipe
456 * @in: file to splice from
457 * @pipe: pipe to splice to
458 * @len: number of bytes to splice
459 * @flags: splice modifier flags
460 *
461 * Will read pages from given file and fill them into a pipe.
Jens Axboe83f91352006-04-02 23:05:09 +0200462 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200463ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
464 struct pipe_inode_info *pipe, size_t len,
465 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200466{
467 ssize_t spliced;
468 int ret;
Jens Axboed366d3982007-06-01 14:54:11 +0200469 loff_t isize, left;
470
471 isize = i_size_read(in->f_mapping->host);
472 if (unlikely(*ppos >= isize))
473 return 0;
474
475 left = isize - *ppos;
476 if (unlikely(left < len))
477 len = left;
Jens Axboe5274f052006-03-30 15:15:30 +0200478
479 ret = 0;
480 spliced = 0;
481 while (len) {
Jens Axboecbb7e572006-04-11 14:57:50 +0200482 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200483
Jens Axboec4f895c2006-04-19 15:56:12 +0200484 if (ret < 0)
Jens Axboe5274f052006-03-30 15:15:30 +0200485 break;
Jens Axboec4f895c2006-04-19 15:56:12 +0200486 else if (!ret) {
487 if (spliced)
488 break;
489 if (flags & SPLICE_F_NONBLOCK) {
490 ret = -EAGAIN;
491 break;
492 }
493 }
Jens Axboe5274f052006-03-30 15:15:30 +0200494
Jens Axboecbb7e572006-04-11 14:57:50 +0200495 *ppos += ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200496 len -= ret;
497 spliced += ret;
498 }
499
500 if (spliced)
501 return spliced;
502
503 return ret;
504}
505
Jens Axboe059a8f32006-04-02 23:06:05 +0200506EXPORT_SYMBOL(generic_file_splice_read);
507
Jens Axboe5274f052006-03-30 15:15:30 +0200508/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200509 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200510 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200511 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200512static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200513 struct pipe_buffer *buf, struct splice_desc *sd)
514{
Jens Axboe6a14b902007-06-14 13:08:55 +0200515 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200516 loff_t pos = sd->pos;
Jens Axboef84d7512006-05-01 19:59:03 +0200517 int ret, more;
Jens Axboe5274f052006-03-30 15:15:30 +0200518
Jens Axboe76ad4d12006-05-03 10:41:33 +0200519 ret = buf->ops->pin(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200520 if (!ret) {
521 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200522
Jens Axboef84d7512006-05-01 19:59:03 +0200523 ret = file->f_op->sendpage(file, buf->page, buf->offset,
524 sd->len, &pos, more);
525 }
Jens Axboe5274f052006-03-30 15:15:30 +0200526
Jens Axboe016b6612006-04-25 15:42:00 +0200527 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200528}
529
530/*
531 * This is a little more tricky than the file -> pipe splicing. There are
532 * basically three cases:
533 *
534 * - Destination page already exists in the address space and there
535 * are users of it. For that case we have no other option that
536 * copying the data. Tough luck.
537 * - Destination page already exists in the address space, but there
538 * are no users of it. Make sure it's uptodate, then drop it. Fall
539 * through to last case.
540 * - Destination page does not exist, we can add the pipe page to
541 * the page cache and avoid the copy.
542 *
Jens Axboe83f91352006-04-02 23:05:09 +0200543 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
544 * sd->flags), we attempt to migrate pages from the pipe to the output
545 * file address space page cache. This is possible if no one else has
546 * the pipe page referenced outside of the pipe and page cache. If
547 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
548 * a new page in the output file page cache and fill/dirty that.
Jens Axboe5274f052006-03-30 15:15:30 +0200549 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200550static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
Jens Axboe5274f052006-03-30 15:15:30 +0200551 struct splice_desc *sd)
552{
Jens Axboe6a14b902007-06-14 13:08:55 +0200553 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200554 struct address_space *mapping = file->f_mapping;
Jens Axboe016b6612006-04-25 15:42:00 +0200555 unsigned int offset, this_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200556 struct page *page;
Jens Axboe5274f052006-03-30 15:15:30 +0200557 pgoff_t index;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200558 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200559
560 /*
Jens Axboe49d0b212006-04-10 09:04:41 +0200561 * make sure the data in this buffer is uptodate
Jens Axboe5274f052006-03-30 15:15:30 +0200562 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200563 ret = buf->ops->pin(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200564 if (unlikely(ret))
565 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200566
567 index = sd->pos >> PAGE_CACHE_SHIFT;
568 offset = sd->pos & ~PAGE_CACHE_MASK;
569
Jens Axboe016b6612006-04-25 15:42:00 +0200570 this_len = sd->len;
571 if (this_len + offset > PAGE_CACHE_SIZE)
572 this_len = PAGE_CACHE_SIZE - offset;
573
Jens Axboe5abc97a2006-03-30 15:16:46 +0200574find_page:
Nick Piggin485ddb42007-03-27 08:55:08 +0200575 page = find_lock_page(mapping, index);
576 if (!page) {
577 ret = -ENOMEM;
578 page = page_cache_alloc_cold(mapping);
579 if (unlikely(!page))
580 goto out_ret;
Jens Axboe9e0267c2006-04-19 15:57:31 +0200581
Nick Piggin485ddb42007-03-27 08:55:08 +0200582 /*
583 * This will also lock the page
584 */
585 ret = add_to_page_cache_lru(page, mapping, index,
586 GFP_KERNEL);
587 if (unlikely(ret))
588 goto out;
589 }
590
Jens Axboe016b6612006-04-25 15:42:00 +0200591 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200592 if (unlikely(ret)) {
593 loff_t isize = i_size_read(mapping->host);
594
595 if (ret != AOP_TRUNCATED_PAGE)
596 unlock_page(page);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200597 page_cache_release(page);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200598 if (ret == AOP_TRUNCATED_PAGE)
599 goto find_page;
600
601 /*
602 * prepare_write() may have instantiated a few blocks
603 * outside i_size. Trim these off again.
604 */
605 if (sd->pos + this_len > isize)
606 vmtruncate(mapping->host, isize);
607
Jens Axboee6e80f22006-10-11 10:03:09 +0200608 goto out_ret;
Jens Axboebfc4ee32006-05-03 10:35:10 +0200609 }
Jens Axboe5274f052006-03-30 15:15:30 +0200610
Jens Axboe0568b402006-05-01 19:50:48 +0200611 if (buf->page != page) {
Jens Axboef84d7512006-05-01 19:59:03 +0200612 /*
613 * Careful, ->map() uses KM_USER0!
614 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200615 char *src = buf->ops->map(pipe, buf, 1);
Jens Axboef84d7512006-05-01 19:59:03 +0200616 char *dst = kmap_atomic(page, KM_USER1);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200617
Jens Axboe016b6612006-04-25 15:42:00 +0200618 memcpy(dst + offset, src + buf->offset, this_len);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200619 flush_dcache_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200620 kunmap_atomic(dst, KM_USER1);
Jens Axboe76ad4d12006-05-03 10:41:33 +0200621 buf->ops->unmap(pipe, buf, src);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200622 }
Jens Axboe5274f052006-03-30 15:15:30 +0200623
Jens Axboe016b6612006-04-25 15:42:00 +0200624 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200625 if (ret) {
626 if (ret == AOP_TRUNCATED_PAGE) {
627 page_cache_release(page);
628 goto find_page;
629 }
630 if (ret < 0)
631 goto out;
Jens Axboe0568b402006-05-01 19:50:48 +0200632 /*
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200633 * Partial write has happened, so 'ret' already initialized by
634 * number of bytes written, Where is nothing we have to do here.
Jens Axboe0568b402006-05-01 19:50:48 +0200635 */
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200636 } else
Jens Axboe0568b402006-05-01 19:50:48 +0200637 ret = this_len;
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200638 /*
639 * Return the number of bytes written and mark page as
640 * accessed, we are now done!
641 */
642 mark_page_accessed(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200643out:
Jens Axboe0568b402006-05-01 19:50:48 +0200644 page_cache_release(page);
Jens Axboe9e0267c2006-04-19 15:57:31 +0200645 unlock_page(page);
Jens Axboee6e80f22006-10-11 10:03:09 +0200646out_ret:
Jens Axboe5274f052006-03-30 15:15:30 +0200647 return ret;
648}
649
Jens Axboe83f91352006-04-02 23:05:09 +0200650/*
651 * Pipe input worker. Most of this logic works like a regular pipe, the
652 * key here is the 'actor' worker passed in that actually moves the data
653 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
654 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200655ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
656 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200657{
Jens Axboe5274f052006-03-30 15:15:30 +0200658 int ret, do_wakeup, err;
Jens Axboe5274f052006-03-30 15:15:30 +0200659
660 ret = 0;
661 do_wakeup = 0;
662
Jens Axboe5274f052006-03-30 15:15:30 +0200663 for (;;) {
Jens Axboe6f767b02006-04-11 13:53:56 +0200664 if (pipe->nrbufs) {
665 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800666 const struct pipe_buf_operations *ops = buf->ops;
Jens Axboe5274f052006-03-30 15:15:30 +0200667
Jens Axboec66ab6f2007-06-12 21:17:17 +0200668 sd->len = buf->len;
669 if (sd->len > sd->total_len)
670 sd->len = sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200671
Jens Axboec66ab6f2007-06-12 21:17:17 +0200672 err = actor(pipe, buf, sd);
Jens Axboe016b6612006-04-25 15:42:00 +0200673 if (err <= 0) {
Jens Axboe5274f052006-03-30 15:15:30 +0200674 if (!ret && err != -ENODATA)
675 ret = err;
676
677 break;
678 }
679
Jens Axboe016b6612006-04-25 15:42:00 +0200680 ret += err;
681 buf->offset += err;
682 buf->len -= err;
683
Jens Axboec66ab6f2007-06-12 21:17:17 +0200684 sd->len -= err;
685 sd->pos += err;
686 sd->total_len -= err;
687 if (sd->len)
Jens Axboe016b6612006-04-25 15:42:00 +0200688 continue;
Ingo Molnar73d62d82006-04-11 13:57:21 +0200689
Jens Axboe5274f052006-03-30 15:15:30 +0200690 if (!buf->len) {
691 buf->ops = NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200692 ops->release(pipe, buf);
Jens Axboe6f767b02006-04-11 13:53:56 +0200693 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
694 pipe->nrbufs--;
695 if (pipe->inode)
696 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200697 }
698
Jens Axboec66ab6f2007-06-12 21:17:17 +0200699 if (!sd->total_len)
Jens Axboe5274f052006-03-30 15:15:30 +0200700 break;
701 }
702
Jens Axboe6f767b02006-04-11 13:53:56 +0200703 if (pipe->nrbufs)
Jens Axboe5274f052006-03-30 15:15:30 +0200704 continue;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200705 if (!pipe->writers)
Jens Axboe5274f052006-03-30 15:15:30 +0200706 break;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200707 if (!pipe->waiting_writers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200708 if (ret)
709 break;
710 }
711
Jens Axboec66ab6f2007-06-12 21:17:17 +0200712 if (sd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700713 if (!ret)
714 ret = -EAGAIN;
715 break;
716 }
717
Jens Axboe5274f052006-03-30 15:15:30 +0200718 if (signal_pending(current)) {
719 if (!ret)
720 ret = -ERESTARTSYS;
721 break;
722 }
723
724 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200725 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200726 if (waitqueue_active(&pipe->wait))
727 wake_up_interruptible_sync(&pipe->wait);
728 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200729 do_wakeup = 0;
730 }
731
Ingo Molnar3a326a22006-04-10 15:18:35 +0200732 pipe_wait(pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200733 }
734
Jens Axboe5274f052006-03-30 15:15:30 +0200735 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200736 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200737 if (waitqueue_active(&pipe->wait))
738 wake_up_interruptible(&pipe->wait);
739 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200740 }
741
Jens Axboe5274f052006-03-30 15:15:30 +0200742 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200743}
Mark Fasheh40bee442007-03-21 13:11:02 +0100744EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200745
Mark Fasheh6da61802006-10-17 18:43:07 +0200746ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
747 loff_t *ppos, size_t len, unsigned int flags,
748 splice_actor *actor)
749{
750 ssize_t ret;
751 struct inode *inode = out->f_mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200752 struct splice_desc sd = {
753 .total_len = len,
754 .flags = flags,
755 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200756 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200757 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200758
759 /*
760 * The actor worker might be calling ->prepare_write and
761 * ->commit_write. Most of the time, these expect i_mutex to
762 * be held. Since this may result in an ABBA deadlock with
763 * pipe->inode, we have to order lock acquiry here.
764 */
765 inode_double_lock(inode, pipe->inode);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200766 ret = __splice_from_pipe(pipe, &sd, actor);
Mark Fasheh6da61802006-10-17 18:43:07 +0200767 inode_double_unlock(inode, pipe->inode);
768
769 return ret;
770}
771
772/**
773 * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
774 * @pipe: pipe info
775 * @out: file to write to
776 * @len: number of bytes to splice
777 * @flags: splice modifier flags
778 *
779 * Will either move or copy pages (determined by @flags options) from
780 * the given pipe inode to the given file. The caller is responsible
781 * for acquiring i_mutex on both inodes.
782 *
783 */
784ssize_t
785generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
786 loff_t *ppos, size_t len, unsigned int flags)
787{
788 struct address_space *mapping = out->f_mapping;
789 struct inode *inode = mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200790 struct splice_desc sd = {
791 .total_len = len,
792 .flags = flags,
793 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200794 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200795 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200796 ssize_t ret;
797 int err;
798
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800799 err = remove_suid(out->f_path.dentry);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200800 if (unlikely(err))
801 return err;
802
Jens Axboec66ab6f2007-06-12 21:17:17 +0200803 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
Mark Fasheh6da61802006-10-17 18:43:07 +0200804 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200805 unsigned long nr_pages;
806
Mark Fasheh6da61802006-10-17 18:43:07 +0200807 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200808 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Mark Fasheh6da61802006-10-17 18:43:07 +0200809
810 /*
811 * If file or inode is SYNC and we actually wrote some data,
812 * sync it.
813 */
814 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
815 err = generic_osync_inode(inode, mapping,
816 OSYNC_METADATA|OSYNC_DATA);
817
818 if (err)
819 ret = err;
820 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200821 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Mark Fasheh6da61802006-10-17 18:43:07 +0200822 }
823
824 return ret;
825}
826
827EXPORT_SYMBOL(generic_file_splice_write_nolock);
828
Jens Axboe83f91352006-04-02 23:05:09 +0200829/**
830 * generic_file_splice_write - splice data from a pipe to a file
Ingo Molnar3a326a22006-04-10 15:18:35 +0200831 * @pipe: pipe info
Jens Axboe83f91352006-04-02 23:05:09 +0200832 * @out: file to write to
833 * @len: number of bytes to splice
834 * @flags: splice modifier flags
835 *
836 * Will either move or copy pages (determined by @flags options) from
837 * the given pipe inode to the given file.
838 *
839 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200840ssize_t
841generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200842 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200843{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200844 struct address_space *mapping = out->f_mapping;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200845 struct inode *inode = mapping->host;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200846 ssize_t ret;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200847 int err;
848
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800849 err = should_remove_suid(out->f_path.dentry);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200850 if (unlikely(err)) {
851 mutex_lock(&inode->i_mutex);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800852 err = __remove_suid(out->f_path.dentry, err);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200853 mutex_unlock(&inode->i_mutex);
854 if (err)
855 return err;
856 }
Ingo Molnar3a326a22006-04-10 15:18:35 +0200857
Jens Axboe00522fb2006-04-26 14:39:29 +0200858 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
Jens Axboea4514eb2006-04-19 15:57:05 +0200859 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200860 unsigned long nr_pages;
861
Jens Axboea4514eb2006-04-19 15:57:05 +0200862 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200863 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200864
Jens Axboea4514eb2006-04-19 15:57:05 +0200865 /*
866 * If file or inode is SYNC and we actually wrote some data,
867 * sync it.
868 */
869 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
Jens Axboea4514eb2006-04-19 15:57:05 +0200870 mutex_lock(&inode->i_mutex);
871 err = generic_osync_inode(inode, mapping,
872 OSYNC_METADATA|OSYNC_DATA);
873 mutex_unlock(&inode->i_mutex);
874
875 if (err)
876 ret = err;
877 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200878 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200879 }
880
881 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200882}
883
Jens Axboe059a8f32006-04-02 23:06:05 +0200884EXPORT_SYMBOL(generic_file_splice_write);
885
Jens Axboe83f91352006-04-02 23:05:09 +0200886/**
887 * generic_splice_sendpage - splice data from a pipe to a socket
888 * @inode: pipe inode
889 * @out: socket to write to
890 * @len: number of bytes to splice
891 * @flags: splice modifier flags
892 *
893 * Will send @len bytes from the pipe to a network socket. No data copying
894 * is involved.
895 *
896 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200897ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200898 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200899{
Jens Axboe00522fb2006-04-26 14:39:29 +0200900 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200901}
902
Jens Axboe059a8f32006-04-02 23:06:05 +0200903EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500904
Jens Axboe83f91352006-04-02 23:05:09 +0200905/*
906 * Attempt to initiate a splice from pipe to file.
907 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200908static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200909 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200910{
Jens Axboe5274f052006-03-30 15:15:30 +0200911 int ret;
912
Jens Axboe49570e92006-04-11 13:56:09 +0200913 if (unlikely(!out->f_op || !out->f_op->splice_write))
Jens Axboe5274f052006-03-30 15:15:30 +0200914 return -EINVAL;
915
Jens Axboe49570e92006-04-11 13:56:09 +0200916 if (unlikely(!(out->f_mode & FMODE_WRITE)))
Jens Axboe5274f052006-03-30 15:15:30 +0200917 return -EBADF;
918
Jens Axboecbb7e572006-04-11 14:57:50 +0200919 ret = rw_verify_area(WRITE, out, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200920 if (unlikely(ret < 0))
921 return ret;
922
Jens Axboecbb7e572006-04-11 14:57:50 +0200923 return out->f_op->splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200924}
925
Jens Axboe83f91352006-04-02 23:05:09 +0200926/*
927 * Attempt to initiate a splice from a file to a pipe.
928 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200929static long do_splice_to(struct file *in, loff_t *ppos,
930 struct pipe_inode_info *pipe, size_t len,
931 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200932{
Jens Axboe5274f052006-03-30 15:15:30 +0200933 int ret;
934
Jens Axboe49570e92006-04-11 13:56:09 +0200935 if (unlikely(!in->f_op || !in->f_op->splice_read))
Jens Axboe5274f052006-03-30 15:15:30 +0200936 return -EINVAL;
937
Jens Axboe49570e92006-04-11 13:56:09 +0200938 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200939 return -EBADF;
940
Jens Axboecbb7e572006-04-11 14:57:50 +0200941 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200942 if (unlikely(ret < 0))
943 return ret;
944
Jens Axboecbb7e572006-04-11 14:57:50 +0200945 return in->f_op->splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200946}
947
Jens Axboec66ab6f2007-06-12 21:17:17 +0200948/*
949 * Splices from an input file to an actor, using a 'direct' pipe.
950 */
951ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
952 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +0200953{
954 struct pipe_inode_info *pipe;
955 long ret, bytes;
956 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200957 size_t len;
958 int i, flags;
Jens Axboeb92ce552006-04-11 13:52:07 +0200959
960 /*
961 * We require the input being a regular file, as we don't want to
962 * randomly drop data for eg socket -> socket splicing. Use the
963 * piped splicing for that!
964 */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800965 i_mode = in->f_path.dentry->d_inode->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +0200966 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
967 return -EINVAL;
968
969 /*
970 * neither in nor out is a pipe, setup an internal pipe attached to
971 * 'out' and transfer the wanted data from 'in' to 'out' through that
972 */
973 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200974 if (unlikely(!pipe)) {
Jens Axboeb92ce552006-04-11 13:52:07 +0200975 pipe = alloc_pipe_info(NULL);
976 if (!pipe)
977 return -ENOMEM;
978
979 /*
980 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200981 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200982 * PIPE_READERS appropriately.
983 */
984 pipe->readers = 1;
985
986 current->splice_pipe = pipe;
987 }
988
989 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200990 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200991 */
992 ret = 0;
993 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200994 len = sd->total_len;
995 flags = sd->flags;
996
997 /*
998 * Don't block on output, we have to drain the direct pipe.
999 */
1000 sd->flags &= ~SPLICE_F_NONBLOCK;
Jens Axboeb92ce552006-04-11 13:52:07 +02001001
1002 while (len) {
1003 size_t read_len, max_read_len;
1004
1005 /*
1006 * Do at most PIPE_BUFFERS pages worth of transfer:
1007 */
1008 max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
1009
Jens Axboec66ab6f2007-06-12 21:17:17 +02001010 ret = do_splice_to(in, &sd->pos, pipe, max_read_len, flags);
Jens Axboeb92ce552006-04-11 13:52:07 +02001011 if (unlikely(ret < 0))
1012 goto out_release;
1013
1014 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001015 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +02001016
1017 /*
1018 * NOTE: nonblocking mode only applies to the input. We
1019 * must not do the output in nonblocking mode as then we
1020 * could get stuck data in the internal pipe:
1021 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001022 ret = actor(pipe, sd);
Jens Axboeb92ce552006-04-11 13:52:07 +02001023 if (unlikely(ret < 0))
1024 goto out_release;
1025
1026 bytes += ret;
1027 len -= ret;
1028
1029 /*
1030 * In nonblocking mode, if we got back a short read then
1031 * that was due to either an IO error or due to the
1032 * pagecache entry not being there. In the IO error case
1033 * the _next_ splice attempt will produce a clean IO error
1034 * return value (not a short read), so in both cases it's
1035 * correct to break out of the loop here:
1036 */
1037 if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
1038 break;
1039 }
1040
1041 pipe->nrbufs = pipe->curbuf = 0;
1042
1043 return bytes;
1044
1045out_release:
1046 /*
1047 * If we did an incomplete transfer we must release
1048 * the pipe buffers in question:
1049 */
1050 for (i = 0; i < PIPE_BUFFERS; i++) {
1051 struct pipe_buffer *buf = pipe->bufs + i;
1052
1053 if (buf->ops) {
1054 buf->ops->release(pipe, buf);
1055 buf->ops = NULL;
1056 }
1057 }
1058 pipe->nrbufs = pipe->curbuf = 0;
1059
1060 /*
1061 * If we transferred some data, return the number of bytes:
1062 */
1063 if (bytes > 0)
1064 return bytes;
1065
1066 return ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001067
1068}
1069EXPORT_SYMBOL(splice_direct_to_actor);
1070
1071static int direct_splice_actor(struct pipe_inode_info *pipe,
1072 struct splice_desc *sd)
1073{
Jens Axboe6a14b902007-06-14 13:08:55 +02001074 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001075
1076 return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
1077}
1078
1079long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1080 size_t len, unsigned int flags)
1081{
1082 struct splice_desc sd = {
1083 .len = len,
1084 .total_len = len,
1085 .flags = flags,
1086 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001087 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001088 };
1089 size_t ret;
1090
1091 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1092 *ppos = sd.pos;
1093 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001094}
1095
Jens Axboe83f91352006-04-02 23:05:09 +02001096/*
Jens Axboeddac0d32006-11-04 12:49:32 +01001097 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1098 * location, so checking ->i_pipe is not enough to verify that this is a
1099 * pipe.
1100 */
1101static inline struct pipe_inode_info *pipe_info(struct inode *inode)
1102{
1103 if (S_ISFIFO(inode->i_mode))
1104 return inode->i_pipe;
1105
1106 return NULL;
1107}
1108
1109/*
Jens Axboe83f91352006-04-02 23:05:09 +02001110 * Determine where to splice to/from.
1111 */
Ingo Molnar529565d2006-04-10 15:18:58 +02001112static long do_splice(struct file *in, loff_t __user *off_in,
1113 struct file *out, loff_t __user *off_out,
1114 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001115{
Ingo Molnar3a326a22006-04-10 15:18:35 +02001116 struct pipe_inode_info *pipe;
Jens Axboecbb7e572006-04-11 14:57:50 +02001117 loff_t offset, *off;
Jens Axboea4514eb2006-04-19 15:57:05 +02001118 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001119
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001120 pipe = pipe_info(in->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001121 if (pipe) {
1122 if (off_in)
1123 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001124 if (off_out) {
1125 if (out->f_op->llseek == no_llseek)
1126 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001127 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001128 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001129 off = &offset;
1130 } else
1131 off = &out->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001132
Jens Axboea4514eb2006-04-19 15:57:05 +02001133 ret = do_splice_from(pipe, out, off, len, flags);
1134
1135 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1136 ret = -EFAULT;
1137
1138 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001139 }
Jens Axboe5274f052006-03-30 15:15:30 +02001140
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001141 pipe = pipe_info(out->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001142 if (pipe) {
1143 if (off_out)
1144 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001145 if (off_in) {
1146 if (in->f_op->llseek == no_llseek)
1147 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001148 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001149 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001150 off = &offset;
1151 } else
1152 off = &in->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001153
Jens Axboea4514eb2006-04-19 15:57:05 +02001154 ret = do_splice_to(in, off, pipe, len, flags);
1155
1156 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1157 ret = -EFAULT;
1158
1159 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001160 }
Jens Axboe5274f052006-03-30 15:15:30 +02001161
1162 return -EINVAL;
1163}
1164
Jens Axboe912d35f2006-04-26 10:59:21 +02001165/*
1166 * Map an iov into an array of pages and offset/length tupples. With the
1167 * partial_page structure, we can map several non-contiguous ranges into
1168 * our ones pages[] map instead of splitting that operation into pieces.
1169 * Could easily be exported as a generic helper for other users, in which
1170 * case one would probably want to add a 'max_nr_pages' parameter as well.
1171 */
1172static int get_iovec_page_array(const struct iovec __user *iov,
1173 unsigned int nr_vecs, struct page **pages,
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001174 struct partial_page *partial, int aligned)
Jens Axboe912d35f2006-04-26 10:59:21 +02001175{
1176 int buffers = 0, error = 0;
1177
1178 /*
1179 * It's ok to take the mmap_sem for reading, even
1180 * across a "get_user()".
1181 */
1182 down_read(&current->mm->mmap_sem);
1183
1184 while (nr_vecs) {
1185 unsigned long off, npages;
1186 void __user *base;
1187 size_t len;
1188 int i;
1189
1190 /*
1191 * Get user address base and length for this iovec.
1192 */
1193 error = get_user(base, &iov->iov_base);
1194 if (unlikely(error))
1195 break;
1196 error = get_user(len, &iov->iov_len);
1197 if (unlikely(error))
1198 break;
1199
1200 /*
1201 * Sanity check this iovec. 0 read succeeds.
1202 */
1203 if (unlikely(!len))
1204 break;
1205 error = -EFAULT;
1206 if (unlikely(!base))
1207 break;
1208
1209 /*
1210 * Get this base offset and number of pages, then map
1211 * in the user pages.
1212 */
1213 off = (unsigned long) base & ~PAGE_MASK;
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001214
1215 /*
1216 * If asked for alignment, the offset must be zero and the
1217 * length a multiple of the PAGE_SIZE.
1218 */
1219 error = -EINVAL;
1220 if (aligned && (off || len & ~PAGE_MASK))
1221 break;
1222
Jens Axboe912d35f2006-04-26 10:59:21 +02001223 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1224 if (npages > PIPE_BUFFERS - buffers)
1225 npages = PIPE_BUFFERS - buffers;
1226
1227 error = get_user_pages(current, current->mm,
1228 (unsigned long) base, npages, 0, 0,
1229 &pages[buffers], NULL);
1230
1231 if (unlikely(error <= 0))
1232 break;
1233
1234 /*
1235 * Fill this contiguous range into the partial page map.
1236 */
1237 for (i = 0; i < error; i++) {
Jens Axboe75914892006-05-02 12:57:18 +02001238 const int plen = min_t(size_t, len, PAGE_SIZE - off);
Jens Axboe912d35f2006-04-26 10:59:21 +02001239
1240 partial[buffers].offset = off;
1241 partial[buffers].len = plen;
1242
1243 off = 0;
1244 len -= plen;
1245 buffers++;
1246 }
1247
1248 /*
1249 * We didn't complete this iov, stop here since it probably
1250 * means we have to move some of this into a pipe to
1251 * be able to continue.
1252 */
1253 if (len)
1254 break;
1255
1256 /*
1257 * Don't continue if we mapped fewer pages than we asked for,
1258 * or if we mapped the max number of pages that we have
1259 * room for.
1260 */
1261 if (error < npages || buffers == PIPE_BUFFERS)
1262 break;
1263
1264 nr_vecs--;
1265 iov++;
1266 }
1267
1268 up_read(&current->mm->mmap_sem);
1269
1270 if (buffers)
1271 return buffers;
1272
1273 return error;
1274}
1275
Jens Axboe6a14b902007-06-14 13:08:55 +02001276static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1277 struct splice_desc *sd)
1278{
1279 char *src;
1280 int ret;
1281
1282 ret = buf->ops->pin(pipe, buf);
1283 if (unlikely(ret))
1284 return ret;
1285
1286 /*
1287 * See if we can use the atomic maps, by prefaulting in the
1288 * pages and doing an atomic copy
1289 */
1290 if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
1291 src = buf->ops->map(pipe, buf, 1);
1292 ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
1293 sd->len);
1294 buf->ops->unmap(pipe, buf, src);
1295 if (!ret) {
1296 ret = sd->len;
1297 goto out;
1298 }
1299 }
1300
1301 /*
1302 * No dice, use slow non-atomic map and copy
1303 */
1304 src = buf->ops->map(pipe, buf, 0);
1305
1306 ret = sd->len;
1307 if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
1308 ret = -EFAULT;
1309
1310out:
1311 if (ret > 0)
1312 sd->u.userptr += ret;
1313 buf->ops->unmap(pipe, buf, src);
1314 return ret;
1315}
1316
1317/*
1318 * For lack of a better implementation, implement vmsplice() to userspace
1319 * as a simple copy of the pipes pages to the user iov.
1320 */
1321static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
1322 unsigned long nr_segs, unsigned int flags)
1323{
1324 struct pipe_inode_info *pipe;
1325 struct splice_desc sd;
1326 ssize_t size;
1327 int error;
1328 long ret;
1329
1330 pipe = pipe_info(file->f_path.dentry->d_inode);
1331 if (!pipe)
1332 return -EBADF;
1333
1334 if (pipe->inode)
1335 mutex_lock(&pipe->inode->i_mutex);
1336
1337 error = ret = 0;
1338 while (nr_segs) {
1339 void __user *base;
1340 size_t len;
1341
1342 /*
1343 * Get user address base and length for this iovec.
1344 */
1345 error = get_user(base, &iov->iov_base);
1346 if (unlikely(error))
1347 break;
1348 error = get_user(len, &iov->iov_len);
1349 if (unlikely(error))
1350 break;
1351
1352 /*
1353 * Sanity check this iovec. 0 read succeeds.
1354 */
1355 if (unlikely(!len))
1356 break;
1357 if (unlikely(!base)) {
1358 error = -EFAULT;
1359 break;
1360 }
1361
1362 sd.len = 0;
1363 sd.total_len = len;
1364 sd.flags = flags;
1365 sd.u.userptr = base;
1366 sd.pos = 0;
1367
1368 size = __splice_from_pipe(pipe, &sd, pipe_to_user);
1369 if (size < 0) {
1370 if (!ret)
1371 ret = size;
1372
1373 break;
1374 }
1375
1376 ret += size;
1377
1378 if (size < len)
1379 break;
1380
1381 nr_segs--;
1382 iov++;
1383 }
1384
1385 if (pipe->inode)
1386 mutex_unlock(&pipe->inode->i_mutex);
1387
1388 if (!ret)
1389 ret = error;
1390
1391 return ret;
1392}
1393
Jens Axboe912d35f2006-04-26 10:59:21 +02001394/*
1395 * vmsplice splices a user address range into a pipe. It can be thought of
1396 * as splice-from-memory, where the regular splice is splice-from-file (or
1397 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001398 */
Jens Axboe6a14b902007-06-14 13:08:55 +02001399static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
1400 unsigned long nr_segs, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001401{
Jens Axboeddac0d32006-11-04 12:49:32 +01001402 struct pipe_inode_info *pipe;
Jens Axboe912d35f2006-04-26 10:59:21 +02001403 struct page *pages[PIPE_BUFFERS];
1404 struct partial_page partial[PIPE_BUFFERS];
1405 struct splice_pipe_desc spd = {
1406 .pages = pages,
1407 .partial = partial,
1408 .flags = flags,
1409 .ops = &user_page_pipe_buf_ops,
1410 };
1411
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001412 pipe = pipe_info(file->f_path.dentry->d_inode);
Jens Axboeddac0d32006-11-04 12:49:32 +01001413 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001414 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001415
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001416 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1417 flags & SPLICE_F_GIFT);
Jens Axboe912d35f2006-04-26 10:59:21 +02001418 if (spd.nr_pages <= 0)
1419 return spd.nr_pages;
1420
Jens Axboe00522fb2006-04-26 14:39:29 +02001421 return splice_to_pipe(pipe, &spd);
Jens Axboe912d35f2006-04-26 10:59:21 +02001422}
1423
Jens Axboe6a14b902007-06-14 13:08:55 +02001424/*
1425 * Note that vmsplice only really supports true splicing _from_ user memory
1426 * to a pipe, not the other way around. Splicing from user memory is a simple
1427 * operation that can be supported without any funky alignment restrictions
1428 * or nasty vm tricks. We simply map in the user memory and fill them into
1429 * a pipe. The reverse isn't quite as easy, though. There are two possible
1430 * solutions for that:
1431 *
1432 * - memcpy() the data internally, at which point we might as well just
1433 * do a regular read() on the buffer anyway.
1434 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1435 * has restriction limitations on both ends of the pipe).
1436 *
1437 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1438 *
1439 */
Jens Axboe912d35f2006-04-26 10:59:21 +02001440asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1441 unsigned long nr_segs, unsigned int flags)
1442{
1443 struct file *file;
1444 long error;
1445 int fput;
1446
Jens Axboe6a14b902007-06-14 13:08:55 +02001447 if (unlikely(nr_segs > UIO_MAXIOV))
1448 return -EINVAL;
1449 else if (unlikely(!nr_segs))
1450 return 0;
1451
Jens Axboe912d35f2006-04-26 10:59:21 +02001452 error = -EBADF;
1453 file = fget_light(fd, &fput);
1454 if (file) {
1455 if (file->f_mode & FMODE_WRITE)
Jens Axboe6a14b902007-06-14 13:08:55 +02001456 error = vmsplice_to_pipe(file, iov, nr_segs, flags);
1457 else if (file->f_mode & FMODE_READ)
1458 error = vmsplice_to_user(file, iov, nr_segs, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001459
1460 fput_light(file, fput);
1461 }
1462
1463 return error;
1464}
1465
Ingo Molnar529565d2006-04-10 15:18:58 +02001466asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1467 int fd_out, loff_t __user *off_out,
1468 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001469{
1470 long error;
1471 struct file *in, *out;
1472 int fput_in, fput_out;
1473
1474 if (unlikely(!len))
1475 return 0;
1476
1477 error = -EBADF;
Ingo Molnar529565d2006-04-10 15:18:58 +02001478 in = fget_light(fd_in, &fput_in);
Jens Axboe5274f052006-03-30 15:15:30 +02001479 if (in) {
1480 if (in->f_mode & FMODE_READ) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001481 out = fget_light(fd_out, &fput_out);
Jens Axboe5274f052006-03-30 15:15:30 +02001482 if (out) {
1483 if (out->f_mode & FMODE_WRITE)
Ingo Molnar529565d2006-04-10 15:18:58 +02001484 error = do_splice(in, off_in,
1485 out, off_out,
1486 len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001487 fput_light(out, fput_out);
1488 }
1489 }
1490
1491 fput_light(in, fput_in);
1492 }
1493
1494 return error;
1495}
Jens Axboe70524492006-04-11 15:51:17 +02001496
1497/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001498 * Make sure there's data to read. Wait for input if we can, otherwise
1499 * return an appropriate error.
1500 */
1501static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1502{
1503 int ret;
1504
1505 /*
1506 * Check ->nrbufs without the inode lock first. This function
1507 * is speculative anyways, so missing one is ok.
1508 */
1509 if (pipe->nrbufs)
1510 return 0;
1511
1512 ret = 0;
1513 mutex_lock(&pipe->inode->i_mutex);
1514
1515 while (!pipe->nrbufs) {
1516 if (signal_pending(current)) {
1517 ret = -ERESTARTSYS;
1518 break;
1519 }
1520 if (!pipe->writers)
1521 break;
1522 if (!pipe->waiting_writers) {
1523 if (flags & SPLICE_F_NONBLOCK) {
1524 ret = -EAGAIN;
1525 break;
1526 }
1527 }
1528 pipe_wait(pipe);
1529 }
1530
1531 mutex_unlock(&pipe->inode->i_mutex);
1532 return ret;
1533}
1534
1535/*
1536 * Make sure there's writeable room. Wait for room if we can, otherwise
1537 * return an appropriate error.
1538 */
1539static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1540{
1541 int ret;
1542
1543 /*
1544 * Check ->nrbufs without the inode lock first. This function
1545 * is speculative anyways, so missing one is ok.
1546 */
1547 if (pipe->nrbufs < PIPE_BUFFERS)
1548 return 0;
1549
1550 ret = 0;
1551 mutex_lock(&pipe->inode->i_mutex);
1552
1553 while (pipe->nrbufs >= PIPE_BUFFERS) {
1554 if (!pipe->readers) {
1555 send_sig(SIGPIPE, current, 0);
1556 ret = -EPIPE;
1557 break;
1558 }
1559 if (flags & SPLICE_F_NONBLOCK) {
1560 ret = -EAGAIN;
1561 break;
1562 }
1563 if (signal_pending(current)) {
1564 ret = -ERESTARTSYS;
1565 break;
1566 }
1567 pipe->waiting_writers++;
1568 pipe_wait(pipe);
1569 pipe->waiting_writers--;
1570 }
1571
1572 mutex_unlock(&pipe->inode->i_mutex);
1573 return ret;
1574}
1575
1576/*
Jens Axboe70524492006-04-11 15:51:17 +02001577 * Link contents of ipipe to opipe.
1578 */
1579static int link_pipe(struct pipe_inode_info *ipipe,
1580 struct pipe_inode_info *opipe,
1581 size_t len, unsigned int flags)
1582{
1583 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001584 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001585
1586 /*
1587 * Potential ABBA deadlock, work around it by ordering lock
1588 * grabbing by inode address. Otherwise two different processes
1589 * could deadlock (one doing tee from A -> B, the other from B -> A).
1590 */
Mark Fasheh62752ee2006-10-17 10:31:38 +02001591 inode_double_lock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001592
Jens Axboeaadd06e2006-07-10 11:00:01 +02001593 do {
Jens Axboe70524492006-04-11 15:51:17 +02001594 if (!opipe->readers) {
1595 send_sig(SIGPIPE, current, 0);
1596 if (!ret)
1597 ret = -EPIPE;
1598 break;
1599 }
Jens Axboe70524492006-04-11 15:51:17 +02001600
1601 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001602 * If we have iterated all input buffers or ran out of
1603 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001604 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001605 if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
Jens Axboe70524492006-04-11 15:51:17 +02001606 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001607
1608 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1609 nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1610
Jens Axboe2a272502006-04-19 15:56:40 +02001611 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001612 * Get a reference to this pipe buffer,
1613 * so we can copy the contents over.
Jens Axboe2a272502006-04-19 15:56:40 +02001614 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001615 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001616
Jens Axboeaadd06e2006-07-10 11:00:01 +02001617 obuf = opipe->bufs + nbuf;
1618 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001619
Jens Axboeaadd06e2006-07-10 11:00:01 +02001620 /*
1621 * Don't inherit the gift flag, we need to
1622 * prevent multiple steals of this page.
1623 */
1624 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1625
1626 if (obuf->len > len)
1627 obuf->len = len;
1628
1629 opipe->nrbufs++;
1630 ret += obuf->len;
1631 len -= obuf->len;
1632 i++;
1633 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001634
Mark Fasheh62752ee2006-10-17 10:31:38 +02001635 inode_double_unlock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001636
Jens Axboeaadd06e2006-07-10 11:00:01 +02001637 /*
1638 * If we put data in the output pipe, wakeup any potential readers.
1639 */
1640 if (ret > 0) {
Jens Axboe70524492006-04-11 15:51:17 +02001641 smp_mb();
1642 if (waitqueue_active(&opipe->wait))
1643 wake_up_interruptible(&opipe->wait);
1644 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1645 }
1646
1647 return ret;
1648}
1649
1650/*
1651 * This is a tee(1) implementation that works on pipes. It doesn't copy
1652 * any data, it simply references the 'in' pages on the 'out' pipe.
1653 * The 'flags' used are the SPLICE_F_* variants, currently the only
1654 * applicable one is SPLICE_F_NONBLOCK.
1655 */
1656static long do_tee(struct file *in, struct file *out, size_t len,
1657 unsigned int flags)
1658{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001659 struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
1660 struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001661 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001662
1663 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001664 * Duplicate the contents of ipipe to opipe without actually
1665 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001666 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001667 if (ipipe && opipe && ipipe != opipe) {
1668 /*
1669 * Keep going, unless we encounter an error. The ipipe/opipe
1670 * ordering doesn't really matter.
1671 */
1672 ret = link_ipipe_prep(ipipe, flags);
1673 if (!ret) {
1674 ret = link_opipe_prep(opipe, flags);
1675 if (!ret) {
1676 ret = link_pipe(ipipe, opipe, len, flags);
1677 if (!ret && (flags & SPLICE_F_NONBLOCK))
1678 ret = -EAGAIN;
1679 }
1680 }
1681 }
Jens Axboe70524492006-04-11 15:51:17 +02001682
Jens Axboeaadd06e2006-07-10 11:00:01 +02001683 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001684}
1685
1686asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1687{
1688 struct file *in;
1689 int error, fput_in;
1690
1691 if (unlikely(!len))
1692 return 0;
1693
1694 error = -EBADF;
1695 in = fget_light(fdin, &fput_in);
1696 if (in) {
1697 if (in->f_mode & FMODE_READ) {
1698 int fput_out;
1699 struct file *out = fget_light(fdout, &fput_out);
1700
1701 if (out) {
1702 if (out->f_mode & FMODE_WRITE)
1703 error = do_tee(in, out, len, flags);
1704 fput_light(out, fput_out);
1705 }
1706 }
1707 fput_light(in, fput_in);
1708 }
1709
1710 return error;
1711}