4 * written by Paul H. Hargrove
7 * 10-06-1999, AV: fixed OOM handling in fifo_open(), moved
8 * initialization there, switched to external
9 * allocation of pipe_inode_info.
13 #include <linux/slab.h>
14 #include <linux/smp_lock.h>
16 #include <linux/pipe_fs_i.h>
18 static void wait_for_partner(struct inode
* inode
, unsigned int *cnt
)
23 pipe_wait(inode
->i_pipe
);
24 if (signal_pending(current
))
29 static void wake_up_partner(struct inode
* inode
)
31 wake_up_interruptible(&inode
->i_pipe
->wait
);
34 static int fifo_open(struct inode
*inode
, struct file
*filp
)
38 mutex_lock(&inode
->i_mutex
);
41 inode
->i_pipe
= alloc_pipe_info(inode
);
47 /* We can only do regular read/write on fifos */
48 filp
->f_mode
&= (FMODE_READ
| FMODE_WRITE
);
50 switch (filp
->f_mode
) {
54 * POSIX.1 says that O_NONBLOCK means return with the FIFO
55 * opened, even when there is no process writing the FIFO.
57 filp
->f_op
= &read_fifo_fops
;
58 inode
->i_pipe
->r_counter
++;
59 if (inode
->i_pipe
->readers
++ == 0)
60 wake_up_partner(inode
);
62 if (!inode
->i_pipe
->writers
) {
63 if ((filp
->f_flags
& O_NONBLOCK
)) {
64 /* suppress POLLHUP until we have
66 filp
->f_version
= inode
->i_pipe
->w_counter
;
69 wait_for_partner(inode
, &inode
->i_pipe
->w_counter
);
70 if(signal_pending(current
))
79 * POSIX.1 says that O_NONBLOCK means return -1 with
80 * errno=ENXIO when there is no process reading the FIFO.
83 if ((filp
->f_flags
& O_NONBLOCK
) && !inode
->i_pipe
->readers
)
86 filp
->f_op
= &write_fifo_fops
;
87 inode
->i_pipe
->w_counter
++;
88 if (!inode
->i_pipe
->writers
++)
89 wake_up_partner(inode
);
91 if (!inode
->i_pipe
->readers
) {
92 wait_for_partner(inode
, &inode
->i_pipe
->r_counter
);
93 if (signal_pending(current
))
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.
105 filp
->f_op
= &rdwr_fifo_fops
;
107 inode
->i_pipe
->readers
++;
108 inode
->i_pipe
->writers
++;
109 inode
->i_pipe
->r_counter
++;
110 inode
->i_pipe
->w_counter
++;
111 if (inode
->i_pipe
->readers
== 1 || inode
->i_pipe
->writers
== 1)
112 wake_up_partner(inode
);
121 mutex_unlock(&inode
->i_mutex
);
125 if (!--inode
->i_pipe
->readers
)
126 wake_up_interruptible(&inode
->i_pipe
->wait
);
131 if (!--inode
->i_pipe
->writers
)
132 wake_up_interruptible(&inode
->i_pipe
->wait
);
137 if (!inode
->i_pipe
->readers
&& !inode
->i_pipe
->writers
)
138 free_pipe_info(inode
);
141 mutex_unlock(&inode
->i_mutex
);
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...
150 const struct file_operations def_fifo_fops
= {
151 .open
= fifo_open
, /* will set read or write pipe_fops */