blob: e7d7080de2f9799860a475a4df50a7742561d09f [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>
23#include <linux/pipe_fs_i.h>
24#include <linux/mm_inline.h>
Jens Axboe5abc97a2006-03-30 15:16:46 +020025#include <linux/swap.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020026#include <linux/writeback.h>
27#include <linux/buffer_head.h>
Jeff Garzika0f06782006-03-30 23:06:13 -050028#include <linux/module.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020029#include <linux/syscalls.h>
Jens Axboe912d35f2006-04-26 10:59:21 +020030#include <linux/uio.h>
Jens Axboe5274f052006-03-30 15:15:30 +020031
Jens Axboe912d35f2006-04-26 10:59:21 +020032struct partial_page {
33 unsigned int offset;
34 unsigned int len;
35};
36
37/*
Jens Axboe00522fb2006-04-26 14:39:29 +020038 * Passed to splice_to_pipe
Jens Axboe912d35f2006-04-26 10:59:21 +020039 */
40struct splice_pipe_desc {
41 struct page **pages; /* page map */
42 struct partial_page *partial; /* pages[] may not be contig */
43 int nr_pages; /* number of pages in map */
44 unsigned int flags; /* splice flags */
Eric Dumazetd4c3cca2006-12-13 00:34:04 -080045 const struct pipe_buf_operations *ops;/* ops associated with output pipe */
Jens Axboe912d35f2006-04-26 10:59:21 +020046};
47
Jens Axboe83f91352006-04-02 23:05:09 +020048/*
49 * Attempt to steal a page from a pipe buffer. This should perhaps go into
50 * a vm helper function, it's already simplified quite a bit by the
51 * addition of remove_mapping(). If success is returned, the caller may
52 * attempt to reuse this page for another destination.
53 */
Jens Axboe76ad4d12006-05-03 10:41:33 +020054static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
Jens Axboe5abc97a2006-03-30 15:16:46 +020055 struct pipe_buffer *buf)
56{
57 struct page *page = buf->page;
Jens Axboe9e94cd42006-06-20 15:01:12 +020058 struct address_space *mapping;
Jens Axboe5abc97a2006-03-30 15:16:46 +020059
Jens Axboe9e0267c2006-04-19 15:57:31 +020060 lock_page(page);
61
Jens Axboe9e94cd42006-06-20 15:01:12 +020062 mapping = page_mapping(page);
63 if (mapping) {
64 WARN_ON(!PageUptodate(page));
Jens Axboe5abc97a2006-03-30 15:16:46 +020065
Jens Axboe9e94cd42006-06-20 15:01:12 +020066 /*
67 * At least for ext2 with nobh option, we need to wait on
68 * writeback completing on this page, since we'll remove it
69 * from the pagecache. Otherwise truncate wont wait on the
70 * page, allowing the disk blocks to be reused by someone else
71 * before we actually wrote our data to them. fs corruption
72 * ensues.
73 */
74 wait_on_page_writeback(page);
Jens Axboead8d6f02006-04-02 23:10:32 +020075
Jens Axboe9e94cd42006-06-20 15:01:12 +020076 if (PagePrivate(page))
Nick Piggin2ae88142006-10-28 10:38:23 -070077 try_to_release_page(page, GFP_KERNEL);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020078
Jens Axboe9e94cd42006-06-20 15:01:12 +020079 /*
80 * If we succeeded in removing the mapping, set LRU flag
81 * and return good.
82 */
83 if (remove_mapping(mapping, page)) {
84 buf->flags |= PIPE_BUF_FLAG_LRU;
85 return 0;
86 }
Jens Axboe9e0267c2006-04-19 15:57:31 +020087 }
Jens Axboe5abc97a2006-03-30 15:16:46 +020088
Jens Axboe9e94cd42006-06-20 15:01:12 +020089 /*
90 * Raced with truncate or failed to remove page from current
91 * address space, unlock and return failure.
92 */
93 unlock_page(page);
94 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +020095}
96
Jens Axboe76ad4d12006-05-03 10:41:33 +020097static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +020098 struct pipe_buffer *buf)
99{
100 page_cache_release(buf->page);
Jens Axboe14328732006-05-03 10:35:26 +0200101 buf->flags &= ~PIPE_BUF_FLAG_LRU;
Jens Axboe5274f052006-03-30 15:15:30 +0200102}
103
Jens Axboe76ad4d12006-05-03 10:41:33 +0200104static int page_cache_pipe_buf_pin(struct pipe_inode_info *pipe,
Jens Axboef84d7512006-05-01 19:59:03 +0200105 struct pipe_buffer *buf)
Jens Axboe5274f052006-03-30 15:15:30 +0200106{
107 struct page *page = buf->page;
Jens Axboe49d0b212006-04-10 09:04:41 +0200108 int err;
Jens Axboe5274f052006-03-30 15:15:30 +0200109
110 if (!PageUptodate(page)) {
Jens Axboe49d0b212006-04-10 09:04:41 +0200111 lock_page(page);
112
113 /*
114 * Page got truncated/unhashed. This will cause a 0-byte
Ingo Molnar73d62d82006-04-11 13:57:21 +0200115 * splice, if this is the first page.
Jens Axboe49d0b212006-04-10 09:04:41 +0200116 */
117 if (!page->mapping) {
118 err = -ENODATA;
119 goto error;
120 }
121
122 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200123 * Uh oh, read-error from disk.
Jens Axboe49d0b212006-04-10 09:04:41 +0200124 */
125 if (!PageUptodate(page)) {
126 err = -EIO;
127 goto error;
128 }
129
130 /*
Jens Axboef84d7512006-05-01 19:59:03 +0200131 * Page is ok afterall, we are done.
Jens Axboe49d0b212006-04-10 09:04:41 +0200132 */
Jens Axboe5274f052006-03-30 15:15:30 +0200133 unlock_page(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200134 }
135
Jens Axboef84d7512006-05-01 19:59:03 +0200136 return 0;
Jens Axboe49d0b212006-04-10 09:04:41 +0200137error:
138 unlock_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200139 return err;
Jens Axboe70524492006-04-11 15:51:17 +0200140}
141
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800142static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
Jens Axboe5274f052006-03-30 15:15:30 +0200143 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200144 .map = generic_pipe_buf_map,
145 .unmap = generic_pipe_buf_unmap,
146 .pin = page_cache_pipe_buf_pin,
Jens Axboe5274f052006-03-30 15:15:30 +0200147 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200148 .steal = page_cache_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200149 .get = generic_pipe_buf_get,
Jens Axboe5274f052006-03-30 15:15:30 +0200150};
151
Jens Axboe912d35f2006-04-26 10:59:21 +0200152static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
153 struct pipe_buffer *buf)
154{
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200155 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
156 return 1;
157
Jens Axboe14328732006-05-03 10:35:26 +0200158 buf->flags |= PIPE_BUF_FLAG_LRU;
Jens Axboe330ab712006-05-02 15:29:57 +0200159 return generic_pipe_buf_steal(pipe, buf);
Jens Axboe912d35f2006-04-26 10:59:21 +0200160}
161
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800162static const struct pipe_buf_operations user_page_pipe_buf_ops = {
Jens Axboe912d35f2006-04-26 10:59:21 +0200163 .can_merge = 0,
Jens Axboef84d7512006-05-01 19:59:03 +0200164 .map = generic_pipe_buf_map,
165 .unmap = generic_pipe_buf_unmap,
166 .pin = generic_pipe_buf_pin,
Jens Axboe912d35f2006-04-26 10:59:21 +0200167 .release = page_cache_pipe_buf_release,
168 .steal = user_page_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200169 .get = generic_pipe_buf_get,
Jens Axboe912d35f2006-04-26 10:59:21 +0200170};
171
Jens Axboe83f91352006-04-02 23:05:09 +0200172/*
173 * Pipe output worker. This sets up our pipe format with the page cache
174 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
175 */
Jens Axboe00522fb2006-04-26 14:39:29 +0200176static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
177 struct splice_pipe_desc *spd)
Jens Axboe5274f052006-03-30 15:15:30 +0200178{
Jens Axboe00de00b2007-06-15 13:14:22 +0200179 unsigned int spd_pages = spd->nr_pages;
Jens Axboe912d35f2006-04-26 10:59:21 +0200180 int ret, do_wakeup, page_nr;
Jens Axboe5274f052006-03-30 15:15:30 +0200181
182 ret = 0;
183 do_wakeup = 0;
Jens Axboe912d35f2006-04-26 10:59:21 +0200184 page_nr = 0;
Jens Axboe5274f052006-03-30 15:15:30 +0200185
Ingo Molnar3a326a22006-04-10 15:18:35 +0200186 if (pipe->inode)
187 mutex_lock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200188
Jens Axboe5274f052006-03-30 15:15:30 +0200189 for (;;) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200190 if (!pipe->readers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200191 send_sig(SIGPIPE, current, 0);
192 if (!ret)
193 ret = -EPIPE;
194 break;
195 }
196
Jens Axboe6f767b02006-04-11 13:53:56 +0200197 if (pipe->nrbufs < PIPE_BUFFERS) {
198 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200199 struct pipe_buffer *buf = pipe->bufs + newbuf;
Jens Axboe5274f052006-03-30 15:15:30 +0200200
Jens Axboe912d35f2006-04-26 10:59:21 +0200201 buf->page = spd->pages[page_nr];
202 buf->offset = spd->partial[page_nr].offset;
203 buf->len = spd->partial[page_nr].len;
204 buf->ops = spd->ops;
Jens Axboe7afa6fd2006-05-01 20:02:33 +0200205 if (spd->flags & SPLICE_F_GIFT)
206 buf->flags |= PIPE_BUF_FLAG_GIFT;
207
Jens Axboe6f767b02006-04-11 13:53:56 +0200208 pipe->nrbufs++;
Jens Axboe912d35f2006-04-26 10:59:21 +0200209 page_nr++;
210 ret += buf->len;
211
Jens Axboe6f767b02006-04-11 13:53:56 +0200212 if (pipe->inode)
213 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200214
Jens Axboe912d35f2006-04-26 10:59:21 +0200215 if (!--spd->nr_pages)
Jens Axboe5274f052006-03-30 15:15:30 +0200216 break;
Jens Axboe6f767b02006-04-11 13:53:56 +0200217 if (pipe->nrbufs < PIPE_BUFFERS)
Jens Axboe5274f052006-03-30 15:15:30 +0200218 continue;
219
220 break;
221 }
222
Jens Axboe912d35f2006-04-26 10:59:21 +0200223 if (spd->flags & SPLICE_F_NONBLOCK) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700224 if (!ret)
225 ret = -EAGAIN;
226 break;
227 }
228
Jens Axboe5274f052006-03-30 15:15:30 +0200229 if (signal_pending(current)) {
230 if (!ret)
231 ret = -ERESTARTSYS;
232 break;
233 }
234
235 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200236 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200237 if (waitqueue_active(&pipe->wait))
238 wake_up_interruptible_sync(&pipe->wait);
239 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Jens Axboe5274f052006-03-30 15:15:30 +0200240 do_wakeup = 0;
241 }
242
Ingo Molnar3a326a22006-04-10 15:18:35 +0200243 pipe->waiting_writers++;
244 pipe_wait(pipe);
245 pipe->waiting_writers--;
Jens Axboe5274f052006-03-30 15:15:30 +0200246 }
247
Jens Axboe02676e52007-06-15 13:16:13 +0200248 if (pipe->inode) {
Ingo Molnar3a326a22006-04-10 15:18:35 +0200249 mutex_unlock(&pipe->inode->i_mutex);
Jens Axboe5274f052006-03-30 15:15:30 +0200250
Jens Axboe02676e52007-06-15 13:16:13 +0200251 if (do_wakeup) {
252 smp_mb();
253 if (waitqueue_active(&pipe->wait))
254 wake_up_interruptible(&pipe->wait);
255 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
256 }
Jens Axboe5274f052006-03-30 15:15:30 +0200257 }
258
Jens Axboe00de00b2007-06-15 13:14:22 +0200259 while (page_nr < spd_pages)
Jens Axboe912d35f2006-04-26 10:59:21 +0200260 page_cache_release(spd->pages[page_nr++]);
Jens Axboe5274f052006-03-30 15:15:30 +0200261
262 return ret;
263}
264
Ingo Molnar3a326a22006-04-10 15:18:35 +0200265static int
Jens Axboecbb7e572006-04-11 14:57:50 +0200266__generic_file_splice_read(struct file *in, loff_t *ppos,
267 struct pipe_inode_info *pipe, size_t len,
268 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200269{
270 struct address_space *mapping = in->f_mapping;
Jens Axboe912d35f2006-04-26 10:59:21 +0200271 unsigned int loff, nr_pages;
Jens Axboe16c523d2006-04-10 09:03:58 +0200272 struct page *pages[PIPE_BUFFERS];
Jens Axboe912d35f2006-04-26 10:59:21 +0200273 struct partial_page partial[PIPE_BUFFERS];
Jens Axboe5274f052006-03-30 15:15:30 +0200274 struct page *page;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200275 pgoff_t index, end_index;
276 loff_t isize;
Jens Axboeeb207962006-04-27 11:05:22 +0200277 int error, page_nr;
Jens Axboe912d35f2006-04-26 10:59:21 +0200278 struct splice_pipe_desc spd = {
279 .pages = pages,
280 .partial = partial,
281 .flags = flags,
282 .ops = &page_cache_pipe_buf_ops,
283 };
Jens Axboe5274f052006-03-30 15:15:30 +0200284
Jens Axboecbb7e572006-04-11 14:57:50 +0200285 index = *ppos >> PAGE_CACHE_SHIFT;
Jens Axboe912d35f2006-04-26 10:59:21 +0200286 loff = *ppos & ~PAGE_CACHE_MASK;
287 nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe5274f052006-03-30 15:15:30 +0200288
289 if (nr_pages > PIPE_BUFFERS)
290 nr_pages = PIPE_BUFFERS;
291
292 /*
Jens Axboe86aa5ac2007-05-08 08:46:19 +0200293 * Don't try to 2nd guess the read-ahead logic, call into
294 * page_cache_readahead() like the page cache reads would do.
Jens Axboe5274f052006-03-30 15:15:30 +0200295 */
Jens Axboe86aa5ac2007-05-08 08:46:19 +0200296 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
Jens Axboe5274f052006-03-30 15:15:30 +0200297
298 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200299 * Now fill in the holes:
Jens Axboe5274f052006-03-30 15:15:30 +0200300 */
Jens Axboe7480a902006-04-11 13:52:47 +0200301 error = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200302
303 /*
304 * Lookup the (hopefully) full range of pages we need.
305 */
306 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
307
308 /*
309 * If find_get_pages_contig() returned fewer pages than we needed,
310 * allocate the rest.
311 */
312 index += spd.nr_pages;
313 while (spd.nr_pages < nr_pages) {
314 /*
315 * Page could be there, find_get_pages_contig() breaks on
316 * the first hole.
317 */
318 page = find_get_page(mapping, index);
319 if (!page) {
320 /*
Jens Axboee27dedd2006-05-01 19:59:54 +0200321 * Make sure the read-ahead engine is notified
322 * about this failure.
323 */
324 handle_ra_miss(mapping, &in->f_ra, index);
325
326 /*
Jens Axboeeb207962006-04-27 11:05:22 +0200327 * page didn't exist, allocate one.
328 */
329 page = page_cache_alloc_cold(mapping);
330 if (!page)
331 break;
332
333 error = add_to_page_cache_lru(page, mapping, index,
Nick Piggin2ae88142006-10-28 10:38:23 -0700334 GFP_KERNEL);
Jens Axboeeb207962006-04-27 11:05:22 +0200335 if (unlikely(error)) {
336 page_cache_release(page);
Jens Axboea0548872006-05-03 10:58:22 +0200337 if (error == -EEXIST)
338 continue;
Jens Axboeeb207962006-04-27 11:05:22 +0200339 break;
340 }
341 /*
342 * add_to_page_cache() locks the page, unlock it
343 * to avoid convoluting the logic below even more.
344 */
345 unlock_page(page);
346 }
347
348 pages[spd.nr_pages++] = page;
349 index++;
350 }
351
352 /*
353 * Now loop over the map and see if we need to start IO on any
354 * pages, fill in the partial map, etc.
355 */
356 index = *ppos >> PAGE_CACHE_SHIFT;
357 nr_pages = spd.nr_pages;
358 spd.nr_pages = 0;
359 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
Jens Axboe82aa5d62006-04-20 13:05:48 +0200360 unsigned int this_len;
361
362 if (!len)
363 break;
364
365 /*
366 * this_len is the max we'll use from this page
367 */
Andrew Mortonba5f5d92006-04-25 15:33:34 +0200368 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
Jens Axboeeb207962006-04-27 11:05:22 +0200369 page = pages[page_nr];
Jens Axboe7480a902006-04-11 13:52:47 +0200370
371 /*
372 * If the page isn't uptodate, we may need to start io on it
373 */
374 if (!PageUptodate(page)) {
Jens Axboec4f895c2006-04-19 15:56:12 +0200375 /*
376 * If in nonblock mode then dont block on waiting
377 * for an in-flight io page
378 */
Fengguang Wu9ae9d682007-05-08 08:44:36 +0200379 if (flags & SPLICE_F_NONBLOCK) {
380 if (TestSetPageLocked(page))
381 break;
382 } else
383 lock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200384
385 /*
386 * page was truncated, stop here. if this isn't the
387 * first page, we'll just complete what we already
388 * added
389 */
390 if (!page->mapping) {
391 unlock_page(page);
Jens Axboe7480a902006-04-11 13:52:47 +0200392 break;
393 }
394 /*
395 * page was already under io and is now done, great
396 */
397 if (PageUptodate(page)) {
398 unlock_page(page);
399 goto fill_it;
400 }
401
Jens Axboe7480a902006-04-11 13:52:47 +0200402 /*
403 * need to read in the page
404 */
405 error = mapping->a_ops->readpage(in, page);
Jens Axboe7480a902006-04-11 13:52:47 +0200406 if (unlikely(error)) {
Jens Axboeeb207962006-04-27 11:05:22 +0200407 /*
408 * We really should re-lookup the page here,
409 * but it complicates things a lot. Instead
410 * lets just do what we already stored, and
411 * we'll get it the next time we are called.
412 */
Jens Axboe7480a902006-04-11 13:52:47 +0200413 if (error == AOP_TRUNCATED_PAGE)
Jens Axboeeb207962006-04-27 11:05:22 +0200414 error = 0;
415
Jens Axboe7480a902006-04-11 13:52:47 +0200416 break;
417 }
Jens Axboe620a3242007-06-07 09:39:42 +0200418 }
419fill_it:
420 /*
421 * i_size must be checked after PageUptodate.
422 */
423 isize = i_size_read(mapping->host);
424 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
425 if (unlikely(!isize || index > end_index))
426 break;
427
428 /*
429 * if this is the last page, see if we need to shrink
430 * the length and stop
431 */
432 if (end_index == index) {
433 unsigned int plen;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200434
435 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200436 * max good bytes in this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200437 */
Jens Axboe620a3242007-06-07 09:39:42 +0200438 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
439 if (plen <= loff)
Jens Axboe91ad66e2006-04-19 15:55:10 +0200440 break;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200441
442 /*
Jens Axboe620a3242007-06-07 09:39:42 +0200443 * force quit after adding this page
Jens Axboe91ad66e2006-04-19 15:55:10 +0200444 */
Jens Axboe620a3242007-06-07 09:39:42 +0200445 this_len = min(this_len, plen - loff);
446 len = this_len;
Jens Axboe7480a902006-04-11 13:52:47 +0200447 }
Jens Axboe620a3242007-06-07 09:39:42 +0200448
Jens Axboeeb207962006-04-27 11:05:22 +0200449 partial[page_nr].offset = loff;
450 partial[page_nr].len = this_len;
Jens Axboe82aa5d62006-04-20 13:05:48 +0200451 len -= this_len;
Jens Axboe91ad66e2006-04-19 15:55:10 +0200452 loff = 0;
Jens Axboeeb207962006-04-27 11:05:22 +0200453 spd.nr_pages++;
454 index++;
Jens Axboe5274f052006-03-30 15:15:30 +0200455 }
456
Jens Axboeeb207962006-04-27 11:05:22 +0200457 /*
Hugh Dickins475ecad2007-06-07 09:36:00 +0200458 * Release any pages at the end, if we quit early. 'page_nr' is how far
Jens Axboeeb207962006-04-27 11:05:22 +0200459 * we got, 'nr_pages' is how many pages are in the map.
460 */
461 while (page_nr < nr_pages)
462 page_cache_release(pages[page_nr++]);
463
Jens Axboe912d35f2006-04-26 10:59:21 +0200464 if (spd.nr_pages)
Jens Axboe00522fb2006-04-26 14:39:29 +0200465 return splice_to_pipe(pipe, &spd);
Jens Axboe5274f052006-03-30 15:15:30 +0200466
Jens Axboe7480a902006-04-11 13:52:47 +0200467 return error;
Jens Axboe5274f052006-03-30 15:15:30 +0200468}
469
Jens Axboe83f91352006-04-02 23:05:09 +0200470/**
471 * generic_file_splice_read - splice data from file to a pipe
472 * @in: file to splice from
473 * @pipe: pipe to splice to
474 * @len: number of bytes to splice
475 * @flags: splice modifier flags
476 *
477 * Will read pages from given file and fill them into a pipe.
Jens Axboe83f91352006-04-02 23:05:09 +0200478 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200479ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
480 struct pipe_inode_info *pipe, size_t len,
481 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200482{
483 ssize_t spliced;
484 int ret;
Jens Axboed366d3982007-06-01 14:54:11 +0200485 loff_t isize, left;
486
487 isize = i_size_read(in->f_mapping->host);
488 if (unlikely(*ppos >= isize))
489 return 0;
490
491 left = isize - *ppos;
492 if (unlikely(left < len))
493 len = left;
Jens Axboe5274f052006-03-30 15:15:30 +0200494
495 ret = 0;
496 spliced = 0;
497 while (len) {
Jens Axboecbb7e572006-04-11 14:57:50 +0200498 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200499
Jens Axboec4f895c2006-04-19 15:56:12 +0200500 if (ret < 0)
Jens Axboe5274f052006-03-30 15:15:30 +0200501 break;
Jens Axboec4f895c2006-04-19 15:56:12 +0200502 else if (!ret) {
503 if (spliced)
504 break;
505 if (flags & SPLICE_F_NONBLOCK) {
506 ret = -EAGAIN;
507 break;
508 }
509 }
Jens Axboe5274f052006-03-30 15:15:30 +0200510
Jens Axboecbb7e572006-04-11 14:57:50 +0200511 *ppos += ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200512 len -= ret;
513 spliced += ret;
514 }
515
516 if (spliced)
517 return spliced;
518
519 return ret;
520}
521
Jens Axboe059a8f32006-04-02 23:06:05 +0200522EXPORT_SYMBOL(generic_file_splice_read);
523
Jens Axboe5274f052006-03-30 15:15:30 +0200524/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200525 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
Jens Axboe016b6612006-04-25 15:42:00 +0200526 * using sendpage(). Return the number of bytes sent.
Jens Axboe5274f052006-03-30 15:15:30 +0200527 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200528static int pipe_to_sendpage(struct pipe_inode_info *pipe,
Jens Axboe5274f052006-03-30 15:15:30 +0200529 struct pipe_buffer *buf, struct splice_desc *sd)
530{
531 struct file *file = sd->file;
532 loff_t pos = sd->pos;
Jens Axboef84d7512006-05-01 19:59:03 +0200533 int ret, more;
Jens Axboe5274f052006-03-30 15:15:30 +0200534
Jens Axboe76ad4d12006-05-03 10:41:33 +0200535 ret = buf->ops->pin(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200536 if (!ret) {
537 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200538
Jens Axboef84d7512006-05-01 19:59:03 +0200539 ret = file->f_op->sendpage(file, buf->page, buf->offset,
540 sd->len, &pos, more);
541 }
Jens Axboe5274f052006-03-30 15:15:30 +0200542
Jens Axboe016b6612006-04-25 15:42:00 +0200543 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200544}
545
546/*
547 * This is a little more tricky than the file -> pipe splicing. There are
548 * basically three cases:
549 *
550 * - Destination page already exists in the address space and there
551 * are users of it. For that case we have no other option that
552 * copying the data. Tough luck.
553 * - Destination page already exists in the address space, but there
554 * are no users of it. Make sure it's uptodate, then drop it. Fall
555 * through to last case.
556 * - Destination page does not exist, we can add the pipe page to
557 * the page cache and avoid the copy.
558 *
Jens Axboe83f91352006-04-02 23:05:09 +0200559 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
560 * sd->flags), we attempt to migrate pages from the pipe to the output
561 * file address space page cache. This is possible if no one else has
562 * the pipe page referenced outside of the pipe and page cache. If
563 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
564 * a new page in the output file page cache and fill/dirty that.
Jens Axboe5274f052006-03-30 15:15:30 +0200565 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200566static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
Jens Axboe5274f052006-03-30 15:15:30 +0200567 struct splice_desc *sd)
568{
569 struct file *file = sd->file;
570 struct address_space *mapping = file->f_mapping;
Jens Axboe016b6612006-04-25 15:42:00 +0200571 unsigned int offset, this_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200572 struct page *page;
Jens Axboe5274f052006-03-30 15:15:30 +0200573 pgoff_t index;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200574 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200575
576 /*
Jens Axboe49d0b212006-04-10 09:04:41 +0200577 * make sure the data in this buffer is uptodate
Jens Axboe5274f052006-03-30 15:15:30 +0200578 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200579 ret = buf->ops->pin(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200580 if (unlikely(ret))
581 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200582
583 index = sd->pos >> PAGE_CACHE_SHIFT;
584 offset = sd->pos & ~PAGE_CACHE_MASK;
585
Jens Axboe016b6612006-04-25 15:42:00 +0200586 this_len = sd->len;
587 if (this_len + offset > PAGE_CACHE_SIZE)
588 this_len = PAGE_CACHE_SIZE - offset;
589
Jens Axboe5abc97a2006-03-30 15:16:46 +0200590find_page:
Nick Piggin485ddb42007-03-27 08:55:08 +0200591 page = find_lock_page(mapping, index);
592 if (!page) {
593 ret = -ENOMEM;
594 page = page_cache_alloc_cold(mapping);
595 if (unlikely(!page))
596 goto out_ret;
Jens Axboe9e0267c2006-04-19 15:57:31 +0200597
Nick Piggin485ddb42007-03-27 08:55:08 +0200598 /*
599 * This will also lock the page
600 */
601 ret = add_to_page_cache_lru(page, mapping, index,
602 GFP_KERNEL);
603 if (unlikely(ret))
604 goto out;
605 }
606
Jens Axboe016b6612006-04-25 15:42:00 +0200607 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200608 if (unlikely(ret)) {
609 loff_t isize = i_size_read(mapping->host);
610
611 if (ret != AOP_TRUNCATED_PAGE)
612 unlock_page(page);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200613 page_cache_release(page);
Jens Axboebfc4ee32006-05-03 10:35:10 +0200614 if (ret == AOP_TRUNCATED_PAGE)
615 goto find_page;
616
617 /*
618 * prepare_write() may have instantiated a few blocks
619 * outside i_size. Trim these off again.
620 */
621 if (sd->pos + this_len > isize)
622 vmtruncate(mapping->host, isize);
623
Jens Axboee6e80f22006-10-11 10:03:09 +0200624 goto out_ret;
Jens Axboebfc4ee32006-05-03 10:35:10 +0200625 }
Jens Axboe5274f052006-03-30 15:15:30 +0200626
Jens Axboe0568b402006-05-01 19:50:48 +0200627 if (buf->page != page) {
Jens Axboef84d7512006-05-01 19:59:03 +0200628 /*
629 * Careful, ->map() uses KM_USER0!
630 */
Jens Axboe76ad4d12006-05-03 10:41:33 +0200631 char *src = buf->ops->map(pipe, buf, 1);
Jens Axboef84d7512006-05-01 19:59:03 +0200632 char *dst = kmap_atomic(page, KM_USER1);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200633
Jens Axboe016b6612006-04-25 15:42:00 +0200634 memcpy(dst + offset, src + buf->offset, this_len);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200635 flush_dcache_page(page);
Jens Axboef84d7512006-05-01 19:59:03 +0200636 kunmap_atomic(dst, KM_USER1);
Jens Axboe76ad4d12006-05-03 10:41:33 +0200637 buf->ops->unmap(pipe, buf, src);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200638 }
Jens Axboe5274f052006-03-30 15:15:30 +0200639
Jens Axboe016b6612006-04-25 15:42:00 +0200640 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200641 if (ret) {
642 if (ret == AOP_TRUNCATED_PAGE) {
643 page_cache_release(page);
644 goto find_page;
645 }
646 if (ret < 0)
647 goto out;
Jens Axboe0568b402006-05-01 19:50:48 +0200648 /*
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200649 * Partial write has happened, so 'ret' already initialized by
650 * number of bytes written, Where is nothing we have to do here.
Jens Axboe0568b402006-05-01 19:50:48 +0200651 */
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200652 } else
Jens Axboe0568b402006-05-01 19:50:48 +0200653 ret = this_len;
Dmitriy Monakhovd9993c32007-03-29 14:24:09 +0200654 /*
655 * Return the number of bytes written and mark page as
656 * accessed, we are now done!
657 */
658 mark_page_accessed(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200659out:
Jens Axboe0568b402006-05-01 19:50:48 +0200660 page_cache_release(page);
Jens Axboe9e0267c2006-04-19 15:57:31 +0200661 unlock_page(page);
Jens Axboee6e80f22006-10-11 10:03:09 +0200662out_ret:
Jens Axboe5274f052006-03-30 15:15:30 +0200663 return ret;
664}
665
Jens Axboe83f91352006-04-02 23:05:09 +0200666/*
667 * Pipe input worker. Most of this logic works like a regular pipe, the
668 * key here is the 'actor' worker passed in that actually moves the data
669 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
670 */
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100671ssize_t __splice_from_pipe(struct pipe_inode_info *pipe,
672 struct file *out, loff_t *ppos, size_t len,
673 unsigned int flags, splice_actor *actor)
Jens Axboe5274f052006-03-30 15:15:30 +0200674{
Jens Axboe5274f052006-03-30 15:15:30 +0200675 int ret, do_wakeup, err;
676 struct splice_desc sd;
677
678 ret = 0;
679 do_wakeup = 0;
680
681 sd.total_len = len;
682 sd.flags = flags;
683 sd.file = out;
Jens Axboecbb7e572006-04-11 14:57:50 +0200684 sd.pos = *ppos;
Jens Axboe5274f052006-03-30 15:15:30 +0200685
Jens Axboe5274f052006-03-30 15:15:30 +0200686 for (;;) {
Jens Axboe6f767b02006-04-11 13:53:56 +0200687 if (pipe->nrbufs) {
688 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800689 const struct pipe_buf_operations *ops = buf->ops;
Jens Axboe5274f052006-03-30 15:15:30 +0200690
691 sd.len = buf->len;
692 if (sd.len > sd.total_len)
693 sd.len = sd.total_len;
694
Ingo Molnar3a326a22006-04-10 15:18:35 +0200695 err = actor(pipe, buf, &sd);
Jens Axboe016b6612006-04-25 15:42:00 +0200696 if (err <= 0) {
Jens Axboe5274f052006-03-30 15:15:30 +0200697 if (!ret && err != -ENODATA)
698 ret = err;
699
700 break;
701 }
702
Jens Axboe016b6612006-04-25 15:42:00 +0200703 ret += err;
704 buf->offset += err;
705 buf->len -= err;
706
707 sd.len -= err;
708 sd.pos += err;
709 sd.total_len -= err;
710 if (sd.len)
711 continue;
Ingo Molnar73d62d82006-04-11 13:57:21 +0200712
Jens Axboe5274f052006-03-30 15:15:30 +0200713 if (!buf->len) {
714 buf->ops = NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200715 ops->release(pipe, buf);
Jens Axboe6f767b02006-04-11 13:53:56 +0200716 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
717 pipe->nrbufs--;
718 if (pipe->inode)
719 do_wakeup = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200720 }
721
Jens Axboe5274f052006-03-30 15:15:30 +0200722 if (!sd.total_len)
723 break;
724 }
725
Jens Axboe6f767b02006-04-11 13:53:56 +0200726 if (pipe->nrbufs)
Jens Axboe5274f052006-03-30 15:15:30 +0200727 continue;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200728 if (!pipe->writers)
Jens Axboe5274f052006-03-30 15:15:30 +0200729 break;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200730 if (!pipe->waiting_writers) {
Jens Axboe5274f052006-03-30 15:15:30 +0200731 if (ret)
732 break;
733 }
734
Linus Torvalds29e35092006-04-02 12:46:35 -0700735 if (flags & SPLICE_F_NONBLOCK) {
736 if (!ret)
737 ret = -EAGAIN;
738 break;
739 }
740
Jens Axboe5274f052006-03-30 15:15:30 +0200741 if (signal_pending(current)) {
742 if (!ret)
743 ret = -ERESTARTSYS;
744 break;
745 }
746
747 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200748 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200749 if (waitqueue_active(&pipe->wait))
750 wake_up_interruptible_sync(&pipe->wait);
751 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200752 do_wakeup = 0;
753 }
754
Ingo Molnar3a326a22006-04-10 15:18:35 +0200755 pipe_wait(pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200756 }
757
Jens Axboe5274f052006-03-30 15:15:30 +0200758 if (do_wakeup) {
Jens Axboec0bd1f62006-04-10 09:03:32 +0200759 smp_mb();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200760 if (waitqueue_active(&pipe->wait))
761 wake_up_interruptible(&pipe->wait);
762 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Jens Axboe5274f052006-03-30 15:15:30 +0200763 }
764
Jens Axboe5274f052006-03-30 15:15:30 +0200765 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200766}
Mark Fasheh40bee44e2007-03-21 13:11:02 +0100767EXPORT_SYMBOL(__splice_from_pipe);
Jens Axboe5274f052006-03-30 15:15:30 +0200768
Mark Fasheh6da61802006-10-17 18:43:07 +0200769ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
770 loff_t *ppos, size_t len, unsigned int flags,
771 splice_actor *actor)
772{
773 ssize_t ret;
774 struct inode *inode = out->f_mapping->host;
775
776 /*
777 * The actor worker might be calling ->prepare_write and
778 * ->commit_write. Most of the time, these expect i_mutex to
779 * be held. Since this may result in an ABBA deadlock with
780 * pipe->inode, we have to order lock acquiry here.
781 */
782 inode_double_lock(inode, pipe->inode);
783 ret = __splice_from_pipe(pipe, out, ppos, len, flags, actor);
784 inode_double_unlock(inode, pipe->inode);
785
786 return ret;
787}
788
789/**
790 * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
791 * @pipe: pipe info
792 * @out: file to write to
793 * @len: number of bytes to splice
794 * @flags: splice modifier flags
795 *
796 * Will either move or copy pages (determined by @flags options) from
797 * the given pipe inode to the given file. The caller is responsible
798 * for acquiring i_mutex on both inodes.
799 *
800 */
801ssize_t
802generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
803 loff_t *ppos, size_t len, unsigned int flags)
804{
805 struct address_space *mapping = out->f_mapping;
806 struct inode *inode = mapping->host;
807 ssize_t ret;
808 int err;
809
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800810 err = remove_suid(out->f_path.dentry);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200811 if (unlikely(err))
812 return err;
813
Mark Fasheh6da61802006-10-17 18:43:07 +0200814 ret = __splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
815 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200816 unsigned long nr_pages;
817
Mark Fasheh6da61802006-10-17 18:43:07 +0200818 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200819 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Mark Fasheh6da61802006-10-17 18:43:07 +0200820
821 /*
822 * If file or inode is SYNC and we actually wrote some data,
823 * sync it.
824 */
825 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
826 err = generic_osync_inode(inode, mapping,
827 OSYNC_METADATA|OSYNC_DATA);
828
829 if (err)
830 ret = err;
831 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200832 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Mark Fasheh6da61802006-10-17 18:43:07 +0200833 }
834
835 return ret;
836}
837
838EXPORT_SYMBOL(generic_file_splice_write_nolock);
839
Jens Axboe83f91352006-04-02 23:05:09 +0200840/**
841 * generic_file_splice_write - splice data from a pipe to a file
Ingo Molnar3a326a22006-04-10 15:18:35 +0200842 * @pipe: pipe info
Jens Axboe83f91352006-04-02 23:05:09 +0200843 * @out: file to write to
844 * @len: number of bytes to splice
845 * @flags: splice modifier flags
846 *
847 * Will either move or copy pages (determined by @flags options) from
848 * the given pipe inode to the given file.
849 *
850 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200851ssize_t
852generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200853 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200854{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200855 struct address_space *mapping = out->f_mapping;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200856 struct inode *inode = mapping->host;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200857 ssize_t ret;
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200858 int err;
859
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800860 err = should_remove_suid(out->f_path.dentry);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200861 if (unlikely(err)) {
862 mutex_lock(&inode->i_mutex);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800863 err = __remove_suid(out->f_path.dentry, err);
Jens Axboe8c34e2d2006-10-17 19:43:22 +0200864 mutex_unlock(&inode->i_mutex);
865 if (err)
866 return err;
867 }
Ingo Molnar3a326a22006-04-10 15:18:35 +0200868
Jens Axboe00522fb2006-04-26 14:39:29 +0200869 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
Jens Axboea4514eb2006-04-19 15:57:05 +0200870 if (ret > 0) {
Jens Axboe17ee4f42007-06-15 13:10:37 +0200871 unsigned long nr_pages;
872
Jens Axboea4514eb2006-04-19 15:57:05 +0200873 *ppos += ret;
Jens Axboe17ee4f42007-06-15 13:10:37 +0200874 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200875
Jens Axboea4514eb2006-04-19 15:57:05 +0200876 /*
877 * If file or inode is SYNC and we actually wrote some data,
878 * sync it.
879 */
880 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
Jens Axboea4514eb2006-04-19 15:57:05 +0200881 mutex_lock(&inode->i_mutex);
882 err = generic_osync_inode(inode, mapping,
883 OSYNC_METADATA|OSYNC_DATA);
884 mutex_unlock(&inode->i_mutex);
885
886 if (err)
887 ret = err;
888 }
Jens Axboe17ee4f42007-06-15 13:10:37 +0200889 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200890 }
891
892 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200893}
894
Jens Axboe059a8f32006-04-02 23:06:05 +0200895EXPORT_SYMBOL(generic_file_splice_write);
896
Jens Axboe83f91352006-04-02 23:05:09 +0200897/**
898 * generic_splice_sendpage - splice data from a pipe to a socket
899 * @inode: pipe inode
900 * @out: socket to write to
901 * @len: number of bytes to splice
902 * @flags: splice modifier flags
903 *
904 * Will send @len bytes from the pipe to a network socket. No data copying
905 * is involved.
906 *
907 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200908ssize_t generic_splice_sendpage(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 Axboe00522fb2006-04-26 14:39:29 +0200911 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
Jens Axboe5274f052006-03-30 15:15:30 +0200912}
913
Jens Axboe059a8f32006-04-02 23:06:05 +0200914EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500915
Jens Axboe83f91352006-04-02 23:05:09 +0200916/*
917 * Attempt to initiate a splice from pipe to file.
918 */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200919static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
Jens Axboecbb7e572006-04-11 14:57:50 +0200920 loff_t *ppos, size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200921{
Jens Axboe5274f052006-03-30 15:15:30 +0200922 int ret;
923
Jens Axboe49570e92006-04-11 13:56:09 +0200924 if (unlikely(!out->f_op || !out->f_op->splice_write))
Jens Axboe5274f052006-03-30 15:15:30 +0200925 return -EINVAL;
926
Jens Axboe49570e92006-04-11 13:56:09 +0200927 if (unlikely(!(out->f_mode & FMODE_WRITE)))
Jens Axboe5274f052006-03-30 15:15:30 +0200928 return -EBADF;
929
Jens Axboecbb7e572006-04-11 14:57:50 +0200930 ret = rw_verify_area(WRITE, out, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200931 if (unlikely(ret < 0))
932 return ret;
933
Jens Axboecbb7e572006-04-11 14:57:50 +0200934 return out->f_op->splice_write(pipe, out, ppos, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200935}
936
Jens Axboe83f91352006-04-02 23:05:09 +0200937/*
938 * Attempt to initiate a splice from a file to a pipe.
939 */
Jens Axboecbb7e572006-04-11 14:57:50 +0200940static long do_splice_to(struct file *in, loff_t *ppos,
941 struct pipe_inode_info *pipe, size_t len,
942 unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200943{
Jens Axboe5274f052006-03-30 15:15:30 +0200944 int ret;
945
Jens Axboe49570e92006-04-11 13:56:09 +0200946 if (unlikely(!in->f_op || !in->f_op->splice_read))
Jens Axboe5274f052006-03-30 15:15:30 +0200947 return -EINVAL;
948
Jens Axboe49570e92006-04-11 13:56:09 +0200949 if (unlikely(!(in->f_mode & FMODE_READ)))
Jens Axboe5274f052006-03-30 15:15:30 +0200950 return -EBADF;
951
Jens Axboecbb7e572006-04-11 14:57:50 +0200952 ret = rw_verify_area(READ, in, ppos, len);
Jens Axboe5274f052006-03-30 15:15:30 +0200953 if (unlikely(ret < 0))
954 return ret;
955
Jens Axboecbb7e572006-04-11 14:57:50 +0200956 return in->f_op->splice_read(in, ppos, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200957}
958
Jens Axboecbb7e572006-04-11 14:57:50 +0200959long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
960 size_t len, unsigned int flags)
Jens Axboeb92ce552006-04-11 13:52:07 +0200961{
962 struct pipe_inode_info *pipe;
963 long ret, bytes;
Jens Axboecbb7e572006-04-11 14:57:50 +0200964 loff_t out_off;
Jens Axboeb92ce552006-04-11 13:52:07 +0200965 umode_t i_mode;
966 int i;
967
968 /*
969 * We require the input being a regular file, as we don't want to
970 * randomly drop data for eg socket -> socket splicing. Use the
971 * piped splicing for that!
972 */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800973 i_mode = in->f_path.dentry->d_inode->i_mode;
Jens Axboeb92ce552006-04-11 13:52:07 +0200974 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
975 return -EINVAL;
976
977 /*
978 * neither in nor out is a pipe, setup an internal pipe attached to
979 * 'out' and transfer the wanted data from 'in' to 'out' through that
980 */
981 pipe = current->splice_pipe;
Jens Axboe49570e92006-04-11 13:56:09 +0200982 if (unlikely(!pipe)) {
Jens Axboeb92ce552006-04-11 13:52:07 +0200983 pipe = alloc_pipe_info(NULL);
984 if (!pipe)
985 return -ENOMEM;
986
987 /*
988 * We don't have an immediate reader, but we'll read the stuff
Jens Axboe00522fb2006-04-26 14:39:29 +0200989 * out of the pipe right after the splice_to_pipe(). So set
Jens Axboeb92ce552006-04-11 13:52:07 +0200990 * PIPE_READERS appropriately.
991 */
992 pipe->readers = 1;
993
994 current->splice_pipe = pipe;
995 }
996
997 /*
Ingo Molnar73d62d82006-04-11 13:57:21 +0200998 * Do the splice.
Jens Axboeb92ce552006-04-11 13:52:07 +0200999 */
1000 ret = 0;
1001 bytes = 0;
Jens Axboecbb7e572006-04-11 14:57:50 +02001002 out_off = 0;
Jens Axboeb92ce552006-04-11 13:52:07 +02001003
1004 while (len) {
1005 size_t read_len, max_read_len;
1006
1007 /*
1008 * Do at most PIPE_BUFFERS pages worth of transfer:
1009 */
1010 max_read_len = min(len, (size_t)(PIPE_BUFFERS*PAGE_SIZE));
1011
Jens Axboecbb7e572006-04-11 14:57:50 +02001012 ret = do_splice_to(in, ppos, pipe, max_read_len, flags);
Jens Axboeb92ce552006-04-11 13:52:07 +02001013 if (unlikely(ret < 0))
1014 goto out_release;
1015
1016 read_len = ret;
1017
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 Axboecbb7e572006-04-11 14:57:50 +02001023 ret = do_splice_from(pipe, out, &out_off, read_len,
Jens Axboeb92ce552006-04-11 13:52:07 +02001024 flags & ~SPLICE_F_NONBLOCK);
1025 if (unlikely(ret < 0))
1026 goto out_release;
1027
1028 bytes += ret;
1029 len -= ret;
1030
1031 /*
1032 * In nonblocking mode, if we got back a short read then
1033 * that was due to either an IO error or due to the
1034 * pagecache entry not being there. In the IO error case
1035 * the _next_ splice attempt will produce a clean IO error
1036 * return value (not a short read), so in both cases it's
1037 * correct to break out of the loop here:
1038 */
1039 if ((flags & SPLICE_F_NONBLOCK) && (read_len < max_read_len))
1040 break;
1041 }
1042
1043 pipe->nrbufs = pipe->curbuf = 0;
1044
1045 return bytes;
1046
1047out_release:
1048 /*
1049 * If we did an incomplete transfer we must release
1050 * the pipe buffers in question:
1051 */
1052 for (i = 0; i < PIPE_BUFFERS; i++) {
1053 struct pipe_buffer *buf = pipe->bufs + i;
1054
1055 if (buf->ops) {
1056 buf->ops->release(pipe, buf);
1057 buf->ops = NULL;
1058 }
1059 }
1060 pipe->nrbufs = pipe->curbuf = 0;
1061
1062 /*
1063 * If we transferred some data, return the number of bytes:
1064 */
1065 if (bytes > 0)
1066 return bytes;
1067
1068 return ret;
1069}
1070
Jens Axboe83f91352006-04-02 23:05:09 +02001071/*
Jens Axboeddac0d32006-11-04 12:49:32 +01001072 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1073 * location, so checking ->i_pipe is not enough to verify that this is a
1074 * pipe.
1075 */
1076static inline struct pipe_inode_info *pipe_info(struct inode *inode)
1077{
1078 if (S_ISFIFO(inode->i_mode))
1079 return inode->i_pipe;
1080
1081 return NULL;
1082}
1083
1084/*
Jens Axboe83f91352006-04-02 23:05:09 +02001085 * Determine where to splice to/from.
1086 */
Ingo Molnar529565d2006-04-10 15:18:58 +02001087static long do_splice(struct file *in, loff_t __user *off_in,
1088 struct file *out, loff_t __user *off_out,
1089 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001090{
Ingo Molnar3a326a22006-04-10 15:18:35 +02001091 struct pipe_inode_info *pipe;
Jens Axboecbb7e572006-04-11 14:57:50 +02001092 loff_t offset, *off;
Jens Axboea4514eb2006-04-19 15:57:05 +02001093 long ret;
Jens Axboe5274f052006-03-30 15:15:30 +02001094
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001095 pipe = pipe_info(in->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001096 if (pipe) {
1097 if (off_in)
1098 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001099 if (off_out) {
1100 if (out->f_op->llseek == no_llseek)
1101 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001102 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001103 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001104 off = &offset;
1105 } else
1106 off = &out->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001107
Jens Axboea4514eb2006-04-19 15:57:05 +02001108 ret = do_splice_from(pipe, out, off, len, flags);
1109
1110 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1111 ret = -EFAULT;
1112
1113 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001114 }
Jens Axboe5274f052006-03-30 15:15:30 +02001115
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001116 pipe = pipe_info(out->f_path.dentry->d_inode);
Ingo Molnar529565d2006-04-10 15:18:58 +02001117 if (pipe) {
1118 if (off_out)
1119 return -ESPIPE;
Jens Axboeb92ce552006-04-11 13:52:07 +02001120 if (off_in) {
1121 if (in->f_op->llseek == no_llseek)
1122 return -EINVAL;
Jens Axboecbb7e572006-04-11 14:57:50 +02001123 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
Jens Axboeb92ce552006-04-11 13:52:07 +02001124 return -EFAULT;
Jens Axboecbb7e572006-04-11 14:57:50 +02001125 off = &offset;
1126 } else
1127 off = &in->f_pos;
Ingo Molnar529565d2006-04-10 15:18:58 +02001128
Jens Axboea4514eb2006-04-19 15:57:05 +02001129 ret = do_splice_to(in, off, pipe, len, flags);
1130
1131 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1132 ret = -EFAULT;
1133
1134 return ret;
Ingo Molnar529565d2006-04-10 15:18:58 +02001135 }
Jens Axboe5274f052006-03-30 15:15:30 +02001136
1137 return -EINVAL;
1138}
1139
Jens Axboe912d35f2006-04-26 10:59:21 +02001140/*
1141 * Map an iov into an array of pages and offset/length tupples. With the
1142 * partial_page structure, we can map several non-contiguous ranges into
1143 * our ones pages[] map instead of splitting that operation into pieces.
1144 * Could easily be exported as a generic helper for other users, in which
1145 * case one would probably want to add a 'max_nr_pages' parameter as well.
1146 */
1147static int get_iovec_page_array(const struct iovec __user *iov,
1148 unsigned int nr_vecs, struct page **pages,
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001149 struct partial_page *partial, int aligned)
Jens Axboe912d35f2006-04-26 10:59:21 +02001150{
1151 int buffers = 0, error = 0;
1152
1153 /*
1154 * It's ok to take the mmap_sem for reading, even
1155 * across a "get_user()".
1156 */
1157 down_read(&current->mm->mmap_sem);
1158
1159 while (nr_vecs) {
1160 unsigned long off, npages;
1161 void __user *base;
1162 size_t len;
1163 int i;
1164
1165 /*
1166 * Get user address base and length for this iovec.
1167 */
1168 error = get_user(base, &iov->iov_base);
1169 if (unlikely(error))
1170 break;
1171 error = get_user(len, &iov->iov_len);
1172 if (unlikely(error))
1173 break;
1174
1175 /*
1176 * Sanity check this iovec. 0 read succeeds.
1177 */
1178 if (unlikely(!len))
1179 break;
1180 error = -EFAULT;
1181 if (unlikely(!base))
1182 break;
1183
1184 /*
1185 * Get this base offset and number of pages, then map
1186 * in the user pages.
1187 */
1188 off = (unsigned long) base & ~PAGE_MASK;
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001189
1190 /*
1191 * If asked for alignment, the offset must be zero and the
1192 * length a multiple of the PAGE_SIZE.
1193 */
1194 error = -EINVAL;
1195 if (aligned && (off || len & ~PAGE_MASK))
1196 break;
1197
Jens Axboe912d35f2006-04-26 10:59:21 +02001198 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1199 if (npages > PIPE_BUFFERS - buffers)
1200 npages = PIPE_BUFFERS - buffers;
1201
1202 error = get_user_pages(current, current->mm,
1203 (unsigned long) base, npages, 0, 0,
1204 &pages[buffers], NULL);
1205
1206 if (unlikely(error <= 0))
1207 break;
1208
1209 /*
1210 * Fill this contiguous range into the partial page map.
1211 */
1212 for (i = 0; i < error; i++) {
Jens Axboe75914892006-05-02 12:57:18 +02001213 const int plen = min_t(size_t, len, PAGE_SIZE - off);
Jens Axboe912d35f2006-04-26 10:59:21 +02001214
1215 partial[buffers].offset = off;
1216 partial[buffers].len = plen;
1217
1218 off = 0;
1219 len -= plen;
1220 buffers++;
1221 }
1222
1223 /*
1224 * We didn't complete this iov, stop here since it probably
1225 * means we have to move some of this into a pipe to
1226 * be able to continue.
1227 */
1228 if (len)
1229 break;
1230
1231 /*
1232 * Don't continue if we mapped fewer pages than we asked for,
1233 * or if we mapped the max number of pages that we have
1234 * room for.
1235 */
1236 if (error < npages || buffers == PIPE_BUFFERS)
1237 break;
1238
1239 nr_vecs--;
1240 iov++;
1241 }
1242
1243 up_read(&current->mm->mmap_sem);
1244
1245 if (buffers)
1246 return buffers;
1247
1248 return error;
1249}
1250
1251/*
1252 * vmsplice splices a user address range into a pipe. It can be thought of
1253 * as splice-from-memory, where the regular splice is splice-from-file (or
1254 * to file). In both cases the output is a pipe, naturally.
1255 *
1256 * Note that vmsplice only supports splicing _from_ user memory to a pipe,
1257 * not the other way around. Splicing from user memory is a simple operation
1258 * that can be supported without any funky alignment restrictions or nasty
1259 * vm tricks. We simply map in the user memory and fill them into a pipe.
1260 * The reverse isn't quite as easy, though. There are two possible solutions
1261 * for that:
1262 *
1263 * - memcpy() the data internally, at which point we might as well just
1264 * do a regular read() on the buffer anyway.
1265 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1266 * has restriction limitations on both ends of the pipe).
1267 *
1268 * Alas, it isn't here.
1269 *
1270 */
1271static long do_vmsplice(struct file *file, const struct iovec __user *iov,
1272 unsigned long nr_segs, unsigned int flags)
1273{
Jens Axboeddac0d32006-11-04 12:49:32 +01001274 struct pipe_inode_info *pipe;
Jens Axboe912d35f2006-04-26 10:59:21 +02001275 struct page *pages[PIPE_BUFFERS];
1276 struct partial_page partial[PIPE_BUFFERS];
1277 struct splice_pipe_desc spd = {
1278 .pages = pages,
1279 .partial = partial,
1280 .flags = flags,
1281 .ops = &user_page_pipe_buf_ops,
1282 };
1283
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001284 pipe = pipe_info(file->f_path.dentry->d_inode);
Jens Axboeddac0d32006-11-04 12:49:32 +01001285 if (!pipe)
Jens Axboe912d35f2006-04-26 10:59:21 +02001286 return -EBADF;
1287 if (unlikely(nr_segs > UIO_MAXIOV))
1288 return -EINVAL;
1289 else if (unlikely(!nr_segs))
1290 return 0;
1291
Jens Axboe7afa6fd2006-05-01 20:02:33 +02001292 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1293 flags & SPLICE_F_GIFT);
Jens Axboe912d35f2006-04-26 10:59:21 +02001294 if (spd.nr_pages <= 0)
1295 return spd.nr_pages;
1296
Jens Axboe00522fb2006-04-26 14:39:29 +02001297 return splice_to_pipe(pipe, &spd);
Jens Axboe912d35f2006-04-26 10:59:21 +02001298}
1299
1300asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1301 unsigned long nr_segs, unsigned int flags)
1302{
1303 struct file *file;
1304 long error;
1305 int fput;
1306
1307 error = -EBADF;
1308 file = fget_light(fd, &fput);
1309 if (file) {
1310 if (file->f_mode & FMODE_WRITE)
1311 error = do_vmsplice(file, iov, nr_segs, flags);
1312
1313 fput_light(file, fput);
1314 }
1315
1316 return error;
1317}
1318
Ingo Molnar529565d2006-04-10 15:18:58 +02001319asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1320 int fd_out, loff_t __user *off_out,
1321 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +02001322{
1323 long error;
1324 struct file *in, *out;
1325 int fput_in, fput_out;
1326
1327 if (unlikely(!len))
1328 return 0;
1329
1330 error = -EBADF;
Ingo Molnar529565d2006-04-10 15:18:58 +02001331 in = fget_light(fd_in, &fput_in);
Jens Axboe5274f052006-03-30 15:15:30 +02001332 if (in) {
1333 if (in->f_mode & FMODE_READ) {
Ingo Molnar529565d2006-04-10 15:18:58 +02001334 out = fget_light(fd_out, &fput_out);
Jens Axboe5274f052006-03-30 15:15:30 +02001335 if (out) {
1336 if (out->f_mode & FMODE_WRITE)
Ingo Molnar529565d2006-04-10 15:18:58 +02001337 error = do_splice(in, off_in,
1338 out, off_out,
1339 len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +02001340 fput_light(out, fput_out);
1341 }
1342 }
1343
1344 fput_light(in, fput_in);
1345 }
1346
1347 return error;
1348}
Jens Axboe70524492006-04-11 15:51:17 +02001349
1350/*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001351 * Make sure there's data to read. Wait for input if we can, otherwise
1352 * return an appropriate error.
1353 */
1354static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1355{
1356 int ret;
1357
1358 /*
1359 * Check ->nrbufs without the inode lock first. This function
1360 * is speculative anyways, so missing one is ok.
1361 */
1362 if (pipe->nrbufs)
1363 return 0;
1364
1365 ret = 0;
1366 mutex_lock(&pipe->inode->i_mutex);
1367
1368 while (!pipe->nrbufs) {
1369 if (signal_pending(current)) {
1370 ret = -ERESTARTSYS;
1371 break;
1372 }
1373 if (!pipe->writers)
1374 break;
1375 if (!pipe->waiting_writers) {
1376 if (flags & SPLICE_F_NONBLOCK) {
1377 ret = -EAGAIN;
1378 break;
1379 }
1380 }
1381 pipe_wait(pipe);
1382 }
1383
1384 mutex_unlock(&pipe->inode->i_mutex);
1385 return ret;
1386}
1387
1388/*
1389 * Make sure there's writeable room. Wait for room if we can, otherwise
1390 * return an appropriate error.
1391 */
1392static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1393{
1394 int ret;
1395
1396 /*
1397 * Check ->nrbufs without the inode lock first. This function
1398 * is speculative anyways, so missing one is ok.
1399 */
1400 if (pipe->nrbufs < PIPE_BUFFERS)
1401 return 0;
1402
1403 ret = 0;
1404 mutex_lock(&pipe->inode->i_mutex);
1405
1406 while (pipe->nrbufs >= PIPE_BUFFERS) {
1407 if (!pipe->readers) {
1408 send_sig(SIGPIPE, current, 0);
1409 ret = -EPIPE;
1410 break;
1411 }
1412 if (flags & SPLICE_F_NONBLOCK) {
1413 ret = -EAGAIN;
1414 break;
1415 }
1416 if (signal_pending(current)) {
1417 ret = -ERESTARTSYS;
1418 break;
1419 }
1420 pipe->waiting_writers++;
1421 pipe_wait(pipe);
1422 pipe->waiting_writers--;
1423 }
1424
1425 mutex_unlock(&pipe->inode->i_mutex);
1426 return ret;
1427}
1428
1429/*
Jens Axboe70524492006-04-11 15:51:17 +02001430 * Link contents of ipipe to opipe.
1431 */
1432static int link_pipe(struct pipe_inode_info *ipipe,
1433 struct pipe_inode_info *opipe,
1434 size_t len, unsigned int flags)
1435{
1436 struct pipe_buffer *ibuf, *obuf;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001437 int ret = 0, i = 0, nbuf;
Jens Axboe70524492006-04-11 15:51:17 +02001438
1439 /*
1440 * Potential ABBA deadlock, work around it by ordering lock
1441 * grabbing by inode address. Otherwise two different processes
1442 * could deadlock (one doing tee from A -> B, the other from B -> A).
1443 */
Mark Fasheh62752ee2006-10-17 10:31:38 +02001444 inode_double_lock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001445
Jens Axboeaadd06e2006-07-10 11:00:01 +02001446 do {
Jens Axboe70524492006-04-11 15:51:17 +02001447 if (!opipe->readers) {
1448 send_sig(SIGPIPE, current, 0);
1449 if (!ret)
1450 ret = -EPIPE;
1451 break;
1452 }
Jens Axboe70524492006-04-11 15:51:17 +02001453
1454 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001455 * If we have iterated all input buffers or ran out of
1456 * output room, break.
Jens Axboe70524492006-04-11 15:51:17 +02001457 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001458 if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
Jens Axboe70524492006-04-11 15:51:17 +02001459 break;
Jens Axboeaadd06e2006-07-10 11:00:01 +02001460
1461 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1462 nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1463
Jens Axboe2a27250e2006-04-19 15:56:40 +02001464 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001465 * Get a reference to this pipe buffer,
1466 * so we can copy the contents over.
Jens Axboe2a27250e2006-04-19 15:56:40 +02001467 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001468 ibuf->ops->get(ipipe, ibuf);
Jens Axboe70524492006-04-11 15:51:17 +02001469
Jens Axboeaadd06e2006-07-10 11:00:01 +02001470 obuf = opipe->bufs + nbuf;
1471 *obuf = *ibuf;
Jens Axboe70524492006-04-11 15:51:17 +02001472
Jens Axboeaadd06e2006-07-10 11:00:01 +02001473 /*
1474 * Don't inherit the gift flag, we need to
1475 * prevent multiple steals of this page.
1476 */
1477 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1478
1479 if (obuf->len > len)
1480 obuf->len = len;
1481
1482 opipe->nrbufs++;
1483 ret += obuf->len;
1484 len -= obuf->len;
1485 i++;
1486 } while (len);
Jens Axboe70524492006-04-11 15:51:17 +02001487
Mark Fasheh62752ee2006-10-17 10:31:38 +02001488 inode_double_unlock(ipipe->inode, opipe->inode);
Jens Axboe70524492006-04-11 15:51:17 +02001489
Jens Axboeaadd06e2006-07-10 11:00:01 +02001490 /*
1491 * If we put data in the output pipe, wakeup any potential readers.
1492 */
1493 if (ret > 0) {
Jens Axboe70524492006-04-11 15:51:17 +02001494 smp_mb();
1495 if (waitqueue_active(&opipe->wait))
1496 wake_up_interruptible(&opipe->wait);
1497 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1498 }
1499
1500 return ret;
1501}
1502
1503/*
1504 * This is a tee(1) implementation that works on pipes. It doesn't copy
1505 * any data, it simply references the 'in' pages on the 'out' pipe.
1506 * The 'flags' used are the SPLICE_F_* variants, currently the only
1507 * applicable one is SPLICE_F_NONBLOCK.
1508 */
1509static long do_tee(struct file *in, struct file *out, size_t len,
1510 unsigned int flags)
1511{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001512 struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
1513 struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
Jens Axboeaadd06e2006-07-10 11:00:01 +02001514 int ret = -EINVAL;
Jens Axboe70524492006-04-11 15:51:17 +02001515
1516 /*
Jens Axboeaadd06e2006-07-10 11:00:01 +02001517 * Duplicate the contents of ipipe to opipe without actually
1518 * copying the data.
Jens Axboe70524492006-04-11 15:51:17 +02001519 */
Jens Axboeaadd06e2006-07-10 11:00:01 +02001520 if (ipipe && opipe && ipipe != opipe) {
1521 /*
1522 * Keep going, unless we encounter an error. The ipipe/opipe
1523 * ordering doesn't really matter.
1524 */
1525 ret = link_ipipe_prep(ipipe, flags);
1526 if (!ret) {
1527 ret = link_opipe_prep(opipe, flags);
1528 if (!ret) {
1529 ret = link_pipe(ipipe, opipe, len, flags);
1530 if (!ret && (flags & SPLICE_F_NONBLOCK))
1531 ret = -EAGAIN;
1532 }
1533 }
1534 }
Jens Axboe70524492006-04-11 15:51:17 +02001535
Jens Axboeaadd06e2006-07-10 11:00:01 +02001536 return ret;
Jens Axboe70524492006-04-11 15:51:17 +02001537}
1538
1539asmlinkage long sys_tee(int fdin, int fdout, size_t len, unsigned int flags)
1540{
1541 struct file *in;
1542 int error, fput_in;
1543
1544 if (unlikely(!len))
1545 return 0;
1546
1547 error = -EBADF;
1548 in = fget_light(fdin, &fput_in);
1549 if (in) {
1550 if (in->f_mode & FMODE_READ) {
1551 int fput_out;
1552 struct file *out = fget_light(fdout, &fput_out);
1553
1554 if (out) {
1555 if (out->f_mode & FMODE_WRITE)
1556 error = do_tee(in, out, len, flags);
1557 fput_light(out, fput_out);
1558 }
1559 }
1560 fput_light(in, fput_in);
1561 }
1562
1563 return error;
1564}