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 */
40 static int page_cache_pipe_buf_steal(struct pipe_inode_info
*info
,
41 struct pipe_buffer
*buf
)
43 struct page
*page
= buf
->page
;
44 struct address_space
*mapping
= page_mapping(page
);
46 WARN_ON(!PageLocked(page
));
47 WARN_ON(!PageUptodate(page
));
49 if (PagePrivate(page
))
50 try_to_release_page(page
, mapping_gfp_mask(mapping
));
52 if (!remove_mapping(mapping
, page
))
56 struct zone
*zone
= page_zone(page
);
58 spin_lock_irq(&zone
->lru_lock
);
59 BUG_ON(!PageLRU(page
));
61 del_page_from_lru(zone
, page
);
62 spin_unlock_irq(&zone
->lru_lock
);
68 static void page_cache_pipe_buf_release(struct pipe_inode_info
*info
,
69 struct pipe_buffer
*buf
)
71 page_cache_release(buf
->page
);
75 static void *page_cache_pipe_buf_map(struct file
*file
,
76 struct pipe_inode_info
*info
,
77 struct pipe_buffer
*buf
)
79 struct page
*page
= buf
->page
;
83 if (!PageUptodate(page
)) {
90 return ERR_PTR(-ENODATA
);
93 return kmap(buf
->page
);
96 static void page_cache_pipe_buf_unmap(struct pipe_inode_info
*info
,
97 struct pipe_buffer
*buf
)
99 unlock_page(buf
->page
);
103 static struct pipe_buf_operations page_cache_pipe_buf_ops
= {
105 .map
= page_cache_pipe_buf_map
,
106 .unmap
= page_cache_pipe_buf_unmap
,
107 .release
= page_cache_pipe_buf_release
,
108 .steal
= page_cache_pipe_buf_steal
,
111 static ssize_t
move_to_pipe(struct inode
*inode
, struct page
**pages
,
112 int nr_pages
, unsigned long offset
,
113 unsigned long len
, unsigned int flags
)
115 struct pipe_inode_info
*info
;
116 int ret
, do_wakeup
, i
;
122 mutex_lock(PIPE_MUTEX(*inode
));
124 info
= inode
->i_pipe
;
128 if (!PIPE_READERS(*inode
)) {
129 send_sig(SIGPIPE
, current
, 0);
136 if (bufs
< PIPE_BUFFERS
) {
137 int newbuf
= (info
->curbuf
+ bufs
) & (PIPE_BUFFERS
- 1);
138 struct pipe_buffer
*buf
= info
->bufs
+ newbuf
;
139 struct page
*page
= pages
[i
++];
140 unsigned long this_len
;
142 this_len
= PAGE_CACHE_SIZE
- offset
;
147 buf
->offset
= offset
;
149 buf
->ops
= &page_cache_pipe_buf_ops
;
150 info
->nrbufs
= ++bufs
;
160 if (bufs
< PIPE_BUFFERS
)
166 if (flags
& SPLICE_F_NONBLOCK
) {
172 if (signal_pending(current
)) {
179 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
180 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
,
185 PIPE_WAITING_WRITERS(*inode
)++;
187 PIPE_WAITING_WRITERS(*inode
)--;
190 mutex_unlock(PIPE_MUTEX(*inode
));
193 wake_up_interruptible(PIPE_WAIT(*inode
));
194 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
, POLL_IN
);
198 page_cache_release(pages
[i
++]);
203 static int __generic_file_splice_read(struct file
*in
, struct inode
*pipe
,
204 size_t len
, unsigned int flags
)
206 struct address_space
*mapping
= in
->f_mapping
;
207 unsigned int offset
, nr_pages
;
208 struct page
*pages
[PIPE_BUFFERS
], *shadow
[PIPE_BUFFERS
];
213 index
= in
->f_pos
>> PAGE_CACHE_SHIFT
;
214 offset
= in
->f_pos
& ~PAGE_CACHE_MASK
;
215 nr_pages
= (len
+ offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
217 if (nr_pages
> PIPE_BUFFERS
)
218 nr_pages
= PIPE_BUFFERS
;
221 * initiate read-ahead on this page range
223 do_page_cache_readahead(mapping
, in
, index
, nr_pages
);
226 * Get as many pages from the page cache as possible..
227 * Start IO on the page cache entries we create (we
228 * can assume that any pre-existing ones we find have
229 * already had IO started on them).
231 i
= find_get_pages(mapping
, index
, nr_pages
, pages
);
234 * common case - we found all pages and they are contiguous,
237 if (i
&& (pages
[i
- 1]->index
== index
+ i
- 1))
241 * fill shadow[] with pages at the right locations, so we only
244 memset(shadow
, 0, nr_pages
* sizeof(struct page
*));
245 for (j
= 0; j
< i
; j
++)
246 shadow
[pages
[j
]->index
- index
] = pages
[j
];
249 * now fill in the holes
251 for (i
= 0, pidx
= index
; i
< nr_pages
; pidx
++, i
++) {
258 * no page there, look one up / create it
260 page
= find_or_create_page(mapping
, pidx
,
261 mapping_gfp_mask(mapping
));
265 if (PageUptodate(page
))
268 error
= mapping
->a_ops
->readpage(in
, page
);
270 if (unlikely(error
)) {
271 page_cache_release(page
);
279 for (i
= 0; i
< nr_pages
; i
++) {
281 page_cache_release(shadow
[i
]);
286 memcpy(pages
, shadow
, i
* sizeof(struct page
*));
289 * Now we splice them into the pipe..
292 return move_to_pipe(pipe
, pages
, i
, offset
, len
, flags
);
295 ssize_t
generic_file_splice_read(struct file
*in
, struct inode
*pipe
,
296 size_t len
, unsigned int flags
)
304 ret
= __generic_file_splice_read(in
, pipe
, len
, flags
);
313 if (!(flags
& SPLICE_F_NONBLOCK
))
326 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
329 static int pipe_to_sendpage(struct pipe_inode_info
*info
,
330 struct pipe_buffer
*buf
, struct splice_desc
*sd
)
332 struct file
*file
= sd
->file
;
333 loff_t pos
= sd
->pos
;
339 * sub-optimal, but we are limited by the pipe ->map. we don't
340 * need a kmap'ed buffer here, we just want to make sure we
341 * have the page pinned if the pipe page originates from the
344 ptr
= buf
->ops
->map(file
, info
, buf
);
348 offset
= pos
& ~PAGE_CACHE_MASK
;
350 ret
= file
->f_op
->sendpage(file
, buf
->page
, offset
, sd
->len
, &pos
,
351 sd
->len
< sd
->total_len
);
353 buf
->ops
->unmap(info
, buf
);
361 * This is a little more tricky than the file -> pipe splicing. There are
362 * basically three cases:
364 * - Destination page already exists in the address space and there
365 * are users of it. For that case we have no other option that
366 * copying the data. Tough luck.
367 * - Destination page already exists in the address space, but there
368 * are no users of it. Make sure it's uptodate, then drop it. Fall
369 * through to last case.
370 * - Destination page does not exist, we can add the pipe page to
371 * the page cache and avoid the copy.
373 * For now we just do the slower thing and always copy pages over, it's
374 * easier than migrating pages from the pipe to the target file. For the
375 * case of doing file | file splicing, the migrate approach had some LRU
378 static int pipe_to_file(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
379 struct splice_desc
*sd
)
381 struct file
*file
= sd
->file
;
382 struct address_space
*mapping
= file
->f_mapping
;
390 * after this, page will be locked and unmapped
392 src
= buf
->ops
->map(file
, info
, buf
);
396 index
= sd
->pos
>> PAGE_CACHE_SHIFT
;
397 offset
= sd
->pos
& ~PAGE_CACHE_MASK
;
401 * reuse buf page, if SPLICE_F_MOVE is set
403 if (sd
->flags
& SPLICE_F_MOVE
) {
404 if (buf
->ops
->steal(info
, buf
))
409 if (add_to_page_cache_lru(page
, mapping
, index
,
410 mapping_gfp_mask(mapping
)))
415 page
= find_or_create_page(mapping
, index
,
416 mapping_gfp_mask(mapping
));
421 * If the page is uptodate, it is also locked. If it isn't
422 * uptodate, we can mark it uptodate if we are filling the
423 * full page. Otherwise we need to read it in first...
425 if (!PageUptodate(page
)) {
426 if (sd
->len
< PAGE_CACHE_SIZE
) {
427 ret
= mapping
->a_ops
->readpage(file
, page
);
433 if (!PageUptodate(page
)) {
435 * page got invalidated, repeat
437 if (!page
->mapping
) {
439 page_cache_release(page
);
446 WARN_ON(!PageLocked(page
));
447 SetPageUptodate(page
);
452 ret
= mapping
->a_ops
->prepare_write(file
, page
, 0, sd
->len
);
453 if (ret
== AOP_TRUNCATED_PAGE
) {
454 page_cache_release(page
);
460 char *dst
= kmap_atomic(page
, KM_USER0
);
462 memcpy(dst
+ offset
, src
+ buf
->offset
, sd
->len
);
463 flush_dcache_page(page
);
464 kunmap_atomic(dst
, KM_USER0
);
467 ret
= mapping
->a_ops
->commit_write(file
, page
, 0, sd
->len
);
468 if (ret
== AOP_TRUNCATED_PAGE
) {
469 page_cache_release(page
);
474 balance_dirty_pages_ratelimited(mapping
);
477 page_cache_release(page
);
480 buf
->ops
->unmap(info
, buf
);
484 typedef int (splice_actor
)(struct pipe_inode_info
*, struct pipe_buffer
*,
485 struct splice_desc
*);
487 static ssize_t
move_from_pipe(struct inode
*inode
, struct file
*out
,
488 size_t len
, unsigned int flags
,
491 struct pipe_inode_info
*info
;
492 int ret
, do_wakeup
, err
;
493 struct splice_desc sd
;
503 mutex_lock(PIPE_MUTEX(*inode
));
505 info
= inode
->i_pipe
;
507 int bufs
= info
->nrbufs
;
510 int curbuf
= info
->curbuf
;
511 struct pipe_buffer
*buf
= info
->bufs
+ curbuf
;
512 struct pipe_buf_operations
*ops
= buf
->ops
;
515 if (sd
.len
> sd
.total_len
)
516 sd
.len
= sd
.total_len
;
518 err
= actor(info
, buf
, &sd
);
520 if (!ret
&& err
!= -ENODATA
)
527 buf
->offset
+= sd
.len
;
531 ops
->release(info
, buf
);
532 curbuf
= (curbuf
+ 1) & (PIPE_BUFFERS
- 1);
533 info
->curbuf
= curbuf
;
534 info
->nrbufs
= --bufs
;
539 sd
.total_len
-= sd
.len
;
546 if (!PIPE_WRITERS(*inode
))
548 if (!PIPE_WAITING_WRITERS(*inode
)) {
553 if (flags
& SPLICE_F_NONBLOCK
) {
559 if (signal_pending(current
)) {
566 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
567 kill_fasync(PIPE_FASYNC_WRITERS(*inode
),SIGIO
,POLL_OUT
);
574 mutex_unlock(PIPE_MUTEX(*inode
));
577 wake_up_interruptible(PIPE_WAIT(*inode
));
578 kill_fasync(PIPE_FASYNC_WRITERS(*inode
), SIGIO
, POLL_OUT
);
581 mutex_lock(&out
->f_mapping
->host
->i_mutex
);
583 mutex_unlock(&out
->f_mapping
->host
->i_mutex
);
588 ssize_t
generic_file_splice_write(struct inode
*inode
, struct file
*out
,
589 size_t len
, unsigned int flags
)
591 struct address_space
*mapping
= out
->f_mapping
;
592 ssize_t ret
= move_from_pipe(inode
, out
, len
, flags
, pipe_to_file
);
595 * if file or inode is SYNC and we actually wrote some data, sync it
597 if (unlikely((out
->f_flags
& O_SYNC
) || IS_SYNC(mapping
->host
))
599 struct inode
*inode
= mapping
->host
;
602 mutex_lock(&inode
->i_mutex
);
603 err
= generic_osync_inode(mapping
->host
, mapping
,
604 OSYNC_METADATA
|OSYNC_DATA
);
605 mutex_unlock(&inode
->i_mutex
);
614 ssize_t
generic_splice_sendpage(struct inode
*inode
, struct file
*out
,
615 size_t len
, unsigned int flags
)
617 return move_from_pipe(inode
, out
, len
, flags
, pipe_to_sendpage
);
620 EXPORT_SYMBOL(generic_file_splice_write
);
621 EXPORT_SYMBOL(generic_file_splice_read
);
623 static long do_splice_from(struct inode
*pipe
, struct file
*out
, size_t len
,
629 if (!out
->f_op
|| !out
->f_op
->splice_write
)
632 if (!(out
->f_mode
& FMODE_WRITE
))
636 ret
= rw_verify_area(WRITE
, out
, &pos
, len
);
637 if (unlikely(ret
< 0))
640 return out
->f_op
->splice_write(pipe
, out
, len
, flags
);
643 static long do_splice_to(struct file
*in
, struct inode
*pipe
, size_t len
,
646 loff_t pos
, isize
, left
;
649 if (!in
->f_op
|| !in
->f_op
->splice_read
)
652 if (!(in
->f_mode
& FMODE_READ
))
656 ret
= rw_verify_area(READ
, in
, &pos
, len
);
657 if (unlikely(ret
< 0))
660 isize
= i_size_read(in
->f_mapping
->host
);
661 if (unlikely(in
->f_pos
>= isize
))
664 left
= isize
- in
->f_pos
;
668 return in
->f_op
->splice_read(in
, pipe
, len
, flags
);
671 static long do_splice(struct file
*in
, struct file
*out
, size_t len
,
676 pipe
= in
->f_dentry
->d_inode
;
678 return do_splice_from(pipe
, out
, len
, flags
);
680 pipe
= out
->f_dentry
->d_inode
;
682 return do_splice_to(in
, pipe
, len
, flags
);
687 asmlinkage
long sys_splice(int fdin
, int fdout
, size_t len
, unsigned int flags
)
690 struct file
*in
, *out
;
691 int fput_in
, fput_out
;
697 in
= fget_light(fdin
, &fput_in
);
699 if (in
->f_mode
& FMODE_READ
) {
700 out
= fget_light(fdout
, &fput_out
);
702 if (out
->f_mode
& FMODE_WRITE
)
703 error
= do_splice(in
, out
, len
, flags
);
704 fput_light(out
, fput_out
);
708 fput_light(in
, fput_in
);