MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / fifo.c
blobe64bb9ffaaa2e54793976cec133ad35eb0b4eaa1
1 /*
2 * linux/fs/fifo.c
4 * written by Paul H. Hargrove
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.
12 #include <linux/mm.h>
13 #include <linux/slab.h>
14 #include <linux/smp_lock.h>
15 #include <linux/fs.h>
16 #include <linux/pipe_fs_i.h>
18 static void wait_for_partner(struct inode* inode, unsigned int *cnt)
20 int cur = *cnt;
22 while (cur == *cnt) {
23 #if 0 // mask by Victor Yu. 02-12-2007
24 pipe_wait(inode->i_pipe);
25 #else
26 pipe_wait(inode->u.i_pipe);
27 #endif
28 if (signal_pending(current))
29 break;
33 static void wake_up_partner(struct inode* inode)
35 #if 0 // mask by Victor Yu. 02-12-2007
36 wake_up_interruptible(&inode->i_pipe->wait);
37 #else
38 wake_up_interruptible(&inode->u.i_pipe->wait);
39 #endif
42 static int fifo_open(struct inode *inode, struct file *filp)
44 struct pipe_inode_info *pipe;
45 int ret;
47 mutex_lock(&inode->i_mutex);
48 #if 0 // mask by Victor Yu. 02-12-2007
49 pipe = inode->i_pipe;
50 #else
51 pipe = inode->u.i_pipe;
52 #endif
53 if (!pipe) {
54 ret = -ENOMEM;
55 pipe = alloc_pipe_info(inode);
56 if (!pipe)
57 goto err_nocleanup;
58 #if 0 // mask by Victor Yu. 02-12-2007
59 inode->i_pipe = pipe;
60 #else
61 inode->u.i_pipe = pipe;
62 #endif
64 filp->f_version = 0;
66 /* We can only do regular read/write on fifos */
67 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
69 switch (filp->f_mode) {
70 case 1:
72 * O_RDONLY
73 * POSIX.1 says that O_NONBLOCK means return with the FIFO
74 * opened, even when there is no process writing the FIFO.
76 filp->f_op = &read_fifo_fops;
77 pipe->r_counter++;
78 if (pipe->readers++ == 0)
79 wake_up_partner(inode);
81 if (!pipe->writers) {
82 if ((filp->f_flags & O_NONBLOCK)) {
83 /* suppress POLLHUP until we have
84 * seen a writer */
85 filp->f_version = pipe->w_counter;
86 } else
88 wait_for_partner(inode, &pipe->w_counter);
89 if(signal_pending(current))
90 goto err_rd;
93 break;
95 case 2:
97 * O_WRONLY
98 * POSIX.1 says that O_NONBLOCK means return -1 with
99 * errno=ENXIO when there is no process reading the FIFO.
101 ret = -ENXIO;
102 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
103 goto err;
105 filp->f_op = &write_fifo_fops;
106 pipe->w_counter++;
107 if (!pipe->writers++)
108 wake_up_partner(inode);
110 if (!pipe->readers) {
111 wait_for_partner(inode, &pipe->r_counter);
112 if (signal_pending(current))
113 goto err_wr;
115 break;
117 case 3:
119 * O_RDWR
120 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
121 * This implementation will NEVER block on a O_RDWR open, since
122 * the process can at least talk to itself.
124 filp->f_op = &rdwr_fifo_fops;
126 pipe->readers++;
127 pipe->writers++;
128 pipe->r_counter++;
129 pipe->w_counter++;
130 if (pipe->readers == 1 || pipe->writers == 1)
131 wake_up_partner(inode);
132 break;
134 default:
135 ret = -EINVAL;
136 goto err;
139 /* Ok! */
140 mutex_unlock(&inode->i_mutex);
141 return 0;
143 err_rd:
144 if (!--pipe->readers)
145 wake_up_interruptible(&pipe->wait);
146 ret = -ERESTARTSYS;
147 goto err;
149 err_wr:
150 if (!--pipe->writers)
151 wake_up_interruptible(&pipe->wait);
152 ret = -ERESTARTSYS;
153 goto err;
155 err:
156 if (!pipe->readers && !pipe->writers)
157 free_pipe_info(inode);
159 err_nocleanup:
160 mutex_unlock(&inode->i_mutex);
161 return ret;
165 * Dummy default file-operations: the only thing this does
166 * is contain the open that then fills in the correct operations
167 * depending on the access mode of the file...
169 const struct file_operations def_fifo_fops = {
170 .open = fifo_open, /* will set read or write pipe_fops */