blob: 7fefb10db8d9d6ad2a187d3c352cd0557beb30f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/pipe.c
3 *
4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
5 */
6
7#include <linux/mm.h>
8#include <linux/file.h>
9#include <linux/poll.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/fs.h>
14#include <linux/mount.h>
15#include <linux/pipe_fs_i.h>
16#include <linux/uio.h>
17#include <linux/highmem.h>
Jens Axboe5274f052006-03-30 15:15:30 +020018#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/uaccess.h>
21#include <asm/ioctls.h>
22
23/*
24 * We use a start+len construction, which provides full use of the
25 * allocated memory.
26 * -- Florian Coosmann (FGC)
27 *
28 * Reads with count = 0 should always return 0.
29 * -- Julian Bradfield 1999-06-07.
30 *
31 * FIFOs and Pipes now generate SIGIO for both readers and writers.
32 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
33 *
34 * pipe_read & write cleanup
35 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
36 */
37
38/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020039void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 DEFINE_WAIT(wait);
42
Ingo Molnard79fc0f2005-09-10 00:26:12 -070043 /*
44 * Pipes are system-local resources, so sleeping on them
45 * is considered a noninteractive wait:
46 */
Ingo Molnar341b4462006-04-11 13:57:45 +020047 prepare_to_wait(&pipe->wait, &wait,
48 TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
Ingo Molnar3a326a22006-04-10 15:18:35 +020049 if (pipe->inode)
50 mutex_unlock(&pipe->inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +020052 finish_wait(&pipe->wait, &wait);
53 if (pipe->inode)
54 mutex_lock(&pipe->inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Arjan van de Ven858119e2006-01-14 13:20:43 -080057static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070058pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
59{
60 unsigned long copy;
61
62 while (len > 0) {
63 while (!iov->iov_len)
64 iov++;
65 copy = min_t(unsigned long, len, iov->iov_len);
66
67 if (copy_from_user(to, iov->iov_base, copy))
68 return -EFAULT;
69 to += copy;
70 len -= copy;
71 iov->iov_base += copy;
72 iov->iov_len -= copy;
73 }
74 return 0;
75}
76
Arjan van de Ven858119e2006-01-14 13:20:43 -080077static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070078pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
79{
80 unsigned long copy;
81
82 while (len > 0) {
83 while (!iov->iov_len)
84 iov++;
85 copy = min_t(unsigned long, len, iov->iov_len);
86
87 if (copy_to_user(iov->iov_base, from, copy))
88 return -EFAULT;
89 from += copy;
90 len -= copy;
91 iov->iov_base += copy;
92 iov->iov_len -= copy;
93 }
94 return 0;
95}
96
Ingo Molnar341b4462006-04-11 13:57:45 +020097static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
98 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct page *page = buf->page;
101
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200102 buf->flags &= ~PIPE_BUF_FLAG_STOLEN;
103
Jens Axboe5274f052006-03-30 15:15:30 +0200104 /*
105 * If nobody else uses this page, and we don't already have a
106 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200107 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200108 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200109 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200110 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200111 else
112 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Ingo Molnar341b4462006-04-11 13:57:45 +0200115static void * anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe,
116 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 return kmap(buf->page);
119}
120
Ingo Molnar341b4462006-04-11 13:57:45 +0200121static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe,
122 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 kunmap(buf->page);
125}
126
Ingo Molnar923f4f22006-04-11 13:53:33 +0200127static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200128 struct pipe_buffer *buf)
129{
Jens Axboe3e7ee3e2006-04-02 23:11:04 +0200130 buf->flags |= PIPE_BUF_FLAG_STOLEN;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200131 return 0;
132}
133
Jens Axboe70524492006-04-11 15:51:17 +0200134static void anon_pipe_buf_get(struct pipe_inode_info *info,
135 struct pipe_buffer *buf)
136{
137 page_cache_get(buf->page);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static struct pipe_buf_operations anon_pipe_buf_ops = {
141 .can_merge = 1,
142 .map = anon_pipe_buf_map,
143 .unmap = anon_pipe_buf_unmap,
144 .release = anon_pipe_buf_release,
Jens Axboe5abc97a2006-03-30 15:16:46 +0200145 .steal = anon_pipe_buf_steal,
Jens Axboe70524492006-04-11 15:51:17 +0200146 .get = anon_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149static ssize_t
150pipe_readv(struct file *filp, const struct iovec *_iov,
151 unsigned long nr_segs, loff_t *ppos)
152{
153 struct inode *inode = filp->f_dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200154 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int do_wakeup;
156 ssize_t ret;
157 struct iovec *iov = (struct iovec *)_iov;
158 size_t total_len;
159
160 total_len = iov_length(iov, nr_segs);
161 /* Null read succeeds. */
162 if (unlikely(total_len == 0))
163 return 0;
164
165 do_wakeup = 0;
166 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200167 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200168 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200170 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200172 int curbuf = pipe->curbuf;
173 struct pipe_buffer *buf = pipe->bufs + curbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct pipe_buf_operations *ops = buf->ops;
175 void *addr;
176 size_t chars = buf->len;
177 int error;
178
179 if (chars > total_len)
180 chars = total_len;
181
Ingo Molnar923f4f22006-04-11 13:53:33 +0200182 addr = ops->map(filp, pipe, buf);
Jens Axboe5274f052006-03-30 15:15:30 +0200183 if (IS_ERR(addr)) {
184 if (!ret)
185 ret = PTR_ERR(addr);
186 break;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200189 ops->unmap(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (unlikely(error)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200191 if (!ret)
192 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
194 }
195 ret += chars;
196 buf->offset += chars;
197 buf->len -= chars;
198 if (!buf->len) {
199 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200200 ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200202 pipe->curbuf = curbuf;
203 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 do_wakeup = 1;
205 }
206 total_len -= chars;
207 if (!total_len)
208 break; /* common path: read succeeded */
209 }
210 if (bufs) /* More to do? */
211 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200212 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200214 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* syscall merging: Usually we must not sleep
216 * if O_NONBLOCK is set, or if we got some data.
217 * But if a writer sleeps in kernel space, then
218 * we can wait for that data without violating POSIX.
219 */
220 if (ret)
221 break;
222 if (filp->f_flags & O_NONBLOCK) {
223 ret = -EAGAIN;
224 break;
225 }
226 }
227 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200228 if (!ret)
229 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
231 }
232 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200233 wake_up_interruptible_sync(&pipe->wait);
234 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200236 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200238 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200239
240 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200242 wake_up_interruptible(&pipe->wait);
243 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 if (ret > 0)
246 file_accessed(filp);
247 return ret;
248}
249
250static ssize_t
251pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
252{
253 struct iovec iov = { .iov_base = buf, .iov_len = count };
Ingo Molnar341b4462006-04-11 13:57:45 +0200254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return pipe_readv(filp, &iov, 1, ppos);
256}
257
258static ssize_t
259pipe_writev(struct file *filp, const struct iovec *_iov,
260 unsigned long nr_segs, loff_t *ppos)
261{
262 struct inode *inode = filp->f_dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200263 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ssize_t ret;
265 int do_wakeup;
266 struct iovec *iov = (struct iovec *)_iov;
267 size_t total_len;
268 ssize_t chars;
269
270 total_len = iov_length(iov, nr_segs);
271 /* Null write succeeds. */
272 if (unlikely(total_len == 0))
273 return 0;
274
275 do_wakeup = 0;
276 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200277 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200278 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Ingo Molnar923f4f22006-04-11 13:53:33 +0200280 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 send_sig(SIGPIPE, current, 0);
282 ret = -EPIPE;
283 goto out;
284 }
285
286 /* We try to merge small writes */
287 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200288 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200289 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
290 (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200291 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct pipe_buf_operations *ops = buf->ops;
293 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboe5274f052006-03-30 15:15:30 +0200296 void *addr;
297 int error;
298
Ingo Molnar923f4f22006-04-11 13:53:33 +0200299 addr = ops->map(filp, pipe, buf);
Jens Axboe5274f052006-03-30 15:15:30 +0200300 if (IS_ERR(addr)) {
301 error = PTR_ERR(addr);
302 goto out;
303 }
304 error = pipe_iov_copy_from_user(offset + addr, iov,
305 chars);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200306 ops->unmap(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ret = error;
308 do_wakeup = 1;
309 if (error)
310 goto out;
311 buf->len += chars;
312 total_len -= chars;
313 ret = chars;
314 if (!total_len)
315 goto out;
316 }
317 }
318
319 for (;;) {
320 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200321
Ingo Molnar923f4f22006-04-11 13:53:33 +0200322 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200324 if (!ret)
325 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200328 bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (bufs < PIPE_BUFFERS) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200330 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
331 struct pipe_buffer *buf = pipe->bufs + newbuf;
332 struct page *page = pipe->tmp_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int error;
334
335 if (!page) {
336 page = alloc_page(GFP_HIGHUSER);
337 if (unlikely(!page)) {
338 ret = ret ? : -ENOMEM;
339 break;
340 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200341 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200343 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * we lock up (O_NONBLOCK-)readers that sleep due to
345 * syscall merging.
346 * FIXME! Is this really true?
347 */
348 do_wakeup = 1;
349 chars = PAGE_SIZE;
350 if (chars > total_len)
351 chars = total_len;
352
353 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
354 kunmap(page);
355 if (unlikely(error)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200356 if (!ret)
357 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
359 }
360 ret += chars;
361
362 /* Insert it into the buffer array */
363 buf->page = page;
364 buf->ops = &anon_pipe_buf_ops;
365 buf->offset = 0;
366 buf->len = chars;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200367 pipe->nrbufs = ++bufs;
368 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 total_len -= chars;
371 if (!total_len)
372 break;
373 }
374 if (bufs < PIPE_BUFFERS)
375 continue;
376 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200377 if (!ret)
378 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380 }
381 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200382 if (!ret)
383 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385 }
386 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200387 wake_up_interruptible_sync(&pipe->wait);
388 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 do_wakeup = 0;
390 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200391 pipe->waiting_writers++;
392 pipe_wait(pipe);
393 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200396 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200398 wake_up_interruptible(&pipe->wait);
399 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800402 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return ret;
404}
405
406static ssize_t
407pipe_write(struct file *filp, const char __user *buf,
408 size_t count, loff_t *ppos)
409{
410 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Ingo Molnar341b4462006-04-11 13:57:45 +0200411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return pipe_writev(filp, &iov, 1, ppos);
413}
414
415static ssize_t
416bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
417{
418 return -EBADF;
419}
420
421static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200422bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
423 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 return -EBADF;
426}
427
428static int
429pipe_ioctl(struct inode *pino, struct file *filp,
430 unsigned int cmd, unsigned long arg)
431{
432 struct inode *inode = filp->f_dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200433 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int count, buf, nrbufs;
435
436 switch (cmd) {
437 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200438 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200439 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200441 buf = pipe->curbuf;
442 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200444 count += pipe->bufs[buf].len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 buf = (buf+1) & (PIPE_BUFFERS-1);
446 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200447 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return put_user(count, (int __user *)arg);
450 default:
451 return -EINVAL;
452 }
453}
454
455/* No kernel lock held - fine */
456static unsigned int
457pipe_poll(struct file *filp, poll_table *wait)
458{
459 unsigned int mask;
460 struct inode *inode = filp->f_dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200461 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int nrbufs;
463
Ingo Molnar923f4f22006-04-11 13:53:33 +0200464 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200467 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 mask = 0;
469 if (filp->f_mode & FMODE_READ) {
470 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200471 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 mask |= POLLHUP;
473 }
474
475 if (filp->f_mode & FMODE_WRITE) {
476 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700477 /*
478 * Most Unices do not set POLLERR for FIFOs but on Linux they
479 * behave exactly like pipes for poll().
480 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200481 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 mask |= POLLERR;
483 }
484
485 return mask;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static int
489pipe_release(struct inode *inode, int decr, int decw)
490{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200491 struct pipe_inode_info *pipe;
492
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200493 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200494 pipe = inode->i_pipe;
495 pipe->readers -= decr;
496 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200497
Ingo Molnar923f4f22006-04-11 13:53:33 +0200498 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 free_pipe_info(inode);
500 } else {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200501 wake_up_interruptible(&pipe->wait);
502 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
503 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200505 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 return 0;
508}
509
510static int
511pipe_read_fasync(int fd, struct file *filp, int on)
512{
513 struct inode *inode = filp->f_dentry->d_inode;
514 int retval;
515
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200516 mutex_lock(&inode->i_mutex);
517 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
518 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (retval < 0)
521 return retval;
522
523 return 0;
524}
525
526
527static int
528pipe_write_fasync(int fd, struct file *filp, int on)
529{
530 struct inode *inode = filp->f_dentry->d_inode;
531 int retval;
532
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200533 mutex_lock(&inode->i_mutex);
534 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
535 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (retval < 0)
538 return retval;
539
540 return 0;
541}
542
543
544static int
545pipe_rdwr_fasync(int fd, struct file *filp, int on)
546{
547 struct inode *inode = filp->f_dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200548 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int retval;
550
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200551 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Ingo Molnar341b4462006-04-11 13:57:45 +0200553 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (retval >= 0)
Ingo Molnar341b4462006-04-11 13:57:45 +0200556 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200558 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (retval < 0)
561 return retval;
562
563 return 0;
564}
565
566
567static int
568pipe_read_release(struct inode *inode, struct file *filp)
569{
570 pipe_read_fasync(-1, filp, 0);
571 return pipe_release(inode, 1, 0);
572}
573
574static int
575pipe_write_release(struct inode *inode, struct file *filp)
576{
577 pipe_write_fasync(-1, filp, 0);
578 return pipe_release(inode, 0, 1);
579}
580
581static int
582pipe_rdwr_release(struct inode *inode, struct file *filp)
583{
584 int decr, decw;
585
586 pipe_rdwr_fasync(-1, filp, 0);
587 decr = (filp->f_mode & FMODE_READ) != 0;
588 decw = (filp->f_mode & FMODE_WRITE) != 0;
589 return pipe_release(inode, decr, decw);
590}
591
592static int
593pipe_read_open(struct inode *inode, struct file *filp)
594{
595 /* We could have perhaps used atomic_t, but this and friends
596 below are the only places. So it doesn't seem worthwhile. */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200597 mutex_lock(&inode->i_mutex);
598 inode->i_pipe->readers++;
599 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 return 0;
602}
603
604static int
605pipe_write_open(struct inode *inode, struct file *filp)
606{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200607 mutex_lock(&inode->i_mutex);
608 inode->i_pipe->writers++;
609 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 return 0;
612}
613
614static int
615pipe_rdwr_open(struct inode *inode, struct file *filp)
616{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200617 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (filp->f_mode & FMODE_READ)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200619 inode->i_pipe->readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (filp->f_mode & FMODE_WRITE)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200621 inode->i_pipe->writers++;
622 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 return 0;
625}
626
627/*
628 * The file_operations structs are not static because they
629 * are also used in linux/fs/fifo.c to do operations on FIFOs.
630 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800631const struct file_operations read_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 .llseek = no_llseek,
633 .read = pipe_read,
634 .readv = pipe_readv,
635 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700636 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .ioctl = pipe_ioctl,
638 .open = pipe_read_open,
639 .release = pipe_read_release,
640 .fasync = pipe_read_fasync,
641};
642
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800643const struct file_operations write_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 .llseek = no_llseek,
645 .read = bad_pipe_r,
646 .write = pipe_write,
647 .writev = pipe_writev,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700648 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .ioctl = pipe_ioctl,
650 .open = pipe_write_open,
651 .release = pipe_write_release,
652 .fasync = pipe_write_fasync,
653};
654
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800655const struct file_operations rdwr_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 .llseek = no_llseek,
657 .read = pipe_read,
658 .readv = pipe_readv,
659 .write = pipe_write,
660 .writev = pipe_writev,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700661 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .ioctl = pipe_ioctl,
663 .open = pipe_rdwr_open,
664 .release = pipe_rdwr_release,
665 .fasync = pipe_rdwr_fasync,
666};
667
Linus Torvaldsa19cbd42006-03-08 14:03:09 -0800668static struct file_operations read_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 .llseek = no_llseek,
670 .read = pipe_read,
671 .readv = pipe_readv,
672 .write = bad_pipe_w,
673 .poll = pipe_poll,
674 .ioctl = pipe_ioctl,
675 .open = pipe_read_open,
676 .release = pipe_read_release,
677 .fasync = pipe_read_fasync,
678};
679
Linus Torvaldsa19cbd42006-03-08 14:03:09 -0800680static struct file_operations write_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 .llseek = no_llseek,
682 .read = bad_pipe_r,
683 .write = pipe_write,
684 .writev = pipe_writev,
685 .poll = pipe_poll,
686 .ioctl = pipe_ioctl,
687 .open = pipe_write_open,
688 .release = pipe_write_release,
689 .fasync = pipe_write_fasync,
690};
691
Linus Torvaldsa19cbd42006-03-08 14:03:09 -0800692static struct file_operations rdwr_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .llseek = no_llseek,
694 .read = pipe_read,
695 .readv = pipe_readv,
696 .write = pipe_write,
697 .writev = pipe_writev,
698 .poll = pipe_poll,
699 .ioctl = pipe_ioctl,
700 .open = pipe_rdwr_open,
701 .release = pipe_rdwr_release,
702 .fasync = pipe_rdwr_fasync,
703};
704
Ingo Molnar3a326a22006-04-10 15:18:35 +0200705struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
706{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200707 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200708
Ingo Molnar923f4f22006-04-11 13:53:33 +0200709 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
710 if (pipe) {
711 init_waitqueue_head(&pipe->wait);
712 pipe->r_counter = pipe->w_counter = 1;
713 pipe->inode = inode;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200714 }
715
Ingo Molnar923f4f22006-04-11 13:53:33 +0200716 return pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200717}
718
Ingo Molnar923f4f22006-04-11 13:53:33 +0200719void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 for (i = 0; i < PIPE_BUFFERS; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200724 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200726 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200728 if (pipe->tmp_page)
729 __free_page(pipe->tmp_page);
730 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Jens Axboeb92ce552006-04-11 13:52:07 +0200733void free_pipe_info(struct inode *inode)
734{
735 __free_pipe_info(inode->i_pipe);
736 inode->i_pipe = NULL;
737}
738
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800739static struct vfsmount *pipe_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int pipefs_delete_dentry(struct dentry *dentry)
741{
742 return 1;
743}
Ingo Molnar341b4462006-04-11 13:57:45 +0200744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static struct dentry_operations pipefs_dentry_operations = {
746 .d_delete = pipefs_delete_dentry,
747};
748
749static struct inode * get_pipe_inode(void)
750{
751 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200752 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (!inode)
755 goto fail_inode;
756
Ingo Molnar923f4f22006-04-11 13:53:33 +0200757 pipe = alloc_pipe_info(inode);
758 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200760 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200761
Ingo Molnar923f4f22006-04-11 13:53:33 +0200762 pipe->readers = pipe->writers = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 inode->i_fop = &rdwr_pipe_fops;
764
765 /*
766 * Mark the inode dirty from the very beginning,
767 * that way it will never be moved to the dirty
768 * list because "mark_inode_dirty()" will think
769 * that it already _is_ on the dirty list.
770 */
771 inode->i_state = I_DIRTY;
772 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
773 inode->i_uid = current->fsuid;
774 inode->i_gid = current->fsgid;
775 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
776 inode->i_blksize = PAGE_SIZE;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return inode;
779
780fail_iput:
781 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783fail_inode:
784 return NULL;
785}
786
787int do_pipe(int *fd)
788{
789 struct qstr this;
790 char name[32];
791 struct dentry *dentry;
792 struct inode * inode;
793 struct file *f1, *f2;
794 int error;
Ingo Molnar341b4462006-04-11 13:57:45 +0200795 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 error = -ENFILE;
798 f1 = get_empty_filp();
799 if (!f1)
800 goto no_files;
801
802 f2 = get_empty_filp();
803 if (!f2)
804 goto close_f1;
805
806 inode = get_pipe_inode();
807 if (!inode)
808 goto close_f12;
809
810 error = get_unused_fd();
811 if (error < 0)
812 goto close_f12_inode;
813 i = error;
814
815 error = get_unused_fd();
816 if (error < 0)
817 goto close_f12_inode_i;
818 j = error;
819
820 error = -ENOMEM;
821 sprintf(name, "[%lu]", inode->i_ino);
822 this.name = name;
823 this.len = strlen(name);
824 this.hash = inode->i_ino; /* will go */
825 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
826 if (!dentry)
827 goto close_f12_inode_i_j;
Ingo Molnar341b4462006-04-11 13:57:45 +0200828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 dentry->d_op = &pipefs_dentry_operations;
830 d_add(dentry, inode);
831 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
832 f1->f_dentry = f2->f_dentry = dget(dentry);
833 f1->f_mapping = f2->f_mapping = inode->i_mapping;
834
835 /* read file */
836 f1->f_pos = f2->f_pos = 0;
837 f1->f_flags = O_RDONLY;
838 f1->f_op = &read_pipe_fops;
839 f1->f_mode = FMODE_READ;
840 f1->f_version = 0;
841
842 /* write file */
843 f2->f_flags = O_WRONLY;
844 f2->f_op = &write_pipe_fops;
845 f2->f_mode = FMODE_WRITE;
846 f2->f_version = 0;
847
848 fd_install(i, f1);
849 fd_install(j, f2);
850 fd[0] = i;
851 fd[1] = j;
Ingo Molnar341b4462006-04-11 13:57:45 +0200852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854
855close_f12_inode_i_j:
856 put_unused_fd(j);
857close_f12_inode_i:
858 put_unused_fd(i);
859close_f12_inode:
860 free_pipe_info(inode);
861 iput(inode);
862close_f12:
863 put_filp(f2);
864close_f1:
865 put_filp(f1);
866no_files:
867 return error;
868}
869
870/*
871 * pipefs should _never_ be mounted by userland - too much of security hassle,
872 * no real gain from having the whole whorehouse mounted. So we don't need
873 * any operations on the root directory. However, we need a non-trivial
874 * d_name - pipe: will go nicely and kill the special-casing in procfs.
875 */
876
Ingo Molnar341b4462006-04-11 13:57:45 +0200877static struct super_block *
878pipefs_get_sb(struct file_system_type *fs_type, int flags,
879 const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
882}
883
884static struct file_system_type pipe_fs_type = {
885 .name = "pipefs",
886 .get_sb = pipefs_get_sb,
887 .kill_sb = kill_anon_super,
888};
889
890static int __init init_pipe_fs(void)
891{
892 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +0200893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!err) {
895 pipe_mnt = kern_mount(&pipe_fs_type);
896 if (IS_ERR(pipe_mnt)) {
897 err = PTR_ERR(pipe_mnt);
898 unregister_filesystem(&pipe_fs_type);
899 }
900 }
901 return err;
902}
903
904static void __exit exit_pipe_fs(void)
905{
906 unregister_filesystem(&pipe_fs_type);
907 mntput(pipe_mnt);
908}
909
910fs_initcall(init_pipe_fs);
911module_exit(exit_pipe_fs);