aboutsummaryrefslogtreecommitdiffstats
path: root/fs/pipe.c
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/pipe.c
parent2dd8c9ad376ccc5d2980b38e96372a8e252ae8d0 (diff)
fold fifo.c into pipe.c
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs/pipe.c')
-rw-r--r--fs/pipe.c138
1 files changed, 138 insertions, 0 deletions
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.