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 int fifo_open(struct inode
* inode
,struct file
* filp
)
18 unsigned long page
= 0;
19 struct pipe_inode_info
*info
, *tmp
= NULL
;
23 tmp
= kmalloc(sizeof(struct pipe_inode_info
),GFP_KERNEL
);
28 page
= __get_free_page(GFP_KERNEL
);
34 PIPE_LOCK(*inode
) = 0;
35 PIPE_START(*inode
) = PIPE_LEN(*inode
) = 0;
36 PIPE_BASE(*inode
) = (char *) page
;
37 PIPE_RD_OPENERS(*inode
) = PIPE_WR_OPENERS(*inode
) = 0;
38 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 0;
39 init_waitqueue_head(&PIPE_WAIT(*inode
));
40 tmp
= NULL
; /* no need to free it */
45 switch( filp
->f_mode
) {
50 * POSIX.1 says that O_NONBLOCK means return with the FIFO
51 * opened, even when there is no process writing the FIFO.
53 filp
->f_op
= &connecting_fifo_fops
;
54 if (!PIPE_READERS(*inode
)++)
55 wake_up_interruptible(&PIPE_WAIT(*inode
));
56 if (!(filp
->f_flags
& O_NONBLOCK
) && !PIPE_WRITERS(*inode
)) {
57 PIPE_RD_OPENERS(*inode
)++;
58 while (!PIPE_WRITERS(*inode
)) {
59 if (signal_pending(current
)) {
60 retval
= -ERESTARTSYS
;
63 interruptible_sleep_on(&PIPE_WAIT(*inode
));
65 if (!--PIPE_RD_OPENERS(*inode
))
66 wake_up_interruptible(&PIPE_WAIT(*inode
));
68 while (PIPE_WR_OPENERS(*inode
))
69 interruptible_sleep_on(&PIPE_WAIT(*inode
));
70 if (PIPE_WRITERS(*inode
))
71 filp
->f_op
= &read_fifo_fops
;
72 if (retval
&& !--PIPE_READERS(*inode
))
73 wake_up_interruptible(&PIPE_WAIT(*inode
));
79 * POSIX.1 says that O_NONBLOCK means return -1 with
80 * errno=ENXIO when there is no process reading the FIFO.
82 if ((filp
->f_flags
& O_NONBLOCK
) && !PIPE_READERS(*inode
)) {
86 filp
->f_op
= &write_fifo_fops
;
87 if (!PIPE_WRITERS(*inode
)++)
88 wake_up_interruptible(&PIPE_WAIT(*inode
));
89 if (!PIPE_READERS(*inode
)) {
90 PIPE_WR_OPENERS(*inode
)++;
91 while (!PIPE_READERS(*inode
)) {
92 if (signal_pending(current
)) {
93 retval
= -ERESTARTSYS
;
96 interruptible_sleep_on(&PIPE_WAIT(*inode
));
98 if (!--PIPE_WR_OPENERS(*inode
))
99 wake_up_interruptible(&PIPE_WAIT(*inode
));
101 while (PIPE_RD_OPENERS(*inode
))
102 interruptible_sleep_on(&PIPE_WAIT(*inode
));
103 if (retval
&& !--PIPE_WRITERS(*inode
))
104 wake_up_interruptible(&PIPE_WAIT(*inode
));
110 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
111 * This implementation will NEVER block on a O_RDWR open, since
112 * the process can at least talk to itself.
114 filp
->f_op
= &rdwr_fifo_fops
;
115 if (!PIPE_READERS(*inode
)++)
116 wake_up_interruptible(&PIPE_WAIT(*inode
));
117 while (PIPE_WR_OPENERS(*inode
))
118 interruptible_sleep_on(&PIPE_WAIT(*inode
));
119 if (!PIPE_WRITERS(*inode
)++)
120 wake_up_interruptible(&PIPE_WAIT(*inode
));
121 while (PIPE_RD_OPENERS(*inode
))
122 interruptible_sleep_on(&PIPE_WAIT(*inode
));
138 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
139 info
= inode
->i_pipe
;
140 inode
->i_pipe
= NULL
;
141 free_page((unsigned long)info
->base
);
151 * Dummy default file-operations: the only thing this does
152 * is contain the open that then fills in the correct operations
153 * depending on the access mode of the file...
155 static struct file_operations def_fifo_fops
= {
163 fifo_open
, /* will set read or write pipe_fops */
170 struct inode_operations fifo_inode_operations
= {
171 &def_fifo_fops
, /* default file operations */
182 NULL
, /* follow_link */
183 NULL
, /* get_block */
185 NULL
, /* writepage */
186 NULL
, /* flushpage */
188 NULL
, /* permission */
190 NULL
/* revalidate */
194 /* Goner. Filesystems do not use it anymore. */
196 void init_fifo(struct inode
* inode
)
198 inode
->i_op
= &fifo_inode_operations
;