aboutsummaryrefslogtreecommitdiffstats
path: root/fs/splice.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/splice.c')
-rw-r--r--fs/splice.c371
1 files changed, 186 insertions, 185 deletions
diff --git a/fs/splice.c b/fs/splice.c
index c18aa7e03e2b..5384a90665d0 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -182,8 +182,7 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
182 do_wakeup = 0; 182 do_wakeup = 0;
183 page_nr = 0; 183 page_nr = 0;
184 184
185 if (pipe->inode) 185 pipe_lock(pipe);
186 mutex_lock(&pipe->inode->i_mutex);
187 186
188 for (;;) { 187 for (;;) {
189 if (!pipe->readers) { 188 if (!pipe->readers) {
@@ -245,15 +244,13 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
245 pipe->waiting_writers--; 244 pipe->waiting_writers--;
246 } 245 }
247 246
248 if (pipe->inode) { 247 pipe_unlock(pipe);
249 mutex_unlock(&pipe->inode->i_mutex);
250 248
251 if (do_wakeup) { 249 if (do_wakeup) {
252 smp_mb(); 250 smp_mb();
253 if (waitqueue_active(&pipe->wait)) 251 if (waitqueue_active(&pipe->wait))
254 wake_up_interruptible(&pipe->wait); 252 wake_up_interruptible(&pipe->wait);
255 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 253 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
256 }
257 } 254 }
258 255
259 while (page_nr < spd_pages) 256 while (page_nr < spd_pages)
@@ -555,8 +552,8 @@ static int pipe_to_sendpage(struct pipe_inode_info *pipe,
555 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create 552 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
556 * a new page in the output file page cache and fill/dirty that. 553 * a new page in the output file page cache and fill/dirty that.
557 */ 554 */
558static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 555int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
559 struct splice_desc *sd) 556 struct splice_desc *sd)
560{ 557{
561 struct file *file = sd->u.file; 558 struct file *file = sd->u.file;
562 struct address_space *mapping = file->f_mapping; 559 struct address_space *mapping = file->f_mapping;
@@ -600,108 +597,178 @@ static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
600out: 597out:
601 return ret; 598 return ret;
602} 599}
600EXPORT_SYMBOL(pipe_to_file);
601
602static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
603{
604 smp_mb();
605 if (waitqueue_active(&pipe->wait))
606 wake_up_interruptible(&pipe->wait);
607 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
608}
603 609
604/** 610/**
605 * __splice_from_pipe - splice data from a pipe to given actor 611 * splice_from_pipe_feed - feed available data from a pipe to a file
606 * @pipe: pipe to splice from 612 * @pipe: pipe to splice from
607 * @sd: information to @actor 613 * @sd: information to @actor
608 * @actor: handler that splices the data 614 * @actor: handler that splices the data
609 * 615 *
610 * Description: 616 * Description:
611 * This function does little more than loop over the pipe and call 617
612 * @actor to do the actual moving of a single struct pipe_buffer to 618 * This function loops over the pipe and calls @actor to do the
613 * the desired destination. See pipe_to_file, pipe_to_sendpage, or 619 * actual moving of a single struct pipe_buffer to the desired
614 * pipe_to_user. 620 * destination. It returns when there's no more buffers left in
621 * the pipe or if the requested number of bytes (@sd->total_len)
622 * have been copied. It returns a positive number (one) if the
623 * pipe needs to be filled with more data, zero if the required
624 * number of bytes have been copied and -errno on error.
615 * 625 *
626 * This, together with splice_from_pipe_{begin,end,next}, may be
627 * used to implement the functionality of __splice_from_pipe() when
628 * locking is required around copying the pipe buffers to the
629 * destination.
616 */ 630 */
617ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, 631int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
618 splice_actor *actor) 632 splice_actor *actor)
619{ 633{
620 int ret, do_wakeup, err; 634 int ret;
621
622 ret = 0;
623 do_wakeup = 0;
624
625 for (;;) {
626 if (pipe->nrbufs) {
627 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
628 const struct pipe_buf_operations *ops = buf->ops;
629 635
630 sd->len = buf->len; 636 while (pipe->nrbufs) {
631 if (sd->len > sd->total_len) 637 struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
632 sd->len = sd->total_len; 638 const struct pipe_buf_operations *ops = buf->ops;
633 639
634 err = actor(pipe, buf, sd); 640 sd->len = buf->len;
635 if (err <= 0) { 641 if (sd->len > sd->total_len)
636 if (!ret && err != -ENODATA) 642 sd->len = sd->total_len;
637 ret = err;
638 643
639 break; 644 ret = actor(pipe, buf, sd);
640 } 645 if (ret <= 0) {
646 if (ret == -ENODATA)
647 ret = 0;
648 return ret;
649 }
650 buf->offset += ret;
651 buf->len -= ret;
641 652
642 ret += err; 653 sd->num_spliced += ret;
643 buf->offset += err; 654 sd->len -= ret;
644 buf->len -= err; 655 sd->pos += ret;
656 sd->total_len -= ret;
645 657
646 sd->len -= err; 658 if (!buf->len) {
647 sd->pos += err; 659 buf->ops = NULL;
648 sd->total_len -= err; 660 ops->release(pipe, buf);
649 if (sd->len) 661 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
650 continue; 662 pipe->nrbufs--;
663 if (pipe->inode)
664 sd->need_wakeup = true;
665 }
651 666
652 if (!buf->len) { 667 if (!sd->total_len)
653 buf->ops = NULL; 668 return 0;
654 ops->release(pipe, buf); 669 }
655 pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1);
656 pipe->nrbufs--;
657 if (pipe->inode)
658 do_wakeup = 1;
659 }
660 670
661 if (!sd->total_len) 671 return 1;
662 break; 672}
663 } 673EXPORT_SYMBOL(splice_from_pipe_feed);
664 674
665 if (pipe->nrbufs) 675/**
666 continue; 676 * splice_from_pipe_next - wait for some data to splice from
677 * @pipe: pipe to splice from
678 * @sd: information about the splice operation
679 *
680 * Description:
681 * This function will wait for some data and return a positive
682 * value (one) if pipe buffers are available. It will return zero
683 * or -errno if no more data needs to be spliced.
684 */
685int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
686{
687 while (!pipe->nrbufs) {
667 if (!pipe->writers) 688 if (!pipe->writers)
668 break; 689 return 0;
669 if (!pipe->waiting_writers) {
670 if (ret)
671 break;
672 }
673 690
674 if (sd->flags & SPLICE_F_NONBLOCK) { 691 if (!pipe->waiting_writers && sd->num_spliced)
675 if (!ret) 692 return 0;
676 ret = -EAGAIN;
677 break;
678 }
679 693
680 if (signal_pending(current)) { 694 if (sd->flags & SPLICE_F_NONBLOCK)
681 if (!ret) 695 return -EAGAIN;
682 ret = -ERESTARTSYS;
683 break;
684 }
685 696
686 if (do_wakeup) { 697 if (signal_pending(current))
687 smp_mb(); 698 return -ERESTARTSYS;
688 if (waitqueue_active(&pipe->wait)) 699
689 wake_up_interruptible_sync(&pipe->wait); 700 if (sd->need_wakeup) {
690 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 701 wakeup_pipe_writers(pipe);
691 do_wakeup = 0; 702 sd->need_wakeup = false;
692 } 703 }
693 704
694 pipe_wait(pipe); 705 pipe_wait(pipe);
695 } 706 }
696 707
697 if (do_wakeup) { 708 return 1;
698 smp_mb(); 709}
699 if (waitqueue_active(&pipe->wait)) 710EXPORT_SYMBOL(splice_from_pipe_next);
700 wake_up_interruptible(&pipe->wait);
701 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
702 }
703 711
704 return ret; 712/**
713 * splice_from_pipe_begin - start splicing from pipe
714 * @pipe: pipe to splice from
715 *
716 * Description:
717 * This function should be called before a loop containing
718 * splice_from_pipe_next() and splice_from_pipe_feed() to
719 * initialize the necessary fields of @sd.
720 */
721void splice_from_pipe_begin(struct splice_desc *sd)
722{
723 sd->num_spliced = 0;
724 sd->need_wakeup = false;
725}
726EXPORT_SYMBOL(splice_from_pipe_begin);
727
728/**
729 * splice_from_pipe_end - finish splicing from pipe
730 * @pipe: pipe to splice from
731 * @sd: information about the splice operation
732 *
733 * Description:
734 * This function will wake up pipe writers if necessary. It should
735 * be called after a loop containing splice_from_pipe_next() and
736 * splice_from_pipe_feed().
737 */
738void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
739{
740 if (sd->need_wakeup)
741 wakeup_pipe_writers(pipe);
742}
743EXPORT_SYMBOL(splice_from_pipe_end);
744
745/**
746 * __splice_from_pipe - splice data from a pipe to given actor
747 * @pipe: pipe to splice from
748 * @sd: information to @actor
749 * @actor: handler that splices the data
750 *
751 * Description:
752 * This function does little more than loop over the pipe and call
753 * @actor to do the actual moving of a single struct pipe_buffer to
754 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
755 * pipe_to_user.
756 *
757 */
758ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
759 splice_actor *actor)
760{
761 int ret;
762
763 splice_from_pipe_begin(sd);
764 do {
765 ret = splice_from_pipe_next(pipe, sd);
766 if (ret > 0)
767 ret = splice_from_pipe_feed(pipe, sd, actor);
768 } while (ret > 0);
769 splice_from_pipe_end(pipe, sd);
770
771 return sd->num_spliced ? sd->num_spliced : ret;
705} 772}
706EXPORT_SYMBOL(__splice_from_pipe); 773EXPORT_SYMBOL(__splice_from_pipe);
707 774
@@ -715,7 +782,7 @@ EXPORT_SYMBOL(__splice_from_pipe);
715 * @actor: handler that splices the data 782 * @actor: handler that splices the data
716 * 783 *
717 * Description: 784 * Description:
718 * See __splice_from_pipe. This function locks the input and output inodes, 785 * See __splice_from_pipe. This function locks the pipe inode,
719 * otherwise it's identical to __splice_from_pipe(). 786 * otherwise it's identical to __splice_from_pipe().
720 * 787 *
721 */ 788 */
@@ -724,7 +791,6 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
724 splice_actor *actor) 791 splice_actor *actor)
725{ 792{
726 ssize_t ret; 793 ssize_t ret;
727 struct inode *inode = out->f_mapping->host;
728 struct splice_desc sd = { 794 struct splice_desc sd = {
729 .total_len = len, 795 .total_len = len,
730 .flags = flags, 796 .flags = flags,
@@ -732,30 +798,15 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
732 .u.file = out, 798 .u.file = out,
733 }; 799 };
734 800
735 /* 801 pipe_lock(pipe);
736 * The actor worker might be calling ->write_begin and
737 * ->write_end. Most of the time, these expect i_mutex to
738 * be held. Since this may result in an ABBA deadlock with
739 * pipe->inode, we have to order lock acquiry here.
740 *
741 * Outer lock must be inode->i_mutex, as pipe_wait() will
742 * release and reacquire pipe->inode->i_mutex, AND inode must
743 * never be a pipe.
744 */
745 WARN_ON(S_ISFIFO(inode->i_mode));
746 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
747 if (pipe->inode)
748 mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_CHILD);
749 ret = __splice_from_pipe(pipe, &sd, actor); 802 ret = __splice_from_pipe(pipe, &sd, actor);
750 if (pipe->inode) 803 pipe_unlock(pipe);
751 mutex_unlock(&pipe->inode->i_mutex);
752 mutex_unlock(&inode->i_mutex);
753 804
754 return ret; 805 return ret;
755} 806}
756 807
757/** 808/**
758 * generic_file_splice_write_nolock - generic_file_splice_write without mutexes 809 * generic_file_splice_write - splice data from a pipe to a file
759 * @pipe: pipe info 810 * @pipe: pipe info
760 * @out: file to write to 811 * @out: file to write to
761 * @ppos: position in @out 812 * @ppos: position in @out
@@ -764,13 +815,12 @@ ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
764 * 815 *
765 * Description: 816 * Description:
766 * Will either move or copy pages (determined by @flags options) from 817 * Will either move or copy pages (determined by @flags options) from
767 * the given pipe inode to the given file. The caller is responsible 818 * the given pipe inode to the given file.
768 * for acquiring i_mutex on both inodes.
769 * 819 *
770 */ 820 */
771ssize_t 821ssize_t
772generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out, 822generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
773 loff_t *ppos, size_t len, unsigned int flags) 823 loff_t *ppos, size_t len, unsigned int flags)
774{ 824{
775 struct address_space *mapping = out->f_mapping; 825 struct address_space *mapping = out->f_mapping;
776 struct inode *inode = mapping->host; 826 struct inode *inode = mapping->host;
@@ -781,76 +831,28 @@ generic_file_splice_write_nolock(struct pipe_inode_info *pipe, struct file *out,
781 .u.file = out, 831 .u.file = out,
782 }; 832 };
783 ssize_t ret; 833 ssize_t ret;
784 int err;
785
786 err = file_remove_suid(out);
787 if (unlikely(err))
788 return err;
789
790 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
791 if (ret > 0) {
792 unsigned long nr_pages;
793 834
794 *ppos += ret; 835 pipe_lock(pipe);
795 nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
796 836
797 /* 837 splice_from_pipe_begin(&sd);
798 * If file or inode is SYNC and we actually wrote some data, 838 do {
799 * sync it. 839 ret = splice_from_pipe_next(pipe, &sd);
800 */ 840 if (ret <= 0)
801 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { 841 break;
802 err = generic_osync_inode(inode, mapping,
803 OSYNC_METADATA|OSYNC_DATA);
804
805 if (err)
806 ret = err;
807 }
808 balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
809 }
810 842
811 return ret; 843 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
812} 844 ret = file_remove_suid(out);
845 if (!ret)
846 ret = splice_from_pipe_feed(pipe, &sd, pipe_to_file);
847 mutex_unlock(&inode->i_mutex);
848 } while (ret > 0);
849 splice_from_pipe_end(pipe, &sd);
813 850
814EXPORT_SYMBOL(generic_file_splice_write_nolock); 851 pipe_unlock(pipe);
815 852
816/** 853 if (sd.num_spliced)
817 * generic_file_splice_write - splice data from a pipe to a file 854 ret = sd.num_spliced;
818 * @pipe: pipe info
819 * @out: file to write to
820 * @ppos: position in @out
821 * @len: number of bytes to splice
822 * @flags: splice modifier flags
823 *
824 * Description:
825 * Will either move or copy pages (determined by @flags options) from
826 * the given pipe inode to the given file.
827 *
828 */
829ssize_t
830generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
831 loff_t *ppos, size_t len, unsigned int flags)
832{
833 struct address_space *mapping = out->f_mapping;
834 struct inode *inode = mapping->host;
835 struct splice_desc sd = {
836 .total_len = len,
837 .flags = flags,
838 .pos = *ppos,
839 .u.file = out,
840 };
841 ssize_t ret;
842 855
843 WARN_ON(S_ISFIFO(inode->i_mode));
844 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
845 ret = file_remove_suid(out);
846 if (likely(!ret)) {
847 if (pipe->inode)
848 mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_CHILD);
849 ret = __splice_from_pipe(pipe, &sd, pipe_to_file);
850 if (pipe->inode)
851 mutex_unlock(&pipe->inode->i_mutex);
852 }
853 mutex_unlock(&inode->i_mutex);
854 if (ret > 0) { 856 if (ret > 0) {
855 unsigned long nr_pages; 857 unsigned long nr_pages;
856 858
@@ -1339,8 +1341,7 @@ static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
1339 if (!pipe) 1341 if (!pipe)
1340 return -EBADF; 1342 return -EBADF;
1341 1343
1342 if (pipe->inode) 1344 pipe_lock(pipe);
1343 mutex_lock(&pipe->inode->i_mutex);
1344 1345
1345 error = ret = 0; 1346 error = ret = 0;
1346 while (nr_segs) { 1347 while (nr_segs) {
@@ -1395,8 +1396,7 @@ static long vmsplice_to_user(struct file *file, const struct iovec __user *iov,
1395 iov++; 1396 iov++;
1396 } 1397 }
1397 1398
1398 if (pipe->inode) 1399 pipe_unlock(pipe);
1399 mutex_unlock(&pipe->inode->i_mutex);
1400 1400
1401 if (!ret) 1401 if (!ret)
1402 ret = error; 1402 ret = error;
@@ -1524,7 +1524,7 @@ static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1524 return 0; 1524 return 0;
1525 1525
1526 ret = 0; 1526 ret = 0;
1527 mutex_lock(&pipe->inode->i_mutex); 1527 pipe_lock(pipe);
1528 1528
1529 while (!pipe->nrbufs) { 1529 while (!pipe->nrbufs) {
1530 if (signal_pending(current)) { 1530 if (signal_pending(current)) {
@@ -1542,7 +1542,7 @@ static int link_ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1542 pipe_wait(pipe); 1542 pipe_wait(pipe);
1543 } 1543 }
1544 1544
1545 mutex_unlock(&pipe->inode->i_mutex); 1545 pipe_unlock(pipe);
1546 return ret; 1546 return ret;
1547} 1547}
1548 1548
@@ -1562,7 +1562,7 @@ static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1562 return 0; 1562 return 0;
1563 1563
1564 ret = 0; 1564 ret = 0;
1565 mutex_lock(&pipe->inode->i_mutex); 1565 pipe_lock(pipe);
1566 1566
1567 while (pipe->nrbufs >= PIPE_BUFFERS) { 1567 while (pipe->nrbufs >= PIPE_BUFFERS) {
1568 if (!pipe->readers) { 1568 if (!pipe->readers) {
@@ -1583,7 +1583,7 @@ static int link_opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1583 pipe->waiting_writers--; 1583 pipe->waiting_writers--;
1584 } 1584 }
1585 1585
1586 mutex_unlock(&pipe->inode->i_mutex); 1586 pipe_unlock(pipe);
1587 return ret; 1587 return ret;
1588} 1588}
1589 1589
@@ -1599,10 +1599,10 @@ static int link_pipe(struct pipe_inode_info *ipipe,
1599 1599
1600 /* 1600 /*
1601 * Potential ABBA deadlock, work around it by ordering lock 1601 * Potential ABBA deadlock, work around it by ordering lock
1602 * grabbing by inode address. Otherwise two different processes 1602 * grabbing by pipe info address. Otherwise two different processes
1603 * could deadlock (one doing tee from A -> B, the other from B -> A). 1603 * could deadlock (one doing tee from A -> B, the other from B -> A).
1604 */ 1604 */
1605 inode_double_lock(ipipe->inode, opipe->inode); 1605 pipe_double_lock(ipipe, opipe);
1606 1606
1607 do { 1607 do {
1608 if (!opipe->readers) { 1608 if (!opipe->readers) {
@@ -1653,7 +1653,8 @@ static int link_pipe(struct pipe_inode_info *ipipe,
1653 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) 1653 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1654 ret = -EAGAIN; 1654 ret = -EAGAIN;
1655 1655
1656 inode_double_unlock(ipipe->inode, opipe->inode); 1656 pipe_unlock(ipipe);
1657 pipe_unlock(opipe);
1657 1658
1658 /* 1659 /*
1659 * If we put data in the output pipe, wakeup any potential readers. 1660 * If we put data in the output pipe, wakeup any potential readers.