blob: 00850e56280db9c972b6997c9c44b7f4b578e0a4 [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;
Jens Axboe497f9622007-06-11 12:00:45 +0200188 buf->private = spd->partial[page_nr].private;
Jens Axboe912d35f2006-04-26 10:59:21 +0200189 buf->ops = spd->ops;
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200190 if (spd->flags & SPLICE_F_GIFT)
191 buf->flags |= PIPE_BUF_FLAG_GIFT;
192
Jens Axboe6f767b02006-04-11 13:53:56 +0200193 pipe->nrbufs++;
Jens Axboe912d35f2006-04-26 10:59:21 +0200194 page_nr++;
195 ret += buf->len;
196
Jens Axboe6f767b02006-04-11 13:53:56 +0200197 if (pipe->inode)
198 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200199
Jens Axboe912d35f2006-04-26 10:59:21 +0200200 if (!--spd->nr_pages)
Jens Axboe5274f052006-03-30 15:15:30 +0200201 break;
Jens Axboe6f767b02006-04-11 13:53:56 +0200202 if (pipe->nrbufs < PIPE_BUFFERS)
Jens Axboe5274f052006-03-30 15:15:30 +0200203 continue;
204
205 break;
206 }
207
Jens Axboe912d35f2006-04-26 10:59:21 +0200208 if (spd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700209 if (!ret)
210 ret = -EAGAIN;
211 break;
212 }
213
Jens Axboe5274f052006-03-30 15:15:30 +0200214 if (signal_pending(current)) {
215 if (!ret)
216 ret = -ERESTARTSYS;
217 break;
218 }
219
220 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200221 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200222 if (waitqueue_active(&pipe->wait))
223 wake_up_interruptible_sync(&pipe->wait);
224 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Jens Axboe5274f052006-03-30 15:15:30 +0200225 do_wakeup = 0;
226 }
227
Ingo Molnar3a326a22006-04-10 15:18:35 +0200228 pipe->waiting_writers++;
229 pipe_wait(pipe);
230 pipe->waiting_writers--;
Jens Axboe5274f052006-03-30 15:15:30 +0200231 }
232
Jens Axboe02676e52007-06-15 13:16:13 +0200233 if (pipe->inode) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200234 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200235
Jens Axboe02676e52007-06-15 13:16:13 +0200236 if (do_wakeup) {
237 smp_mb();
238 if (waitqueue_active(&pipe->wait))
239 wake_up_interruptible(&pipe->wait);
240 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
241 }
Jens Axboe5274f052006-03-30 15:15:30 +0200242 }
243
Jens Axboe00de00b2007-06-15 13:14:22 +0200244 while (page_nr < spd_pages)
Jens Axboe912d35f2006-04-26 10:59:21 +0200245 page_cache_release(spd->pages[page_nr++]);
Jens Axboe5274f052006-03-30 15:15:30 +0200246
247 return ret;
248}
249
Ingo Molnar3a326a22006-04-10 15:18:35 +0200250static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200251__generic_file_splice_read(struct file *in, loff_t *ppos,
252 struct pipe_inode_info *pipe, size_t len,
253 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200254{
255 struct address_space *mapping = in->f_mapping;
Jens Axboe912d35f2006-04-26 10:59:21 +0200256 unsigned int loff, nr_pages;
Jens Axboe16c523d2006-04-10 09:03:58 +0200257 struct page *pages[PIPE_BUFFERS];
Jens Axboe912d35f2006-04-26 10:59:21 +0200258 struct partial_page partial[PIPE_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200259 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200260 pgoff_t index, end_index;
261 loff_t isize;
Jens Axboeeb207962006-04-27 11:05:22 +0200262 int error, page_nr;
Jens Axboe912d35f2006-04-26 10:59:21 +0200263 struct splice_pipe_desc spd = {
264 .pages = pages,
265 .partial = partial,
266 .flags = flags,
267 .ops = &page_cache_pipe_buf_ops,
268 };
Jens Axboe5274f052006-03-30 15:15:30 +0200269
Jens Axboecbb7e572006-04-11 14:57:50 +0200270 index = *ppos >> PAGE_CACHE_SHIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +0200271 loff = *ppos & ~PAGE_CACHE_MASK;
272 nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe5274f052006-03-30 15:15:30 +0200273
274 if (nr_pages > PIPE_BUFFERS)
275 nr_pages = PIPE_BUFFERS;
276
277 /*
Jens Axboe86aa5ac2007-05-08 08:46:19 +0200278 * Don't try to 2nd guess the read-ahead logic, call into
279 * page_cache_readahead() like the page cache reads would do.
Jens Axboe5274f052006-03-30 15:15:30 +0200280 */
Jens Axboe86aa5ac2007-05-08 08:46:19 +0200281 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
Jens Axboe5274f052006-03-30 15:15:30 +0200282
283 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200284 * Now fill in the holes:
Jens Axboe5274f052006-03-30 15:15:30 +0200285 */
Jens Axboe7480a902006-04-11 13:52:47 +0200286 error = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200287
288 /*
289 * Lookup the (hopefully) full range of pages we need.
290 */
291 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
292
293 /*
294 * If find_get_pages_contig() returned fewer pages than we needed,
295 * allocate the rest.
296 */
297 index += spd.nr_pages;
298 while (spd.nr_pages < nr_pages) {
299 /*
300 * Page could be there, find_get_pages_contig() breaks on
301 * the first hole.
302 */
303 page = find_get_page(mapping, index);
304 if (!page) {
305 /*
Jens Axboee27dedd2006-05-01 19:59:54 +0200306 * Make sure the read-ahead engine is notified
307 * about this failure.
308 */
309 handle_ra_miss(mapping, &in->f_ra, index);
310
311 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200312 * page didn't exist, allocate one.
313 */
314 page = page_cache_alloc_cold(mapping);
315 if (!page)
316 break;
317
318 error = add_to_page_cache_lru(page, mapping, index,
Nick Piggin2ae88142006-10-28 10:38:23 -0700319 GFP_KERNEL);
Jens Axboeeb207962006-04-27 11:05:22 +0200320 if (unlikely(error)) {
321 page_cache_release(page);
Jens Axboea0548872006-05-03 10:58:22 +0200322 if (error == -EEXIST)
323 continue;
Jens Axboeeb207962006-04-27 11:05:22 +0200324 break;
325 }
326 /*
327 * add_to_page_cache() locks the page, unlock it
328 * to avoid convoluting the logic below even more.
329 */
330 unlock_page(page);
331 }
332
333 pages[spd.nr_pages++] = page;
334 index++;
335 }
336
337 /*
338 * Now loop over the map and see if we need to start IO on any
339 * pages, fill in the partial map, etc.
340 */
341 index = *ppos >> PAGE_CACHE_SHIFT;
342 nr_pages = spd.nr_pages;
343 spd.nr_pages = 0;
344 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200345 unsigned int this_len;
346
347 if (!len)
348 break;
349
350 /*
351 * this_len is the max we'll use from this page
352 */
Andrew Mortonba5f5d92006-04-25 15:33:34 +0200353 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
Jens Axboeeb207962006-04-27 11:05:22 +0200354 page = pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200355
356 /*
357 * If the page isn't uptodate, we may need to start io on it
358 */
359 if (!PageUptodate(page)) {
Jens Axboec4f895c2006-04-19 15:56:12 +0200360 /*
361 * If in nonblock mode then dont block on waiting
362 * for an in-flight io page
363 */
Fengguang Wu9ae9d682007-05-08 08:44:36 +0200364 if (flags & SPLICE_F_NONBLOCK) {
365 if (TestSetPageLocked(page))
366 break;
367 } else
368 lock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200369
370 /*
371 * page was truncated, stop here. if this isn't the
372 * first page, we'll just complete what we already
373 * added
374 */
375 if (!page->mapping) {
376 unlock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200377 break;
378 }
379 /*
380 * page was already under io and is now done, great
381 */
382 if (PageUptodate(page)) {
383 unlock_page(page);
384 goto fill_it;
385 }
386
Jens Axboe7480a902006-04-11 13:52:47 +0200387 /*
388 * need to read in the page
389 */
390 error = mapping->a_ops->readpage(in, page);
Jens Axboe7480a902006-04-11 13:52:47 +0200391 if (unlikely(error)) {
Jens Axboeeb207962006-04-27 11:05:22 +0200392 /*
393 * We really should re-lookup the page here,
394 * but it complicates things a lot. Instead
395 * lets just do what we already stored, and
396 * we'll get it the next time we are called.
397 */
Jens Axboe7480a902006-04-11 13:52:47 +0200398 if (error == AOP_TRUNCATED_PAGE)
Jens Axboeeb207962006-04-27 11:05:22 +0200399 error = 0;
400
Jens Axboe7480a902006-04-11 13:52:47 +0200401 break;
402 }
Jens Axboe620a3242007-06-07 09:39:42 +0200403 }
404fill_it:
405 /*
406 * i_size must be checked after PageUptodate.
407 */
408 isize = i_size_read(mapping->host);
409 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
410 if (unlikely(!isize || index > end_index))
411 break;
412
413 /*
414 * if this is the last page, see if we need to shrink
415 * the length and stop
416 */
417 if (end_index == index) {
418 unsigned int plen;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200419
420 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200421 * max good bytes in this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200422 */
Jens Axboe620a3242007-06-07 09:39:42 +0200423 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
424 if (plen <= loff)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200425 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200426
427 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200428 * force quit after adding this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200429 */
Jens Axboe620a3242007-06-07 09:39:42 +0200430 this_len = min(this_len, plen - loff);
431 len = this_len;
Jens Axboe7480a902006-04-11 13:52:47 +0200432 }
Jens Axboe620a3242007-06-07 09:39:42 +0200433
Jens Axboeeb207962006-04-27 11:05:22 +0200434 partial[page_nr].offset = loff;
435 partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200436 len -= this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200437 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200438 spd.nr_pages++;
439 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200440 }
441
Jens Axboeeb207962006-04-27 11:05:22 +0200442 /*
Hugh Dickins475ecad2007-06-07 09:36:00 +0200443 * Release any pages at the end, if we quit early. 'page_nr' is how far
Jens Axboeeb207962006-04-27 11:05:22 +0200444 * we got, 'nr_pages' is how many pages are in the map.
445 */
446 while (page_nr < nr_pages)
447 page_cache_release(pages[page_nr++]);
448
Jens Axboe912d35f2006-04-26 10:59:21 +0200449 if (spd.nr_pages)
Jens Axboe00522fb2006-04-26 14:39:29 +0200450 return splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200451
Jens Axboe7480a902006-04-11 13:52:47 +0200452 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200453}
454
Jens Axboe83f91352006-04-02 23:05:09 +0200455/**
456 * generic_file_splice_read - splice data from file to a pipe
457 * @in: file to splice from
458 * @pipe: pipe to splice to
459 * @len: number of bytes to splice
460 * @flags: splice modifier flags
461 *
462 * Will read pages from given file and fill them into a pipe.
Jens Axboe83f91352006-04-02 23:05:09 +0200463 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200464ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
465 struct pipe_inode_info *pipe, size_t len,
466 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200467{
468 ssize_t spliced;
469 int ret;
Jens Axboed366d3982007-06-01 14:54:11 +0200470 loff_t isize, left;
471
472 isize = i_size_read(in->f_mapping->host);
473 if (unlikely(*ppos >= isize))
474 return 0;
475
476 left = isize - *ppos;
477 if (unlikely(left < len))
478 len = left;
Jens Axboe5274f052006-03-30 15:15:30 +0200479
480 ret = 0;
481 spliced = 0;
482 while (len) {
Jens Axboecbb7e572006-04-11 14:57:50 +0200483 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200484
Jens Axboec4f895c2006-04-19 15:56:12 +0200485 if (ret < 0)
Jens Axboe5274f052006-03-30 15:15:30 +0200486 break;
Jens Axboec4f895c2006-04-19 15:56:12 +0200487 else if (!ret) {
488 if (spliced)
489 break;
490 if (flags & SPLICE_F_NONBLOCK) {
491 ret = -EAGAIN;
492 break;
493 }
494 }
Jens Axboe5274f052006-03-30 15:15:30 +0200495
Jens Axboecbb7e572006-04-11 14:57:50 +0200496 *ppos += ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200497 len -= ret;
498 spliced += ret;
499 }
500
501 if (spliced)
502 return spliced;
503
504 return ret;
505}
506
Jens Axboe059a8f32006-04-02 23:06:05 +0200507EXPORT_SYMBOL(generic_file_splice_read);
508
Jens Axboe5274f052006-03-30 15:15:30 +0200509/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200510 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200511 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200512 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200513static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200514 struct pipe_buffer *buf, struct splice_desc *sd)
515{
Jens Axboe6a14b902007-06-14 13:08:55 +0200516 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200517 loff_t pos = sd->pos;
Jens Axboef84d7512006-05-01 19:59:03 +0200518 int ret, more;
Jens Axboe5274f052006-03-30 15:15:30 +0200519
Jens Axboe76ad4d12006-05-03 10:41:33 +0200520 ret = buf->ops->pin(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200521 if (!ret) {
522 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200523
Jens Axboef84d7512006-05-01 19:59:03 +0200524 ret = file->f_op->sendpage(file, buf->page, buf->offset,
525 sd->len, &pos, more);
526 }
Jens Axboe5274f052006-03-30 15:15:30 +0200527
Jens Axboe016b6612006-04-25 15:42:00 +0200528 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200529}
530
531/*
532 * This is a little more tricky than the file -> pipe splicing. There are
533 * basically three cases:
534 *
535 * - Destination page already exists in the address space and there
536 * are users of it. For that case we have no other option that
537 * copying the data. Tough luck.
538 * - Destination page already exists in the address space, but there
539 * are no users of it. Make sure it's uptodate, then drop it. Fall
540 * through to last case.
541 * - Destination page does not exist, we can add the pipe page to
542 * the page cache and avoid the copy.
543 *
Jens Axboe83f91352006-04-02 23:05:09 +0200544 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
545 * sd->flags), we attempt to migrate pages from the pipe to the output
546 * file address space page cache. This is possible if no one else has
547 * the pipe page referenced outside of the pipe and page cache. If
548 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
549 * a new page in the output file page cache and fill/dirty that.
Jens Axboe5274f052006-03-30 15:15:30 +0200550 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200551static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
Jens Axboe5274f052006-03-30 15:15:30 +0200552 struct splice_desc *sd)
553{
Jens Axboe6a14b902007-06-14 13:08:55 +0200554 struct file *file = sd->u.file;
Jens Axboe5274f052006-03-30 15:15:30 +0200555 struct address_space *mapping = file->f_mapping;
Jens Axboe016b6612006-04-25 15:42:00 +0200556 unsigned int offset, this_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200557 struct page *page;
Jens Axboe5274f052006-03-30 15:15:30 +0200558 pgoff_t index;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200559 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200560
561 /*
Jens Axboe49d0b212006-04-10 09:04:41 +0200562 * make sure the data in this buffer is uptodate
Jens Axboe5274f052006-03-30 15:15:30 +0200563 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200564 ret = buf->ops->pin(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200565 if (unlikely(ret))
566 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200567
568 index = sd->pos >> PAGE_CACHE_SHIFT;
569 offset = sd->pos & ~PAGE_CACHE_MASK;
570
Jens Axboe016b6612006-04-25 15:42:00 +0200571 this_len = sd->len;
572 if (this_len + offset > PAGE_CACHE_SIZE)
573 this_len = PAGE_CACHE_SIZE - offset;
574
Jens Axboe5abc97a2006-03-30 15:16:46 +0200575find_page:
Nick Piggin485ddb42007-03-27 08:55:08 +0200576 page = find_lock_page(mapping, index);
577 if (!page) {
578 ret = -ENOMEM;
579 page = page_cache_alloc_cold(mapping);
580 if (unlikely(!page))
581 goto out_ret;
Jens Axboe9e0267c2006-04-19 15:57:31 +0200582
Nick Piggin485ddb42007-03-27 08:55:08 +0200583 /*
584 * This will also lock the page
585 */
586 ret = add_to_page_cache_lru(page, mapping, index,
587 GFP_KERNEL);
588 if (unlikely(ret))
589 goto out;
590 }
591
Jens Axboe016b6612006-04-25 15:42:00 +0200592 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200593 if (unlikely(ret)) {
594 loff_t isize = i_size_read(mapping->host);
595
596 if (ret != AOP_TRUNCATED_PAGE)
597 unlock_page(page);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200598 page_cache_release(page);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200599 if (ret == AOP_TRUNCATED_PAGE)
600 goto find_page;
601
602 /*
603 * prepare_write() may have instantiated a few blocks
604 * outside i_size. Trim these off again.
605 */
606 if (sd->pos + this_len > isize)
607 vmtruncate(mapping->host, isize);
608
Jens Axboee6e80f22006-10-11 10:03:09 +0200609 goto out_ret;
Jens Axboebfc4ee32006-05-03 10:35:10 +0200610 }
Jens Axboe5274f052006-03-30 15:15:30 +0200611
Jens Axboe0568b402006-05-01 19:50:48 +0200612 if (buf->page != page) {
Jens Axboef84d7512006-05-01 19:59:03 +0200613 /*
614 * Careful, ->map() uses KM_USER0!
615 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200616 char *src = buf->ops->map(pipe, buf, 1);
Jens Axboef84d7512006-05-01 19:59:03 +0200617 char *dst = kmap_atomic(page, KM_USER1);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200618
Jens Axboe016b6612006-04-25 15:42:00 +0200619 memcpy(dst + offset, src + buf->offset, this_len);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200620 flush_dcache_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200621 kunmap_atomic(dst, KM_USER1);
Jens Axboe76ad4d12006-05-03 10:41:33 +0200622 buf->ops->unmap(pipe, buf, src);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200623 }
Jens Axboe5274f052006-03-30 15:15:30 +0200624
Jens Axboe016b6612006-04-25 15:42:00 +0200625 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200626 if (ret) {
627 if (ret == AOP_TRUNCATED_PAGE) {
628 page_cache_release(page);
629 goto find_page;
630 }
631 if (ret < 0)
632 goto out;
Jens Axboe0568b402006-05-01 19:50:48 +0200633 /*
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200634 * Partial write has happened, so 'ret' already initialized by
635 * number of bytes written, Where is nothing we have to do here.
Jens Axboe0568b402006-05-01 19:50:48 +0200636 */
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200637 } else
Jens Axboe0568b402006-05-01 19:50:48 +0200638 ret = this_len;
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200639 /*
640 * Return the number of bytes written and mark page as
641 * accessed, we are now done!
642 */
643 mark_page_accessed(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200644out:
Jens Axboe0568b402006-05-01 19:50:48 +0200645 page_cache_release(page);
Jens Axboe9e0267c2006-04-19 15:57:31 +0200646 unlock_page(page);
Jens Axboee6e80f22006-10-11 10:03:09 +0200647out_ret:
Jens Axboe5274f052006-03-30 15:15:30 +0200648 return ret;
649}
650
Jens Axboe83f91352006-04-02 23:05:09 +0200651/*
652 * Pipe input worker. Most of this logic works like a regular pipe, the
653 * key here is the 'actor' worker passed in that actually moves the data
654 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
655 */
Jens Axboec66ab6f2007-06-12 21:17:17 +0200656ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
657 splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200658{
Jens Axboe5274f052006-03-30 15:15:30 +0200659 int ret, do_wakeup, err;
Jens Axboe5274f052006-03-30 15:15:30 +0200660
661 ret = 0;
662 do_wakeup = 0;
663
Jens Axboe5274f052006-03-30 15:15:30 +0200664 for (;;) {
Jens Axboe6f767b02006-04-11 13:53:56 +0200665 if (pipe->nrbufs) {
666 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800667 const struct pipe_buf_operations *ops = buf->ops;
Jens Axboe5274f052006-03-30 15:15:30 +0200668
Jens Axboec66ab6f2007-06-12 21:17:17 +0200669 sd->len = buf->len;
670 if (sd->len > sd->total_len)
671 sd->len = sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200672
Jens Axboec66ab6f2007-06-12 21:17:17 +0200673 err = actor(pipe, buf, sd);
Jens Axboe016b6612006-04-25 15:42:00 +0200674 if (err <= 0) {
Jens Axboe5274f052006-03-30 15:15:30 +0200675 if (!ret && err != -ENODATA)
676 ret = err;
677
678 break;
679 }
680
Jens Axboe016b6612006-04-25 15:42:00 +0200681 ret += err;
682 buf->offset += err;
683 buf->len -= err;
684
Jens Axboec66ab6f2007-06-12 21:17:17 +0200685 sd->len -= err;
686 sd->pos += err;
687 sd->total_len -= err;
688 if (sd->len)
Jens Axboe016b6612006-04-25 15:42:00 +0200689 continue;
Ingo Molnar73d62d82006-04-11 13:57:21 +0200690
Jens Axboe5274f052006-03-30 15:15:30 +0200691 if (!buf->len) {
692 buf->ops = NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200693 ops->release(pipe, buf);
Jens Axboe6f767b02006-04-11 13:53:56 +0200694 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
695 pipe->nrbufs--;
696 if (pipe->inode)
697 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200698 }
699
Jens Axboec66ab6f2007-06-12 21:17:17 +0200700 if (!sd->total_len)
Jens Axboe5274f052006-03-30 15:15:30 +0200701 break;
702 }
703
Jens Axboe6f767b02006-04-11 13:53:56 +0200704 if (pipe->nrbufs)
Jens Axboe5274f052006-03-30 15:15:30 +0200705 continue;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200706 if (!pipe->writers)
Jens Axboe5274f052006-03-30 15:15:30 +0200707 break;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200708 if (!pipe->waiting_writers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200709 if (ret)
710 break;
711 }
712
Jens Axboec66ab6f2007-06-12 21:17:17 +0200713 if (sd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700714 if (!ret)
715 ret = -EAGAIN;
716 break;
717 }
718
Jens Axboe5274f052006-03-30 15:15:30 +0200719 if (signal_pending(current)) {
720 if (!ret)
721 ret = -ERESTARTSYS;
722 break;
723 }
724
725 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200726 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200727 if (waitqueue_active(&pipe->wait))
728 wake_up_interruptible_sync(&pipe->wait);
729 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200730 do_wakeup = 0;
731 }
732
Ingo Molnar3a326a22006-04-10 15:18:35 +0200733 pipe_wait(pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200734 }
735
Jens Axboe5274f052006-03-30 15:15:30 +0200736 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200737 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200738 if (waitqueue_active(&pipe->wait))
739 wake_up_interruptible(&pipe->wait);
740 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200741 }
742
Jens Axboe5274f052006-03-30 15:15:30 +0200743 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200744}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100745EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200746
Mark Fasheh6da61802006-10-17 18:43:07 +0200747ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
748 loff_t *ppos, size_t len, unsigned int flags,
749 splice_actor *actor)
750{
751 ssize_t ret;
752 struct inode *inode = out->f_mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200753 struct splice_desc sd = {
754 .total_len = len,
755 .flags = flags,
756 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200757 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200758 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200759
760 /*
761 * The actor worker might be calling ->prepare_write and
762 * ->commit_write. Most of the time, these expect i_mutex to
763 * be held. Since this may result in an ABBA deadlock with
764 * pipe->inode, we have to order lock acquiry here.
765 */
766 inode_double_lock(inode, pipe->inode);
Jens Axboec66ab6f2007-06-12 21:17:17 +0200767 ret = __splice_from_pipe(pipe, &sd, actor);
Mark Fasheh6da61802006-10-17 18:43:07 +0200768 inode_double_unlock(inode, pipe->inode);
769
770 return ret;
771}
772
773/**
774 * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
775 * @pipe: pipe info
776 * @out: file to write to
777 * @len: number of bytes to splice
778 * @flags: splice modifier flags
779 *
780 * Will either move or copy pages (determined by @flags options) from
781 * the given pipe inode to the given file. The caller is responsible
782 * for acquiring i_mutex on both inodes.
783 *
784 */
785ssize_t
786generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
787 loff_t *ppos, size_t len, unsigned int flags)
788{
789 struct address_space *mapping = out->f_mapping;
790 struct inode *inode = mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200791 struct splice_desc sd = {
792 .total_len = len,
793 .flags = flags,
794 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +0200795 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +0200796 };
Mark Fasheh6da61802006-10-17 18:43:07 +0200797 ssize_t ret;
798 int err;
799
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800800 err = remove_suid(out->f_path.dentry);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200801 if (unlikely(err))
802 return err;
803
Jens Axboec66ab6f2007-06-12 21:17:17 +0200804 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
Mark Fasheh6da61802006-10-17 18:43:07 +0200805 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200806 unsigned long nr_pages;
807
Mark Fasheh6da61802006-10-17 18:43:07 +0200808 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200809 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Mark Fasheh6da61802006-10-17 18:43:07 +0200810
811 /*
812 * If file or inode is SYNC and we actually wrote some data,
813 * sync it.
814 */
815 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
816 err = generic_osync_inode(inode, mapping,
817 OSYNC_METADATA|OSYNC_DATA);
818
819 if (err)
820 ret = err;
821 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200822 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Mark Fasheh6da61802006-10-17 18:43:07 +0200823 }
824
825 return ret;
826}
827
828EXPORT_SYMBOL(generic_file_splice_write_nolock);
829
Jens Axboe83f91352006-04-02 23:05:09 +0200830/**
831 * generic_file_splice_write - splice data from a pipe to a file
Ingo Molnar3a326a22006-04-10 15:18:35 +0200832 * @pipe: pipe info
Jens Axboe83f91352006-04-02 23:05:09 +0200833 * @out: file to write to
834 * @len: number of bytes to splice
835 * @flags: splice modifier flags
836 *
837 * Will either move or copy pages (determined by @flags options) from
838 * the given pipe inode to the given file.
839 *
840 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200841ssize_t
842generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200843 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200844{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200845 struct address_space *mapping = out->f_mapping;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200846 struct inode *inode = mapping->host;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200847 ssize_t ret;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200848 int err;
849
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800850 err = should_remove_suid(out->f_path.dentry);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200851 if (unlikely(err)) {
852 mutex_lock(&inode->i_mutex);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800853 err = __remove_suid(out->f_path.dentry, err);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200854 mutex_unlock(&inode->i_mutex);
855 if (err)
856 return err;
857 }
Ingo Molnar3a326a22006-04-10 15:18:35 +0200858
Jens Axboe00522fb2006-04-26 14:39:29 +0200859 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
Jens Axboea4514eb2006-04-19 15:57:05 +0200860 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200861 unsigned long nr_pages;
862
Jens Axboea4514eb2006-04-19 15:57:05 +0200863 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200864 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200865
Jens Axboea4514eb2006-04-19 15:57:05 +0200866 /*
867 * If file or inode is SYNC and we actually wrote some data,
868 * sync it.
869 */
870 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
Jens Axboea4514eb2006-04-19 15:57:05 +0200871 mutex_lock(&inode->i_mutex);
872 err = generic_osync_inode(inode, mapping,
873 OSYNC_METADATA|OSYNC_DATA);
874 mutex_unlock(&inode->i_mutex);
875
876 if (err)
877 ret = err;
878 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200879 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200880 }
881
882 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200883}
884
Jens Axboe059a8f32006-04-02 23:06:05 +0200885EXPORT_SYMBOL(generic_file_splice_write);
886
Jens Axboe83f91352006-04-02 23:05:09 +0200887/**
888 * generic_splice_sendpage - splice data from a pipe to a socket
889 * @inode: pipe inode
890 * @out: socket to write to
891 * @len: number of bytes to splice
892 * @flags: splice modifier flags
893 *
894 * Will send @len bytes from the pipe to a network socket. No data copying
895 * is involved.
896 *
897 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200898ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200899 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200900{
Jens Axboe00522fb2006-04-26 14:39:29 +0200901 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200902}
903
Jens Axboe059a8f32006-04-02 23:06:05 +0200904EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500905
Jens Axboe83f91352006-04-02 23:05:09 +0200906/*
907 * Attempt to initiate a splice from pipe to file.
908 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200909static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200910 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200911{
Jens Axboe5274f052006-03-30 15:15:30 +0200912 int ret;
913
Jens Axboe49570e92006-04-11 13:56:09 +0200914 if (unlikely(!out->f_op || !out->f_op->splice_write))
Jens Axboe5274f052006-03-30 15:15:30 +0200915 return -EINVAL;
916
Jens Axboe49570e92006-04-11 13:56:09 +0200917 if (unlikely(!(out->f_mode & FMODE_WRITE)))
Jens Axboe5274f052006-03-30 15:15:30 +0200918 return -EBADF;
919
Jens Axboecbb7e572006-04-11 14:57:50 +0200920 ret = rw_verify_area(WRITE, out, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200921 if (unlikely(ret < 0))
922 return ret;
923
Jens Axboecbb7e572006-04-11 14:57:50 +0200924 return out->f_op->splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200925}
926
Jens Axboe83f91352006-04-02 23:05:09 +0200927/*
928 * Attempt to initiate a splice from a file to a pipe.
929 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200930static long do_splice_to(struct file *in, loff_t *ppos,
931 struct pipe_inode_info *pipe, size_t len,
932 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200933{
Jens Axboe5274f052006-03-30 15:15:30 +0200934 int ret;
935
Jens Axboe49570e92006-04-11 13:56:09 +0200936 if (unlikely(!in->f_op || !in->f_op->splice_read))
Jens Axboe5274f052006-03-30 15:15:30 +0200937 return -EINVAL;
938
Jens Axboe49570e92006-04-11 13:56:09 +0200939 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200940 return -EBADF;
941
Jens Axboecbb7e572006-04-11 14:57:50 +0200942 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200943 if (unlikely(ret < 0))
944 return ret;
945
Jens Axboecbb7e572006-04-11 14:57:50 +0200946 return in->f_op->splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200947}
948
Jens Axboec66ab6f2007-06-12 21:17:17 +0200949/*
950 * Splices from an input file to an actor, using a 'direct' pipe.
951 */
952ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
953 splice_direct_actor *actor)
Jens Axboeb92ce552006-04-11 13:52:07 +0200954{
955 struct pipe_inode_info *pipe;
956 long ret, bytes;
957 umode_t i_mode;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200958 size_t len;
959 int i, flags;
Jens Axboeb92ce552006-04-11 13:52:07 +0200960
961 /*
962 * We require the input being a regular file, as we don't want to
963 * randomly drop data for eg socket -> socket splicing. Use the
964 * piped splicing for that!
965 */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800966 i_mode = in->f_path.dentry->d_inode->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +0200967 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
968 return -EINVAL;
969
970 /*
971 * neither in nor out is a pipe, setup an internal pipe attached to
972 * 'out' and transfer the wanted data from 'in' to 'out' through that
973 */
974 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200975 if (unlikely(!pipe)) {
Jens Axboeb92ce552006-04-11 13:52:07 +0200976 pipe = alloc_pipe_info(NULL);
977 if (!pipe)
978 return -ENOMEM;
979
980 /*
981 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200982 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200983 * PIPE_READERS appropriately.
984 */
985 pipe->readers = 1;
986
987 current->splice_pipe = pipe;
988 }
989
990 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200991 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200992 */
993 ret = 0;
994 bytes = 0;
Jens Axboec66ab6f2007-06-12 21:17:17 +0200995 len = sd->total_len;
996 flags = sd->flags;
997
998 /*
999 * Don't block on output, we have to drain the direct pipe.
1000 */
1001 sd->flags &= ~SPLICE_F_NONBLOCK;
Jens Axboeb92ce552006-04-11 13:52:07 +02001002
1003 while (len) {
1004 size_t read_len, max_read_len;
1005
1006 /*
1007 * Do at most PIPE_BUFFERS pages worth of transfer:
1008 */
1009 max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
1010
Jens Axboec66ab6f2007-06-12 21:17:17 +02001011 ret = do_splice_to(in, &sd->pos, pipe, max_read_len, flags);
Jens Axboeb92ce552006-04-11 13:52:07 +02001012 if (unlikely(ret < 0))
1013 goto out_release;
1014
1015 read_len = ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001016 sd->total_len = read_len;
Jens Axboeb92ce552006-04-11 13:52:07 +02001017
1018 /*
1019 * NOTE: nonblocking mode only applies to the input. We
1020 * must not do the output in nonblocking mode as then we
1021 * could get stuck data in the internal pipe:
1022 */
Jens Axboec66ab6f2007-06-12 21:17:17 +02001023 ret = actor(pipe, sd);
Jens Axboeb92ce552006-04-11 13:52:07 +02001024 if (unlikely(ret < 0))
1025 goto out_release;
1026
1027 bytes += ret;
1028 len -= ret;
1029
1030 /*
1031 * In nonblocking mode, if we got back a short read then
1032 * that was due to either an IO error or due to the
1033 * pagecache entry not being there. In the IO error case
1034 * the _next_ splice attempt will produce a clean IO error
1035 * return value (not a short read), so in both cases it's
1036 * correct to break out of the loop here:
1037 */
1038 if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
1039 break;
1040 }
1041
1042 pipe->nrbufs = pipe->curbuf = 0;
1043
1044 return bytes;
1045
1046out_release:
1047 /*
1048 * If we did an incomplete transfer we must release
1049 * the pipe buffers in question:
1050 */
1051 for (i = 0; i < PIPE_BUFFERS; i++) {
1052 struct pipe_buffer *buf = pipe->bufs + i;
1053
1054 if (buf->ops) {
1055 buf->ops->release(pipe, buf);
1056 buf->ops = NULL;
1057 }
1058 }
1059 pipe->nrbufs = pipe->curbuf = 0;
1060
1061 /*
1062 * If we transferred some data, return the number of bytes:
1063 */
1064 if (bytes > 0)
1065 return bytes;
1066
1067 return ret;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001068
1069}
1070EXPORT_SYMBOL(splice_direct_to_actor);
1071
1072static int direct_splice_actor(struct pipe_inode_info *pipe,
1073 struct splice_desc *sd)
1074{
Jens Axboe6a14b902007-06-14 13:08:55 +02001075 struct file *file = sd->u.file;
Jens Axboec66ab6f2007-06-12 21:17:17 +02001076
1077 return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
1078}
1079
1080long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1081 size_t len, unsigned int flags)
1082{
1083 struct splice_desc sd = {
1084 .len = len,
1085 .total_len = len,
1086 .flags = flags,
1087 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02001088 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +02001089 };
1090 size_t ret;
1091
1092 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1093 *ppos = sd.pos;
1094 return ret;
Jens Axboeb92ce552006-04-11 13:52:07 +02001095}
1096
Jens Axboe83f91352006-04-02 23:05:09 +02001097/*
Jens Axboeddac0d32006-11-04 12:49:32 +01001098 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1099 * location, so checking ->i_pipe is not enough to verify that this is a
1100 * pipe.
1101 */
1102static inline struct pipe_inode_info *pipe_info(struct inode *inode)
1103{
1104 if (S_ISFIFO(inode->i_mode))
1105 return inode->i_pipe;
1106
1107 return NULL;
1108}
1109
1110/*
Jens Axboe83f91352006-04-02 23:05:09 +02001111 * Determine where to splice to/from.
1112 */
Ingo Molnar529565d2006-04-10 15:18:58 +02001113static long do_splice(struct file *in, loff_t __user *off_in,
1114 struct file *out, loff_t __user *off_out,
1115 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001116{
Ingo Molnar3a326a22006-04-10 15:18:35 +02001117 struct pipe_inode_info *pipe;
Jens Axboecbb7e572006-04-11 14:57:50 +02001118 loff_t offset, *off;
Jens Axboea4514eb2006-04-19 15:57:05 +02001119 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001120
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001121 pipe = pipe_info(in->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001122 if (pipe) {
1123 if (off_in)
1124 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001125 if (off_out) {
1126 if (out->f_op->llseek == no_llseek)
1127 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001128 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001129 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001130 off = &offset;
1131 } else
1132 off = &out->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001133
Jens Axboea4514eb2006-04-19 15:57:05 +02001134 ret = do_splice_from(pipe, out, off, len, flags);
1135
1136 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1137 ret = -EFAULT;
1138
1139 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001140 }
Jens Axboe5274f052006-03-30 15:15:30 +02001141
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001142 pipe = pipe_info(out->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001143 if (pipe) {
1144 if (off_out)
1145 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001146 if (off_in) {
1147 if (in->f_op->llseek == no_llseek)
1148 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001149 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001150 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001151 off = &offset;
1152 } else
1153 off = &in->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001154
Jens Axboea4514eb2006-04-19 15:57:05 +02001155 ret = do_splice_to(in, off, pipe, len, flags);
1156
1157 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1158 ret = -EFAULT;
1159
1160 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001161 }
Jens Axboe5274f052006-03-30 15:15:30 +02001162
1163 return -EINVAL;
1164}
1165
Jens Axboe912d35f2006-04-26 10:59:21 +02001166/*
1167 * Map an iov into an array of pages and offset/length tupples. With the
1168 * partial_page structure, we can map several non-contiguous ranges into
1169 * our ones pages[] map instead of splitting that operation into pieces.
1170 * Could easily be exported as a generic helper for other users, in which
1171 * case one would probably want to add a 'max_nr_pages' parameter as well.
1172 */
1173static int get_iovec_page_array(const struct iovec __user *iov,
1174 unsigned int nr_vecs, struct page **pages,
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001175 struct partial_page *partial, int aligned)
Jens Axboe912d35f2006-04-26 10:59:21 +02001176{
1177 int buffers = 0, error = 0;
1178
1179 /*
1180 * It's ok to take the mmap_sem for reading, even
1181 * across a "get_user()".
1182 */
1183 down_read(&current->mm->mmap_sem);
1184
1185 while (nr_vecs) {
1186 unsigned long off, npages;
1187 void __user *base;
1188 size_t len;
1189 int i;
1190
1191 /*
1192 * Get user address base and length for this iovec.
1193 */
1194 error = get_user(base, &iov->iov_base);
1195 if (unlikely(error))
1196 break;
1197 error = get_user(len, &iov->iov_len);
1198 if (unlikely(error))
1199 break;
1200
1201 /*
1202 * Sanity check this iovec. 0 read succeeds.
1203 */
1204 if (unlikely(!len))
1205 break;
1206 error = -EFAULT;
1207 if (unlikely(!base))
1208 break;
1209
1210 /*
1211 * Get this base offset and number of pages, then map
1212 * in the user pages.
1213 */
1214 off = (unsigned long) base & ~PAGE_MASK;
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001215
1216 /*
1217 * If asked for alignment, the offset must be zero and the
1218 * length a multiple of the PAGE_SIZE.
1219 */
1220 error = -EINVAL;
1221 if (aligned && (off || len & ~PAGE_MASK))
1222 break;
1223
Jens Axboe912d35f2006-04-26 10:59:21 +02001224 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1225 if (npages > PIPE_BUFFERS - buffers)
1226 npages = PIPE_BUFFERS - buffers;
1227
1228 error = get_user_pages(current, current->mm,
1229 (unsigned long) base, npages, 0, 0,
1230 &pages[buffers], NULL);
1231
1232 if (unlikely(error <= 0))
1233 break;
1234
1235 /*
1236 * Fill this contiguous range into the partial page map.
1237 */
1238 for (i = 0; i < error; i++) {
Jens Axboe75914892006-05-02 12:57:18 +02001239 const int plen = min_t(size_t, len, PAGE_SIZE - off);
Jens Axboe912d35f2006-04-26 10:59:21 +02001240
1241 partial[buffers].offset = off;
1242 partial[buffers].len = plen;
1243
1244 off = 0;
1245 len -= plen;
1246 buffers++;
1247 }
1248
1249 /*
1250 * We didn't complete this iov, stop here since it probably
1251 * means we have to move some of this into a pipe to
1252 * be able to continue.
1253 */
1254 if (len)
1255 break;
1256
1257 /*
1258 * Don't continue if we mapped fewer pages than we asked for,
1259 * or if we mapped the max number of pages that we have
1260 * room for.
1261 */
1262 if (error < npages || buffers == PIPE_BUFFERS)
1263 break;
1264
1265 nr_vecs--;
1266 iov++;
1267 }
1268
1269 up_read(&current->mm->mmap_sem);
1270
1271 if (buffers)
1272 return buffers;
1273
1274 return error;
1275}
1276
Jens Axboe6a14b902007-06-14 13:08:55 +02001277static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1278 struct splice_desc *sd)
1279{
1280 char *src;
1281 int ret;
1282
1283 ret = buf->ops->pin(pipe, buf);
1284 if (unlikely(ret))
1285 return ret;
1286
1287 /*
1288 * See if we can use the atomic maps, by prefaulting in the
1289 * pages and doing an atomic copy
1290 */
1291 if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
1292 src = buf->ops->map(pipe, buf, 1);
1293 ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
1294 sd->len);
1295 buf->ops->unmap(pipe, buf, src);
1296 if (!ret) {
1297 ret = sd->len;
1298 goto out;
1299 }
1300 }
1301
1302 /*
1303 * No dice, use slow non-atomic map and copy
1304 */
1305 src = buf->ops->map(pipe, buf, 0);
1306
1307 ret = sd->len;
1308 if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
1309 ret = -EFAULT;
1310
1311out:
1312 if (ret > 0)
1313 sd->u.userptr += ret;
1314 buf->ops->unmap(pipe, buf, src);
1315 return ret;
1316}
1317
1318/*
1319 * For lack of a better implementation, implement vmsplice() to userspace
1320 * as a simple copy of the pipes pages to the user iov.
1321 */
1322static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
1323 unsigned long nr_segs, unsigned int flags)
1324{
1325 struct pipe_inode_info *pipe;
1326 struct splice_desc sd;
1327 ssize_t size;
1328 int error;
1329 long ret;
1330
1331 pipe = pipe_info(file->f_path.dentry->d_inode);
1332 if (!pipe)
1333 return -EBADF;
1334
1335 if (pipe->inode)
1336 mutex_lock(&pipe->inode->i_mutex);
1337
1338 error = ret = 0;
1339 while (nr_segs) {
1340 void __user *base;
1341 size_t len;
1342
1343 /*
1344 * Get user address base and length for this iovec.
1345 */
1346 error = get_user(base, &iov->iov_base);
1347 if (unlikely(error))
1348 break;
1349 error = get_user(len, &iov->iov_len);
1350 if (unlikely(error))
1351 break;
1352
1353 /*
1354 * Sanity check this iovec. 0 read succeeds.
1355 */
1356 if (unlikely(!len))
1357 break;
1358 if (unlikely(!base)) {
1359 error = -EFAULT;
1360 break;
1361 }
1362
1363 sd.len = 0;
1364 sd.total_len = len;
1365 sd.flags = flags;
1366 sd.u.userptr = base;
1367 sd.pos = 0;
1368
1369 size = __splice_from_pipe(pipe, &sd, pipe_to_user);
1370 if (size < 0) {
1371 if (!ret)
1372 ret = size;
1373
1374 break;
1375 }
1376
1377 ret += size;
1378
1379 if (size < len)
1380 break;
1381
1382 nr_segs--;
1383 iov++;
1384 }
1385
1386 if (pipe->inode)
1387 mutex_unlock(&pipe->inode->i_mutex);
1388
1389 if (!ret)
1390 ret = error;
1391
1392 return ret;
1393}
1394
Jens Axboe912d35f2006-04-26 10:59:21 +02001395/*
1396 * vmsplice splices a user address range into a pipe. It can be thought of
1397 * as splice-from-memory, where the regular splice is splice-from-file (or
1398 * to file). In both cases the output is a pipe, naturally.
Jens Axboe912d35f2006-04-26 10:59:21 +02001399 */
Jens Axboe6a14b902007-06-14 13:08:55 +02001400static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
1401 unsigned long nr_segs, unsigned int flags)
Jens Axboe912d35f2006-04-26 10:59:21 +02001402{
Jens Axboeddac0d32006-11-04 12:49:32 +01001403 struct pipe_inode_info *pipe;
Jens Axboe912d35f2006-04-26 10:59:21 +02001404 struct page *pages[PIPE_BUFFERS];
1405 struct partial_page partial[PIPE_BUFFERS];
1406 struct splice_pipe_desc spd = {
1407 .pages = pages,
1408 .partial = partial,
1409 .flags = flags,
1410 .ops = &user_page_pipe_buf_ops,
1411 };
1412
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001413 pipe = pipe_info(file->f_path.dentry->d_inode);
Jens Axboeddac0d32006-11-04 12:49:32 +01001414 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001415 return -EBADF;
Jens Axboe912d35f2006-04-26 10:59:21 +02001416
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001417 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1418 flags & SPLICE_F_GIFT);
Jens Axboe912d35f2006-04-26 10:59:21 +02001419 if (spd.nr_pages <= 0)
1420 return spd.nr_pages;
1421
Jens Axboe00522fb2006-04-26 14:39:29 +02001422 return splice_to_pipe(pipe, &spd);
Jens Axboe912d35f2006-04-26 10:59:21 +02001423}
1424
Jens Axboe6a14b902007-06-14 13:08:55 +02001425/*
1426 * Note that vmsplice only really supports true splicing _from_ user memory
1427 * to a pipe, not the other way around. Splicing from user memory is a simple
1428 * operation that can be supported without any funky alignment restrictions
1429 * or nasty vm tricks. We simply map in the user memory and fill them into
1430 * a pipe. The reverse isn't quite as easy, though. There are two possible
1431 * solutions for that:
1432 *
1433 * - memcpy() the data internally, at which point we might as well just
1434 * do a regular read() on the buffer anyway.
1435 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1436 * has restriction limitations on both ends of the pipe).
1437 *
1438 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1439 *
1440 */
Jens Axboe912d35f2006-04-26 10:59:21 +02001441asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1442 unsigned long nr_segs, unsigned int flags)
1443{
1444 struct file *file;
1445 long error;
1446 int fput;
1447
Jens Axboe6a14b902007-06-14 13:08:55 +02001448 if (unlikely(nr_segs > UIO_MAXIOV))
1449 return -EINVAL;
1450 else if (unlikely(!nr_segs))
1451 return 0;
1452
Jens Axboe912d35f2006-04-26 10:59:21 +02001453 error = -EBADF;
1454 file = fget_light(fd, &fput);
1455 if (file) {
1456 if (file->f_mode & FMODE_WRITE)
Jens Axboe6a14b902007-06-14 13:08:55 +02001457 error = vmsplice_to_pipe(file, iov, nr_segs, flags);
1458 else if (file->f_mode & FMODE_READ)
1459 error = vmsplice_to_user(file, iov, nr_segs, flags);
Jens Axboe912d35f2006-04-26 10:59:21 +02001460
1461 fput_light(file, fput);
1462 }
1463
1464 return error;
1465}
1466
Ingo Molnar529565d2006-04-10 15:18:58 +02001467asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1468 int fd_out, loff_t __user *off_out,
1469 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001470{
1471 long error;
1472 struct file *in, *out;
1473 int fput_in, fput_out;
1474
1475 if (unlikely(!len))
1476 return 0;
1477
1478 error = -EBADF;
Ingo Molnar529565d2006-04-10 15:18:58 +02001479 in = fget_light(fd_in, &fput_in);
Jens Axboe5274f052006-03-30 15:15:30 +02001480 if (in) {
1481 if (in->f_mode & FMODE_READ) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001482 out = fget_light(fd_out, &fput_out);
Jens Axboe5274f052006-03-30 15:15:30 +02001483 if (out) {
1484 if (out->f_mode & FMODE_WRITE)
Ingo Molnar529565d2006-04-10 15:18:58 +02001485 error = do_splice(in, off_in,
1486 out, off_out,
1487 len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001488 fput_light(out, fput_out);
1489 }
1490 }
1491
1492 fput_light(in, fput_in);
1493 }
1494
1495 return error;
1496}
Jens Axboe70524492006-04-11 15:51:17 +02001497
1498/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001499 * Make sure there's data to read. Wait for input if we can, otherwise
1500 * return an appropriate error.
1501 */
1502static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1503{
1504 int ret;
1505
1506 /*
1507 * Check ->nrbufs without the inode lock first. This function
1508 * is speculative anyways, so missing one is ok.
1509 */
1510 if (pipe->nrbufs)
1511 return 0;
1512
1513 ret = 0;
1514 mutex_lock(&pipe->inode->i_mutex);
1515
1516 while (!pipe->nrbufs) {
1517 if (signal_pending(current)) {
1518 ret = -ERESTARTSYS;
1519 break;
1520 }
1521 if (!pipe->writers)
1522 break;
1523 if (!pipe->waiting_writers) {
1524 if (flags & SPLICE_F_NONBLOCK) {
1525 ret = -EAGAIN;
1526 break;
1527 }
1528 }
1529 pipe_wait(pipe);
1530 }
1531
1532 mutex_unlock(&pipe->inode->i_mutex);
1533 return ret;
1534}
1535
1536/*
1537 * Make sure there's writeable room. Wait for room if we can, otherwise
1538 * return an appropriate error.
1539 */
1540static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1541{
1542 int ret;
1543
1544 /*
1545 * Check ->nrbufs without the inode lock first. This function
1546 * is speculative anyways, so missing one is ok.
1547 */
1548 if (pipe->nrbufs < PIPE_BUFFERS)
1549 return 0;
1550
1551 ret = 0;
1552 mutex_lock(&pipe->inode->i_mutex);
1553
1554 while (pipe->nrbufs >= PIPE_BUFFERS) {
1555 if (!pipe->readers) {
1556 send_sig(SIGPIPE, current, 0);
1557 ret = -EPIPE;
1558 break;
1559 }
1560 if (flags & SPLICE_F_NONBLOCK) {
1561 ret = -EAGAIN;
1562 break;
1563 }
1564 if (signal_pending(current)) {
1565 ret = -ERESTARTSYS;
1566 break;
1567 }
1568 pipe->waiting_writers++;
1569 pipe_wait(pipe);
1570 pipe->waiting_writers--;
1571 }
1572
1573 mutex_unlock(&pipe->inode->i_mutex);
1574 return ret;
1575}
1576
1577/*
Jens Axboe70524492006-04-11 15:51:17 +02001578 * Link contents of ipipe to opipe.
1579 */
1580static int link_pipe(struct pipe_inode_info *ipipe,
1581 struct pipe_inode_info *opipe,
1582 size_t len, unsigned int flags)
1583{
1584 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001585 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001586
1587 /*
1588 * Potential ABBA deadlock, work around it by ordering lock
1589 * grabbing by inode address. Otherwise two different processes
1590 * could deadlock (one doing tee from A -> B, the other from B -> A).
1591 */
Mark Fasheh62752ee2006-10-17 10:31:38 +02001592 inode_double_lock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001593
Jens Axboeaadd06e2006-07-10 11:00:01 +02001594 do {
Jens Axboe70524492006-04-11 15:51:17 +02001595 if (!opipe->readers) {
1596 send_sig(SIGPIPE, current, 0);
1597 if (!ret)
1598 ret = -EPIPE;
1599 break;
1600 }
Jens Axboe70524492006-04-11 15:51:17 +02001601
1602 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001603 * If we have iterated all input buffers or ran out of
1604 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001605 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001606 if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
Jens Axboe70524492006-04-11 15:51:17 +02001607 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001608
1609 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1610 nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1611
Jens Axboe2a27250e2006-04-19 15:56:40 +02001612 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001613 * Get a reference to this pipe buffer,
1614 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001615 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001616 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001617
Jens Axboeaadd06e2006-07-10 11:00:01 +02001618 obuf = opipe->bufs + nbuf;
1619 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001620
Jens Axboeaadd06e2006-07-10 11:00:01 +02001621 /*
1622 * Don't inherit the gift flag, we need to
1623 * prevent multiple steals of this page.
1624 */
1625 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1626
1627 if (obuf->len > len)
1628 obuf->len = len;
1629
1630 opipe->nrbufs++;
1631 ret += obuf->len;
1632 len -= obuf->len;
1633 i++;
1634 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001635
Mark Fasheh62752ee2006-10-17 10:31:38 +02001636 inode_double_unlock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001637
Jens Axboeaadd06e2006-07-10 11:00:01 +02001638 /*
1639 * If we put data in the output pipe, wakeup any potential readers.
1640 */
1641 if (ret > 0) {
Jens Axboe70524492006-04-11 15:51:17 +02001642 smp_mb();
1643 if (waitqueue_active(&opipe->wait))
1644 wake_up_interruptible(&opipe->wait);
1645 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1646 }
1647
1648 return ret;
1649}
1650
1651/*
1652 * This is a tee(1) implementation that works on pipes. It doesn't copy
1653 * any data, it simply references the 'in' pages on the 'out' pipe.
1654 * The 'flags' used are the SPLICE_F_* variants, currently the only
1655 * applicable one is SPLICE_F_NONBLOCK.
1656 */
1657static long do_tee(struct file *in, struct file *out, size_t len,
1658 unsigned int flags)
1659{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001660 struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
1661 struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001662 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001663
1664 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001665 * Duplicate the contents of ipipe to opipe without actually
1666 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001667 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001668 if (ipipe && opipe && ipipe != opipe) {
1669 /*
1670 * Keep going, unless we encounter an error. The ipipe/opipe
1671 * ordering doesn't really matter.
1672 */
1673 ret = link_ipipe_prep(ipipe, flags);
1674 if (!ret) {
1675 ret = link_opipe_prep(opipe, flags);
1676 if (!ret) {
1677 ret = link_pipe(ipipe, opipe, len, flags);
1678 if (!ret && (flags & SPLICE_F_NONBLOCK))
1679 ret = -EAGAIN;
1680 }
1681 }
1682 }
Jens Axboe70524492006-04-11 15:51:17 +02001683
Jens Axboeaadd06e2006-07-10 11:00:01 +02001684 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001685}
1686
1687asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1688{
1689 struct file *in;
1690 int error, fput_in;
1691
1692 if (unlikely(!len))
1693 return 0;
1694
1695 error = -EBADF;
1696 in = fget_light(fdin, &fput_in);
1697 if (in) {
1698 if (in->f_mode & FMODE_READ) {
1699 int fput_out;
1700 struct file *out = fget_light(fdout, &fput_out);
1701
1702 if (out) {
1703 if (out->f_mode & FMODE_WRITE)
1704 error = do_tee(in, out, len, flags);
1705 fput_light(out, fput_out);
1706 }
1707 }
1708 fput_light(in, fput_in);
1709 }
1710
1711 return error;
1712}