2 * "splice": joining two ropes together by interweaving their strands.
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.
8 * The traditional unix read/write is extended with a "splice()" operation
9 * that transfers data buffers to or from a pipe buffer.
11 * Named by Larry McVoy, original implementation from Linus, extended by
12 * Jens to support splicing to files and fixing the initial implementation
15 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
16 * Copyright (C) 2005 Linus Torvalds <torvalds@osdl.org>
20 #include <linux/file.h>
21 #include <linux/pagemap.h>
22 #include <linux/pipe_fs_i.h>
23 #include <linux/mm_inline.h>
24 #include <linux/swap.h>
25 #include <linux/writeback.h>
26 #include <linux/buffer_head.h>
27 #include <linux/module.h>
28 #include <linux/syscalls.h>
31 * Passed to the actors
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 */
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.
46 static int page_cache_pipe_buf_steal(struct pipe_inode_info
*info
,
47 struct pipe_buffer
*buf
)
49 struct page
*page
= buf
->page
;
50 struct address_space
*mapping
= page_mapping(page
);
52 WARN_ON(!PageLocked(page
));
53 WARN_ON(!PageUptodate(page
));
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.
62 wait_on_page_writeback(page
);
64 if (PagePrivate(page
))
65 try_to_release_page(page
, mapping_gfp_mask(mapping
));
67 if (!remove_mapping(mapping
, page
))
71 struct zone
*zone
= page_zone(page
);
73 spin_lock_irq(&zone
->lru_lock
);
74 BUG_ON(!PageLRU(page
));
76 del_page_from_lru(zone
, page
);
77 spin_unlock_irq(&zone
->lru_lock
);
83 static void page_cache_pipe_buf_release(struct pipe_inode_info
*info
,
84 struct pipe_buffer
*buf
)
86 page_cache_release(buf
->page
);
90 static void *page_cache_pipe_buf_map(struct file
*file
,
91 struct pipe_inode_info
*info
,
92 struct pipe_buffer
*buf
)
94 struct page
*page
= buf
->page
;
98 if (!PageUptodate(page
)) {
100 return ERR_PTR(-EIO
);
103 if (!page
->mapping
) {
105 return ERR_PTR(-ENODATA
);
108 return kmap(buf
->page
);
111 static void page_cache_pipe_buf_unmap(struct pipe_inode_info
*info
,
112 struct pipe_buffer
*buf
)
114 unlock_page(buf
->page
);
118 static struct pipe_buf_operations page_cache_pipe_buf_ops
= {
120 .map
= page_cache_pipe_buf_map
,
121 .unmap
= page_cache_pipe_buf_unmap
,
122 .release
= page_cache_pipe_buf_release
,
123 .steal
= page_cache_pipe_buf_steal
,
127 * Pipe output worker. This sets up our pipe format with the page cache
128 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
130 static ssize_t
move_to_pipe(struct inode
*inode
, struct page
**pages
,
131 int nr_pages
, unsigned long offset
,
132 unsigned long len
, unsigned int flags
)
134 struct pipe_inode_info
*info
;
135 int ret
, do_wakeup
, i
;
141 mutex_lock(PIPE_MUTEX(*inode
));
143 info
= inode
->i_pipe
;
147 if (!PIPE_READERS(*inode
)) {
148 send_sig(SIGPIPE
, current
, 0);
155 if (bufs
< PIPE_BUFFERS
) {
156 int newbuf
= (info
->curbuf
+ bufs
) & (PIPE_BUFFERS
- 1);
157 struct pipe_buffer
*buf
= info
->bufs
+ newbuf
;
158 struct page
*page
= pages
[i
++];
159 unsigned long this_len
;
161 this_len
= PAGE_CACHE_SIZE
- offset
;
166 buf
->offset
= offset
;
168 buf
->ops
= &page_cache_pipe_buf_ops
;
169 info
->nrbufs
= ++bufs
;
179 if (bufs
< PIPE_BUFFERS
)
185 if (flags
& SPLICE_F_NONBLOCK
) {
191 if (signal_pending(current
)) {
198 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
199 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
,
204 PIPE_WAITING_WRITERS(*inode
)++;
206 PIPE_WAITING_WRITERS(*inode
)--;
209 mutex_unlock(PIPE_MUTEX(*inode
));
212 wake_up_interruptible(PIPE_WAIT(*inode
));
213 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
, POLL_IN
);
217 page_cache_release(pages
[i
++]);
222 static int __generic_file_splice_read(struct file
*in
, struct inode
*pipe
,
223 size_t len
, unsigned int flags
)
225 struct address_space
*mapping
= in
->f_mapping
;
226 unsigned int offset
, nr_pages
;
227 struct page
*pages
[PIPE_BUFFERS
], *shadow
[PIPE_BUFFERS
];
232 index
= in
->f_pos
>> PAGE_CACHE_SHIFT
;
233 offset
= in
->f_pos
& ~PAGE_CACHE_MASK
;
234 nr_pages
= (len
+ offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
236 if (nr_pages
> PIPE_BUFFERS
)
237 nr_pages
= PIPE_BUFFERS
;
240 * initiate read-ahead on this page range
242 do_page_cache_readahead(mapping
, in
, index
, nr_pages
);
245 * Get as many pages from the page cache as possible..
246 * Start IO on the page cache entries we create (we
247 * can assume that any pre-existing ones we find have
248 * already had IO started on them).
250 i
= find_get_pages(mapping
, index
, nr_pages
, pages
);
253 * common case - we found all pages and they are contiguous,
256 if (i
&& (pages
[i
- 1]->index
== index
+ i
- 1))
260 * fill shadow[] with pages at the right locations, so we only
263 memset(shadow
, 0, nr_pages
* sizeof(struct page
*));
264 for (j
= 0; j
< i
; j
++)
265 shadow
[pages
[j
]->index
- index
] = pages
[j
];
268 * now fill in the holes
270 for (i
= 0, pidx
= index
; i
< nr_pages
; pidx
++, i
++) {
277 * no page there, look one up / create it
279 page
= find_or_create_page(mapping
, pidx
,
280 mapping_gfp_mask(mapping
));
284 if (PageUptodate(page
))
287 error
= mapping
->a_ops
->readpage(in
, page
);
289 if (unlikely(error
)) {
290 page_cache_release(page
);
298 for (i
= 0; i
< nr_pages
; i
++) {
300 page_cache_release(shadow
[i
]);
305 memcpy(pages
, shadow
, i
* sizeof(struct page
*));
308 * Now we splice them into the pipe..
311 return move_to_pipe(pipe
, pages
, i
, offset
, len
, flags
);
315 * generic_file_splice_read - splice data from file to a pipe
316 * @in: file to splice from
317 * @pipe: pipe to splice to
318 * @len: number of bytes to splice
319 * @flags: splice modifier flags
321 * Will read pages from given file and fill them into a pipe.
324 ssize_t
generic_file_splice_read(struct file
*in
, struct inode
*pipe
,
325 size_t len
, unsigned int flags
)
333 ret
= __generic_file_splice_read(in
, pipe
, len
, flags
);
342 if (!(flags
& SPLICE_F_NONBLOCK
))
354 EXPORT_SYMBOL(generic_file_splice_read
);
357 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
360 static int pipe_to_sendpage(struct pipe_inode_info
*info
,
361 struct pipe_buffer
*buf
, struct splice_desc
*sd
)
363 struct file
*file
= sd
->file
;
364 loff_t pos
= sd
->pos
;
371 * sub-optimal, but we are limited by the pipe ->map. we don't
372 * need a kmap'ed buffer here, we just want to make sure we
373 * have the page pinned if the pipe page originates from the
376 ptr
= buf
->ops
->map(file
, info
, buf
);
380 offset
= pos
& ~PAGE_CACHE_MASK
;
381 more
= (sd
->flags
& SPLICE_F_MORE
) || sd
->len
< sd
->total_len
;
383 ret
= file
->f_op
->sendpage(file
, buf
->page
, offset
, sd
->len
, &pos
,more
);
385 buf
->ops
->unmap(info
, buf
);
393 * This is a little more tricky than the file -> pipe splicing. There are
394 * basically three cases:
396 * - Destination page already exists in the address space and there
397 * are users of it. For that case we have no other option that
398 * copying the data. Tough luck.
399 * - Destination page already exists in the address space, but there
400 * are no users of it. Make sure it's uptodate, then drop it. Fall
401 * through to last case.
402 * - Destination page does not exist, we can add the pipe page to
403 * the page cache and avoid the copy.
405 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
406 * sd->flags), we attempt to migrate pages from the pipe to the output
407 * file address space page cache. This is possible if no one else has
408 * the pipe page referenced outside of the pipe and page cache. If
409 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
410 * a new page in the output file page cache and fill/dirty that.
412 static int pipe_to_file(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
413 struct splice_desc
*sd
)
415 struct file
*file
= sd
->file
;
416 struct address_space
*mapping
= file
->f_mapping
;
424 * after this, page will be locked and unmapped
426 src
= buf
->ops
->map(file
, info
, buf
);
430 index
= sd
->pos
>> PAGE_CACHE_SHIFT
;
431 offset
= sd
->pos
& ~PAGE_CACHE_MASK
;
435 * reuse buf page, if SPLICE_F_MOVE is set
437 if (sd
->flags
& SPLICE_F_MOVE
) {
439 * If steal succeeds, buf->page is now pruned from the vm
440 * side (LRU and page cache) and we can reuse it.
442 if (buf
->ops
->steal(info
, buf
))
447 if (add_to_page_cache_lru(page
, mapping
, index
,
448 mapping_gfp_mask(mapping
)))
453 page
= find_or_create_page(mapping
, index
,
454 mapping_gfp_mask(mapping
));
459 * If the page is uptodate, it is also locked. If it isn't
460 * uptodate, we can mark it uptodate if we are filling the
461 * full page. Otherwise we need to read it in first...
463 if (!PageUptodate(page
)) {
464 if (sd
->len
< PAGE_CACHE_SIZE
) {
465 ret
= mapping
->a_ops
->readpage(file
, page
);
471 if (!PageUptodate(page
)) {
473 * page got invalidated, repeat
475 if (!page
->mapping
) {
477 page_cache_release(page
);
484 WARN_ON(!PageLocked(page
));
485 SetPageUptodate(page
);
490 ret
= mapping
->a_ops
->prepare_write(file
, page
, 0, sd
->len
);
491 if (ret
== AOP_TRUNCATED_PAGE
) {
492 page_cache_release(page
);
498 char *dst
= kmap_atomic(page
, KM_USER0
);
500 memcpy(dst
+ offset
, src
+ buf
->offset
, sd
->len
);
501 flush_dcache_page(page
);
502 kunmap_atomic(dst
, KM_USER0
);
505 ret
= mapping
->a_ops
->commit_write(file
, page
, 0, sd
->len
);
506 if (ret
== AOP_TRUNCATED_PAGE
) {
507 page_cache_release(page
);
512 balance_dirty_pages_ratelimited(mapping
);
515 page_cache_release(page
);
518 buf
->ops
->unmap(info
, buf
);
522 typedef int (splice_actor
)(struct pipe_inode_info
*, struct pipe_buffer
*,
523 struct splice_desc
*);
526 * Pipe input worker. Most of this logic works like a regular pipe, the
527 * key here is the 'actor' worker passed in that actually moves the data
528 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
530 static ssize_t
move_from_pipe(struct inode
*inode
, struct file
*out
,
531 size_t len
, unsigned int flags
,
534 struct pipe_inode_info
*info
;
535 int ret
, do_wakeup
, err
;
536 struct splice_desc sd
;
546 mutex_lock(PIPE_MUTEX(*inode
));
548 info
= inode
->i_pipe
;
550 int bufs
= info
->nrbufs
;
553 int curbuf
= info
->curbuf
;
554 struct pipe_buffer
*buf
= info
->bufs
+ curbuf
;
555 struct pipe_buf_operations
*ops
= buf
->ops
;
558 if (sd
.len
> sd
.total_len
)
559 sd
.len
= sd
.total_len
;
561 err
= actor(info
, buf
, &sd
);
563 if (!ret
&& err
!= -ENODATA
)
570 buf
->offset
+= sd
.len
;
574 ops
->release(info
, buf
);
575 curbuf
= (curbuf
+ 1) & (PIPE_BUFFERS
- 1);
576 info
->curbuf
= curbuf
;
577 info
->nrbufs
= --bufs
;
582 sd
.total_len
-= sd
.len
;
589 if (!PIPE_WRITERS(*inode
))
591 if (!PIPE_WAITING_WRITERS(*inode
)) {
596 if (flags
& SPLICE_F_NONBLOCK
) {
602 if (signal_pending(current
)) {
609 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
610 kill_fasync(PIPE_FASYNC_WRITERS(*inode
),SIGIO
,POLL_OUT
);
617 mutex_unlock(PIPE_MUTEX(*inode
));
620 wake_up_interruptible(PIPE_WAIT(*inode
));
621 kill_fasync(PIPE_FASYNC_WRITERS(*inode
), SIGIO
, POLL_OUT
);
624 mutex_lock(&out
->f_mapping
->host
->i_mutex
);
626 mutex_unlock(&out
->f_mapping
->host
->i_mutex
);
632 * generic_file_splice_write - splice data from a pipe to a file
634 * @out: file to write to
635 * @len: number of bytes to splice
636 * @flags: splice modifier flags
638 * Will either move or copy pages (determined by @flags options) from
639 * the given pipe inode to the given file.
642 ssize_t
generic_file_splice_write(struct inode
*inode
, struct file
*out
,
643 size_t len
, unsigned int flags
)
645 struct address_space
*mapping
= out
->f_mapping
;
646 ssize_t ret
= move_from_pipe(inode
, out
, len
, flags
, pipe_to_file
);
649 * if file or inode is SYNC and we actually wrote some data, sync it
651 if (unlikely((out
->f_flags
& O_SYNC
) || IS_SYNC(mapping
->host
))
653 struct inode
*inode
= mapping
->host
;
656 mutex_lock(&inode
->i_mutex
);
657 err
= generic_osync_inode(mapping
->host
, mapping
,
658 OSYNC_METADATA
|OSYNC_DATA
);
659 mutex_unlock(&inode
->i_mutex
);
668 EXPORT_SYMBOL(generic_file_splice_write
);
671 * generic_splice_sendpage - splice data from a pipe to a socket
673 * @out: socket to write to
674 * @len: number of bytes to splice
675 * @flags: splice modifier flags
677 * Will send @len bytes from the pipe to a network socket. No data copying
681 ssize_t
generic_splice_sendpage(struct inode
*inode
, struct file
*out
,
682 size_t len
, unsigned int flags
)
684 return move_from_pipe(inode
, out
, len
, flags
, pipe_to_sendpage
);
687 EXPORT_SYMBOL(generic_splice_sendpage
);
690 * Attempt to initiate a splice from pipe to file.
692 static long do_splice_from(struct inode
*pipe
, struct file
*out
, size_t len
,
698 if (!out
->f_op
|| !out
->f_op
->splice_write
)
701 if (!(out
->f_mode
& FMODE_WRITE
))
705 ret
= rw_verify_area(WRITE
, out
, &pos
, len
);
706 if (unlikely(ret
< 0))
709 return out
->f_op
->splice_write(pipe
, out
, len
, flags
);
713 * Attempt to initiate a splice from a file to a pipe.
715 static long do_splice_to(struct file
*in
, struct inode
*pipe
, size_t len
,
718 loff_t pos
, isize
, left
;
721 if (!in
->f_op
|| !in
->f_op
->splice_read
)
724 if (!(in
->f_mode
& FMODE_READ
))
728 ret
= rw_verify_area(READ
, in
, &pos
, len
);
729 if (unlikely(ret
< 0))
732 isize
= i_size_read(in
->f_mapping
->host
);
733 if (unlikely(in
->f_pos
>= isize
))
736 left
= isize
- in
->f_pos
;
740 return in
->f_op
->splice_read(in
, pipe
, len
, flags
);
744 * Determine where to splice to/from.
746 static long do_splice(struct file
*in
, struct file
*out
, size_t len
,
751 pipe
= in
->f_dentry
->d_inode
;
753 return do_splice_from(pipe
, out
, len
, flags
);
755 pipe
= out
->f_dentry
->d_inode
;
757 return do_splice_to(in
, pipe
, len
, flags
);
762 asmlinkage
long sys_splice(int fdin
, int fdout
, size_t len
, unsigned int flags
)
765 struct file
*in
, *out
;
766 int fput_in
, fput_out
;
772 in
= fget_light(fdin
, &fput_in
);
774 if (in
->f_mode
& FMODE_READ
) {
775 out
= fget_light(fdout
, &fput_out
);
777 if (out
->f_mode
& FMODE_WRITE
)
778 error
= do_splice(in
, out
, len
, flags
);
779 fput_light(out
, fput_out
);
783 fput_light(in
, fput_in
);