]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/fifo.c
[PATCH] splice: speedup __generic_file_splice_read
[net-next-2.6.git] / fs / fifo.c
CommitLineData
1da177e4
LT
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/slab.h>
14#include <linux/smp_lock.h>
15#include <linux/fs.h>
16#include <linux/pipe_fs_i.h>
17
3a326a2c 18static void wait_for_partner(struct inode* inode, unsigned int *cnt)
1da177e4
LT
19{
20 int cur = *cnt;
3a326a2c
IM
21
22 while (cur == *cnt) {
23 pipe_wait(inode->i_pipe);
24 if (signal_pending(current))
1da177e4
LT
25 break;
26 }
27}
28
29static void wake_up_partner(struct inode* inode)
30{
31 wake_up_interruptible(PIPE_WAIT(*inode));
32}
33
34static int fifo_open(struct inode *inode, struct file *filp)
35{
36 int ret;
37
d19e9974 38 mutex_lock(PIPE_MUTEX(*inode));
1da177e4
LT
39 if (!inode->i_pipe) {
40 ret = -ENOMEM;
3a326a2c
IM
41 inode->i_pipe = alloc_pipe_info(inode);
42 if (!inode->i_pipe)
1da177e4
LT
43 goto err_nocleanup;
44 }
45 filp->f_version = 0;
46
47 /* We can only do regular read/write on fifos */
48 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
49
50 switch (filp->f_mode) {
51 case 1:
52 /*
53 * O_RDONLY
54 * POSIX.1 says that O_NONBLOCK means return with the FIFO
55 * opened, even when there is no process writing the FIFO.
56 */
57 filp->f_op = &read_fifo_fops;
58 PIPE_RCOUNTER(*inode)++;
59 if (PIPE_READERS(*inode)++ == 0)
60 wake_up_partner(inode);
61
62 if (!PIPE_WRITERS(*inode)) {
63 if ((filp->f_flags & O_NONBLOCK)) {
64 /* suppress POLLHUP until we have
65 * seen a writer */
66 filp->f_version = PIPE_WCOUNTER(*inode);
67 } else
68 {
69 wait_for_partner(inode, &PIPE_WCOUNTER(*inode));
70 if(signal_pending(current))
71 goto err_rd;
72 }
73 }
74 break;
75
76 case 2:
77 /*
78 * O_WRONLY
79 * POSIX.1 says that O_NONBLOCK means return -1 with
80 * errno=ENXIO when there is no process reading the FIFO.
81 */
82 ret = -ENXIO;
83 if ((filp->f_flags & O_NONBLOCK) && !PIPE_READERS(*inode))
84 goto err;
85
86 filp->f_op = &write_fifo_fops;
87 PIPE_WCOUNTER(*inode)++;
88 if (!PIPE_WRITERS(*inode)++)
89 wake_up_partner(inode);
90
91 if (!PIPE_READERS(*inode)) {
92 wait_for_partner(inode, &PIPE_RCOUNTER(*inode));
93 if (signal_pending(current))
94 goto err_wr;
95 }
96 break;
97
98 case 3:
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_fifo_fops;
106
107 PIPE_READERS(*inode)++;
108 PIPE_WRITERS(*inode)++;
109 PIPE_RCOUNTER(*inode)++;
110 PIPE_WCOUNTER(*inode)++;
111 if (PIPE_READERS(*inode) == 1 || PIPE_WRITERS(*inode) == 1)
112 wake_up_partner(inode);
113 break;
114
115 default:
116 ret = -EINVAL;
117 goto err;
118 }
119
120 /* Ok! */
1b1dcc1b 121 mutex_unlock(PIPE_MUTEX(*inode));
1da177e4
LT
122 return 0;
123
124err_rd:
125 if (!--PIPE_READERS(*inode))
126 wake_up_interruptible(PIPE_WAIT(*inode));
127 ret = -ERESTARTSYS;
128 goto err;
129
130err_wr:
131 if (!--PIPE_WRITERS(*inode))
132 wake_up_interruptible(PIPE_WAIT(*inode));
133 ret = -ERESTARTSYS;
134 goto err;
135
136err:
137 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode))
138 free_pipe_info(inode);
139
140err_nocleanup:
1b1dcc1b 141 mutex_unlock(PIPE_MUTEX(*inode));
1da177e4
LT
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 */
4b6f5d20 150const struct file_operations def_fifo_fops = {
1da177e4
LT
151 .open = fifo_open, /* will set read or write pipe_fops */
152};