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/module.h>
28 * Passed to the actors
31 unsigned int len
, total_len
; /* current and remaining length */
32 unsigned int flags
; /* splice flags */
33 struct file
*file
; /* file to read/write */
34 loff_t pos
; /* file position */
37 static int page_cache_pipe_buf_steal(struct pipe_inode_info
*info
,
38 struct pipe_buffer
*buf
)
40 struct page
*page
= buf
->page
;
42 WARN_ON(!PageLocked(page
));
43 WARN_ON(!PageUptodate(page
));
45 if (!remove_mapping(page_mapping(page
), page
))
49 struct zone
*zone
= page_zone(page
);
51 spin_lock_irq(&zone
->lru_lock
);
52 BUG_ON(!PageLRU(page
));
54 del_page_from_lru(zone
, page
);
55 spin_unlock_irq(&zone
->lru_lock
);
62 static void page_cache_pipe_buf_release(struct pipe_inode_info
*info
,
63 struct pipe_buffer
*buf
)
65 page_cache_release(buf
->page
);
70 static void *page_cache_pipe_buf_map(struct file
*file
,
71 struct pipe_inode_info
*info
,
72 struct pipe_buffer
*buf
)
74 struct page
*page
= buf
->page
;
78 if (!PageUptodate(page
)) {
85 return ERR_PTR(-ENODATA
);
88 return kmap(buf
->page
);
91 static void page_cache_pipe_buf_unmap(struct pipe_inode_info
*info
,
92 struct pipe_buffer
*buf
)
95 unlock_page(buf
->page
);
99 static struct pipe_buf_operations page_cache_pipe_buf_ops
= {
101 .map
= page_cache_pipe_buf_map
,
102 .unmap
= page_cache_pipe_buf_unmap
,
103 .release
= page_cache_pipe_buf_release
,
104 .steal
= page_cache_pipe_buf_steal
,
107 static ssize_t
move_to_pipe(struct inode
*inode
, struct page
**pages
,
108 int nr_pages
, unsigned long offset
,
109 unsigned long len
, unsigned int flags
)
111 struct pipe_inode_info
*info
;
112 int ret
, do_wakeup
, i
;
118 mutex_lock(PIPE_MUTEX(*inode
));
120 info
= inode
->i_pipe
;
124 if (!PIPE_READERS(*inode
)) {
125 send_sig(SIGPIPE
, current
, 0);
132 if (bufs
< PIPE_BUFFERS
) {
133 int newbuf
= (info
->curbuf
+ bufs
) & (PIPE_BUFFERS
- 1);
134 struct pipe_buffer
*buf
= info
->bufs
+ newbuf
;
135 struct page
*page
= pages
[i
++];
136 unsigned long this_len
;
138 this_len
= PAGE_CACHE_SIZE
- offset
;
143 buf
->offset
= offset
;
145 buf
->ops
= &page_cache_pipe_buf_ops
;
146 info
->nrbufs
= ++bufs
;
156 if (bufs
< PIPE_BUFFERS
)
162 if (flags
& SPLICE_F_NONBLOCK
) {
168 if (signal_pending(current
)) {
175 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
176 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
,
181 PIPE_WAITING_WRITERS(*inode
)++;
183 PIPE_WAITING_WRITERS(*inode
)--;
186 mutex_unlock(PIPE_MUTEX(*inode
));
189 wake_up_interruptible(PIPE_WAIT(*inode
));
190 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
, POLL_IN
);
194 page_cache_release(pages
[i
++]);
199 static int __generic_file_splice_read(struct file
*in
, struct inode
*pipe
,
200 size_t len
, unsigned int flags
)
202 struct address_space
*mapping
= in
->f_mapping
;
203 unsigned int offset
, nr_pages
;
204 struct page
*pages
[PIPE_BUFFERS
], *shadow
[PIPE_BUFFERS
];
209 index
= in
->f_pos
>> PAGE_CACHE_SHIFT
;
210 offset
= in
->f_pos
& ~PAGE_CACHE_MASK
;
211 nr_pages
= (len
+ offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
213 if (nr_pages
> PIPE_BUFFERS
)
214 nr_pages
= PIPE_BUFFERS
;
217 * initiate read-ahead on this page range
219 do_page_cache_readahead(mapping
, in
, index
, nr_pages
);
222 * Get as many pages from the page cache as possible..
223 * Start IO on the page cache entries we create (we
224 * can assume that any pre-existing ones we find have
225 * already had IO started on them).
227 i
= find_get_pages(mapping
, index
, nr_pages
, pages
);
230 * common case - we found all pages and they are contiguous,
233 if (i
&& (pages
[i
- 1]->index
== index
+ i
- 1))
237 * fill shadow[] with pages at the right locations, so we only
240 memset(shadow
, 0, nr_pages
* sizeof(struct page
*));
241 for (j
= 0; j
< i
; j
++)
242 shadow
[pages
[j
]->index
- index
] = pages
[j
];
245 * now fill in the holes
247 for (i
= 0, pidx
= index
; i
< nr_pages
; pidx
++, i
++) {
254 * no page there, look one up / create it
256 page
= find_or_create_page(mapping
, pidx
,
257 mapping_gfp_mask(mapping
));
261 if (PageUptodate(page
))
264 error
= mapping
->a_ops
->readpage(in
, page
);
266 if (unlikely(error
)) {
267 page_cache_release(page
);
275 for (i
= 0; i
< nr_pages
; i
++) {
277 page_cache_release(shadow
[i
]);
282 memcpy(pages
, shadow
, i
* sizeof(struct page
*));
285 * Now we splice them into the pipe..
288 return move_to_pipe(pipe
, pages
, i
, offset
, len
, flags
);
291 ssize_t
generic_file_splice_read(struct file
*in
, struct inode
*pipe
,
292 size_t len
, unsigned int flags
)
300 ret
= __generic_file_splice_read(in
, pipe
, len
, flags
);
309 if (!(flags
& SPLICE_F_NONBLOCK
))
322 * Send 'len' bytes to socket from 'file' at position 'pos' using sendpage().
324 static int pipe_to_sendpage(struct pipe_inode_info
*info
,
325 struct pipe_buffer
*buf
, struct splice_desc
*sd
)
327 struct file
*file
= sd
->file
;
328 loff_t pos
= sd
->pos
;
334 * sub-optimal, but we are limited by the pipe ->map. we don't
335 * need a kmap'ed buffer here, we just want to make sure we
336 * have the page pinned if the pipe page originates from the
339 ptr
= buf
->ops
->map(file
, info
, buf
);
343 offset
= pos
& ~PAGE_CACHE_MASK
;
345 ret
= file
->f_op
->sendpage(file
, buf
->page
, offset
, sd
->len
, &pos
,
346 sd
->len
< sd
->total_len
);
348 buf
->ops
->unmap(info
, buf
);
356 * This is a little more tricky than the file -> pipe splicing. There are
357 * basically three cases:
359 * - Destination page already exists in the address space and there
360 * are users of it. For that case we have no other option that
361 * copying the data. Tough luck.
362 * - Destination page already exists in the address space, but there
363 * are no users of it. Make sure it's uptodate, then drop it. Fall
364 * through to last case.
365 * - Destination page does not exist, we can add the pipe page to
366 * the page cache and avoid the copy.
368 * For now we just do the slower thing and always copy pages over, it's
369 * easier than migrating pages from the pipe to the target file. For the
370 * case of doing file | file splicing, the migrate approach had some LRU
373 static int pipe_to_file(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
374 struct splice_desc
*sd
)
376 struct file
*file
= sd
->file
;
377 struct address_space
*mapping
= file
->f_mapping
;
385 * after this, page will be locked and unmapped
387 src
= buf
->ops
->map(file
, info
, buf
);
391 index
= sd
->pos
>> PAGE_CACHE_SHIFT
;
392 offset
= sd
->pos
& ~PAGE_CACHE_MASK
;
395 * reuse buf page, if SPLICE_F_MOVE is set
397 if (sd
->flags
& SPLICE_F_MOVE
) {
398 if (buf
->ops
->steal(info
, buf
))
402 if (add_to_page_cache_lru(page
, mapping
, index
,
403 mapping_gfp_mask(mapping
)))
408 page
= find_or_create_page(mapping
, index
,
409 mapping_gfp_mask(mapping
));
414 * If the page is uptodate, it is also locked. If it isn't
415 * uptodate, we can mark it uptodate if we are filling the
416 * full page. Otherwise we need to read it in first...
418 if (!PageUptodate(page
)) {
419 if (sd
->len
< PAGE_CACHE_SIZE
) {
420 ret
= mapping
->a_ops
->readpage(file
, page
);
426 if (!PageUptodate(page
)) {
428 * page got invalidated, repeat
430 if (!page
->mapping
) {
432 page_cache_release(page
);
439 WARN_ON(!PageLocked(page
));
440 SetPageUptodate(page
);
445 ret
= mapping
->a_ops
->prepare_write(file
, page
, 0, sd
->len
);
450 char *dst
= kmap_atomic(page
, KM_USER0
);
452 memcpy(dst
+ offset
, src
+ buf
->offset
, sd
->len
);
453 flush_dcache_page(page
);
454 kunmap_atomic(dst
, KM_USER0
);
457 ret
= mapping
->a_ops
->commit_write(file
, page
, 0, sd
->len
);
461 set_page_dirty(page
);
462 ret
= write_one_page(page
, 0);
467 page_cache_release(page
);
468 buf
->ops
->unmap(info
, buf
);
472 typedef int (splice_actor
)(struct pipe_inode_info
*, struct pipe_buffer
*,
473 struct splice_desc
*);
475 static ssize_t
move_from_pipe(struct inode
*inode
, struct file
*out
,
476 size_t len
, unsigned int flags
,
479 struct pipe_inode_info
*info
;
480 int ret
, do_wakeup
, err
;
481 struct splice_desc sd
;
491 mutex_lock(PIPE_MUTEX(*inode
));
493 info
= inode
->i_pipe
;
495 int bufs
= info
->nrbufs
;
498 int curbuf
= info
->curbuf
;
499 struct pipe_buffer
*buf
= info
->bufs
+ curbuf
;
500 struct pipe_buf_operations
*ops
= buf
->ops
;
503 if (sd
.len
> sd
.total_len
)
504 sd
.len
= sd
.total_len
;
506 err
= actor(info
, buf
, &sd
);
508 if (!ret
&& err
!= -ENODATA
)
515 buf
->offset
+= sd
.len
;
519 ops
->release(info
, buf
);
520 curbuf
= (curbuf
+ 1) & (PIPE_BUFFERS
- 1);
521 info
->curbuf
= curbuf
;
522 info
->nrbufs
= --bufs
;
527 sd
.total_len
-= sd
.len
;
534 if (!PIPE_WRITERS(*inode
))
536 if (!PIPE_WAITING_WRITERS(*inode
)) {
541 if (flags
& SPLICE_F_NONBLOCK
) {
547 if (signal_pending(current
)) {
554 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
555 kill_fasync(PIPE_FASYNC_WRITERS(*inode
),SIGIO
,POLL_OUT
);
562 mutex_unlock(PIPE_MUTEX(*inode
));
565 wake_up_interruptible(PIPE_WAIT(*inode
));
566 kill_fasync(PIPE_FASYNC_WRITERS(*inode
), SIGIO
, POLL_OUT
);
569 mutex_lock(&out
->f_mapping
->host
->i_mutex
);
571 mutex_unlock(&out
->f_mapping
->host
->i_mutex
);
576 ssize_t
generic_file_splice_write(struct inode
*inode
, struct file
*out
,
577 size_t len
, unsigned int flags
)
579 return move_from_pipe(inode
, out
, len
, flags
, pipe_to_file
);
582 ssize_t
generic_splice_sendpage(struct inode
*inode
, struct file
*out
,
583 size_t len
, unsigned int flags
)
585 return move_from_pipe(inode
, out
, len
, flags
, pipe_to_sendpage
);
588 EXPORT_SYMBOL(generic_file_splice_write
);
589 EXPORT_SYMBOL(generic_file_splice_read
);
591 static long do_splice_from(struct inode
*pipe
, struct file
*out
, size_t len
,
597 if (!out
->f_op
|| !out
->f_op
->splice_write
)
600 if (!(out
->f_mode
& FMODE_WRITE
))
604 ret
= rw_verify_area(WRITE
, out
, &pos
, len
);
605 if (unlikely(ret
< 0))
608 return out
->f_op
->splice_write(pipe
, out
, len
, flags
);
611 static long do_splice_to(struct file
*in
, struct inode
*pipe
, size_t len
,
614 loff_t pos
, isize
, left
;
617 if (!in
->f_op
|| !in
->f_op
->splice_read
)
620 if (!(in
->f_mode
& FMODE_READ
))
624 ret
= rw_verify_area(READ
, in
, &pos
, len
);
625 if (unlikely(ret
< 0))
628 isize
= i_size_read(in
->f_mapping
->host
);
629 if (unlikely(in
->f_pos
>= isize
))
632 left
= isize
- in
->f_pos
;
636 return in
->f_op
->splice_read(in
, pipe
, len
, flags
);
639 static long do_splice(struct file
*in
, struct file
*out
, size_t len
,
644 pipe
= in
->f_dentry
->d_inode
;
646 return do_splice_from(pipe
, out
, len
, flags
);
648 pipe
= out
->f_dentry
->d_inode
;
650 return do_splice_to(in
, pipe
, len
, flags
);
655 asmlinkage
long sys_splice(int fdin
, int fdout
, size_t len
, unsigned int flags
)
658 struct file
*in
, *out
;
659 int fput_in
, fput_out
;
665 in
= fget_light(fdin
, &fput_in
);
667 if (in
->f_mode
& FMODE_READ
) {
668 out
= fget_light(fdout
, &fput_out
);
670 if (out
->f_mode
& FMODE_WRITE
)
671 error
= do_splice(in
, out
, len
, flags
);
672 fput_light(out
, fput_out
);
676 fput_light(in
, fput_in
);