blob: cf6f4345ceb0125baf86b7a029e7fdf8712f76c3 [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
Anders Kaseorg05d290d2012-07-15 17:14:25 -040017static int 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 }
Anders Kaseorg05d290d2012-07-15 17:14:25 -040026 return cur == *cnt ? -ERESTARTSYS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
29static void wake_up_partner(struct inode* inode)
30{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +020031 wake_up_interruptible(&inode->i_pipe->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
34static int fifo_open(struct inode *inode, struct file *filp)
35{
Ingo Molnar923f4f22006-04-11 13:53:33 +020036 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int ret;
38
Ingo Molnar9aeedfc2006-04-11 13:53:10 +020039 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +020040 pipe = inode->i_pipe;
41 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 ret = -ENOMEM;
Ingo Molnar923f4f22006-04-11 13:53:33 +020043 pipe = alloc_pipe_info(inode);
44 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 goto err_nocleanup;
Ingo Molnar923f4f22006-04-11 13:53:33 +020046 inode->i_pipe = pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48 filp->f_version = 0;
49
50 /* We can only do regular read/write on fifos */
51 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
52
53 switch (filp->f_mode) {
Al Viroaeb5d722008-09-02 15:28:45 -040054 case FMODE_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /*
56 * O_RDONLY
57 * POSIX.1 says that O_NONBLOCK means return with the FIFO
58 * opened, even when there is no process writing the FIFO.
59 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +020060 filp->f_op = &read_pipefifo_fops;
Ingo Molnar923f4f22006-04-11 13:53:33 +020061 pipe->r_counter++;
62 if (pipe->readers++ == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 wake_up_partner(inode);
64
Ingo Molnar923f4f22006-04-11 13:53:33 +020065 if (!pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if ((filp->f_flags & O_NONBLOCK)) {
67 /* suppress POLLHUP until we have
68 * seen a writer */
Ingo Molnar923f4f22006-04-11 13:53:33 +020069 filp->f_version = pipe->w_counter;
David Jenniff38c082011-02-23 16:51:05 +010070 } else {
Anders Kaseorg05d290d2012-07-15 17:14:25 -040071 if (wait_for_partner(inode, &pipe->w_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 goto err_rd;
73 }
74 }
75 break;
76
Al Viroaeb5d722008-09-02 15:28:45 -040077 case FMODE_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /*
79 * O_WRONLY
80 * POSIX.1 says that O_NONBLOCK means return -1 with
81 * errno=ENXIO when there is no process reading the FIFO.
82 */
83 ret = -ENXIO;
Ingo Molnar923f4f22006-04-11 13:53:33 +020084 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 goto err;
86
Denys Vlasenkod2d96482008-07-01 14:16:09 +020087 filp->f_op = &write_pipefifo_fops;
Ingo Molnar923f4f22006-04-11 13:53:33 +020088 pipe->w_counter++;
89 if (!pipe->writers++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 wake_up_partner(inode);
91
Ingo Molnar923f4f22006-04-11 13:53:33 +020092 if (!pipe->readers) {
Anders Kaseorg05d290d2012-07-15 17:14:25 -040093 if (wait_for_partner(inode, &pipe->r_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 goto err_wr;
95 }
96 break;
97
Al Viroaeb5d722008-09-02 15:28:45 -040098 case FMODE_READ | FMODE_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /*
100 * O_RDWR
101 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
102 * This implementation will NEVER block on a O_RDWR open, since
103 * the process can at least talk to itself.
104 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200105 filp->f_op = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar923f4f22006-04-11 13:53:33 +0200107 pipe->readers++;
108 pipe->writers++;
109 pipe->r_counter++;
110 pipe->w_counter++;
111 if (pipe->readers == 1 || pipe->writers == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 wake_up_partner(inode);
113 break;
114
115 default:
116 ret = -EINVAL;
117 goto err;
118 }
119
120 /* Ok! */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200121 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
123
124err_rd:
Ingo Molnar923f4f22006-04-11 13:53:33 +0200125 if (!--pipe->readers)
126 wake_up_interruptible(&pipe->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 ret = -ERESTARTSYS;
128 goto err;
129
130err_wr:
Ingo Molnar923f4f22006-04-11 13:53:33 +0200131 if (!--pipe->writers)
132 wake_up_interruptible(&pipe->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 ret = -ERESTARTSYS;
134 goto err;
135
136err:
Ingo Molnar923f4f22006-04-11 13:53:33 +0200137 if (!pipe->readers && !pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 free_pipe_info(inode);
139
140err_nocleanup:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200141 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret;
143}
144
145/*
146 * Dummy default file-operations: the only thing this does
147 * is contain the open that then fills in the correct operations
148 * depending on the access mode of the file...
149 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800150const struct file_operations def_fifo_fops = {
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200151 .open = fifo_open, /* will set read_ or write_pipefifo_fops */
Arnd Bergmann6038f372010-08-15 18:52:59 +0200152 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};