aboutsummaryrefslogtreecommitdiffstats
path: root/fs/pipe.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/pipe.c')
-rw-r--r--fs/pipe.c460
1 files changed, 236 insertions, 224 deletions
diff --git a/fs/pipe.c b/fs/pipe.c
index bd3479db4b62..d2c45e14e6d8 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -21,10 +21,13 @@
21#include <linux/audit.h> 21#include <linux/audit.h>
22#include <linux/syscalls.h> 22#include <linux/syscalls.h>
23#include <linux/fcntl.h> 23#include <linux/fcntl.h>
24#include <linux/aio.h>
24 25
25#include <asm/uaccess.h> 26#include <asm/uaccess.h>
26#include <asm/ioctls.h> 27#include <asm/ioctls.h>
27 28
29#include "internal.h"
30
28/* 31/*
29 * The max size that a non-root user is allowed to grow the pipe. Can 32 * The max size that a non-root user is allowed to grow the pipe. Can
30 * be set by root in /proc/sys/fs/pipe-max-size 33 * be set by root in /proc/sys/fs/pipe-max-size
@@ -53,8 +56,8 @@ unsigned int pipe_min_size = PAGE_SIZE;
53 56
54static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass) 57static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
55{ 58{
56 if (pipe->inode) 59 if (pipe->files)
57 mutex_lock_nested(&pipe->inode->i_mutex, subclass); 60 mutex_lock_nested(&pipe->mutex, subclass);
58} 61}
59 62
60void pipe_lock(struct pipe_inode_info *pipe) 63void pipe_lock(struct pipe_inode_info *pipe)
@@ -68,11 +71,21 @@ EXPORT_SYMBOL(pipe_lock);
68 71
69void pipe_unlock(struct pipe_inode_info *pipe) 72void pipe_unlock(struct pipe_inode_info *pipe)
70{ 73{
71 if (pipe->inode) 74 if (pipe->files)
72 mutex_unlock(&pipe->inode->i_mutex); 75 mutex_unlock(&pipe->mutex);
73} 76}
74EXPORT_SYMBOL(pipe_unlock); 77EXPORT_SYMBOL(pipe_unlock);
75 78
79static inline void __pipe_lock(struct pipe_inode_info *pipe)
80{
81 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
82}
83
84static inline void __pipe_unlock(struct pipe_inode_info *pipe)
85{
86 mutex_unlock(&pipe->mutex);
87}
88
76void pipe_double_lock(struct pipe_inode_info *pipe1, 89void pipe_double_lock(struct pipe_inode_info *pipe1,
77 struct pipe_inode_info *pipe2) 90 struct pipe_inode_info *pipe2)
78{ 91{
@@ -361,8 +374,7 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov,
361 unsigned long nr_segs, loff_t pos) 374 unsigned long nr_segs, loff_t pos)
362{ 375{
363 struct file *filp = iocb->ki_filp; 376 struct file *filp = iocb->ki_filp;
364 struct inode *inode = filp->f_path.dentry->d_inode; 377 struct pipe_inode_info *pipe = filp->private_data;
365 struct pipe_inode_info *pipe;
366 int do_wakeup; 378 int do_wakeup;
367 ssize_t ret; 379 ssize_t ret;
368 struct iovec *iov = (struct iovec *)_iov; 380 struct iovec *iov = (struct iovec *)_iov;
@@ -375,8 +387,7 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov,
375 387
376 do_wakeup = 0; 388 do_wakeup = 0;
377 ret = 0; 389 ret = 0;
378 mutex_lock(&inode->i_mutex); 390 __pipe_lock(pipe);
379 pipe = inode->i_pipe;
380 for (;;) { 391 for (;;) {
381 int bufs = pipe->nrbufs; 392 int bufs = pipe->nrbufs;
382 if (bufs) { 393 if (bufs) {
@@ -464,7 +475,7 @@ redo:
464 } 475 }
465 pipe_wait(pipe); 476 pipe_wait(pipe);
466 } 477 }
467 mutex_unlock(&inode->i_mutex); 478 __pipe_unlock(pipe);
468 479
469 /* Signal writers asynchronously that there is more room. */ 480 /* Signal writers asynchronously that there is more room. */
470 if (do_wakeup) { 481 if (do_wakeup) {
@@ -486,8 +497,7 @@ pipe_write(struct kiocb *iocb, const struct iovec *_iov,
486 unsigned long nr_segs, loff_t ppos) 497 unsigned long nr_segs, loff_t ppos)
487{ 498{
488 struct file *filp = iocb->ki_filp; 499 struct file *filp = iocb->ki_filp;
489 struct inode *inode = filp->f_path.dentry->d_inode; 500 struct pipe_inode_info *pipe = filp->private_data;
490 struct pipe_inode_info *pipe;
491 ssize_t ret; 501 ssize_t ret;
492 int do_wakeup; 502 int do_wakeup;
493 struct iovec *iov = (struct iovec *)_iov; 503 struct iovec *iov = (struct iovec *)_iov;
@@ -501,8 +511,7 @@ pipe_write(struct kiocb *iocb, const struct iovec *_iov,
501 511
502 do_wakeup = 0; 512 do_wakeup = 0;
503 ret = 0; 513 ret = 0;
504 mutex_lock(&inode->i_mutex); 514 __pipe_lock(pipe);
505 pipe = inode->i_pipe;
506 515
507 if (!pipe->readers) { 516 if (!pipe->readers) {
508 send_sig(SIGPIPE, current, 0); 517 send_sig(SIGPIPE, current, 0);
@@ -649,7 +658,7 @@ redo2:
649 pipe->waiting_writers--; 658 pipe->waiting_writers--;
650 } 659 }
651out: 660out:
652 mutex_unlock(&inode->i_mutex); 661 __pipe_unlock(pipe);
653 if (do_wakeup) { 662 if (do_wakeup) {
654 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM); 663 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
655 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 664 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
@@ -662,29 +671,14 @@ out:
662 return ret; 671 return ret;
663} 672}
664 673
665static ssize_t
666bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
667{
668 return -EBADF;
669}
670
671static ssize_t
672bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
673 loff_t *ppos)
674{
675 return -EBADF;
676}
677
678static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 674static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
679{ 675{
680 struct inode *inode = filp->f_path.dentry->d_inode; 676 struct pipe_inode_info *pipe = filp->private_data;
681 struct pipe_inode_info *pipe;
682 int count, buf, nrbufs; 677 int count, buf, nrbufs;
683 678
684 switch (cmd) { 679 switch (cmd) {
685 case FIONREAD: 680 case FIONREAD:
686 mutex_lock(&inode->i_mutex); 681 __pipe_lock(pipe);
687 pipe = inode->i_pipe;
688 count = 0; 682 count = 0;
689 buf = pipe->curbuf; 683 buf = pipe->curbuf;
690 nrbufs = pipe->nrbufs; 684 nrbufs = pipe->nrbufs;
@@ -692,7 +686,7 @@ static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
692 count += pipe->bufs[buf].len; 686 count += pipe->bufs[buf].len;
693 buf = (buf+1) & (pipe->buffers - 1); 687 buf = (buf+1) & (pipe->buffers - 1);
694 } 688 }
695 mutex_unlock(&inode->i_mutex); 689 __pipe_unlock(pipe);
696 690
697 return put_user(count, (int __user *)arg); 691 return put_user(count, (int __user *)arg);
698 default: 692 default:
@@ -705,8 +699,7 @@ static unsigned int
705pipe_poll(struct file *filp, poll_table *wait) 699pipe_poll(struct file *filp, poll_table *wait)
706{ 700{
707 unsigned int mask; 701 unsigned int mask;
708 struct inode *inode = filp->f_path.dentry->d_inode; 702 struct pipe_inode_info *pipe = filp->private_data;
709 struct pipe_inode_info *pipe = inode->i_pipe;
710 int nrbufs; 703 int nrbufs;
711 704
712 poll_wait(filp, &pipe->wait, wait); 705 poll_wait(filp, &pipe->wait, wait);
@@ -734,194 +727,56 @@ pipe_poll(struct file *filp, poll_table *wait)
734} 727}
735 728
736static int 729static int
737pipe_release(struct inode *inode, int decr, int decw) 730pipe_release(struct inode *inode, struct file *file)
738{ 731{
739 struct pipe_inode_info *pipe; 732 struct pipe_inode_info *pipe = inode->i_pipe;
733 int kill = 0;
740 734
741 mutex_lock(&inode->i_mutex); 735 __pipe_lock(pipe);
742 pipe = inode->i_pipe; 736 if (file->f_mode & FMODE_READ)
743 pipe->readers -= decr; 737 pipe->readers--;
744 pipe->writers -= decw; 738 if (file->f_mode & FMODE_WRITE)
739 pipe->writers--;
745 740
746 if (!pipe->readers && !pipe->writers) { 741 if (pipe->readers || pipe->writers) {
747 free_pipe_info(inode);
748 } else {
749 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP); 742 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
750 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 743 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
751 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); 744 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
752 } 745 }
753 mutex_unlock(&inode->i_mutex); 746 spin_lock(&inode->i_lock);
754 747 if (!--pipe->files) {
755 return 0; 748 inode->i_pipe = NULL;
756} 749 kill = 1;
757 750 }
758static int 751 spin_unlock(&inode->i_lock);
759pipe_read_fasync(int fd, struct file *filp, int on) 752 __pipe_unlock(pipe);
760{
761 struct inode *inode = filp->f_path.dentry->d_inode;
762 int retval;
763
764 mutex_lock(&inode->i_mutex);
765 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
766 mutex_unlock(&inode->i_mutex);
767
768 return retval;
769}
770
771
772static int
773pipe_write_fasync(int fd, struct file *filp, int on)
774{
775 struct inode *inode = filp->f_path.dentry->d_inode;
776 int retval;
777 753
778 mutex_lock(&inode->i_mutex); 754 if (kill)
779 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers); 755 free_pipe_info(pipe);
780 mutex_unlock(&inode->i_mutex);
781 756
782 return retval; 757 return 0;
783} 758}
784 759
785
786static int 760static int
787pipe_rdwr_fasync(int fd, struct file *filp, int on) 761pipe_fasync(int fd, struct file *filp, int on)
788{ 762{
789 struct inode *inode = filp->f_path.dentry->d_inode; 763 struct pipe_inode_info *pipe = filp->private_data;
790 struct pipe_inode_info *pipe = inode->i_pipe; 764 int retval = 0;
791 int retval;
792 765
793 mutex_lock(&inode->i_mutex); 766 __pipe_lock(pipe);
794 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); 767 if (filp->f_mode & FMODE_READ)
795 if (retval >= 0) { 768 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
769 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
796 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); 770 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
797 if (retval < 0) /* this can happen only if on == T */ 771 if (retval < 0 && (filp->f_mode & FMODE_READ))
772 /* this can happen only if on == T */
798 fasync_helper(-1, filp, 0, &pipe->fasync_readers); 773 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
799 } 774 }
800 mutex_unlock(&inode->i_mutex); 775 __pipe_unlock(pipe);
801 return retval; 776 return retval;
802} 777}
803 778
804 779struct pipe_inode_info *alloc_pipe_info(void)
805static int
806pipe_read_release(struct inode *inode, struct file *filp)
807{
808 return pipe_release(inode, 1, 0);
809}
810
811static int
812pipe_write_release(struct inode *inode, struct file *filp)
813{
814 return pipe_release(inode, 0, 1);
815}
816
817static int
818pipe_rdwr_release(struct inode *inode, struct file *filp)
819{
820 int decr, decw;
821
822 decr = (filp->f_mode & FMODE_READ) != 0;
823 decw = (filp->f_mode & FMODE_WRITE) != 0;
824 return pipe_release(inode, decr, decw);
825}
826
827static int
828pipe_read_open(struct inode *inode, struct file *filp)
829{
830 int ret = -ENOENT;
831
832 mutex_lock(&inode->i_mutex);
833
834 if (inode->i_pipe) {
835 ret = 0;
836 inode->i_pipe->readers++;
837 }
838
839 mutex_unlock(&inode->i_mutex);
840
841 return ret;
842}
843
844static int
845pipe_write_open(struct inode *inode, struct file *filp)
846{
847 int ret = -ENOENT;
848
849 mutex_lock(&inode->i_mutex);
850
851 if (inode->i_pipe) {
852 ret = 0;
853 inode->i_pipe->writers++;
854 }
855
856 mutex_unlock(&inode->i_mutex);
857
858 return ret;
859}
860
861static int
862pipe_rdwr_open(struct inode *inode, struct file *filp)
863{
864 int ret = -ENOENT;
865
866 mutex_lock(&inode->i_mutex);
867
868 if (inode->i_pipe) {
869 ret = 0;
870 if (filp->f_mode & FMODE_READ)
871 inode->i_pipe->readers++;
872 if (filp->f_mode & FMODE_WRITE)
873 inode->i_pipe->writers++;
874 }
875
876 mutex_unlock(&inode->i_mutex);
877
878 return ret;
879}
880
881/*
882 * The file_operations structs are not static because they
883 * are also used in linux/fs/fifo.c to do operations on FIFOs.
884 *
885 * Pipes reuse fifos' file_operations structs.
886 */
887const struct file_operations read_pipefifo_fops = {
888 .llseek = no_llseek,
889 .read = do_sync_read,
890 .aio_read = pipe_read,
891 .write = bad_pipe_w,
892 .poll = pipe_poll,
893 .unlocked_ioctl = pipe_ioctl,
894 .open = pipe_read_open,
895 .release = pipe_read_release,
896 .fasync = pipe_read_fasync,
897};
898
899const struct file_operations write_pipefifo_fops = {
900 .llseek = no_llseek,
901 .read = bad_pipe_r,
902 .write = do_sync_write,
903 .aio_write = pipe_write,
904 .poll = pipe_poll,
905 .unlocked_ioctl = pipe_ioctl,
906 .open = pipe_write_open,
907 .release = pipe_write_release,
908 .fasync = pipe_write_fasync,
909};
910
911const struct file_operations rdwr_pipefifo_fops = {
912 .llseek = no_llseek,
913 .read = do_sync_read,
914 .aio_read = pipe_read,
915 .write = do_sync_write,
916 .aio_write = pipe_write,
917 .poll = pipe_poll,
918 .unlocked_ioctl = pipe_ioctl,
919 .open = pipe_rdwr_open,
920 .release = pipe_rdwr_release,
921 .fasync = pipe_rdwr_fasync,
922};
923
924struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
925{ 780{
926 struct pipe_inode_info *pipe; 781 struct pipe_inode_info *pipe;
927 782
@@ -931,8 +786,8 @@ struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
931 if (pipe->bufs) { 786 if (pipe->bufs) {
932 init_waitqueue_head(&pipe->wait); 787 init_waitqueue_head(&pipe->wait);
933 pipe->r_counter = pipe->w_counter = 1; 788 pipe->r_counter = pipe->w_counter = 1;
934 pipe->inode = inode;
935 pipe->buffers = PIPE_DEF_BUFFERS; 789 pipe->buffers = PIPE_DEF_BUFFERS;
790 mutex_init(&pipe->mutex);
936 return pipe; 791 return pipe;
937 } 792 }
938 kfree(pipe); 793 kfree(pipe);
@@ -941,7 +796,7 @@ struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
941 return NULL; 796 return NULL;
942} 797}
943 798
944void __free_pipe_info(struct pipe_inode_info *pipe) 799void free_pipe_info(struct pipe_inode_info *pipe)
945{ 800{
946 int i; 801 int i;
947 802
@@ -956,12 +811,6 @@ void __free_pipe_info(struct pipe_inode_info *pipe)
956 kfree(pipe); 811 kfree(pipe);
957} 812}
958 813
959void free_pipe_info(struct inode *inode)
960{
961 __free_pipe_info(inode->i_pipe);
962 inode->i_pipe = NULL;
963}
964
965static struct vfsmount *pipe_mnt __read_mostly; 814static struct vfsmount *pipe_mnt __read_mostly;
966 815
967/* 816/*
@@ -987,13 +836,14 @@ static struct inode * get_pipe_inode(void)
987 836
988 inode->i_ino = get_next_ino(); 837 inode->i_ino = get_next_ino();
989 838
990 pipe = alloc_pipe_info(inode); 839 pipe = alloc_pipe_info();
991 if (!pipe) 840 if (!pipe)
992 goto fail_iput; 841 goto fail_iput;
993 inode->i_pipe = pipe;
994 842
843 inode->i_pipe = pipe;
844 pipe->files = 2;
995 pipe->readers = pipe->writers = 1; 845 pipe->readers = pipe->writers = 1;
996 inode->i_fop = &rdwr_pipefifo_fops; 846 inode->i_fop = &pipefifo_fops;
997 847
998 /* 848 /*
999 * Mark the inode dirty from the very beginning, 849 * Mark the inode dirty from the very beginning,
@@ -1036,17 +886,19 @@ int create_pipe_files(struct file **res, int flags)
1036 d_instantiate(path.dentry, inode); 886 d_instantiate(path.dentry, inode);
1037 887
1038 err = -ENFILE; 888 err = -ENFILE;
1039 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops); 889 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
1040 if (!f) 890 if (IS_ERR(f))
1041 goto err_dentry; 891 goto err_dentry;
1042 892
1043 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)); 893 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
894 f->private_data = inode->i_pipe;
1044 895
1045 res[0] = alloc_file(&path, FMODE_READ, &read_pipefifo_fops); 896 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
1046 if (!res[0]) 897 if (IS_ERR(res[0]))
1047 goto err_file; 898 goto err_file;
1048 899
1049 path_get(&path); 900 path_get(&path);
901 res[0]->private_data = inode->i_pipe;
1050 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK); 902 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
1051 res[1] = f; 903 res[1] = f;
1052 return 0; 904 return 0;
@@ -1054,12 +906,12 @@ int create_pipe_files(struct file **res, int flags)
1054err_file: 906err_file:
1055 put_filp(f); 907 put_filp(f);
1056err_dentry: 908err_dentry:
1057 free_pipe_info(inode); 909 free_pipe_info(inode->i_pipe);
1058 path_put(&path); 910 path_put(&path);
1059 return err; 911 return err;
1060 912
1061err_inode: 913err_inode:
1062 free_pipe_info(inode); 914 free_pipe_info(inode->i_pipe);
1063 iput(inode); 915 iput(inode);
1064 return err; 916 return err;
1065} 917}
@@ -1141,6 +993,168 @@ SYSCALL_DEFINE1(pipe, int __user *, fildes)
1141 return sys_pipe2(fildes, 0); 993 return sys_pipe2(fildes, 0);
1142} 994}
1143 995
996static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
997{
998 int cur = *cnt;
999
1000 while (cur == *cnt) {
1001 pipe_wait(pipe);
1002 if (signal_pending(current))
1003 break;
1004 }
1005 return cur == *cnt ? -ERESTARTSYS : 0;
1006}
1007
1008static void wake_up_partner(struct pipe_inode_info *pipe)
1009{
1010 wake_up_interruptible(&pipe->wait);
1011}
1012
1013static int fifo_open(struct inode *inode, struct file *filp)
1014{
1015 struct pipe_inode_info *pipe;
1016 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
1017 int kill = 0;
1018 int ret;
1019
1020 filp->f_version = 0;
1021
1022 spin_lock(&inode->i_lock);
1023 if (inode->i_pipe) {
1024 pipe = inode->i_pipe;
1025 pipe->files++;
1026 spin_unlock(&inode->i_lock);
1027 } else {
1028 spin_unlock(&inode->i_lock);
1029 pipe = alloc_pipe_info();
1030 if (!pipe)
1031 return -ENOMEM;
1032 pipe->files = 1;
1033 spin_lock(&inode->i_lock);
1034 if (unlikely(inode->i_pipe)) {
1035 inode->i_pipe->files++;
1036 spin_unlock(&inode->i_lock);
1037 free_pipe_info(pipe);
1038 pipe = inode->i_pipe;
1039 } else {
1040 inode->i_pipe = pipe;
1041 spin_unlock(&inode->i_lock);
1042 }
1043 }
1044 filp->private_data = pipe;
1045 /* OK, we have a pipe and it's pinned down */
1046
1047 __pipe_lock(pipe);
1048
1049 /* We can only do regular read/write on fifos */
1050 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
1051
1052 switch (filp->f_mode) {
1053 case FMODE_READ:
1054 /*
1055 * O_RDONLY
1056 * POSIX.1 says that O_NONBLOCK means return with the FIFO
1057 * opened, even when there is no process writing the FIFO.
1058 */
1059 pipe->r_counter++;
1060 if (pipe->readers++ == 0)
1061 wake_up_partner(pipe);
1062
1063 if (!is_pipe && !pipe->writers) {
1064 if ((filp->f_flags & O_NONBLOCK)) {
1065 /* suppress POLLHUP until we have
1066 * seen a writer */
1067 filp->f_version = pipe->w_counter;
1068 } else {
1069 if (wait_for_partner(pipe, &pipe->w_counter))
1070 goto err_rd;
1071 }
1072 }
1073 break;
1074
1075 case FMODE_WRITE:
1076 /*
1077 * O_WRONLY
1078 * POSIX.1 says that O_NONBLOCK means return -1 with
1079 * errno=ENXIO when there is no process reading the FIFO.
1080 */
1081 ret = -ENXIO;
1082 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
1083 goto err;
1084
1085 pipe->w_counter++;
1086 if (!pipe->writers++)
1087 wake_up_partner(pipe);
1088
1089 if (!is_pipe && !pipe->readers) {
1090 if (wait_for_partner(pipe, &pipe->r_counter))
1091 goto err_wr;
1092 }
1093 break;
1094
1095 case FMODE_READ | FMODE_WRITE:
1096 /*
1097 * O_RDWR
1098 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1099 * This implementation will NEVER block on a O_RDWR open, since
1100 * the process can at least talk to itself.
1101 */
1102
1103 pipe->readers++;
1104 pipe->writers++;
1105 pipe->r_counter++;
1106 pipe->w_counter++;
1107 if (pipe->readers == 1 || pipe->writers == 1)
1108 wake_up_partner(pipe);
1109 break;
1110
1111 default:
1112 ret = -EINVAL;
1113 goto err;
1114 }
1115
1116 /* Ok! */
1117 __pipe_unlock(pipe);
1118 return 0;
1119
1120err_rd:
1121 if (!--pipe->readers)
1122 wake_up_interruptible(&pipe->wait);
1123 ret = -ERESTARTSYS;
1124 goto err;
1125
1126err_wr:
1127 if (!--pipe->writers)
1128 wake_up_interruptible(&pipe->wait);
1129 ret = -ERESTARTSYS;
1130 goto err;
1131
1132err:
1133 spin_lock(&inode->i_lock);
1134 if (!--pipe->files) {
1135 inode->i_pipe = NULL;
1136 kill = 1;
1137 }
1138 spin_unlock(&inode->i_lock);
1139 __pipe_unlock(pipe);
1140 if (kill)
1141 free_pipe_info(pipe);
1142 return ret;
1143}
1144
1145const struct file_operations pipefifo_fops = {
1146 .open = fifo_open,
1147 .llseek = no_llseek,
1148 .read = do_sync_read,
1149 .aio_read = pipe_read,
1150 .write = do_sync_write,
1151 .aio_write = pipe_write,
1152 .poll = pipe_poll,
1153 .unlocked_ioctl = pipe_ioctl,
1154 .release = pipe_release,
1155 .fasync = pipe_fasync,
1156};
1157
1144/* 1158/*
1145 * Allocate a new array of pipe buffers and copy the info over. Returns the 1159 * Allocate a new array of pipe buffers and copy the info over. Returns the
1146 * pipe size if successful, or return -ERROR on error. 1160 * pipe size if successful, or return -ERROR on error.
@@ -1226,9 +1240,7 @@ int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1226 */ 1240 */
1227struct pipe_inode_info *get_pipe_info(struct file *file) 1241struct pipe_inode_info *get_pipe_info(struct file *file)
1228{ 1242{
1229 struct inode *i = file->f_path.dentry->d_inode; 1243 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
1230
1231 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1232} 1244}
1233 1245
1234long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg) 1246long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -1240,7 +1252,7 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1240 if (!pipe) 1252 if (!pipe)
1241 return -EBADF; 1253 return -EBADF;
1242 1254
1243 mutex_lock(&pipe->inode->i_mutex); 1255 __pipe_lock(pipe);
1244 1256
1245 switch (cmd) { 1257 switch (cmd) {
1246 case F_SETPIPE_SZ: { 1258 case F_SETPIPE_SZ: {
@@ -1269,7 +1281,7 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1269 } 1281 }
1270 1282
1271out: 1283out:
1272 mutex_unlock(&pipe->inode->i_mutex); 1284 __pipe_unlock(pipe);
1273 return ret; 1285 return ret;
1274} 1286}
1275 1287