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>
27 * Passed to the actors
30 unsigned int len, total_len; /* current and remaining length */
31 unsigned int flags; /* splice flags */
32 struct file *file; /* file to read/write */
33 loff_t pos; /* file position */
36 static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
37 struct pipe_buffer *buf)
39 struct page *page = buf->page;
41 WARN_ON(!PageLocked(page));
42 WARN_ON(!PageUptodate(page));
44 if (!remove_mapping(page_mapping(page), page))
48 struct zone *zone = page_zone(page);
50 spin_lock_irq(&zone->lru_lock);
51 BUG_ON(!PageLRU(page));
53 del_page_from_lru(zone, page);
54 spin_unlock_irq(&zone->lru_lock);
61 static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
62 struct pipe_buffer *buf)
64 page_cache_release(buf->page);
69 static void *page_cache_pipe_buf_map(struct file *file,
70 struct pipe_inode_info *info,
71 struct pipe_buffer *buf)
73 struct page *page = buf->page;
77 if (!PageUptodate(page)) {
84 return ERR_PTR(-ENODATA);
87 return kmap(buf->page);
90 static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
91 struct pipe_buffer *buf)
94 unlock_page(buf->page);
98 static struct pipe_buf_operations page_cache_pipe_buf_ops = {
100 .map = page_cache_pipe_buf_map,
101 .unmap = page_cache_pipe_buf_unmap,
102 .release = page_cache_pipe_buf_release,
103 .steal = page_cache_pipe_buf_steal,
106 static ssize_t move_to_pipe(struct inode *inode, struct page **pages,
107 int nr_pages, unsigned long offset,
110 struct pipe_inode_info *info;
111 int ret, do_wakeup, i;
117 mutex_lock(PIPE_MUTEX(*inode));
119 info = inode->i_pipe;
123 if (!PIPE_READERS(*inode)) {
124 send_sig(SIGPIPE, current, 0);
131 if (bufs < PIPE_BUFFERS) {
132 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS - 1);
133 struct pipe_buffer *buf = info->bufs + newbuf;
134 struct page *page = pages[i++];
135 unsigned long this_len;
137 this_len = PAGE_CACHE_SIZE - offset;
142 buf->offset = offset;
144 buf->ops = &page_cache_pipe_buf_ops;
145 info->nrbufs = ++bufs;
155 if (bufs < PIPE_BUFFERS)
161 if (signal_pending(current)) {
168 wake_up_interruptible_sync(PIPE_WAIT(*inode));
169 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO,
174 PIPE_WAITING_WRITERS(*inode)++;
176 PIPE_WAITING_WRITERS(*inode)--;
179 mutex_unlock(PIPE_MUTEX(*inode));
182 wake_up_interruptible(PIPE_WAIT(*inode));
183 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
187 page_cache_release(pages[i++]);
192 static int __generic_file_splice_read(struct file *in, struct inode *pipe,
195 struct address_space *mapping = in->f_mapping;
196 unsigned int offset, nr_pages;
197 struct page *pages[PIPE_BUFFERS], *shadow[PIPE_BUFFERS];
202 index = in->f_pos >> PAGE_CACHE_SHIFT;
203 offset = in->f_pos & ~PAGE_CACHE_MASK;
204 nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
206 if (nr_pages > PIPE_BUFFERS)
207 nr_pages = PIPE_BUFFERS;
210 * initiate read-ahead on this page range
212 do_page_cache_readahead(mapping, in, index, nr_pages);
215 * Get as many pages from the page cache as possible..
216 * Start IO on the page cache entries we create (we
217 * can assume that any pre-existing ones we find have
218 * already had IO started on them).
220 i = find_get_pages(mapping, index, nr_pages, pages);
223 * common case - we found all pages and they are contiguous,
226 if (i && (pages[i - 1]->index == index + i - 1))
230 * fill shadow[] with pages at the right locations, so we only
233 memset(shadow, 0, i * sizeof(struct page *));
234 for (j = 0, pidx = index; j < i; pidx++, j++)
235 shadow[pages[j]->index - pidx] = pages[j];
238 * now fill in the holes
240 for (i = 0, pidx = index; i < nr_pages; pidx++, i++) {
247 * no page there, look one up / create it
249 page = find_or_create_page(mapping, pidx,
250 mapping_gfp_mask(mapping));
254 if (PageUptodate(page))
257 error = mapping->a_ops->readpage(in, page);
259 if (unlikely(error)) {
260 page_cache_release(page);
268 for (i = 0; i < nr_pages; i++) {
270 page_cache_release(shadow[i]);
275 memcpy(pages, shadow, i * sizeof(struct page *));
278 * Now we splice them into the pipe..
281 return move_to_pipe(pipe, pages, i, offset, len);
284 ssize_t generic_file_splice_read(struct file *in, struct inode *pipe,
285 size_t len, unsigned int flags)
293 ret = __generic_file_splice_read(in, pipe, len);
310 * Send 'len' bytes to socket from 'file' at position 'pos' using sendpage().
312 static int pipe_to_sendpage(struct pipe_inode_info *info,
313 struct pipe_buffer *buf, struct splice_desc *sd)
315 struct file *file = sd->file;
316 loff_t pos = sd->pos;
322 * sub-optimal, but we are limited by the pipe ->map. we don't
323 * need a kmap'ed buffer here, we just want to make sure we
324 * have the page pinned if the pipe page originates from the
327 ptr = buf->ops->map(file, info, buf);
331 offset = pos & ~PAGE_CACHE_MASK;
333 ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,
334 sd->len < sd->total_len);
336 buf->ops->unmap(info, buf);
344 * This is a little more tricky than the file -> pipe splicing. There are
345 * basically three cases:
347 * - Destination page already exists in the address space and there
348 * are users of it. For that case we have no other option that
349 * copying the data. Tough luck.
350 * - Destination page already exists in the address space, but there
351 * are no users of it. Make sure it's uptodate, then drop it. Fall
352 * through to last case.
353 * - Destination page does not exist, we can add the pipe page to
354 * the page cache and avoid the copy.
356 * For now we just do the slower thing and always copy pages over, it's
357 * easier than migrating pages from the pipe to the target file. For the
358 * case of doing file | file splicing, the migrate approach had some LRU
361 static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
362 struct splice_desc *sd)
364 struct file *file = sd->file;
365 struct address_space *mapping = file->f_mapping;
373 * after this, page will be locked and unmapped
375 src = buf->ops->map(file, info, buf);
379 index = sd->pos >> PAGE_CACHE_SHIFT;
380 offset = sd->pos & ~PAGE_CACHE_MASK;
383 * reuse buf page, if SPLICE_F_MOVE is set
385 if (sd->flags & SPLICE_F_MOVE) {
386 if (buf->ops->steal(info, buf))
390 if (add_to_page_cache_lru(page, mapping, index,
391 mapping_gfp_mask(mapping)))
396 page = find_or_create_page(mapping, index,
397 mapping_gfp_mask(mapping));
402 * If the page is uptodate, it is also locked. If it isn't
403 * uptodate, we can mark it uptodate if we are filling the
404 * full page. Otherwise we need to read it in first...
406 if (!PageUptodate(page)) {
407 if (sd->len < PAGE_CACHE_SIZE) {
408 ret = mapping->a_ops->readpage(file, page);
414 if (!PageUptodate(page)) {
416 * page got invalidated, repeat
418 if (!page->mapping) {
420 page_cache_release(page);
427 WARN_ON(!PageLocked(page));
428 SetPageUptodate(page);
433 ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
438 char *dst = kmap_atomic(page, KM_USER0);
440 memcpy(dst + offset, src + buf->offset, sd->len);
441 flush_dcache_page(page);
442 kunmap_atomic(dst, KM_USER0);
445 ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
449 set_page_dirty(page);
450 ret = write_one_page(page, 0);
455 page_cache_release(page);
456 buf->ops->unmap(info, buf);
460 typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
461 struct splice_desc *);
463 static ssize_t move_from_pipe(struct inode *inode, struct file *out,
464 size_t len, unsigned int flags,
467 struct pipe_inode_info *info;
468 int ret, do_wakeup, err;
469 struct splice_desc sd;
479 mutex_lock(PIPE_MUTEX(*inode));
481 info = inode->i_pipe;
483 int bufs = info->nrbufs;
486 int curbuf = info->curbuf;
487 struct pipe_buffer *buf = info->bufs + curbuf;
488 struct pipe_buf_operations *ops = buf->ops;
491 if (sd.len > sd.total_len)
492 sd.len = sd.total_len;
494 err = actor(info, buf, &sd);
496 if (!ret && err != -ENODATA)
503 buf->offset += sd.len;
507 ops->release(info, buf);
508 curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1);
509 info->curbuf = curbuf;
510 info->nrbufs = --bufs;
515 sd.total_len -= sd.len;
522 if (!PIPE_WRITERS(*inode))
524 if (!PIPE_WAITING_WRITERS(*inode)) {
529 if (signal_pending(current)) {
536 wake_up_interruptible_sync(PIPE_WAIT(*inode));
537 kill_fasync(PIPE_FASYNC_WRITERS(*inode),SIGIO,POLL_OUT);
544 mutex_unlock(PIPE_MUTEX(*inode));
547 wake_up_interruptible(PIPE_WAIT(*inode));
548 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
551 mutex_lock(&out->f_mapping->host->i_mutex);
553 mutex_unlock(&out->f_mapping->host->i_mutex);
558 ssize_t generic_file_splice_write(struct inode *inode, struct file *out,
559 size_t len, unsigned int flags)
561 return move_from_pipe(inode, out, len, flags, pipe_to_file);
564 ssize_t generic_splice_sendpage(struct inode *inode, struct file *out,
565 size_t len, unsigned int flags)
567 return move_from_pipe(inode, out, len, flags, pipe_to_sendpage);
570 static long do_splice_from(struct inode *pipe, struct file *out, size_t len,
576 if (!out->f_op || !out->f_op->splice_write)
579 if (!(out->f_mode & FMODE_WRITE))
583 ret = rw_verify_area(WRITE, out, &pos, len);
584 if (unlikely(ret < 0))
587 return out->f_op->splice_write(pipe, out, len, flags);
590 static long do_splice_to(struct file *in, struct inode *pipe, size_t len,
593 loff_t pos, isize, left;
596 if (!in->f_op || !in->f_op->splice_read)
599 if (!(in->f_mode & FMODE_READ))
603 ret = rw_verify_area(READ, in, &pos, len);
604 if (unlikely(ret < 0))
607 isize = i_size_read(in->f_mapping->host);
608 if (unlikely(in->f_pos >= isize))
611 left = isize - in->f_pos;
615 return in->f_op->splice_read(in, pipe, len, flags);
618 static long do_splice(struct file *in, struct file *out, size_t len,
623 pipe = in->f_dentry->d_inode;
625 return do_splice_from(pipe, out, len, flags);
627 pipe = out->f_dentry->d_inode;
629 return do_splice_to(in, pipe, len, flags);
634 asmlinkage long sys_splice(int fdin, int fdout, size_t len, unsigned int flags)
637 struct file *in, *out;
638 int fput_in, fput_out;
644 in = fget_light(fdin, &fput_in);
646 if (in->f_mode & FMODE_READ) {
647 out = fget_light(fdout, &fput_out);
649 if (out->f_mode & FMODE_WRITE)
650 error = do_splice(in, out, len, flags);
651 fput_light(out, fput_out);
655 fput_light(in, fput_in);