]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/fifo.c
xps: Transmit Packet Steering
[net-next-2.6.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
3a326a2c 17static void 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 }
26}
27
28static void wake_up_partner(struct inode* inode)
29{
9aeedfc4 30 wake_up_interruptible(&inode->i_pipe->wait);
1da177e4
LT
31}
32
33static int fifo_open(struct inode *inode, struct file *filp)
34{
923f4f23 35 struct pipe_inode_info *pipe;
1da177e4
LT
36 int ret;
37
9aeedfc4 38 mutex_lock(&inode->i_mutex);
923f4f23
IM
39 pipe = inode->i_pipe;
40 if (!pipe) {
1da177e4 41 ret = -ENOMEM;
923f4f23
IM
42 pipe = alloc_pipe_info(inode);
43 if (!pipe)
1da177e4 44 goto err_nocleanup;
923f4f23 45 inode->i_pipe = pipe;
1da177e4
LT
46 }
47 filp->f_version = 0;
48
49 /* We can only do regular read/write on fifos */
50 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
51
52 switch (filp->f_mode) {
aeb5d727 53 case FMODE_READ:
1da177e4
LT
54 /*
55 * O_RDONLY
56 * POSIX.1 says that O_NONBLOCK means return with the FIFO
57 * opened, even when there is no process writing the FIFO.
58 */
d2d9648e 59 filp->f_op = &read_pipefifo_fops;
923f4f23
IM
60 pipe->r_counter++;
61 if (pipe->readers++ == 0)
1da177e4
LT
62 wake_up_partner(inode);
63
923f4f23 64 if (!pipe->writers) {
1da177e4
LT
65 if ((filp->f_flags & O_NONBLOCK)) {
66 /* suppress POLLHUP until we have
67 * seen a writer */
923f4f23 68 filp->f_version = pipe->w_counter;
1da177e4
LT
69 } else
70 {
923f4f23 71 wait_for_partner(inode, &pipe->w_counter);
1da177e4
LT
72 if(signal_pending(current))
73 goto err_rd;
74 }
75 }
76 break;
77
aeb5d727 78 case FMODE_WRITE:
1da177e4
LT
79 /*
80 * O_WRONLY
81 * POSIX.1 says that O_NONBLOCK means return -1 with
82 * errno=ENXIO when there is no process reading the FIFO.
83 */
84 ret = -ENXIO;
923f4f23 85 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
1da177e4
LT
86 goto err;
87
d2d9648e 88 filp->f_op = &write_pipefifo_fops;
923f4f23
IM
89 pipe->w_counter++;
90 if (!pipe->writers++)
1da177e4
LT
91 wake_up_partner(inode);
92
923f4f23
IM
93 if (!pipe->readers) {
94 wait_for_partner(inode, &pipe->r_counter);
1da177e4
LT
95 if (signal_pending(current))
96 goto err_wr;
97 }
98 break;
99
aeb5d727 100 case FMODE_READ | FMODE_WRITE:
1da177e4
LT
101 /*
102 * O_RDWR
103 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
104 * This implementation will NEVER block on a O_RDWR open, since
105 * the process can at least talk to itself.
106 */
d2d9648e 107 filp->f_op = &rdwr_pipefifo_fops;
1da177e4 108
923f4f23
IM
109 pipe->readers++;
110 pipe->writers++;
111 pipe->r_counter++;
112 pipe->w_counter++;
113 if (pipe->readers == 1 || pipe->writers == 1)
1da177e4
LT
114 wake_up_partner(inode);
115 break;
116
117 default:
118 ret = -EINVAL;
119 goto err;
120 }
121
122 /* Ok! */
9aeedfc4 123 mutex_unlock(&inode->i_mutex);
1da177e4
LT
124 return 0;
125
126err_rd:
923f4f23
IM
127 if (!--pipe->readers)
128 wake_up_interruptible(&pipe->wait);
1da177e4
LT
129 ret = -ERESTARTSYS;
130 goto err;
131
132err_wr:
923f4f23
IM
133 if (!--pipe->writers)
134 wake_up_interruptible(&pipe->wait);
1da177e4
LT
135 ret = -ERESTARTSYS;
136 goto err;
137
138err:
923f4f23 139 if (!pipe->readers && !pipe->writers)
1da177e4
LT
140 free_pipe_info(inode);
141
142err_nocleanup:
9aeedfc4 143 mutex_unlock(&inode->i_mutex);
1da177e4
LT
144 return ret;
145}
146
147/*
148 * Dummy default file-operations: the only thing this does
149 * is contain the open that then fills in the correct operations
150 * depending on the access mode of the file...
151 */
4b6f5d20 152const struct file_operations def_fifo_fops = {
d2d9648e 153 .open = fifo_open, /* will set read_ or write_pipefifo_fops */
6038f373 154 .llseek = noop_llseek,
1da177e4 155};