Linux-2.4.0-test2
[davej-history.git] / fs / fifo.c
blob25a08e7574cda1dc6af82cb314b24231521fe0d6
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/malloc.h>
15 static void wait_for_partner(struct inode* inode, unsigned int* cnt)
17 int cur = *cnt;
18 while(cur == *cnt) {
19 pipe_wait(inode);
20 if(signal_pending(current))
21 break;
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)
32 int ret;
34 ret = -ERESTARTSYS;
35 if (down_interruptible(PIPE_SEM(*inode)))
36 goto err_nolock_nocleanup;
38 if (!inode->i_pipe) {
39 ret = -ENOMEM;
40 if(!pipe_new(inode))
41 goto err_nocleanup;
43 filp->f_version = 0;
45 switch (filp->f_mode) {
46 case 1:
48 * O_RDONLY
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
60 * seen a writer */
61 filp->f_version = PIPE_WCOUNTER(*inode);
62 } else
64 wait_for_partner(inode, &PIPE_WCOUNTER(*inode));
65 if(signal_pending(current))
66 goto err_rd;
69 break;
71 case 2:
73 * O_WRONLY
74 * POSIX.1 says that O_NONBLOCK means return -1 with
75 * errno=ENXIO when there is no process reading the FIFO.
77 ret = -ENXIO;
78 if ((filp->f_flags & O_NONBLOCK) && !PIPE_READERS(*inode))
79 goto err;
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))
89 goto err_wr;
91 break;
93 case 3:
95 * O_RDWR
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);
108 break;
110 default:
111 ret = -EINVAL;
112 goto err;
115 /* Ok! */
116 up(PIPE_SEM(*inode));
117 return 0;
119 err_rd:
120 if (!--PIPE_READERS(*inode))
121 wake_up_interruptible(PIPE_WAIT(*inode));
122 ret = -ERESTARTSYS;
123 goto err;
125 err_wr:
126 if (!--PIPE_WRITERS(*inode))
127 wake_up_interruptible(PIPE_WAIT(*inode));
128 ret = -ERESTARTSYS;
129 goto err;
131 err:
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);
136 kfree(info);
139 err_nocleanup:
140 up(PIPE_SEM(*inode));
142 err_nolock_nocleanup:
143 return ret;
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 */