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>
26 * Passed to the actors
29 unsigned int len, total_len; /* current and remaining length */
30 unsigned int flags; /* splice flags */
31 struct file *file; /* file to read/write */
32 loff_t pos; /* file position */
35 static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
36 struct pipe_buffer *buf)
38 page_cache_release(buf->page);
42 static void *page_cache_pipe_buf_map(struct file *file,
43 struct pipe_inode_info *info,
44 struct pipe_buffer *buf)
46 struct page *page = buf->page;
50 if (!PageUptodate(page)) {
57 return ERR_PTR(-ENODATA);
60 return kmap(buf->page);
63 static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
64 struct pipe_buffer *buf)
66 unlock_page(buf->page);
70 static struct pipe_buf_operations page_cache_pipe_buf_ops = {
72 .map = page_cache_pipe_buf_map,
73 .unmap = page_cache_pipe_buf_unmap,
74 .release = page_cache_pipe_buf_release,
77 static ssize_t move_to_pipe(struct inode *inode, struct page **pages,
78 int nr_pages, unsigned long offset,
81 struct pipe_inode_info *info;
82 int ret, do_wakeup, i;
88 mutex_lock(PIPE_MUTEX(*inode));
94 if (!PIPE_READERS(*inode)) {
95 send_sig(SIGPIPE, current, 0);
102 if (bufs < PIPE_BUFFERS) {
103 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS - 1);
104 struct pipe_buffer *buf = info->bufs + newbuf;
105 struct page *page = pages[i++];
106 unsigned long this_len;
108 this_len = PAGE_CACHE_SIZE - offset;
113 buf->offset = offset;
115 buf->ops = &page_cache_pipe_buf_ops;
116 info->nrbufs = ++bufs;
126 if (bufs < PIPE_BUFFERS)
132 if (signal_pending(current)) {
139 wake_up_interruptible_sync(PIPE_WAIT(*inode));
140 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO,
145 PIPE_WAITING_WRITERS(*inode)++;
147 PIPE_WAITING_WRITERS(*inode)--;
150 mutex_unlock(PIPE_MUTEX(*inode));
153 wake_up_interruptible(PIPE_WAIT(*inode));
154 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
158 page_cache_release(pages[i++]);
163 static int __generic_file_splice_read(struct file *in, struct inode *pipe,
166 struct address_space *mapping = in->f_mapping;
167 unsigned int offset, nr_pages;
168 struct page *pages[PIPE_BUFFERS], *shadow[PIPE_BUFFERS];
173 index = in->f_pos >> PAGE_CACHE_SHIFT;
174 offset = in->f_pos & ~PAGE_CACHE_MASK;
175 nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
177 if (nr_pages > PIPE_BUFFERS)
178 nr_pages = PIPE_BUFFERS;
181 * initiate read-ahead on this page range
183 do_page_cache_readahead(mapping, in, index, nr_pages);
186 * Get as many pages from the page cache as possible..
187 * Start IO on the page cache entries we create (we
188 * can assume that any pre-existing ones we find have
189 * already had IO started on them).
191 i = find_get_pages(mapping, index, nr_pages, pages);
194 * common case - we found all pages and they are contiguous,
197 if (i && (pages[i - 1]->index == index + i - 1))
201 * fill shadow[] with pages at the right locations, so we only
204 memset(shadow, 0, i * sizeof(struct page *));
205 for (j = 0, pidx = index; j < i; pidx++, j++)
206 shadow[pages[j]->index - pidx] = pages[j];
209 * now fill in the holes
211 for (i = 0, pidx = index; i < nr_pages; pidx++, i++) {
218 * no page there, look one up / create it
220 page = find_or_create_page(mapping, pidx,
221 mapping_gfp_mask(mapping));
225 if (PageUptodate(page))
228 error = mapping->a_ops->readpage(in, page);
230 if (unlikely(error)) {
231 page_cache_release(page);
239 for (i = 0; i < nr_pages; i++) {
241 page_cache_release(shadow[i]);
246 memcpy(pages, shadow, i * sizeof(struct page *));
249 * Now we splice them into the pipe..
252 return move_to_pipe(pipe, pages, i, offset, len);
255 ssize_t generic_file_splice_read(struct file *in, struct inode *pipe,
256 size_t len, unsigned int flags)
264 ret = __generic_file_splice_read(in, pipe, len);
281 * Send 'len' bytes to socket from 'file' at position 'pos' using sendpage().
283 static int pipe_to_sendpage(struct pipe_inode_info *info,
284 struct pipe_buffer *buf, struct splice_desc *sd)
286 struct file *file = sd->file;
287 loff_t pos = sd->pos;
293 * sub-optimal, but we are limited by the pipe ->map. we don't
294 * need a kmap'ed buffer here, we just want to make sure we
295 * have the page pinned if the pipe page originates from the
298 ptr = buf->ops->map(file, info, buf);
302 offset = pos & ~PAGE_CACHE_MASK;
304 ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,
305 sd->len < sd->total_len);
307 buf->ops->unmap(info, buf);
315 * This is a little more tricky than the file -> pipe splicing. There are
316 * basically three cases:
318 * - Destination page already exists in the address space and there
319 * are users of it. For that case we have no other option that
320 * copying the data. Tough luck.
321 * - Destination page already exists in the address space, but there
322 * are no users of it. Make sure it's uptodate, then drop it. Fall
323 * through to last case.
324 * - Destination page does not exist, we can add the pipe page to
325 * the page cache and avoid the copy.
327 * For now we just do the slower thing and always copy pages over, it's
328 * easier than migrating pages from the pipe to the target file. For the
329 * case of doing file | file splicing, the migrate approach had some LRU
332 static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
333 struct splice_desc *sd)
335 struct file *file = sd->file;
336 struct address_space *mapping = file->f_mapping;
344 * after this, page will be locked and unmapped
346 src = buf->ops->map(file, info, buf);
350 index = sd->pos >> PAGE_CACHE_SHIFT;
351 offset = sd->pos & ~PAGE_CACHE_MASK;
355 page = find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
360 * If the page is uptodate, it is also locked. If it isn't
361 * uptodate, we can mark it uptodate if we are filling the
362 * full page. Otherwise we need to read it in first...
364 if (!PageUptodate(page)) {
365 if (sd->len < PAGE_CACHE_SIZE) {
366 ret = mapping->a_ops->readpage(file, page);
372 if (!PageUptodate(page)) {
374 * page got invalidated, repeat
376 if (!page->mapping) {
378 page_cache_release(page);
385 WARN_ON(!PageLocked(page));
386 SetPageUptodate(page);
390 ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
394 dst = kmap_atomic(page, KM_USER0);
395 memcpy(dst + offset, src + buf->offset, sd->len);
396 flush_dcache_page(page);
397 kunmap_atomic(dst, KM_USER0);
399 ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
403 set_page_dirty(page);
404 ret = write_one_page(page, 0);
408 page_cache_release(page);
409 buf->ops->unmap(info, buf);
413 typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
414 struct splice_desc *);
416 static ssize_t move_from_pipe(struct inode *inode, struct file *out,
417 size_t len, unsigned int flags,
420 struct pipe_inode_info *info;
421 int ret, do_wakeup, err;
422 struct splice_desc sd;
432 mutex_lock(PIPE_MUTEX(*inode));
434 info = inode->i_pipe;
436 int bufs = info->nrbufs;
439 int curbuf = info->curbuf;
440 struct pipe_buffer *buf = info->bufs + curbuf;
441 struct pipe_buf_operations *ops = buf->ops;
444 if (sd.len > sd.total_len)
445 sd.len = sd.total_len;
447 err = actor(info, buf, &sd);
449 if (!ret && err != -ENODATA)
456 buf->offset += sd.len;
460 ops->release(info, buf);
461 curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1);
462 info->curbuf = curbuf;
463 info->nrbufs = --bufs;
468 sd.total_len -= sd.len;
475 if (!PIPE_WRITERS(*inode))
477 if (!PIPE_WAITING_WRITERS(*inode)) {
482 if (signal_pending(current)) {
489 wake_up_interruptible_sync(PIPE_WAIT(*inode));
490 kill_fasync(PIPE_FASYNC_WRITERS(*inode),SIGIO,POLL_OUT);
497 mutex_unlock(PIPE_MUTEX(*inode));
500 wake_up_interruptible(PIPE_WAIT(*inode));
501 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
504 mutex_lock(&out->f_mapping->host->i_mutex);
506 mutex_unlock(&out->f_mapping->host->i_mutex);
511 ssize_t generic_file_splice_write(struct inode *inode, struct file *out,
512 size_t len, unsigned int flags)
514 return move_from_pipe(inode, out, len, flags, pipe_to_file);
517 ssize_t generic_splice_sendpage(struct inode *inode, struct file *out,
518 size_t len, unsigned int flags)
520 return move_from_pipe(inode, out, len, flags, pipe_to_sendpage);
523 static long do_splice_from(struct inode *pipe, struct file *out, size_t len,
529 if (!out->f_op || !out->f_op->splice_write)
532 if (!(out->f_mode & FMODE_WRITE))
536 ret = rw_verify_area(WRITE, out, &pos, len);
537 if (unlikely(ret < 0))
540 return out->f_op->splice_write(pipe, out, len, flags);
543 static long do_splice_to(struct file *in, struct inode *pipe, size_t len,
546 loff_t pos, isize, left;
549 if (!in->f_op || !in->f_op->splice_read)
552 if (!(in->f_mode & FMODE_READ))
556 ret = rw_verify_area(READ, in, &pos, len);
557 if (unlikely(ret < 0))
560 isize = i_size_read(in->f_mapping->host);
561 if (unlikely(in->f_pos >= isize))
564 left = isize - in->f_pos;
568 return in->f_op->splice_read(in, pipe, len, flags);
571 static long do_splice(struct file *in, struct file *out, size_t len,
576 pipe = in->f_dentry->d_inode;
578 return do_splice_from(pipe, out, len, flags);
580 pipe = out->f_dentry->d_inode;
582 return do_splice_to(in, pipe, len, flags);
587 asmlinkage long sys_splice(int fdin, int fdout, size_t len, unsigned int flags)
590 struct file *in, *out;
591 int fput_in, fput_out;
597 in = fget_light(fdin, &fput_in);
599 if (in->f_mode & FMODE_READ) {
600 out = fget_light(fdout, &fput_out);
602 if (out->f_mode & FMODE_WRITE)
603 error = do_splice(in, out, len, flags);
604 fput_light(out, fput_out);
608 fput_light(in, fput_in);