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/malloc.h>
15 static void wait_for_partner(struct inode
* inode
, unsigned int* cnt
)
20 if(signal_pending(current
))
25 static void wake_up_partner(struct inode
* inode
)
27 wake_up_interruptible(PIPE_WAIT(*inode
));
30 static int fifo_open(struct inode
*inode
, struct file
*filp
)
35 if (down_interruptible(PIPE_SEM(*inode
)))
36 goto err_nolock_nocleanup
;
45 switch (filp
->f_mode
) {
49 * POSIX.1 says that O_NONBLOCK means return with the FIFO
50 * opened, even when there is no process writing the FIFO.
52 filp
->f_op
= &read_fifo_fops
;
53 PIPE_RCOUNTER(*inode
)++;
54 if (PIPE_READERS(*inode
)++ == 0)
55 wake_up_partner(inode
);
57 if (!PIPE_WRITERS(*inode
)) {
58 if ((filp
->f_flags
& O_NONBLOCK
)) {
59 /* suppress POLLHUP until we have
61 filp
->f_version
= PIPE_WCOUNTER(*inode
);
64 wait_for_partner(inode
, &PIPE_WCOUNTER(*inode
));
65 if(signal_pending(current
))
74 * POSIX.1 says that O_NONBLOCK means return -1 with
75 * errno=ENXIO when there is no process reading the FIFO.
78 if ((filp
->f_flags
& O_NONBLOCK
) && !PIPE_READERS(*inode
))
81 filp
->f_op
= &write_fifo_fops
;
82 PIPE_WCOUNTER(*inode
)++;
83 if (!PIPE_WRITERS(*inode
)++)
84 wake_up_partner(inode
);
86 if (!PIPE_READERS(*inode
)) {
87 wait_for_partner(inode
, &PIPE_RCOUNTER(*inode
));
88 if (signal_pending(current
))
96 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
97 * This implementation will NEVER block on a O_RDWR open, since
98 * the process can at least talk to itself.
100 filp
->f_op
= &rdwr_fifo_fops
;
102 PIPE_READERS(*inode
)++;
103 PIPE_WRITERS(*inode
)++;
104 PIPE_RCOUNTER(*inode
)++;
105 PIPE_WCOUNTER(*inode
)++;
106 if (PIPE_READERS(*inode
) == 1 || PIPE_WRITERS(*inode
) == 1)
107 wake_up_partner(inode
);
116 up(PIPE_SEM(*inode
));
120 if (!--PIPE_READERS(*inode
))
121 wake_up_interruptible(PIPE_WAIT(*inode
));
126 if (!--PIPE_WRITERS(*inode
))
127 wake_up_interruptible(PIPE_WAIT(*inode
));
132 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
133 struct pipe_inode_info
*info
= inode
->i_pipe
;
134 inode
->i_pipe
= NULL
;
135 free_page((unsigned long)info
->base
);
140 up(PIPE_SEM(*inode
));
142 err_nolock_nocleanup
:
147 * Dummy default file-operations: the only thing this does
148 * is contain the open that then fills in the correct operations
149 * depending on the access mode of the file...
151 struct file_operations def_fifo_fops
= {
152 open
: fifo_open
, /* will set read or write pipe_fops */