USB: usb-storage: augment unusual_devs entry for Simple Tech/Datafab
[linux-2.6/mini2440.git] / fs / splice.c
blob4c1029a64fb8aff703a1f7add15486702268b987
1 /*
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, network, direct splicing, etc and
13 * fixing lots of bugs.
15 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
16 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
17 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/pagemap.h>
23 #include <linux/splice.h>
24 #include <linux/memcontrol.h>
25 #include <linux/mm_inline.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/buffer_head.h>
29 #include <linux/module.h>
30 #include <linux/syscalls.h>
31 #include <linux/uio.h>
32 #include <linux/security.h>
35 * Attempt to steal a page from a pipe buffer. This should perhaps go into
36 * a vm helper function, it's already simplified quite a bit by the
37 * addition of remove_mapping(). If success is returned, the caller may
38 * attempt to reuse this page for another destination.
40 static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
41 struct pipe_buffer *buf)
43 struct page *page = buf->page;
44 struct address_space *mapping;
46 lock_page(page);
48 mapping = page_mapping(page);
49 if (mapping) {
50 WARN_ON(!PageUptodate(page));
53 * At least for ext2 with nobh option, we need to wait on
54 * writeback completing on this page, since we'll remove it
55 * from the pagecache. Otherwise truncate wont wait on the
56 * page, allowing the disk blocks to be reused by someone else
57 * before we actually wrote our data to them. fs corruption
58 * ensues.
60 wait_on_page_writeback(page);
62 if (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
63 goto out_unlock;
66 * If we succeeded in removing the mapping, set LRU flag
67 * and return good.
69 if (remove_mapping(mapping, page)) {
70 buf->flags |= PIPE_BUF_FLAG_LRU;
71 return 0;
76 * Raced with truncate or failed to remove page from current
77 * address space, unlock and return failure.
79 out_unlock:
80 unlock_page(page);
81 return 1;
84 static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
85 struct pipe_buffer *buf)
87 page_cache_release(buf->page);
88 buf->flags &= ~PIPE_BUF_FLAG_LRU;
92 * Check whether the contents of buf is OK to access. Since the content
93 * is a page cache page, IO may be in flight.
95 static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
96 struct pipe_buffer *buf)
98 struct page *page = buf->page;
99 int err;
101 if (!PageUptodate(page)) {
102 lock_page(page);
105 * Page got truncated/unhashed. This will cause a 0-byte
106 * splice, if this is the first page.
108 if (!page->mapping) {
109 err = -ENODATA;
110 goto error;
114 * Uh oh, read-error from disk.
116 if (!PageUptodate(page)) {
117 err = -EIO;
118 goto error;
122 * Page is ok afterall, we are done.
124 unlock_page(page);
127 return 0;
128 error:
129 unlock_page(page);
130 return err;
133 static const struct pipe_buf_operations page_cache_pipe_buf_ops = {
134 .can_merge = 0,
135 .map = generic_pipe_buf_map,
136 .unmap = generic_pipe_buf_unmap,
137 .confirm = page_cache_pipe_buf_confirm,
138 .release = page_cache_pipe_buf_release,
139 .steal = page_cache_pipe_buf_steal,
140 .get = generic_pipe_buf_get,
143 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
144 struct pipe_buffer *buf)
146 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
147 return 1;
149 buf->flags |= PIPE_BUF_FLAG_LRU;
150 return generic_pipe_buf_steal(pipe, buf);
153 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
154 .can_merge = 0,
155 .map = generic_pipe_buf_map,
156 .unmap = generic_pipe_buf_unmap,
157 .confirm = generic_pipe_buf_confirm,
158 .release = page_cache_pipe_buf_release,
159 .steal = user_page_pipe_buf_steal,
160 .get = generic_pipe_buf_get,
164 * splice_to_pipe - fill passed data into a pipe
165 * @pipe: pipe to fill
166 * @spd: data to fill
168 * Description:
169 * @spd contains a map of pages and len/offset tuples, along with
170 * the struct pipe_buf_operations associated with these pages. This
171 * function will link that data to the pipe.
174 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
175 struct splice_pipe_desc *spd)
177 unsigned int spd_pages = spd->nr_pages;
178 int ret, do_wakeup, page_nr;
180 ret = 0;
181 do_wakeup = 0;
182 page_nr = 0;
184 if (pipe->inode)
185 mutex_lock(&pipe->inode->i_mutex);
187 for (;;) {
188 if (!pipe->readers) {
189 send_sig(SIGPIPE, current, 0);
190 if (!ret)
191 ret = -EPIPE;
192 break;
195 if (pipe->nrbufs < PIPE_BUFFERS) {
196 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
197 struct pipe_buffer *buf = pipe->bufs + newbuf;
199 buf->page = spd->pages[page_nr];
200 buf->offset = spd->partial[page_nr].offset;
201 buf->len = spd->partial[page_nr].len;
202 buf->private = spd->partial[page_nr].private;
203 buf->ops = spd->ops;
204 if (spd->flags & SPLICE_F_GIFT)
205 buf->flags |= PIPE_BUF_FLAG_GIFT;
207 pipe->nrbufs++;
208 page_nr++;
209 ret += buf->len;
211 if (pipe->inode)
212 do_wakeup = 1;
214 if (!--spd->nr_pages)
215 break;
216 if (pipe->nrbufs < PIPE_BUFFERS)
217 continue;
219 break;
222 if (spd->flags & SPLICE_F_NONBLOCK) {
223 if (!ret)
224 ret = -EAGAIN;
225 break;
228 if (signal_pending(current)) {
229 if (!ret)
230 ret = -ERESTARTSYS;
231 break;
234 if (do_wakeup) {
235 smp_mb();
236 if (waitqueue_active(&pipe->wait))
237 wake_up_interruptible_sync(&pipe->wait);
238 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
239 do_wakeup = 0;
242 pipe->waiting_writers++;
243 pipe_wait(pipe);
244 pipe->waiting_writers--;
247 if (pipe->inode) {
248 mutex_unlock(&pipe->inode->i_mutex);
250 if (do_wakeup) {
251 smp_mb();
252 if (waitqueue_active(&pipe->wait))
253 wake_up_interruptible(&pipe->wait);
254 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
258 while (page_nr < spd_pages)
259 spd->spd_release(spd, page_nr++);
261 return ret;
264 static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
266 page_cache_release(spd->pages[i]);
269 static int
270 __generic_file_splice_read(struct file *in, loff_t *ppos,
271 struct pipe_inode_info *pipe, size_t len,
272 unsigned int flags)
274 struct address_space *mapping = in->f_mapping;
275 unsigned int loff, nr_pages, req_pages;
276 struct page *pages[PIPE_BUFFERS];
277 struct partial_page partial[PIPE_BUFFERS];
278 struct page *page;
279 pgoff_t index, end_index;
280 loff_t isize;
281 int error, page_nr;
282 struct splice_pipe_desc spd = {
283 .pages = pages,
284 .partial = partial,
285 .flags = flags,
286 .ops = &page_cache_pipe_buf_ops,
287 .spd_release = spd_release_page,
290 index = *ppos >> PAGE_CACHE_SHIFT;
291 loff = *ppos & ~PAGE_CACHE_MASK;
292 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
293 nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS);
296 * Lookup the (hopefully) full range of pages we need.
298 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
299 index += spd.nr_pages;
302 * If find_get_pages_contig() returned fewer pages than we needed,
303 * readahead/allocate the rest and fill in the holes.
305 if (spd.nr_pages < nr_pages)
306 page_cache_sync_readahead(mapping, &in->f_ra, in,
307 index, req_pages - spd.nr_pages);
309 error = 0;
310 while (spd.nr_pages < nr_pages) {
312 * Page could be there, find_get_pages_contig() breaks on
313 * the first hole.
315 page = find_get_page(mapping, index);
316 if (!page) {
318 * page didn't exist, allocate one.
320 page = page_cache_alloc_cold(mapping);
321 if (!page)
322 break;
324 error = add_to_page_cache_lru(page, mapping, index,
325 mapping_gfp_mask(mapping));
326 if (unlikely(error)) {
327 page_cache_release(page);
328 if (error == -EEXIST)
329 continue;
330 break;
333 * add_to_page_cache() locks the page, unlock it
334 * to avoid convoluting the logic below even more.
336 unlock_page(page);
339 pages[spd.nr_pages++] = page;
340 index++;
344 * Now loop over the map and see if we need to start IO on any
345 * pages, fill in the partial map, etc.
347 index = *ppos >> PAGE_CACHE_SHIFT;
348 nr_pages = spd.nr_pages;
349 spd.nr_pages = 0;
350 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
351 unsigned int this_len;
353 if (!len)
354 break;
357 * this_len is the max we'll use from this page
359 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
360 page = pages[page_nr];
362 if (PageReadahead(page))
363 page_cache_async_readahead(mapping, &in->f_ra, in,
364 page, index, req_pages - page_nr);
367 * If the page isn't uptodate, we may need to start io on it
369 if (!PageUptodate(page)) {
371 * If in nonblock mode then dont block on waiting
372 * for an in-flight io page
374 if (flags & SPLICE_F_NONBLOCK) {
375 if (!trylock_page(page)) {
376 error = -EAGAIN;
377 break;
379 } else
380 lock_page(page);
383 * Page was truncated, or invalidated by the
384 * filesystem. Redo the find/create, but this time the
385 * page is kept locked, so there's no chance of another
386 * race with truncate/invalidate.
388 if (!page->mapping) {
389 unlock_page(page);
390 page = find_or_create_page(mapping, index,
391 mapping_gfp_mask(mapping));
393 if (!page) {
394 error = -ENOMEM;
395 break;
397 page_cache_release(pages[page_nr]);
398 pages[page_nr] = page;
401 * page was already under io and is now done, great
403 if (PageUptodate(page)) {
404 unlock_page(page);
405 goto fill_it;
409 * need to read in the page
411 error = mapping->a_ops->readpage(in, page);
412 if (unlikely(error)) {
414 * We really should re-lookup the page here,
415 * but it complicates things a lot. Instead
416 * lets just do what we already stored, and
417 * we'll get it the next time we are called.
419 if (error == AOP_TRUNCATED_PAGE)
420 error = 0;
422 break;
425 fill_it:
427 * i_size must be checked after PageUptodate.
429 isize = i_size_read(mapping->host);
430 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
431 if (unlikely(!isize || index > end_index))
432 break;
435 * if this is the last page, see if we need to shrink
436 * the length and stop
438 if (end_index == index) {
439 unsigned int plen;
442 * max good bytes in this page
444 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
445 if (plen <= loff)
446 break;
449 * force quit after adding this page
451 this_len = min(this_len, plen - loff);
452 len = this_len;
455 partial[page_nr].offset = loff;
456 partial[page_nr].len = this_len;
457 len -= this_len;
458 loff = 0;
459 spd.nr_pages++;
460 index++;
464 * Release any pages at the end, if we quit early. 'page_nr' is how far
465 * we got, 'nr_pages' is how many pages are in the map.
467 while (page_nr < nr_pages)
468 page_cache_release(pages[page_nr++]);
469 in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
471 if (spd.nr_pages)
472 return splice_to_pipe(pipe, &spd);
474 return error;
478 * generic_file_splice_read - splice data from file to a pipe
479 * @in: file to splice from
480 * @ppos: position in @in
481 * @pipe: pipe to splice to
482 * @len: number of bytes to splice
483 * @flags: splice modifier flags
485 * Description:
486 * Will read pages from given file and fill them into a pipe. Can be
487 * used as long as the address_space operations for the source implements
488 * a readpage() hook.
491 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
492 struct pipe_inode_info *pipe, size_t len,
493 unsigned int flags)
495 loff_t isize, left;
496 int ret;
498 isize = i_size_read(in->f_mapping->host);
499 if (unlikely(*ppos >= isize))
500 return 0;
502 left = isize - *ppos;
503 if (unlikely(left < len))
504 len = left;
506 ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
507 if (ret > 0)
508 *ppos += ret;
510 return ret;
513 EXPORT_SYMBOL(generic_file_splice_read);
516 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
517 * using sendpage(). Return the number of bytes sent.
519 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
520 struct pipe_buffer *buf, struct splice_desc *sd)
522 struct file *file = sd->u.file;
523 loff_t pos = sd->pos;
524 int ret, more;
526 ret = buf->ops->confirm(pipe, buf);
527 if (!ret) {
528 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
530 ret = file->f_op->sendpage(file, buf->page, buf->offset,
531 sd->len, &pos, more);
534 return ret;
538 * This is a little more tricky than the file -> pipe splicing. There are
539 * basically three cases:
541 * - Destination page already exists in the address space and there
542 * are users of it. For that case we have no other option that
543 * copying the data. Tough luck.
544 * - Destination page already exists in the address space, but there
545 * are no users of it. Make sure it's uptodate, then drop it. Fall
546 * through to last case.
547 * - Destination page does not exist, we can add the pipe page to
548 * the page cache and avoid the copy.
550 * If asked to move pages to the output file (SPLICE_F_MOVE is set in
551 * sd->flags), we attempt to migrate pages from the pipe to the output
552 * file address space page cache. This is possible if no one else has
553 * the pipe page referenced outside of the pipe and page cache. If
554 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
555 * a new page in the output file page cache and fill/dirty that.
557 static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
558 struct splice_desc *sd)
560 struct file *file = sd->u.file;
561 struct address_space *mapping = file->f_mapping;
562 unsigned int offset, this_len;
563 struct page *page;
564 void *fsdata;
565 int ret;
568 * make sure the data in this buffer is uptodate
570 ret = buf->ops->confirm(pipe, buf);
571 if (unlikely(ret))
572 return ret;
574 offset = sd->pos & ~PAGE_CACHE_MASK;
576 this_len = sd->len;
577 if (this_len + offset > PAGE_CACHE_SIZE)
578 this_len = PAGE_CACHE_SIZE - offset;
580 ret = pagecache_write_begin(file, mapping, sd->pos, this_len,
581 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
582 if (unlikely(ret))
583 goto out;
585 if (buf->page != page) {
587 * Careful, ->map() uses KM_USER0!
589 char *src = buf->ops->map(pipe, buf, 1);
590 char *dst = kmap_atomic(page, KM_USER1);
592 memcpy(dst + offset, src + buf->offset, this_len);
593 flush_dcache_page(page);
594 kunmap_atomic(dst, KM_USER1);
595 buf->ops->unmap(pipe, buf, src);
597 ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len,
598 page, fsdata);
599 out:
600 return ret;
604 * __splice_from_pipe - splice data from a pipe to given actor
605 * @pipe: pipe to splice from
606 * @sd: information to @actor
607 * @actor: handler that splices the data
609 * Description:
610 * This function does little more than loop over the pipe and call
611 * @actor to do the actual moving of a single struct pipe_buffer to
612 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
613 * pipe_to_user.
616 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
617 splice_actor *actor)
619 int ret, do_wakeup, err;
621 ret = 0;
622 do_wakeup = 0;
624 for (;;) {
625 if (pipe->nrbufs) {
626 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
627 const struct pipe_buf_operations *ops = buf->ops;
629 sd->len = buf->len;
630 if (sd->len > sd->total_len)
631 sd->len = sd->total_len;
633 err = actor(pipe, buf, sd);
634 if (err <= 0) {
635 if (!ret && err != -ENODATA)
636 ret = err;
638 break;
641 ret += err;
642 buf->offset += err;
643 buf->len -= err;
645 sd->len -= err;
646 sd->pos += err;
647 sd->total_len -= err;
648 if (sd->len)
649 continue;
651 if (!buf->len) {
652 buf->ops = NULL;
653 ops->release(pipe, buf);
654 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
655 pipe->nrbufs--;
656 if (pipe->inode)
657 do_wakeup = 1;
660 if (!sd->total_len)
661 break;
664 if (pipe->nrbufs)
665 continue;
666 if (!pipe->writers)
667 break;
668 if (!pipe->waiting_writers) {
669 if (ret)
670 break;
673 if (sd->flags & SPLICE_F_NONBLOCK) {
674 if (!ret)
675 ret = -EAGAIN;
676 break;
679 if (signal_pending(current)) {
680 if (!ret)
681 ret = -ERESTARTSYS;
682 break;
685 if (do_wakeup) {
686 smp_mb();
687 if (waitqueue_active(&pipe->wait))
688 wake_up_interruptible_sync(&pipe->wait);
689 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
690 do_wakeup = 0;
693 pipe_wait(pipe);
696 if (do_wakeup) {
697 smp_mb();
698 if (waitqueue_active(&pipe->wait))
699 wake_up_interruptible(&pipe->wait);
700 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
703 return ret;
705 EXPORT_SYMBOL(__splice_from_pipe);
708 * splice_from_pipe - splice data from a pipe to a file
709 * @pipe: pipe to splice from
710 * @out: file to splice to
711 * @ppos: position in @out
712 * @len: how many bytes to splice
713 * @flags: splice modifier flags
714 * @actor: handler that splices the data
716 * Description:
717 * See __splice_from_pipe. This function locks the input and output inodes,
718 * otherwise it's identical to __splice_from_pipe().
721 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
722 loff_t *ppos, size_t len, unsigned int flags,
723 splice_actor *actor)
725 ssize_t ret;
726 struct inode *inode = out->f_mapping->host;
727 struct splice_desc sd = {
728 .total_len = len,
729 .flags = flags,
730 .pos = *ppos,
731 .u.file = out,
735 * The actor worker might be calling ->write_begin and
736 * ->write_end. Most of the time, these expect i_mutex to
737 * be held. Since this may result in an ABBA deadlock with
738 * pipe->inode, we have to order lock acquiry here.
740 * Outer lock must be inode->i_mutex, as pipe_wait() will
741 * release and reacquire pipe->inode->i_mutex, AND inode must
742 * never be a pipe.
744 WARN_ON(S_ISFIFO(inode->i_mode));
745 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
746 if (pipe->inode)
747 mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_CHILD);
748 ret = __splice_from_pipe(pipe, &sd, actor);
749 if (pipe->inode)
750 mutex_unlock(&pipe->inode->i_mutex);
751 mutex_unlock(&inode->i_mutex);
753 return ret;
757 * generic_file_splice_write_nolock - generic_file_splice_write without mutexes
758 * @pipe: pipe info
759 * @out: file to write to
760 * @ppos: position in @out
761 * @len: number of bytes to splice
762 * @flags: splice modifier flags
764 * Description:
765 * Will either move or copy pages (determined by @flags options) from
766 * the given pipe inode to the given file. The caller is responsible
767 * for acquiring i_mutex on both inodes.
770 ssize_t
771 generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
772 loff_t *ppos, size_t len, unsigned int flags)
774 struct address_space *mapping = out->f_mapping;
775 struct inode *inode = mapping->host;
776 struct splice_desc sd = {
777 .total_len = len,
778 .flags = flags,
779 .pos = *ppos,
780 .u.file = out,
782 ssize_t ret;
783 int err;
785 err = file_remove_suid(out);
786 if (unlikely(err))
787 return err;
789 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
790 if (ret > 0) {
791 unsigned long nr_pages;
793 *ppos += ret;
794 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
797 * If file or inode is SYNC and we actually wrote some data,
798 * sync it.
800 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
801 err = generic_osync_inode(inode, mapping,
802 OSYNC_METADATA|OSYNC_DATA);
804 if (err)
805 ret = err;
807 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
810 return ret;
813 EXPORT_SYMBOL(generic_file_splice_write_nolock);
816 * generic_file_splice_write - splice data from a pipe to a file
817 * @pipe: pipe info
818 * @out: file to write to
819 * @ppos: position in @out
820 * @len: number of bytes to splice
821 * @flags: splice modifier flags
823 * Description:
824 * Will either move or copy pages (determined by @flags options) from
825 * the given pipe inode to the given file.
828 ssize_t
829 generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
830 loff_t *ppos, size_t len, unsigned int flags)
832 struct address_space *mapping = out->f_mapping;
833 struct inode *inode = mapping->host;
834 struct splice_desc sd = {
835 .total_len = len,
836 .flags = flags,
837 .pos = *ppos,
838 .u.file = out,
840 ssize_t ret;
842 WARN_ON(S_ISFIFO(inode->i_mode));
843 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
844 ret = file_remove_suid(out);
845 if (likely(!ret)) {
846 if (pipe->inode)
847 mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_CHILD);
848 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
849 if (pipe->inode)
850 mutex_unlock(&pipe->inode->i_mutex);
852 mutex_unlock(&inode->i_mutex);
853 if (ret > 0) {
854 unsigned long nr_pages;
856 *ppos += ret;
857 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
860 * If file or inode is SYNC and we actually wrote some data,
861 * sync it.
863 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
864 int err;
866 mutex_lock(&inode->i_mutex);
867 err = generic_osync_inode(inode, mapping,
868 OSYNC_METADATA|OSYNC_DATA);
869 mutex_unlock(&inode->i_mutex);
871 if (err)
872 ret = err;
874 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
877 return ret;
880 EXPORT_SYMBOL(generic_file_splice_write);
883 * generic_splice_sendpage - splice data from a pipe to a socket
884 * @pipe: pipe to splice from
885 * @out: socket to write to
886 * @ppos: position in @out
887 * @len: number of bytes to splice
888 * @flags: splice modifier flags
890 * Description:
891 * Will send @len bytes from the pipe to a network socket. No data copying
892 * is involved.
895 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
896 loff_t *ppos, size_t len, unsigned int flags)
898 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
901 EXPORT_SYMBOL(generic_splice_sendpage);
904 * Attempt to initiate a splice from pipe to file.
906 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
907 loff_t *ppos, size_t len, unsigned int flags)
909 int ret;
911 if (unlikely(!out->f_op || !out->f_op->splice_write))
912 return -EINVAL;
914 if (unlikely(!(out->f_mode & FMODE_WRITE)))
915 return -EBADF;
917 if (unlikely(out->f_flags & O_APPEND))
918 return -EINVAL;
920 ret = rw_verify_area(WRITE, out, ppos, len);
921 if (unlikely(ret < 0))
922 return ret;
924 return out->f_op->splice_write(pipe, out, ppos, len, flags);
928 * Attempt to initiate a splice from a file to a pipe.
930 static long do_splice_to(struct file *in, loff_t *ppos,
931 struct pipe_inode_info *pipe, size_t len,
932 unsigned int flags)
934 int ret;
936 if (unlikely(!in->f_op || !in->f_op->splice_read))
937 return -EINVAL;
939 if (unlikely(!(in->f_mode & FMODE_READ)))
940 return -EBADF;
942 ret = rw_verify_area(READ, in, ppos, len);
943 if (unlikely(ret < 0))
944 return ret;
946 return in->f_op->splice_read(in, ppos, pipe, len, flags);
950 * splice_direct_to_actor - splices data directly between two non-pipes
951 * @in: file to splice from
952 * @sd: actor information on where to splice to
953 * @actor: handles the data splicing
955 * Description:
956 * This is a special case helper to splice directly between two
957 * points, without requiring an explicit pipe. Internally an allocated
958 * pipe is cached in the process, and reused during the lifetime of
959 * that process.
962 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
963 splice_direct_actor *actor)
965 struct pipe_inode_info *pipe;
966 long ret, bytes;
967 umode_t i_mode;
968 size_t len;
969 int i, flags;
972 * We require the input being a regular file, as we don't want to
973 * randomly drop data for eg socket -> socket splicing. Use the
974 * piped splicing for that!
976 i_mode = in->f_path.dentry->d_inode->i_mode;
977 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
978 return -EINVAL;
981 * neither in nor out is a pipe, setup an internal pipe attached to
982 * 'out' and transfer the wanted data from 'in' to 'out' through that
984 pipe = current->splice_pipe;
985 if (unlikely(!pipe)) {
986 pipe = alloc_pipe_info(NULL);
987 if (!pipe)
988 return -ENOMEM;
991 * We don't have an immediate reader, but we'll read the stuff
992 * out of the pipe right after the splice_to_pipe(). So set
993 * PIPE_READERS appropriately.
995 pipe->readers = 1;
997 current->splice_pipe = pipe;
1001 * Do the splice.
1003 ret = 0;
1004 bytes = 0;
1005 len = sd->total_len;
1006 flags = sd->flags;
1009 * Don't block on output, we have to drain the direct pipe.
1011 sd->flags &= ~SPLICE_F_NONBLOCK;
1013 while (len) {
1014 size_t read_len;
1015 loff_t pos = sd->pos, prev_pos = pos;
1017 ret = do_splice_to(in, &pos, pipe, len, flags);
1018 if (unlikely(ret <= 0))
1019 goto out_release;
1021 read_len = ret;
1022 sd->total_len = read_len;
1025 * NOTE: nonblocking mode only applies to the input. We
1026 * must not do the output in nonblocking mode as then we
1027 * could get stuck data in the internal pipe:
1029 ret = actor(pipe, sd);
1030 if (unlikely(ret <= 0)) {
1031 sd->pos = prev_pos;
1032 goto out_release;
1035 bytes += ret;
1036 len -= ret;
1037 sd->pos = pos;
1039 if (ret < read_len) {
1040 sd->pos = prev_pos + ret;
1041 goto out_release;
1045 done:
1046 pipe->nrbufs = pipe->curbuf = 0;
1047 file_accessed(in);
1048 return bytes;
1050 out_release:
1052 * If we did an incomplete transfer we must release
1053 * the pipe buffers in question:
1055 for (i = 0; i < PIPE_BUFFERS; i++) {
1056 struct pipe_buffer *buf = pipe->bufs + i;
1058 if (buf->ops) {
1059 buf->ops->release(pipe, buf);
1060 buf->ops = NULL;
1064 if (!bytes)
1065 bytes = ret;
1067 goto done;
1069 EXPORT_SYMBOL(splice_direct_to_actor);
1071 static int direct_splice_actor(struct pipe_inode_info *pipe,
1072 struct splice_desc *sd)
1074 struct file *file = sd->u.file;
1076 return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags);
1080 * do_splice_direct - splices data directly between two files
1081 * @in: file to splice from
1082 * @ppos: input file offset
1083 * @out: file to splice to
1084 * @len: number of bytes to splice
1085 * @flags: splice modifier flags
1087 * Description:
1088 * For use by do_sendfile(). splice can easily emulate sendfile, but
1089 * doing it in the application would incur an extra system call
1090 * (splice in + splice out, as compared to just sendfile()). So this helper
1091 * can splice directly through a process-private pipe.
1094 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1095 size_t len, unsigned int flags)
1097 struct splice_desc sd = {
1098 .len = len,
1099 .total_len = len,
1100 .flags = flags,
1101 .pos = *ppos,
1102 .u.file = out,
1104 long ret;
1106 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1107 if (ret > 0)
1108 *ppos = sd.pos;
1110 return ret;
1114 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1115 * location, so checking ->i_pipe is not enough to verify that this is a
1116 * pipe.
1118 static inline struct pipe_inode_info *pipe_info(struct inode *inode)
1120 if (S_ISFIFO(inode->i_mode))
1121 return inode->i_pipe;
1123 return NULL;
1127 * Determine where to splice to/from.
1129 static long do_splice(struct file *in, loff_t __user *off_in,
1130 struct file *out, loff_t __user *off_out,
1131 size_t len, unsigned int flags)
1133 struct pipe_inode_info *pipe;
1134 loff_t offset, *off;
1135 long ret;
1137 pipe = pipe_info(in->f_path.dentry->d_inode);
1138 if (pipe) {
1139 if (off_in)
1140 return -ESPIPE;
1141 if (off_out) {
1142 if (out->f_op->llseek == no_llseek)
1143 return -EINVAL;
1144 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1145 return -EFAULT;
1146 off = &offset;
1147 } else
1148 off = &out->f_pos;
1150 ret = do_splice_from(pipe, out, off, len, flags);
1152 if (off_out && copy_to_user(off_out, off, sizeof(loff_t)))
1153 ret = -EFAULT;
1155 return ret;
1158 pipe = pipe_info(out->f_path.dentry->d_inode);
1159 if (pipe) {
1160 if (off_out)
1161 return -ESPIPE;
1162 if (off_in) {
1163 if (in->f_op->llseek == no_llseek)
1164 return -EINVAL;
1165 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1166 return -EFAULT;
1167 off = &offset;
1168 } else
1169 off = &in->f_pos;
1171 ret = do_splice_to(in, off, pipe, len, flags);
1173 if (off_in && copy_to_user(off_in, off, sizeof(loff_t)))
1174 ret = -EFAULT;
1176 return ret;
1179 return -EINVAL;
1183 * Map an iov into an array of pages and offset/length tupples. With the
1184 * partial_page structure, we can map several non-contiguous ranges into
1185 * our ones pages[] map instead of splitting that operation into pieces.
1186 * Could easily be exported as a generic helper for other users, in which
1187 * case one would probably want to add a 'max_nr_pages' parameter as well.
1189 static int get_iovec_page_array(const struct iovec __user *iov,
1190 unsigned int nr_vecs, struct page **pages,
1191 struct partial_page *partial, int aligned)
1193 int buffers = 0, error = 0;
1195 while (nr_vecs) {
1196 unsigned long off, npages;
1197 struct iovec entry;
1198 void __user *base;
1199 size_t len;
1200 int i;
1202 error = -EFAULT;
1203 if (copy_from_user(&entry, iov, sizeof(entry)))
1204 break;
1206 base = entry.iov_base;
1207 len = entry.iov_len;
1210 * Sanity check this iovec. 0 read succeeds.
1212 error = 0;
1213 if (unlikely(!len))
1214 break;
1215 error = -EFAULT;
1216 if (!access_ok(VERIFY_READ, base, len))
1217 break;
1220 * Get this base offset and number of pages, then map
1221 * in the user pages.
1223 off = (unsigned long) base & ~PAGE_MASK;
1226 * If asked for alignment, the offset must be zero and the
1227 * length a multiple of the PAGE_SIZE.
1229 error = -EINVAL;
1230 if (aligned && (off || len & ~PAGE_MASK))
1231 break;
1233 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1234 if (npages > PIPE_BUFFERS - buffers)
1235 npages = PIPE_BUFFERS - buffers;
1237 error = get_user_pages_fast((unsigned long)base, npages,
1238 0, &pages[buffers]);
1240 if (unlikely(error <= 0))
1241 break;
1244 * Fill this contiguous range into the partial page map.
1246 for (i = 0; i < error; i++) {
1247 const int plen = min_t(size_t, len, PAGE_SIZE - off);
1249 partial[buffers].offset = off;
1250 partial[buffers].len = plen;
1252 off = 0;
1253 len -= plen;
1254 buffers++;
1258 * We didn't complete this iov, stop here since it probably
1259 * means we have to move some of this into a pipe to
1260 * be able to continue.
1262 if (len)
1263 break;
1266 * Don't continue if we mapped fewer pages than we asked for,
1267 * or if we mapped the max number of pages that we have
1268 * room for.
1270 if (error < npages || buffers == PIPE_BUFFERS)
1271 break;
1273 nr_vecs--;
1274 iov++;
1277 if (buffers)
1278 return buffers;
1280 return error;
1283 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1284 struct splice_desc *sd)
1286 char *src;
1287 int ret;
1289 ret = buf->ops->confirm(pipe, buf);
1290 if (unlikely(ret))
1291 return ret;
1294 * See if we can use the atomic maps, by prefaulting in the
1295 * pages and doing an atomic copy
1297 if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) {
1298 src = buf->ops->map(pipe, buf, 1);
1299 ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset,
1300 sd->len);
1301 buf->ops->unmap(pipe, buf, src);
1302 if (!ret) {
1303 ret = sd->len;
1304 goto out;
1309 * No dice, use slow non-atomic map and copy
1311 src = buf->ops->map(pipe, buf, 0);
1313 ret = sd->len;
1314 if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len))
1315 ret = -EFAULT;
1317 buf->ops->unmap(pipe, buf, src);
1318 out:
1319 if (ret > 0)
1320 sd->u.userptr += ret;
1321 return ret;
1325 * For lack of a better implementation, implement vmsplice() to userspace
1326 * as a simple copy of the pipes pages to the user iov.
1328 static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
1329 unsigned long nr_segs, unsigned int flags)
1331 struct pipe_inode_info *pipe;
1332 struct splice_desc sd;
1333 ssize_t size;
1334 int error;
1335 long ret;
1337 pipe = pipe_info(file->f_path.dentry->d_inode);
1338 if (!pipe)
1339 return -EBADF;
1341 if (pipe->inode)
1342 mutex_lock(&pipe->inode->i_mutex);
1344 error = ret = 0;
1345 while (nr_segs) {
1346 void __user *base;
1347 size_t len;
1350 * Get user address base and length for this iovec.
1352 error = get_user(base, &iov->iov_base);
1353 if (unlikely(error))
1354 break;
1355 error = get_user(len, &iov->iov_len);
1356 if (unlikely(error))
1357 break;
1360 * Sanity check this iovec. 0 read succeeds.
1362 if (unlikely(!len))
1363 break;
1364 if (unlikely(!base)) {
1365 error = -EFAULT;
1366 break;
1369 if (unlikely(!access_ok(VERIFY_WRITE, base, len))) {
1370 error = -EFAULT;
1371 break;
1374 sd.len = 0;
1375 sd.total_len = len;
1376 sd.flags = flags;
1377 sd.u.userptr = base;
1378 sd.pos = 0;
1380 size = __splice_from_pipe(pipe, &sd, pipe_to_user);
1381 if (size < 0) {
1382 if (!ret)
1383 ret = size;
1385 break;
1388 ret += size;
1390 if (size < len)
1391 break;
1393 nr_segs--;
1394 iov++;
1397 if (pipe->inode)
1398 mutex_unlock(&pipe->inode->i_mutex);
1400 if (!ret)
1401 ret = error;
1403 return ret;
1407 * vmsplice splices a user address range into a pipe. It can be thought of
1408 * as splice-from-memory, where the regular splice is splice-from-file (or
1409 * to file). In both cases the output is a pipe, naturally.
1411 static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
1412 unsigned long nr_segs, unsigned int flags)
1414 struct pipe_inode_info *pipe;
1415 struct page *pages[PIPE_BUFFERS];
1416 struct partial_page partial[PIPE_BUFFERS];
1417 struct splice_pipe_desc spd = {
1418 .pages = pages,
1419 .partial = partial,
1420 .flags = flags,
1421 .ops = &user_page_pipe_buf_ops,
1422 .spd_release = spd_release_page,
1425 pipe = pipe_info(file->f_path.dentry->d_inode);
1426 if (!pipe)
1427 return -EBADF;
1429 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1430 flags & SPLICE_F_GIFT);
1431 if (spd.nr_pages <= 0)
1432 return spd.nr_pages;
1434 return splice_to_pipe(pipe, &spd);
1438 * Note that vmsplice only really supports true splicing _from_ user memory
1439 * to a pipe, not the other way around. Splicing from user memory is a simple
1440 * operation that can be supported without any funky alignment restrictions
1441 * or nasty vm tricks. We simply map in the user memory and fill them into
1442 * a pipe. The reverse isn't quite as easy, though. There are two possible
1443 * solutions for that:
1445 * - memcpy() the data internally, at which point we might as well just
1446 * do a regular read() on the buffer anyway.
1447 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1448 * has restriction limitations on both ends of the pipe).
1450 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1453 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
1454 unsigned long, nr_segs, unsigned int, flags)
1456 struct file *file;
1457 long error;
1458 int fput;
1460 if (unlikely(nr_segs > UIO_MAXIOV))
1461 return -EINVAL;
1462 else if (unlikely(!nr_segs))
1463 return 0;
1465 error = -EBADF;
1466 file = fget_light(fd, &fput);
1467 if (file) {
1468 if (file->f_mode & FMODE_WRITE)
1469 error = vmsplice_to_pipe(file, iov, nr_segs, flags);
1470 else if (file->f_mode & FMODE_READ)
1471 error = vmsplice_to_user(file, iov, nr_segs, flags);
1473 fput_light(file, fput);
1476 return error;
1479 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1480 int, fd_out, loff_t __user *, off_out,
1481 size_t, len, unsigned int, flags)
1483 long error;
1484 struct file *in, *out;
1485 int fput_in, fput_out;
1487 if (unlikely(!len))
1488 return 0;
1490 error = -EBADF;
1491 in = fget_light(fd_in, &fput_in);
1492 if (in) {
1493 if (in->f_mode & FMODE_READ) {
1494 out = fget_light(fd_out, &fput_out);
1495 if (out) {
1496 if (out->f_mode & FMODE_WRITE)
1497 error = do_splice(in, off_in,
1498 out, off_out,
1499 len, flags);
1500 fput_light(out, fput_out);
1504 fput_light(in, fput_in);
1507 return error;
1511 * Make sure there's data to read. Wait for input if we can, otherwise
1512 * return an appropriate error.
1514 static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1516 int ret;
1519 * Check ->nrbufs without the inode lock first. This function
1520 * is speculative anyways, so missing one is ok.
1522 if (pipe->nrbufs)
1523 return 0;
1525 ret = 0;
1526 mutex_lock(&pipe->inode->i_mutex);
1528 while (!pipe->nrbufs) {
1529 if (signal_pending(current)) {
1530 ret = -ERESTARTSYS;
1531 break;
1533 if (!pipe->writers)
1534 break;
1535 if (!pipe->waiting_writers) {
1536 if (flags & SPLICE_F_NONBLOCK) {
1537 ret = -EAGAIN;
1538 break;
1541 pipe_wait(pipe);
1544 mutex_unlock(&pipe->inode->i_mutex);
1545 return ret;
1549 * Make sure there's writeable room. Wait for room if we can, otherwise
1550 * return an appropriate error.
1552 static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1554 int ret;
1557 * Check ->nrbufs without the inode lock first. This function
1558 * is speculative anyways, so missing one is ok.
1560 if (pipe->nrbufs < PIPE_BUFFERS)
1561 return 0;
1563 ret = 0;
1564 mutex_lock(&pipe->inode->i_mutex);
1566 while (pipe->nrbufs >= PIPE_BUFFERS) {
1567 if (!pipe->readers) {
1568 send_sig(SIGPIPE, current, 0);
1569 ret = -EPIPE;
1570 break;
1572 if (flags & SPLICE_F_NONBLOCK) {
1573 ret = -EAGAIN;
1574 break;
1576 if (signal_pending(current)) {
1577 ret = -ERESTARTSYS;
1578 break;
1580 pipe->waiting_writers++;
1581 pipe_wait(pipe);
1582 pipe->waiting_writers--;
1585 mutex_unlock(&pipe->inode->i_mutex);
1586 return ret;
1590 * Link contents of ipipe to opipe.
1592 static int link_pipe(struct pipe_inode_info *ipipe,
1593 struct pipe_inode_info *opipe,
1594 size_t len, unsigned int flags)
1596 struct pipe_buffer *ibuf, *obuf;
1597 int ret = 0, i = 0, nbuf;
1600 * Potential ABBA deadlock, work around it by ordering lock
1601 * grabbing by inode address. Otherwise two different processes
1602 * could deadlock (one doing tee from A -> B, the other from B -> A).
1604 inode_double_lock(ipipe->inode, opipe->inode);
1606 do {
1607 if (!opipe->readers) {
1608 send_sig(SIGPIPE, current, 0);
1609 if (!ret)
1610 ret = -EPIPE;
1611 break;
1615 * If we have iterated all input buffers or ran out of
1616 * output room, break.
1618 if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS)
1619 break;
1621 ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1));
1622 nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1);
1625 * Get a reference to this pipe buffer,
1626 * so we can copy the contents over.
1628 ibuf->ops->get(ipipe, ibuf);
1630 obuf = opipe->bufs + nbuf;
1631 *obuf = *ibuf;
1634 * Don't inherit the gift flag, we need to
1635 * prevent multiple steals of this page.
1637 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1639 if (obuf->len > len)
1640 obuf->len = len;
1642 opipe->nrbufs++;
1643 ret += obuf->len;
1644 len -= obuf->len;
1645 i++;
1646 } while (len);
1649 * return EAGAIN if we have the potential of some data in the
1650 * future, otherwise just return 0
1652 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1653 ret = -EAGAIN;
1655 inode_double_unlock(ipipe->inode, opipe->inode);
1658 * If we put data in the output pipe, wakeup any potential readers.
1660 if (ret > 0) {
1661 smp_mb();
1662 if (waitqueue_active(&opipe->wait))
1663 wake_up_interruptible(&opipe->wait);
1664 kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN);
1667 return ret;
1671 * This is a tee(1) implementation that works on pipes. It doesn't copy
1672 * any data, it simply references the 'in' pages on the 'out' pipe.
1673 * The 'flags' used are the SPLICE_F_* variants, currently the only
1674 * applicable one is SPLICE_F_NONBLOCK.
1676 static long do_tee(struct file *in, struct file *out, size_t len,
1677 unsigned int flags)
1679 struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode);
1680 struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode);
1681 int ret = -EINVAL;
1684 * Duplicate the contents of ipipe to opipe without actually
1685 * copying the data.
1687 if (ipipe && opipe && ipipe != opipe) {
1689 * Keep going, unless we encounter an error. The ipipe/opipe
1690 * ordering doesn't really matter.
1692 ret = link_ipipe_prep(ipipe, flags);
1693 if (!ret) {
1694 ret = link_opipe_prep(opipe, flags);
1695 if (!ret)
1696 ret = link_pipe(ipipe, opipe, len, flags);
1700 return ret;
1703 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1705 struct file *in;
1706 int error, fput_in;
1708 if (unlikely(!len))
1709 return 0;
1711 error = -EBADF;
1712 in = fget_light(fdin, &fput_in);
1713 if (in) {
1714 if (in->f_mode & FMODE_READ) {
1715 int fput_out;
1716 struct file *out = fget_light(fdout, &fput_out);
1718 if (out) {
1719 if (out->f_mode & FMODE_WRITE)
1720 error = do_tee(in, out, len, flags);
1721 fput_light(out, fput_out);
1724 fput_light(in, fput_in);
1727 return error;