blob: 4e303c22d5ee53613682530bd1705a0a3cec8382 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/fifo.c
3 *
4 * written by Paul H. Hargrove
5 *
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.
10 */
11
12#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/fs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pipe_fs_i.h>
16
Ingo Molnar3a326a22006-04-10 15:18:35 +020017static void wait_for_partner(struct inode* inode, unsigned int *cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
19 int cur = *cnt;
Ingo Molnar3a326a22006-04-10 15:18:35 +020020
21 while (cur == *cnt) {
22 pipe_wait(inode->i_pipe);
23 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 break;
25 }
26}
27
28static void wake_up_partner(struct inode* inode)
29{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +020030 wake_up_interruptible(&inode->i_pipe->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
33static int fifo_open(struct inode *inode, struct file *filp)
34{
Ingo Molnar923f4f22006-04-11 13:53:33 +020035 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 int ret;
37
Ingo Molnar9aeedfc2006-04-11 13:53:10 +020038 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +020039 pipe = inode->i_pipe;
40 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 ret = -ENOMEM;
Ingo Molnar923f4f22006-04-11 13:53:33 +020042 pipe = alloc_pipe_info(inode);
43 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 goto err_nocleanup;
Ingo Molnar923f4f22006-04-11 13:53:33 +020045 inode->i_pipe = pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47 filp->f_version = 0;
48
49 /* We can only do regular read/write on fifos */
50 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
51
52 switch (filp->f_mode) {
Al Viroaeb5d722008-09-02 15:28:45 -040053 case FMODE_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /*
55 * O_RDONLY
56 * POSIX.1 says that O_NONBLOCK means return with the FIFO
57 * opened, even when there is no process writing the FIFO.
58 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +020059 filp->f_op = &read_pipefifo_fops;
Ingo Molnar923f4f22006-04-11 13:53:33 +020060 pipe->r_counter++;
61 if (pipe->readers++ == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 wake_up_partner(inode);
63
Ingo Molnar923f4f22006-04-11 13:53:33 +020064 if (!pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if ((filp->f_flags & O_NONBLOCK)) {
66 /* suppress POLLHUP until we have
67 * seen a writer */
Ingo Molnar923f4f22006-04-11 13:53:33 +020068 filp->f_version = pipe->w_counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 } else
70 {
Ingo Molnar923f4f22006-04-11 13:53:33 +020071 wait_for_partner(inode, &pipe->w_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if(signal_pending(current))
73 goto err_rd;
74 }
75 }
76 break;
77
Al Viroaeb5d722008-09-02 15:28:45 -040078 case FMODE_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /*
80 * O_WRONLY
81 * POSIX.1 says that O_NONBLOCK means return -1 with
82 * errno=ENXIO when there is no process reading the FIFO.
83 */
84 ret = -ENXIO;
Ingo Molnar923f4f22006-04-11 13:53:33 +020085 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 goto err;
87
Denys Vlasenkod2d96482008-07-01 14:16:09 +020088 filp->f_op = &write_pipefifo_fops;
Ingo Molnar923f4f22006-04-11 13:53:33 +020089 pipe->w_counter++;
90 if (!pipe->writers++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 wake_up_partner(inode);
92
Ingo Molnar923f4f22006-04-11 13:53:33 +020093 if (!pipe->readers) {
94 wait_for_partner(inode, &pipe->r_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (signal_pending(current))
96 goto err_wr;
97 }
98 break;
99
Al Viroaeb5d722008-09-02 15:28:45 -0400100 case FMODE_READ | FMODE_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /*
102 * O_RDWR
103 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
104 * This implementation will NEVER block on a O_RDWR open, since
105 * the process can at least talk to itself.
106 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200107 filp->f_op = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Ingo Molnar923f4f22006-04-11 13:53:33 +0200109 pipe->readers++;
110 pipe->writers++;
111 pipe->r_counter++;
112 pipe->w_counter++;
113 if (pipe->readers == 1 || pipe->writers == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 wake_up_partner(inode);
115 break;
116
117 default:
118 ret = -EINVAL;
119 goto err;
120 }
121
122 /* Ok! */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200123 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125
126err_rd:
Ingo Molnar923f4f22006-04-11 13:53:33 +0200127 if (!--pipe->readers)
128 wake_up_interruptible(&pipe->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 ret = -ERESTARTSYS;
130 goto err;
131
132err_wr:
Ingo Molnar923f4f22006-04-11 13:53:33 +0200133 if (!--pipe->writers)
134 wake_up_interruptible(&pipe->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 ret = -ERESTARTSYS;
136 goto err;
137
138err:
Ingo Molnar923f4f22006-04-11 13:53:33 +0200139 if (!pipe->readers && !pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 free_pipe_info(inode);
141
142err_nocleanup:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200143 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return ret;
145}
146
147/*
148 * Dummy default file-operations: the only thing this does
149 * is contain the open that then fills in the correct operations
150 * depending on the access mode of the file...
151 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800152const struct file_operations def_fifo_fops = {
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200153 .open = fifo_open, /* will set read_ or write_pipefifo_fops */
Arnd Bergmann6038f372010-08-15 18:52:59 +0200154 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};