aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorAl Viro <viro@zeniv.linux.org.uk>2013-03-12 09:46:27 -0400
committerAl Viro <viro@zeniv.linux.org.uk>2013-04-09 14:12:58 -0400
commitf776c738883bc949e654568a565aee5a7d3fe133 (patch)
treeb83f74a02ebcb8341d689f63c65899da17ef0b39 /fs
parent2dd8c9ad376ccc5d2980b38e96372a8e252ae8d0 (diff)
fold fifo.c into pipe.c
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs')
-rw-r--r--fs/Makefile2
-rw-r--r--fs/fifo.c153
-rw-r--r--fs/pipe.c138
3 files changed, 139 insertions, 154 deletions
diff --git a/fs/Makefile b/fs/Makefile
index 9d53192236fc..b691a965dc1a 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -7,7 +7,7 @@
7 7
8obj-y := open.o read_write.o file_table.o super.o \ 8obj-y := open.o read_write.o file_table.o super.o \
9 char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \ 9 char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \
10 ioctl.o readdir.o select.o fifo.o dcache.o inode.o \ 10 ioctl.o readdir.o select.o dcache.o inode.o \
11 attr.o bad_inode.o file.o filesystems.o namespace.o \ 11 attr.o bad_inode.o file.o filesystems.o namespace.o \
12 seq_file.o xattr.o libfs.o fs-writeback.o \ 12 seq_file.o xattr.o libfs.o fs-writeback.o \
13 pnode.o drop_caches.o splice.o sync.o utimes.o \ 13 pnode.o drop_caches.o splice.o sync.o utimes.o \
diff --git a/fs/fifo.c b/fs/fifo.c
deleted file mode 100644
index cf6f4345ceb0..000000000000
--- a/fs/fifo.c
+++ /dev/null
@@ -1,153 +0,0 @@
1/*
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>
13#include <linux/fs.h>
14#include <linux/sched.h>
15#include <linux/pipe_fs_i.h>
16
17static int wait_for_partner(struct inode* inode, unsigned int *cnt)
18{
19 int cur = *cnt;
20
21 while (cur == *cnt) {
22 pipe_wait(inode->i_pipe);
23 if (signal_pending(current))
24 break;
25 }
26 return cur == *cnt ? -ERESTARTSYS : 0;
27}
28
29static void wake_up_partner(struct inode* inode)
30{
31 wake_up_interruptible(&inode->i_pipe->wait);
32}
33
34static int fifo_open(struct inode *inode, struct file *filp)
35{
36 struct pipe_inode_info *pipe;
37 int ret;
38
39 mutex_lock(&inode->i_mutex);
40 pipe = inode->i_pipe;
41 if (!pipe) {
42 ret = -ENOMEM;
43 pipe = alloc_pipe_info(inode);
44 if (!pipe)
45 goto err_nocleanup;
46 inode->i_pipe = pipe;
47 }
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) {
54 case FMODE_READ:
55 /*
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 */
60 filp->f_op = &read_pipefifo_fops;
61 pipe->r_counter++;
62 if (pipe->readers++ == 0)
63 wake_up_partner(inode);
64
65 if (!pipe->writers) {
66 if ((filp->f_flags & O_NONBLOCK)) {
67 /* suppress POLLHUP until we have
68 * seen a writer */
69 filp->f_version = pipe->w_counter;
70 } else {
71 if (wait_for_partner(inode, &pipe->w_counter))
72 goto err_rd;
73 }
74 }
75 break;
76
77 case FMODE_WRITE:
78 /*
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;
84 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
85 goto err;
86
87 filp->f_op = &write_pipefifo_fops;
88 pipe->w_counter++;
89 if (!pipe->writers++)
90 wake_up_partner(inode);
91
92 if (!pipe->readers) {
93 if (wait_for_partner(inode, &pipe->r_counter))
94 goto err_wr;
95 }
96 break;
97
98 case FMODE_READ | FMODE_WRITE:
99 /*
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 */
105 filp->f_op = &rdwr_pipefifo_fops;
106
107 pipe->readers++;
108 pipe->writers++;
109 pipe->r_counter++;
110 pipe->w_counter++;
111 if (pipe->readers == 1 || pipe->writers == 1)
112 wake_up_partner(inode);
113 break;
114
115 default:
116 ret = -EINVAL;
117 goto err;
118 }
119
120 /* Ok! */
121 mutex_unlock(&inode->i_mutex);
122 return 0;
123
124err_rd:
125 if (!--pipe->readers)
126 wake_up_interruptible(&pipe->wait);
127 ret = -ERESTARTSYS;
128 goto err;
129
130err_wr:
131 if (!--pipe->writers)
132 wake_up_interruptible(&pipe->wait);
133 ret = -ERESTARTSYS;
134 goto err;
135
136err:
137 if (!pipe->readers && !pipe->writers)
138 free_pipe_info(inode);
139
140err_nocleanup:
141 mutex_unlock(&inode->i_mutex);
142 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 */
150const struct file_operations def_fifo_fops = {
151 .open = fifo_open, /* will set read_ or write_pipefifo_fops */
152 .llseek = noop_llseek,
153};
diff --git a/fs/pipe.c b/fs/pipe.c
index 2234f3f61f8d..aed80c25cd40 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -1144,6 +1144,144 @@ SYSCALL_DEFINE1(pipe, int __user *, fildes)
1144 return sys_pipe2(fildes, 0); 1144 return sys_pipe2(fildes, 0);
1145} 1145}
1146 1146
1147static int wait_for_partner(struct inode* inode, unsigned int *cnt)
1148{
1149 int cur = *cnt;
1150
1151 while (cur == *cnt) {
1152 pipe_wait(inode->i_pipe);
1153 if (signal_pending(current))
1154 break;
1155 }
1156 return cur == *cnt ? -ERESTARTSYS : 0;
1157}
1158
1159static void wake_up_partner(struct inode* inode)
1160{
1161 wake_up_interruptible(&inode->i_pipe->wait);
1162}
1163
1164static int fifo_open(struct inode *inode, struct file *filp)
1165{
1166 struct pipe_inode_info *pipe;
1167 int ret;
1168
1169 mutex_lock(&inode->i_mutex);
1170 pipe = inode->i_pipe;
1171 if (!pipe) {
1172 ret = -ENOMEM;
1173 pipe = alloc_pipe_info(inode);
1174 if (!pipe)
1175 goto err_nocleanup;
1176 inode->i_pipe = pipe;
1177 }
1178 filp->f_version = 0;
1179
1180 /* We can only do regular read/write on fifos */
1181 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
1182
1183 switch (filp->f_mode) {
1184 case FMODE_READ:
1185 /*
1186 * O_RDONLY
1187 * POSIX.1 says that O_NONBLOCK means return with the FIFO
1188 * opened, even when there is no process writing the FIFO.
1189 */
1190 filp->f_op = &read_pipefifo_fops;
1191 pipe->r_counter++;
1192 if (pipe->readers++ == 0)
1193 wake_up_partner(inode);
1194
1195 if (!pipe->writers) {
1196 if ((filp->f_flags & O_NONBLOCK)) {
1197 /* suppress POLLHUP until we have
1198 * seen a writer */
1199 filp->f_version = pipe->w_counter;
1200 } else {
1201 if (wait_for_partner(inode, &pipe->w_counter))
1202 goto err_rd;
1203 }
1204 }
1205 break;
1206
1207 case FMODE_WRITE:
1208 /*
1209 * O_WRONLY
1210 * POSIX.1 says that O_NONBLOCK means return -1 with
1211 * errno=ENXIO when there is no process reading the FIFO.
1212 */
1213 ret = -ENXIO;
1214 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
1215 goto err;
1216
1217 filp->f_op = &write_pipefifo_fops;
1218 pipe->w_counter++;
1219 if (!pipe->writers++)
1220 wake_up_partner(inode);
1221
1222 if (!pipe->readers) {
1223 if (wait_for_partner(inode, &pipe->r_counter))
1224 goto err_wr;
1225 }
1226 break;
1227
1228 case FMODE_READ | FMODE_WRITE:
1229 /*
1230 * O_RDWR
1231 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1232 * This implementation will NEVER block on a O_RDWR open, since
1233 * the process can at least talk to itself.
1234 */
1235 filp->f_op = &rdwr_pipefifo_fops;
1236
1237 pipe->readers++;
1238 pipe->writers++;
1239 pipe->r_counter++;
1240 pipe->w_counter++;
1241 if (pipe->readers == 1 || pipe->writers == 1)
1242 wake_up_partner(inode);
1243 break;
1244
1245 default:
1246 ret = -EINVAL;
1247 goto err;
1248 }
1249
1250 /* Ok! */
1251 mutex_unlock(&inode->i_mutex);
1252 return 0;
1253
1254err_rd:
1255 if (!--pipe->readers)
1256 wake_up_interruptible(&pipe->wait);
1257 ret = -ERESTARTSYS;
1258 goto err;
1259
1260err_wr:
1261 if (!--pipe->writers)
1262 wake_up_interruptible(&pipe->wait);
1263 ret = -ERESTARTSYS;
1264 goto err;
1265
1266err:
1267 if (!pipe->readers && !pipe->writers)
1268 free_pipe_info(inode);
1269
1270err_nocleanup:
1271 mutex_unlock(&inode->i_mutex);
1272 return ret;
1273}
1274
1275/*
1276 * Dummy default file-operations: the only thing this does
1277 * is contain the open that then fills in the correct operations
1278 * depending on the access mode of the file...
1279 */
1280const struct file_operations def_fifo_fops = {
1281 .open = fifo_open, /* will set read_ or write_pipefifo_fops */
1282 .llseek = noop_llseek,
1283};
1284
1147/* 1285/*
1148 * Allocate a new array of pipe buffers and copy the info over. Returns the 1286 * Allocate a new array of pipe buffers and copy the info over. Returns the
1149 * pipe size if successful, or return -ERROR on error. 1287 * pipe size if successful, or return -ERROR on error.