1 /*
2 * linux/fs/fifo.c
3 *
4 * written by Paul H. Hargrove
5 */
6
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/fcntl.h>
11
12 extern struct file_operations read_pipe_fops;
13 extern struct file_operations write_pipe_fops;
14 extern struct file_operations rdwr_pipe_fops;
15
16 static int fifo_open(struct inode * inode,struct file * filp)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
17 {
18 int retval = 0;
19 unsigned long page;
20
21 switch( filp->f_mode ) {
22
23 case 1:
24 /*
25 * O_RDONLY
26 * POSIX.1 says that O_NONBLOCK means return with the FIFO
27 * opened, even when there is no process writing the FIFO.
28 */
29 filp->f_op = &read_pipe_fops;
30 PIPE_READERS(*inode)++;
31 if (!(filp->f_flags & O_NONBLOCK))
32 while (!PIPE_WRITERS(*inode)) {
33 if (PIPE_HEAD(*inode) != PIPE_TAIL(*inode))
34 break;
35 if (current->signal & ~current->blocked) {
36 retval = -ERESTARTSYS;
37 break;
38 }
39 interruptible_sleep_on(&PIPE_READ_WAIT(*inode));
40 }
41 if (retval)
42 PIPE_READERS(*inode)--;
43 break;
44
45 case 2:
46 /*
47 * O_WRONLY
48 * POSIX.1 says that O_NONBLOCK means return -1 with
49 * errno=ENXIO when there is no process reading the FIFO.
50 */
51 if ((filp->f_flags & O_NONBLOCK) && !PIPE_READERS(*inode)) {
52 retval = -ENXIO;
53 break;
54 }
55 filp->f_op = &write_pipe_fops;
56 PIPE_WRITERS(*inode)++;
57 while (!PIPE_READERS(*inode)) {
58 if (current->signal & ~current->blocked) {
59 retval = -ERESTARTSYS;
60 break;
61 }
62 interruptible_sleep_on(&PIPE_WRITE_WAIT(*inode));
63 }
64 if (retval)
65 PIPE_WRITERS(*inode)--;
66 break;
67
68 case 3:
69 /*
70 * O_RDWR
71 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
72 * This implementation will NEVER block on a O_RDWR open, since
73 * the process can at least talk to itself.
74 */
75 filp->f_op = &rdwr_pipe_fops;
76 PIPE_WRITERS(*inode) += 1;
77 PIPE_READERS(*inode) += 1;
78 break;
79
80 default:
81 retval = -EINVAL;
82 }
83 if (PIPE_WRITERS(*inode))
84 wake_up(&PIPE_READ_WAIT(*inode));
85 if (PIPE_READERS(*inode))
86 wake_up(&PIPE_WRITE_WAIT(*inode));
87 if (retval || inode->i_size)
88 return retval;
89 page = get_free_page(GFP_KERNEL);
90 if (inode->i_size) {
91 free_page(page);
92 return 0;
93 }
94 if (!page)
95 return -ENOMEM;
96 inode->i_size = page;
97 return 0;
98 }
99
100 /*
101 * Dummy default file-operations: the only thing this does
102 * is contain the open that then fills in the correct operations
103 * depending on the access mode of the file...
104 */
105 struct file_operations def_fifo_fops = {
106 NULL,
107 NULL,
108 NULL,
109 NULL,
110 NULL,
111 NULL,
112 fifo_open, /* will set read or write pipe_fops */
113 NULL
114 };