blob: b450acdff3975e86713e87df718e57ca74f0d7b9 [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
12 * Jens to support splicing to files and fixing the initial implementation
13 * bugs.
14 *
15 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
16 * Copyright (C) 2005 Linus Torvalds <torvalds@osdl.org>
17 *
18 */
19#include <linux/fs.h>
20#include <linux/file.h>
21#include <linux/pagemap.h>
22#include <linux/pipe_fs_i.h>
23#include <linux/mm_inline.h>
Jens Axboe5abc97a2006-03-30 15:16:46 +020024#include <linux/swap.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020025#include <linux/writeback.h>
26#include <linux/buffer_head.h>
Jeff Garzika0f06782006-03-30 23:06:13 -050027#include <linux/module.h>
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020028#include <linux/syscalls.h>
Jens Axboe5274f052006-03-30 15:15:30 +020029
30/*
31 * Passed to the actors
32 */
33struct splice_desc {
34 unsigned int len, total_len; /* current and remaining length */
35 unsigned int flags; /* splice flags */
36 struct file *file; /* file to read/write */
37 loff_t pos; /* file position */
38};
39
Jens Axboe83f91352006-04-02 23:05:09 +020040/*
41 * Attempt to steal a page from a pipe buffer. This should perhaps go into
42 * a vm helper function, it's already simplified quite a bit by the
43 * addition of remove_mapping(). If success is returned, the caller may
44 * attempt to reuse this page for another destination.
45 */
Jens Axboe5abc97a2006-03-30 15:16:46 +020046static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
47 struct pipe_buffer *buf)
48{
49 struct page *page = buf->page;
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020050 struct address_space *mapping = page_mapping(page);
Jens Axboe5abc97a2006-03-30 15:16:46 +020051
52 WARN_ON(!PageLocked(page));
53 WARN_ON(!PageUptodate(page));
54
Jens Axboead8d6f02006-04-02 23:10:32 +020055 /*
56 * At least for ext2 with nobh option, we need to wait on writeback
57 * completing on this page, since we'll remove it from the pagecache.
58 * Otherwise truncate wont wait on the page, allowing the disk
59 * blocks to be reused by someone else before we actually wrote our
60 * data to them. fs corruption ensues.
61 */
62 wait_on_page_writeback(page);
63
Jens Axboe4f6f0bd2006-04-02 23:04:46 +020064 if (PagePrivate(page))
65 try_to_release_page(page, mapping_gfp_mask(mapping));
66
67 if (!remove_mapping(mapping, page))
Jens Axboe5abc97a2006-03-30 15:16:46 +020068 return 1;
69
Jens Axboe3e7ee3e2006-04-02 23:11:04 +020070 buf->flags |= PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU;
Jens Axboe5abc97a2006-03-30 15:16:46 +020071 return 0;
72}
73
Jens Axboe5274f052006-03-30 15:15:30 +020074static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
75 struct pipe_buffer *buf)
76{
77 page_cache_release(buf->page);
78 buf->page = NULL;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +020079 buf->flags &= ~(PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU);
Jens Axboe5274f052006-03-30 15:15:30 +020080}
81
82static void *page_cache_pipe_buf_map(struct file *file,
83 struct pipe_inode_info *info,
84 struct pipe_buffer *buf)
85{
86 struct page *page = buf->page;
87
88 lock_page(page);
89
90 if (!PageUptodate(page)) {
91 unlock_page(page);
92 return ERR_PTR(-EIO);
93 }
94
95 if (!page->mapping) {
96 unlock_page(page);
97 return ERR_PTR(-ENODATA);
98 }
99
100 return kmap(buf->page);
101}
102
103static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
104 struct pipe_buffer *buf)
105{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200106 unlock_page(buf->page);
Jens Axboe5274f052006-03-30 15:15:30 +0200107 kunmap(buf->page);
108}
109
110static struct pipe_buf_operations page_cache_pipe_buf_ops = {
111 .can_merge = 0,
112 .map = page_cache_pipe_buf_map,
113 .unmap = page_cache_pipe_buf_unmap,
114 .release = page_cache_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200115 .steal = page_cache_pipe_buf_steal,
Jens Axboe5274f052006-03-30 15:15:30 +0200116};
117
Jens Axboe83f91352006-04-02 23:05:09 +0200118/*
119 * Pipe output worker. This sets up our pipe format with the page cache
120 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
121 */
Jens Axboe5274f052006-03-30 15:15:30 +0200122static ssize_t move_to_pipe(struct inode *inode, struct page **pages,
123 int nr_pages, unsigned long offset,
Linus Torvalds29e35092006-04-02 12:46:35 -0700124 unsigned long len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200125{
126 struct pipe_inode_info *info;
127 int ret, do_wakeup, i;
128
129 ret = 0;
130 do_wakeup = 0;
131 i = 0;
132
133 mutex_lock(PIPE_MUTEX(*inode));
134
135 info = inode->i_pipe;
136 for (;;) {
137 int bufs;
138
139 if (!PIPE_READERS(*inode)) {
140 send_sig(SIGPIPE, current, 0);
141 if (!ret)
142 ret = -EPIPE;
143 break;
144 }
145
146 bufs = info->nrbufs;
147 if (bufs < PIPE_BUFFERS) {
148 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS - 1);
149 struct pipe_buffer *buf = info->bufs + newbuf;
150 struct page *page = pages[i++];
151 unsigned long this_len;
152
153 this_len = PAGE_CACHE_SIZE - offset;
154 if (this_len > len)
155 this_len = len;
156
157 buf->page = page;
158 buf->offset = offset;
159 buf->len = this_len;
160 buf->ops = &page_cache_pipe_buf_ops;
161 info->nrbufs = ++bufs;
162 do_wakeup = 1;
163
164 ret += this_len;
165 len -= this_len;
166 offset = 0;
167 if (!--nr_pages)
168 break;
169 if (!len)
170 break;
171 if (bufs < PIPE_BUFFERS)
172 continue;
173
174 break;
175 }
176
Linus Torvalds29e35092006-04-02 12:46:35 -0700177 if (flags & SPLICE_F_NONBLOCK) {
178 if (!ret)
179 ret = -EAGAIN;
180 break;
181 }
182
Jens Axboe5274f052006-03-30 15:15:30 +0200183 if (signal_pending(current)) {
184 if (!ret)
185 ret = -ERESTARTSYS;
186 break;
187 }
188
189 if (do_wakeup) {
190 wake_up_interruptible_sync(PIPE_WAIT(*inode));
191 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO,
192 POLL_IN);
193 do_wakeup = 0;
194 }
195
196 PIPE_WAITING_WRITERS(*inode)++;
197 pipe_wait(inode);
198 PIPE_WAITING_WRITERS(*inode)--;
199 }
200
201 mutex_unlock(PIPE_MUTEX(*inode));
202
203 if (do_wakeup) {
204 wake_up_interruptible(PIPE_WAIT(*inode));
205 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
206 }
207
208 while (i < nr_pages)
209 page_cache_release(pages[i++]);
210
211 return ret;
212}
213
214static int __generic_file_splice_read(struct file *in, struct inode *pipe,
Linus Torvalds29e35092006-04-02 12:46:35 -0700215 size_t len, unsigned int flags)
Jens Axboe5274f052006-03-30 15:15:30 +0200216{
217 struct address_space *mapping = in->f_mapping;
218 unsigned int offset, nr_pages;
219 struct page *pages[PIPE_BUFFERS], *shadow[PIPE_BUFFERS];
220 struct page *page;
221 pgoff_t index, pidx;
222 int i, j;
223
224 index = in->f_pos >> PAGE_CACHE_SHIFT;
225 offset = in->f_pos & ~PAGE_CACHE_MASK;
226 nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
227
228 if (nr_pages > PIPE_BUFFERS)
229 nr_pages = PIPE_BUFFERS;
230
231 /*
232 * initiate read-ahead on this page range
233 */
234 do_page_cache_readahead(mapping, in, index, nr_pages);
235
236 /*
237 * Get as many pages from the page cache as possible..
238 * Start IO on the page cache entries we create (we
239 * can assume that any pre-existing ones we find have
240 * already had IO started on them).
241 */
242 i = find_get_pages(mapping, index, nr_pages, pages);
243
244 /*
245 * common case - we found all pages and they are contiguous,
246 * kick them off
247 */
248 if (i && (pages[i - 1]->index == index + i - 1))
249 goto splice_them;
250
251 /*
252 * fill shadow[] with pages at the right locations, so we only
253 * have to fill holes
254 */
Jens Axboe53cd9ae2006-04-02 23:04:21 +0200255 memset(shadow, 0, nr_pages * sizeof(struct page *));
256 for (j = 0; j < i; j++)
257 shadow[pages[j]->index - index] = pages[j];
Jens Axboe5274f052006-03-30 15:15:30 +0200258
259 /*
260 * now fill in the holes
261 */
262 for (i = 0, pidx = index; i < nr_pages; pidx++, i++) {
263 int error;
264
265 if (shadow[i])
266 continue;
267
268 /*
269 * no page there, look one up / create it
270 */
271 page = find_or_create_page(mapping, pidx,
272 mapping_gfp_mask(mapping));
273 if (!page)
274 break;
275
276 if (PageUptodate(page))
277 unlock_page(page);
278 else {
279 error = mapping->a_ops->readpage(in, page);
280
281 if (unlikely(error)) {
282 page_cache_release(page);
283 break;
284 }
285 }
286 shadow[i] = page;
287 }
288
289 if (!i) {
290 for (i = 0; i < nr_pages; i++) {
291 if (shadow[i])
292 page_cache_release(shadow[i]);
293 }
294 return 0;
295 }
296
297 memcpy(pages, shadow, i * sizeof(struct page *));
298
299 /*
300 * Now we splice them into the pipe..
301 */
302splice_them:
Linus Torvalds29e35092006-04-02 12:46:35 -0700303 return move_to_pipe(pipe, pages, i, offset, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200304}
305
Jens Axboe83f91352006-04-02 23:05:09 +0200306/**
307 * generic_file_splice_read - splice data from file to a pipe
308 * @in: file to splice from
309 * @pipe: pipe to splice to
310 * @len: number of bytes to splice
311 * @flags: splice modifier flags
312 *
313 * Will read pages from given file and fill them into a pipe.
314 *
315 */
Jens Axboe5274f052006-03-30 15:15:30 +0200316ssize_t generic_file_splice_read(struct file *in, struct inode *pipe,
317 size_t len, unsigned int flags)
318{
319 ssize_t spliced;
320 int ret;
321
322 ret = 0;
323 spliced = 0;
324 while (len) {
Linus Torvalds29e35092006-04-02 12:46:35 -0700325 ret = __generic_file_splice_read(in, pipe, len, flags);
Jens Axboe5274f052006-03-30 15:15:30 +0200326
327 if (ret <= 0)
328 break;
329
330 in->f_pos += ret;
331 len -= ret;
332 spliced += ret;
Linus Torvalds29e35092006-04-02 12:46:35 -0700333
334 if (!(flags & SPLICE_F_NONBLOCK))
335 continue;
336 ret = -EAGAIN;
337 break;
Jens Axboe5274f052006-03-30 15:15:30 +0200338 }
339
340 if (spliced)
341 return spliced;
342
343 return ret;
344}
345
Jens Axboe059a8f32006-04-02 23:06:05 +0200346EXPORT_SYMBOL(generic_file_splice_read);
347
Jens Axboe5274f052006-03-30 15:15:30 +0200348/*
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200349 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
350 * using sendpage().
Jens Axboe5274f052006-03-30 15:15:30 +0200351 */
352static int pipe_to_sendpage(struct pipe_inode_info *info,
353 struct pipe_buffer *buf, struct splice_desc *sd)
354{
355 struct file *file = sd->file;
356 loff_t pos = sd->pos;
357 unsigned int offset;
358 ssize_t ret;
359 void *ptr;
Jens Axboeb2b39fa2006-04-02 23:05:41 +0200360 int more;
Jens Axboe5274f052006-03-30 15:15:30 +0200361
362 /*
363 * sub-optimal, but we are limited by the pipe ->map. we don't
364 * need a kmap'ed buffer here, we just want to make sure we
365 * have the page pinned if the pipe page originates from the
366 * page cache
367 */
368 ptr = buf->ops->map(file, info, buf);
369 if (IS_ERR(ptr))
370 return PTR_ERR(ptr);
371
372 offset = pos & ~PAGE_CACHE_MASK;
Jens Axboeb2b39fa2006-04-02 23:05:41 +0200373 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
Jens Axboe5274f052006-03-30 15:15:30 +0200374
Jens Axboeb2b39fa2006-04-02 23:05:41 +0200375 ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,more);
Jens Axboe5274f052006-03-30 15:15:30 +0200376
377 buf->ops->unmap(info, buf);
378 if (ret == sd->len)
379 return 0;
380
381 return -EIO;
382}
383
384/*
385 * This is a little more tricky than the file -> pipe splicing. There are
386 * basically three cases:
387 *
388 * - Destination page already exists in the address space and there
389 * are users of it. For that case we have no other option that
390 * copying the data. Tough luck.
391 * - Destination page already exists in the address space, but there
392 * are no users of it. Make sure it's uptodate, then drop it. Fall
393 * through to last case.
394 * - Destination page does not exist, we can add the pipe page to
395 * the page cache and avoid the copy.
396 *
Jens Axboe83f91352006-04-02 23:05:09 +0200397 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
398 * sd->flags), we attempt to migrate pages from the pipe to the output
399 * file address space page cache. This is possible if no one else has
400 * the pipe page referenced outside of the pipe and page cache. If
401 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
402 * a new page in the output file page cache and fill/dirty that.
Jens Axboe5274f052006-03-30 15:15:30 +0200403 */
404static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
405 struct splice_desc *sd)
406{
407 struct file *file = sd->file;
408 struct address_space *mapping = file->f_mapping;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200409 gfp_t gfp_mask = mapping_gfp_mask(mapping);
Jens Axboe5274f052006-03-30 15:15:30 +0200410 unsigned int offset;
411 struct page *page;
Jens Axboe5274f052006-03-30 15:15:30 +0200412 pgoff_t index;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200413 char *src;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200414 int ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200415
416 /*
417 * after this, page will be locked and unmapped
418 */
419 src = buf->ops->map(file, info, buf);
420 if (IS_ERR(src))
421 return PTR_ERR(src);
422
423 index = sd->pos >> PAGE_CACHE_SHIFT;
424 offset = sd->pos & ~PAGE_CACHE_MASK;
425
Jens Axboe5274f052006-03-30 15:15:30 +0200426 /*
Jens Axboe5abc97a2006-03-30 15:16:46 +0200427 * reuse buf page, if SPLICE_F_MOVE is set
Jens Axboe5274f052006-03-30 15:15:30 +0200428 */
Jens Axboe5abc97a2006-03-30 15:16:46 +0200429 if (sd->flags & SPLICE_F_MOVE) {
Jens Axboe83f91352006-04-02 23:05:09 +0200430 /*
431 * If steal succeeds, buf->page is now pruned from the vm
432 * side (LRU and page cache) and we can reuse it.
433 */
Jens Axboe5abc97a2006-03-30 15:16:46 +0200434 if (buf->ops->steal(info, buf))
435 goto find_page;
Jens Axboe5274f052006-03-30 15:15:30 +0200436
Jens Axboe5abc97a2006-03-30 15:16:46 +0200437 page = buf->page;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200438 if (add_to_page_cache(page, mapping, index, gfp_mask))
Jens Axboe5abc97a2006-03-30 15:16:46 +0200439 goto find_page;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200440
441 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
442 lru_cache_add(page);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200443 } else {
444find_page:
445 ret = -ENOMEM;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200446 page = find_or_create_page(mapping, index, gfp_mask);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200447 if (!page)
448 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200449
Jens Axboe5abc97a2006-03-30 15:16:46 +0200450 /*
451 * If the page is uptodate, it is also locked. If it isn't
452 * uptodate, we can mark it uptodate if we are filling the
453 * full page. Otherwise we need to read it in first...
454 */
455 if (!PageUptodate(page)) {
456 if (sd->len < PAGE_CACHE_SIZE) {
457 ret = mapping->a_ops->readpage(file, page);
458 if (unlikely(ret))
459 goto out;
460
461 lock_page(page);
462
463 if (!PageUptodate(page)) {
464 /*
465 * page got invalidated, repeat
466 */
467 if (!page->mapping) {
468 unlock_page(page);
469 page_cache_release(page);
470 goto find_page;
471 }
472 ret = -EIO;
473 goto out;
Jens Axboe5274f052006-03-30 15:15:30 +0200474 }
Jens Axboe5abc97a2006-03-30 15:16:46 +0200475 } else {
476 WARN_ON(!PageLocked(page));
477 SetPageUptodate(page);
Jens Axboe5274f052006-03-30 15:15:30 +0200478 }
Jens Axboe5274f052006-03-30 15:15:30 +0200479 }
480 }
481
482 ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200483 if (ret == AOP_TRUNCATED_PAGE) {
484 page_cache_release(page);
485 goto find_page;
486 } else if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200487 goto out;
488
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200489 if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
Jens Axboe5abc97a2006-03-30 15:16:46 +0200490 char *dst = kmap_atomic(page, KM_USER0);
491
492 memcpy(dst + offset, src + buf->offset, sd->len);
493 flush_dcache_page(page);
494 kunmap_atomic(dst, KM_USER0);
495 }
Jens Axboe5274f052006-03-30 15:15:30 +0200496
497 ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200498 if (ret == AOP_TRUNCATED_PAGE) {
499 page_cache_release(page);
500 goto find_page;
501 } else if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200502 goto out;
503
Jens Axboec7f21e42006-04-10 09:01:01 +0200504 mark_page_accessed(page);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200505 balance_dirty_pages_ratelimited(mapping);
Jens Axboe5274f052006-03-30 15:15:30 +0200506out:
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200507 if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) {
Jens Axboe5abc97a2006-03-30 15:16:46 +0200508 page_cache_release(page);
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200509 unlock_page(page);
510 }
Jens Axboe5274f052006-03-30 15:15:30 +0200511 buf->ops->unmap(info, buf);
512 return ret;
513}
514
515typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
516 struct splice_desc *);
517
Jens Axboe83f91352006-04-02 23:05:09 +0200518/*
519 * Pipe input worker. Most of this logic works like a regular pipe, the
520 * key here is the 'actor' worker passed in that actually moves the data
521 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
522 */
Jens Axboe5274f052006-03-30 15:15:30 +0200523static ssize_t move_from_pipe(struct inode *inode, struct file *out,
524 size_t len, unsigned int flags,
525 splice_actor *actor)
526{
527 struct pipe_inode_info *info;
528 int ret, do_wakeup, err;
529 struct splice_desc sd;
530
531 ret = 0;
532 do_wakeup = 0;
533
534 sd.total_len = len;
535 sd.flags = flags;
536 sd.file = out;
537 sd.pos = out->f_pos;
538
539 mutex_lock(PIPE_MUTEX(*inode));
540
541 info = inode->i_pipe;
542 for (;;) {
543 int bufs = info->nrbufs;
544
545 if (bufs) {
546 int curbuf = info->curbuf;
547 struct pipe_buffer *buf = info->bufs + curbuf;
548 struct pipe_buf_operations *ops = buf->ops;
549
550 sd.len = buf->len;
551 if (sd.len > sd.total_len)
552 sd.len = sd.total_len;
553
554 err = actor(info, buf, &sd);
555 if (err) {
556 if (!ret && err != -ENODATA)
557 ret = err;
558
559 break;
560 }
561
562 ret += sd.len;
563 buf->offset += sd.len;
564 buf->len -= sd.len;
565 if (!buf->len) {
566 buf->ops = NULL;
567 ops->release(info, buf);
568 curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1);
569 info->curbuf = curbuf;
570 info->nrbufs = --bufs;
571 do_wakeup = 1;
572 }
573
574 sd.pos += sd.len;
575 sd.total_len -= sd.len;
576 if (!sd.total_len)
577 break;
578 }
579
580 if (bufs)
581 continue;
582 if (!PIPE_WRITERS(*inode))
583 break;
584 if (!PIPE_WAITING_WRITERS(*inode)) {
585 if (ret)
586 break;
587 }
588
Linus Torvalds29e35092006-04-02 12:46:35 -0700589 if (flags & SPLICE_F_NONBLOCK) {
590 if (!ret)
591 ret = -EAGAIN;
592 break;
593 }
594
Jens Axboe5274f052006-03-30 15:15:30 +0200595 if (signal_pending(current)) {
596 if (!ret)
597 ret = -ERESTARTSYS;
598 break;
599 }
600
601 if (do_wakeup) {
602 wake_up_interruptible_sync(PIPE_WAIT(*inode));
603 kill_fasync(PIPE_FASYNC_WRITERS(*inode),SIGIO,POLL_OUT);
604 do_wakeup = 0;
605 }
606
607 pipe_wait(inode);
608 }
609
610 mutex_unlock(PIPE_MUTEX(*inode));
611
612 if (do_wakeup) {
613 wake_up_interruptible(PIPE_WAIT(*inode));
614 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
615 }
616
617 mutex_lock(&out->f_mapping->host->i_mutex);
618 out->f_pos = sd.pos;
619 mutex_unlock(&out->f_mapping->host->i_mutex);
620 return ret;
621
622}
623
Jens Axboe83f91352006-04-02 23:05:09 +0200624/**
625 * generic_file_splice_write - splice data from a pipe to a file
626 * @inode: pipe inode
627 * @out: file to write to
628 * @len: number of bytes to splice
629 * @flags: splice modifier flags
630 *
631 * Will either move or copy pages (determined by @flags options) from
632 * the given pipe inode to the given file.
633 *
634 */
Jens Axboe5274f052006-03-30 15:15:30 +0200635ssize_t generic_file_splice_write(struct inode *inode, struct file *out,
636 size_t len, unsigned int flags)
637{
Jens Axboe4f6f0bd2006-04-02 23:04:46 +0200638 struct address_space *mapping = out->f_mapping;
639 ssize_t ret = move_from_pipe(inode, out, len, flags, pipe_to_file);
640
641 /*
642 * if file or inode is SYNC and we actually wrote some data, sync it
643 */
644 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(mapping->host))
645 && ret > 0) {
646 struct inode *inode = mapping->host;
647 int err;
648
649 mutex_lock(&inode->i_mutex);
650 err = generic_osync_inode(mapping->host, mapping,
651 OSYNC_METADATA|OSYNC_DATA);
652 mutex_unlock(&inode->i_mutex);
653
654 if (err)
655 ret = err;
656 }
657
658 return ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200659}
660
Jens Axboe059a8f32006-04-02 23:06:05 +0200661EXPORT_SYMBOL(generic_file_splice_write);
662
Jens Axboe83f91352006-04-02 23:05:09 +0200663/**
664 * generic_splice_sendpage - splice data from a pipe to a socket
665 * @inode: pipe inode
666 * @out: socket to write to
667 * @len: number of bytes to splice
668 * @flags: splice modifier flags
669 *
670 * Will send @len bytes from the pipe to a network socket. No data copying
671 * is involved.
672 *
673 */
Jens Axboe5274f052006-03-30 15:15:30 +0200674ssize_t generic_splice_sendpage(struct inode *inode, struct file *out,
675 size_t len, unsigned int flags)
676{
677 return move_from_pipe(inode, out, len, flags, pipe_to_sendpage);
678}
679
Jens Axboe059a8f32006-04-02 23:06:05 +0200680EXPORT_SYMBOL(generic_splice_sendpage);
Jeff Garzika0f06782006-03-30 23:06:13 -0500681
Jens Axboe83f91352006-04-02 23:05:09 +0200682/*
683 * Attempt to initiate a splice from pipe to file.
684 */
Jens Axboe5274f052006-03-30 15:15:30 +0200685static long do_splice_from(struct inode *pipe, struct file *out, size_t len,
686 unsigned int flags)
687{
688 loff_t pos;
689 int ret;
690
691 if (!out->f_op || !out->f_op->splice_write)
692 return -EINVAL;
693
694 if (!(out->f_mode & FMODE_WRITE))
695 return -EBADF;
696
697 pos = out->f_pos;
698 ret = rw_verify_area(WRITE, out, &pos, len);
699 if (unlikely(ret < 0))
700 return ret;
701
702 return out->f_op->splice_write(pipe, out, len, flags);
703}
704
Jens Axboe83f91352006-04-02 23:05:09 +0200705/*
706 * Attempt to initiate a splice from a file to a pipe.
707 */
Jens Axboe5274f052006-03-30 15:15:30 +0200708static long do_splice_to(struct file *in, struct inode *pipe, size_t len,
709 unsigned int flags)
710{
711 loff_t pos, isize, left;
712 int ret;
713
714 if (!in->f_op || !in->f_op->splice_read)
715 return -EINVAL;
716
717 if (!(in->f_mode & FMODE_READ))
718 return -EBADF;
719
720 pos = in->f_pos;
721 ret = rw_verify_area(READ, in, &pos, len);
722 if (unlikely(ret < 0))
723 return ret;
724
725 isize = i_size_read(in->f_mapping->host);
726 if (unlikely(in->f_pos >= isize))
727 return 0;
728
729 left = isize - in->f_pos;
730 if (left < len)
731 len = left;
732
733 return in->f_op->splice_read(in, pipe, len, flags);
734}
735
Jens Axboe83f91352006-04-02 23:05:09 +0200736/*
737 * Determine where to splice to/from.
738 */
Jens Axboe5274f052006-03-30 15:15:30 +0200739static long do_splice(struct file *in, struct file *out, size_t len,
740 unsigned int flags)
741{
742 struct inode *pipe;
743
744 pipe = in->f_dentry->d_inode;
745 if (pipe->i_pipe)
746 return do_splice_from(pipe, out, len, flags);
747
748 pipe = out->f_dentry->d_inode;
749 if (pipe->i_pipe)
750 return do_splice_to(in, pipe, len, flags);
751
752 return -EINVAL;
753}
754
755asmlinkage long sys_splice(int fdin, int fdout, size_t len, unsigned int flags)
756{
757 long error;
758 struct file *in, *out;
759 int fput_in, fput_out;
760
761 if (unlikely(!len))
762 return 0;
763
764 error = -EBADF;
765 in = fget_light(fdin, &fput_in);
766 if (in) {
767 if (in->f_mode & FMODE_READ) {
768 out = fget_light(fdout, &fput_out);
769 if (out) {
770 if (out->f_mode & FMODE_WRITE)
771 error = do_splice(in, out, len, flags);
772 fput_light(out, fput_out);
773 }
774 }
775
776 fput_light(in, fput_in);
777 }
778
779 return error;
780}