iscsi-target: Fix immediate queue starvation regression with DATAIN
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / fifo.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/fifo.c
3 *
4 * written by Paul H. Hargrove
5 *
6 * Fixes:
7 * 10-06-1999, AV: fixed OOM handling in fifo_open(), moved
8 * initialization there, switched to external
9 * allocation of pipe_inode_info.
10 */
11
12#include <linux/mm.h>
1da177e4 13#include <linux/fs.h>
e8edc6e0 14#include <linux/sched.h>
1da177e4
LT
15#include <linux/pipe_fs_i.h>
16
05d290d6 17static int wait_for_partner(struct inode* inode, unsigned int *cnt)
1da177e4
LT
18{
19 int cur = *cnt;
3a326a2c
IM
20
21 while (cur == *cnt) {
22 pipe_wait(inode->i_pipe);
23 if (signal_pending(current))
1da177e4
LT
24 break;
25 }
05d290d6 26 return cur == *cnt ? -ERESTARTSYS : 0;
1da177e4
LT
27}
28
29static void wake_up_partner(struct inode* inode)
30{
9aeedfc4 31 wake_up_interruptible(&inode->i_pipe->wait);
1da177e4
LT
32}
33
34static int fifo_open(struct inode *inode, struct file *filp)
35{
923f4f23 36 struct pipe_inode_info *pipe;
1da177e4
LT
37 int ret;
38
9aeedfc4 39 mutex_lock(&inode->i_mutex);
923f4f23
IM
40 pipe = inode->i_pipe;
41 if (!pipe) {
1da177e4 42 ret = -ENOMEM;
923f4f23
IM
43 pipe = alloc_pipe_info(inode);
44 if (!pipe)
1da177e4 45 goto err_nocleanup;
923f4f23 46 inode->i_pipe = pipe;
1da177e4
LT
47 }
48 filp->f_version = 0;
49
50 /* We can only do regular read/write on fifos */
51 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
52
53 switch (filp->f_mode) {
aeb5d727 54 case FMODE_READ:
1da177e4
LT
55 /*
56 * O_RDONLY
57 * POSIX.1 says that O_NONBLOCK means return with the FIFO
58 * opened, even when there is no process writing the FIFO.
59 */
d2d9648e 60 filp->f_op = &read_pipefifo_fops;
923f4f23
IM
61 pipe->r_counter++;
62 if (pipe->readers++ == 0)
1da177e4
LT
63 wake_up_partner(inode);
64
923f4f23 65 if (!pipe->writers) {
1da177e4
LT
66 if ((filp->f_flags & O_NONBLOCK)) {
67 /* suppress POLLHUP until we have
68 * seen a writer */
923f4f23 69 filp->f_version = pipe->w_counter;
ff38c083 70 } else {
05d290d6 71 if (wait_for_partner(inode, &pipe->w_counter))
1da177e4
LT
72 goto err_rd;
73 }
74 }
75 break;
76
aeb5d727 77 case FMODE_WRITE:
1da177e4
LT
78 /*
79 * O_WRONLY
80 * POSIX.1 says that O_NONBLOCK means return -1 with
81 * errno=ENXIO when there is no process reading the FIFO.
82 */
83 ret = -ENXIO;
923f4f23 84 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
1da177e4
LT
85 goto err;
86
d2d9648e 87 filp->f_op = &write_pipefifo_fops;
923f4f23
IM
88 pipe->w_counter++;
89 if (!pipe->writers++)
1da177e4
LT
90 wake_up_partner(inode);
91
923f4f23 92 if (!pipe->readers) {
05d290d6 93 if (wait_for_partner(inode, &pipe->r_counter))
1da177e4
LT
94 goto err_wr;
95 }
96 break;
97
aeb5d727 98 case FMODE_READ | FMODE_WRITE:
1da177e4
LT
99 /*
100 * O_RDWR
101 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
102 * This implementation will NEVER block on a O_RDWR open, since
103 * the process can at least talk to itself.
104 */
d2d9648e 105 filp->f_op = &rdwr_pipefifo_fops;
1da177e4 106
923f4f23
IM
107 pipe->readers++;
108 pipe->writers++;
109 pipe->r_counter++;
110 pipe->w_counter++;
111 if (pipe->readers == 1 || pipe->writers == 1)
1da177e4
LT
112 wake_up_partner(inode);
113 break;
114
115 default:
116 ret = -EINVAL;
117 goto err;
118 }
119
120 /* Ok! */
9aeedfc4 121 mutex_unlock(&inode->i_mutex);
1da177e4
LT
122 return 0;
123
124err_rd:
923f4f23
IM
125 if (!--pipe->readers)
126 wake_up_interruptible(&pipe->wait);
1da177e4
LT
127 ret = -ERESTARTSYS;
128 goto err;
129
130err_wr:
923f4f23
IM
131 if (!--pipe->writers)
132 wake_up_interruptible(&pipe->wait);
1da177e4
LT
133 ret = -ERESTARTSYS;
134 goto err;
135
136err:
923f4f23 137 if (!pipe->readers && !pipe->writers)
1da177e4
LT
138 free_pipe_info(inode);
139
140err_nocleanup:
9aeedfc4 141 mutex_unlock(&inode->i_mutex);
1da177e4
LT
142 return ret;
143}
144
145/*
146 * Dummy default file-operations: the only thing this does
147 * is contain the open that then fills in the correct operations
148 * depending on the access mode of the file...
149 */
4b6f5d20 150const struct file_operations def_fifo_fops = {
d2d9648e 151 .open = fifo_open, /* will set read_ or write_pipefifo_fops */
6038f373 152 .llseek = noop_llseek,
1da177e4 153};