blob: 3a89592bdf577930b4c1698c4811261aa467cbf2 [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>
Al Virodb349502007-02-07 01:48:00 -050019#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/uaccess.h>
22#include <asm/ioctls.h>
23
24/*
25 * We use a start+len construction, which provides full use of the
26 * allocated memory.
27 * -- Florian Coosmann (FGC)
28 *
29 * Reads with count = 0 should always return 0.
30 * -- Julian Bradfield 1999-06-07.
31 *
32 * FIFOs and Pipes now generate SIGIO for both readers and writers.
33 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
34 *
35 * pipe_read & write cleanup
36 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
37 */
38
39/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020040void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 DEFINE_WAIT(wait);
43
Ingo Molnard79fc0f2005-09-10 00:26:12 -070044 /*
45 * Pipes are system-local resources, so sleeping on them
46 * is considered a noninteractive wait:
47 */
Ingo Molnar341b4462006-04-11 13:57:45 +020048 prepare_to_wait(&pipe->wait, &wait,
49 TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
Ingo Molnar3a326a22006-04-10 15:18:35 +020050 if (pipe->inode)
51 mutex_unlock(&pipe->inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +020053 finish_wait(&pipe->wait, &wait);
54 if (pipe->inode)
55 mutex_lock(&pipe->inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Arjan van de Ven858119e2006-01-14 13:20:43 -080058static int
Jens Axboef6762b72006-05-01 20:02:05 +020059pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
60 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 unsigned long copy;
63
64 while (len > 0) {
65 while (!iov->iov_len)
66 iov++;
67 copy = min_t(unsigned long, len, iov->iov_len);
68
Jens Axboef6762b72006-05-01 20:02:05 +020069 if (atomic) {
70 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
71 return -EFAULT;
72 } else {
73 if (copy_from_user(to, iov->iov_base, copy))
74 return -EFAULT;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 to += copy;
77 len -= copy;
78 iov->iov_base += copy;
79 iov->iov_len -= copy;
80 }
81 return 0;
82}
83
Arjan van de Ven858119e2006-01-14 13:20:43 -080084static int
Jens Axboef6762b72006-05-01 20:02:05 +020085pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
86 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 unsigned long copy;
89
90 while (len > 0) {
91 while (!iov->iov_len)
92 iov++;
93 copy = min_t(unsigned long, len, iov->iov_len);
94
Jens Axboef6762b72006-05-01 20:02:05 +020095 if (atomic) {
96 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
97 return -EFAULT;
98 } else {
99 if (copy_to_user(iov->iov_base, from, copy))
100 return -EFAULT;
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 from += copy;
103 len -= copy;
104 iov->iov_base += copy;
105 iov->iov_len -= copy;
106 }
107 return 0;
108}
109
Jens Axboef6762b72006-05-01 20:02:05 +0200110/*
111 * Attempt to pre-fault in the user memory, so we can use atomic copies.
112 * Returns the number of bytes not faulted in.
113 */
114static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
115{
116 while (!iov->iov_len)
117 iov++;
118
119 while (len > 0) {
120 unsigned long this_len;
121
122 this_len = min_t(unsigned long, len, iov->iov_len);
123 if (fault_in_pages_writeable(iov->iov_base, this_len))
124 break;
125
126 len -= this_len;
127 iov++;
128 }
129
130 return len;
131}
132
133/*
134 * Pre-fault in the user memory, so we can use atomic copies.
135 */
136static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
137{
138 while (!iov->iov_len)
139 iov++;
140
141 while (len > 0) {
142 unsigned long this_len;
143
144 this_len = min_t(unsigned long, len, iov->iov_len);
145 fault_in_pages_readable(iov->iov_base, this_len);
146 len -= this_len;
147 iov++;
148 }
149}
150
Ingo Molnar341b4462006-04-11 13:57:45 +0200151static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
152 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct page *page = buf->page;
155
Jens Axboe5274f052006-03-30 15:15:30 +0200156 /*
157 * If nobody else uses this page, and we don't already have a
158 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200159 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200160 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200161 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200162 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200163 else
164 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Jens Axboef84d7512006-05-01 19:59:03 +0200167void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200168 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Jens Axboef6762b72006-05-01 20:02:05 +0200170 if (atomic) {
171 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
172 return kmap_atomic(buf->page, KM_USER0);
173 }
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return kmap(buf->page);
176}
177
Jens Axboef84d7512006-05-01 19:59:03 +0200178void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200179 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Jens Axboef6762b72006-05-01 20:02:05 +0200181 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
182 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
183 kunmap_atomic(map_data, KM_USER0);
184 } else
185 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Jens Axboe330ab712006-05-02 15:29:57 +0200188int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
189 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200190{
Jens Axboe46e678c2006-04-30 16:36:32 +0200191 struct page *page = buf->page;
192
193 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200194 lock_page(page);
195 return 0;
196 }
197
198 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200199}
200
Jens Axboef84d7512006-05-01 19:59:03 +0200201void generic_pipe_buf_get(struct pipe_inode_info *info, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200202{
203 page_cache_get(buf->page);
204}
205
Jens Axboef84d7512006-05-01 19:59:03 +0200206int generic_pipe_buf_pin(struct pipe_inode_info *info, struct pipe_buffer *buf)
207{
208 return 0;
209}
210
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800211static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200213 .map = generic_pipe_buf_map,
214 .unmap = generic_pipe_buf_unmap,
215 .pin = generic_pipe_buf_pin,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200217 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200218 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
221static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700222pipe_read(struct kiocb *iocb, const struct iovec *_iov,
223 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700225 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800226 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200227 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int do_wakeup;
229 ssize_t ret;
230 struct iovec *iov = (struct iovec *)_iov;
231 size_t total_len;
232
233 total_len = iov_length(iov, nr_segs);
234 /* Null read succeeds. */
235 if (unlikely(total_len == 0))
236 return 0;
237
238 do_wakeup = 0;
239 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200240 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200241 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200243 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200245 int curbuf = pipe->curbuf;
246 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800247 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 void *addr;
249 size_t chars = buf->len;
Jens Axboef6762b72006-05-01 20:02:05 +0200250 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (chars > total_len)
253 chars = total_len;
254
Jens Axboef84d7512006-05-01 19:59:03 +0200255 error = ops->pin(pipe, buf);
256 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200257 if (!ret)
Jens Axboef84d7512006-05-01 19:59:03 +0200258 error = ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200259 break;
260 }
Jens Axboef84d7512006-05-01 19:59:03 +0200261
Jens Axboef6762b72006-05-01 20:02:05 +0200262 atomic = !iov_fault_in_pages_write(iov, chars);
263redo:
264 addr = ops->map(pipe, buf, atomic);
265 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
266 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200268 /*
269 * Just retry with the slow path if we failed.
270 */
271 if (atomic) {
272 atomic = 0;
273 goto redo;
274 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200275 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200276 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 }
279 ret += chars;
280 buf->offset += chars;
281 buf->len -= chars;
282 if (!buf->len) {
283 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200284 ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200286 pipe->curbuf = curbuf;
287 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 do_wakeup = 1;
289 }
290 total_len -= chars;
291 if (!total_len)
292 break; /* common path: read succeeded */
293 }
294 if (bufs) /* More to do? */
295 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200296 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200298 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* syscall merging: Usually we must not sleep
300 * if O_NONBLOCK is set, or if we got some data.
301 * But if a writer sleeps in kernel space, then
302 * we can wait for that data without violating POSIX.
303 */
304 if (ret)
305 break;
306 if (filp->f_flags & O_NONBLOCK) {
307 ret = -EAGAIN;
308 break;
309 }
310 }
311 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200312 if (!ret)
313 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
315 }
316 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200317 wake_up_interruptible_sync(&pipe->wait);
318 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200320 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200322 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200323
324 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200326 wake_up_interruptible(&pipe->wait);
327 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 if (ret > 0)
330 file_accessed(filp);
331 return ret;
332}
333
334static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700335pipe_write(struct kiocb *iocb, const struct iovec *_iov,
336 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700338 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800339 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200340 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 ssize_t ret;
342 int do_wakeup;
343 struct iovec *iov = (struct iovec *)_iov;
344 size_t total_len;
345 ssize_t chars;
346
347 total_len = iov_length(iov, nr_segs);
348 /* Null write succeeds. */
349 if (unlikely(total_len == 0))
350 return 0;
351
352 do_wakeup = 0;
353 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200354 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200355 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Ingo Molnar923f4f22006-04-11 13:53:33 +0200357 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 send_sig(SIGPIPE, current, 0);
359 ret = -EPIPE;
360 goto out;
361 }
362
363 /* We try to merge small writes */
364 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200365 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200366 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
367 (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200368 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800369 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200373 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200374 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200375
Jens Axboef84d7512006-05-01 19:59:03 +0200376 error = ops->pin(pipe, buf);
377 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200378 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200379
Jens Axboef6762b72006-05-01 20:02:05 +0200380 iov_fault_in_pages_read(iov, chars);
381redo1:
382 addr = ops->map(pipe, buf, atomic);
Jens Axboe5274f052006-03-30 15:15:30 +0200383 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200384 chars, atomic);
385 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 ret = error;
387 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200388 if (error) {
389 if (atomic) {
390 atomic = 0;
391 goto redo1;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 buf->len += chars;
396 total_len -= chars;
397 ret = chars;
398 if (!total_len)
399 goto out;
400 }
401 }
402
403 for (;;) {
404 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200405
Ingo Molnar923f4f22006-04-11 13:53:33 +0200406 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200408 if (!ret)
409 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
411 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200412 bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (bufs < PIPE_BUFFERS) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200414 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
415 struct pipe_buffer *buf = pipe->bufs + newbuf;
416 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200417 char *src;
418 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (!page) {
421 page = alloc_page(GFP_HIGHUSER);
422 if (unlikely(!page)) {
423 ret = ret ? : -ENOMEM;
424 break;
425 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200426 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200428 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * we lock up (O_NONBLOCK-)readers that sleep due to
430 * syscall merging.
431 * FIXME! Is this really true?
432 */
433 do_wakeup = 1;
434 chars = PAGE_SIZE;
435 if (chars > total_len)
436 chars = total_len;
437
Jens Axboef6762b72006-05-01 20:02:05 +0200438 iov_fault_in_pages_read(iov, chars);
439redo2:
440 if (atomic)
441 src = kmap_atomic(page, KM_USER0);
442 else
443 src = kmap(page);
444
445 error = pipe_iov_copy_from_user(src, iov, chars,
446 atomic);
447 if (atomic)
448 kunmap_atomic(src, KM_USER0);
449 else
450 kunmap(page);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200453 if (atomic) {
454 atomic = 0;
455 goto redo2;
456 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200457 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200458 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 }
461 ret += chars;
462
463 /* Insert it into the buffer array */
464 buf->page = page;
465 buf->ops = &anon_pipe_buf_ops;
466 buf->offset = 0;
467 buf->len = chars;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200468 pipe->nrbufs = ++bufs;
469 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 total_len -= chars;
472 if (!total_len)
473 break;
474 }
475 if (bufs < PIPE_BUFFERS)
476 continue;
477 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200478 if (!ret)
479 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 }
482 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200483 if (!ret)
484 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 }
487 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200488 wake_up_interruptible_sync(&pipe->wait);
489 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 do_wakeup = 0;
491 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200492 pipe->waiting_writers++;
493 pipe_wait(pipe);
494 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200497 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200499 wake_up_interruptible(&pipe->wait);
500 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800503 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return ret;
505}
506
507static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
509{
510 return -EBADF;
511}
512
513static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200514bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
515 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 return -EBADF;
518}
519
520static int
521pipe_ioctl(struct inode *pino, struct file *filp,
522 unsigned int cmd, unsigned long arg)
523{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800524 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200525 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int count, buf, nrbufs;
527
528 switch (cmd) {
529 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200530 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200531 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200533 buf = pipe->curbuf;
534 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200536 count += pipe->bufs[buf].len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 buf = (buf+1) & (PIPE_BUFFERS-1);
538 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200539 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return put_user(count, (int __user *)arg);
542 default:
543 return -EINVAL;
544 }
545}
546
547/* No kernel lock held - fine */
548static unsigned int
549pipe_poll(struct file *filp, poll_table *wait)
550{
551 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800552 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200553 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int nrbufs;
555
Ingo Molnar923f4f22006-04-11 13:53:33 +0200556 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200559 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 mask = 0;
561 if (filp->f_mode & FMODE_READ) {
562 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200563 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 mask |= POLLHUP;
565 }
566
567 if (filp->f_mode & FMODE_WRITE) {
568 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700569 /*
570 * Most Unices do not set POLLERR for FIFOs but on Linux they
571 * behave exactly like pipes for poll().
572 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200573 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 mask |= POLLERR;
575 }
576
577 return mask;
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static int
581pipe_release(struct inode *inode, int decr, int decw)
582{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200583 struct pipe_inode_info *pipe;
584
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200585 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200586 pipe = inode->i_pipe;
587 pipe->readers -= decr;
588 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200589
Ingo Molnar923f4f22006-04-11 13:53:33 +0200590 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 free_pipe_info(inode);
592 } else {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200593 wake_up_interruptible(&pipe->wait);
594 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
595 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200597 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 return 0;
600}
601
602static int
603pipe_read_fasync(int fd, struct file *filp, int on)
604{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800605 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int retval;
607
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200608 mutex_lock(&inode->i_mutex);
609 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
610 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (retval < 0)
613 return retval;
614
615 return 0;
616}
617
618
619static int
620pipe_write_fasync(int fd, struct file *filp, int on)
621{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800622 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int retval;
624
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200625 mutex_lock(&inode->i_mutex);
626 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
627 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (retval < 0)
630 return retval;
631
632 return 0;
633}
634
635
636static int
637pipe_rdwr_fasync(int fd, struct file *filp, int on)
638{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800639 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200640 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int retval;
642
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200643 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Ingo Molnar341b4462006-04-11 13:57:45 +0200645 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (retval >= 0)
Ingo Molnar341b4462006-04-11 13:57:45 +0200648 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200650 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (retval < 0)
653 return retval;
654
655 return 0;
656}
657
658
659static int
660pipe_read_release(struct inode *inode, struct file *filp)
661{
662 pipe_read_fasync(-1, filp, 0);
663 return pipe_release(inode, 1, 0);
664}
665
666static int
667pipe_write_release(struct inode *inode, struct file *filp)
668{
669 pipe_write_fasync(-1, filp, 0);
670 return pipe_release(inode, 0, 1);
671}
672
673static int
674pipe_rdwr_release(struct inode *inode, struct file *filp)
675{
676 int decr, decw;
677
678 pipe_rdwr_fasync(-1, filp, 0);
679 decr = (filp->f_mode & FMODE_READ) != 0;
680 decw = (filp->f_mode & FMODE_WRITE) != 0;
681 return pipe_release(inode, decr, decw);
682}
683
684static int
685pipe_read_open(struct inode *inode, struct file *filp)
686{
687 /* We could have perhaps used atomic_t, but this and friends
688 below are the only places. So it doesn't seem worthwhile. */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200689 mutex_lock(&inode->i_mutex);
690 inode->i_pipe->readers++;
691 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 return 0;
694}
695
696static int
697pipe_write_open(struct inode *inode, struct file *filp)
698{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200699 mutex_lock(&inode->i_mutex);
700 inode->i_pipe->writers++;
701 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 return 0;
704}
705
706static int
707pipe_rdwr_open(struct inode *inode, struct file *filp)
708{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200709 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (filp->f_mode & FMODE_READ)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200711 inode->i_pipe->readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (filp->f_mode & FMODE_WRITE)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200713 inode->i_pipe->writers++;
714 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 return 0;
717}
718
719/*
720 * The file_operations structs are not static because they
721 * are also used in linux/fs/fifo.c to do operations on FIFOs.
722 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800723const struct file_operations read_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700725 .read = do_sync_read,
726 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700728 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 .ioctl = pipe_ioctl,
730 .open = pipe_read_open,
731 .release = pipe_read_release,
732 .fasync = pipe_read_fasync,
733};
734
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800735const struct file_operations write_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 .llseek = no_llseek,
737 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700738 .write = do_sync_write,
739 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700740 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .ioctl = pipe_ioctl,
742 .open = pipe_write_open,
743 .release = pipe_write_release,
744 .fasync = pipe_write_fasync,
745};
746
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800747const struct file_operations rdwr_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700749 .read = do_sync_read,
750 .aio_read = pipe_read,
751 .write = do_sync_write,
752 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700753 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 .ioctl = pipe_ioctl,
755 .open = pipe_rdwr_open,
756 .release = pipe_rdwr_release,
757 .fasync = pipe_rdwr_fasync,
758};
759
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800760static const struct file_operations read_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700762 .read = do_sync_read,
763 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 .write = bad_pipe_w,
765 .poll = pipe_poll,
766 .ioctl = pipe_ioctl,
767 .open = pipe_read_open,
768 .release = pipe_read_release,
769 .fasync = pipe_read_fasync,
770};
771
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800772static const struct file_operations write_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 .llseek = no_llseek,
774 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700775 .write = do_sync_write,
776 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 .poll = pipe_poll,
778 .ioctl = pipe_ioctl,
779 .open = pipe_write_open,
780 .release = pipe_write_release,
781 .fasync = pipe_write_fasync,
782};
783
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800784static const struct file_operations rdwr_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700786 .read = do_sync_read,
787 .aio_read = pipe_read,
788 .write = do_sync_write,
789 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 .poll = pipe_poll,
791 .ioctl = pipe_ioctl,
792 .open = pipe_rdwr_open,
793 .release = pipe_rdwr_release,
794 .fasync = pipe_rdwr_fasync,
795};
796
Ingo Molnar3a326a22006-04-10 15:18:35 +0200797struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
798{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200799 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200800
Ingo Molnar923f4f22006-04-11 13:53:33 +0200801 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
802 if (pipe) {
803 init_waitqueue_head(&pipe->wait);
804 pipe->r_counter = pipe->w_counter = 1;
805 pipe->inode = inode;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200806 }
807
Ingo Molnar923f4f22006-04-11 13:53:33 +0200808 return pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200809}
810
Ingo Molnar923f4f22006-04-11 13:53:33 +0200811void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 for (i = 0; i < PIPE_BUFFERS; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200816 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200818 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200820 if (pipe->tmp_page)
821 __free_page(pipe->tmp_page);
822 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Jens Axboeb92ce552006-04-11 13:52:07 +0200825void free_pipe_info(struct inode *inode)
826{
827 __free_pipe_info(inode->i_pipe);
828 inode->i_pipe = NULL;
829}
830
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800831static struct vfsmount *pipe_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832static int pipefs_delete_dentry(struct dentry *dentry)
833{
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800834 /*
835 * At creation time, we pretended this dentry was hashed
836 * (by clearing DCACHE_UNHASHED bit in d_flags)
837 * At delete time, we restore the truth : not hashed.
838 * (so that dput() can proceed correctly)
839 */
840 dentry->d_flags |= DCACHE_UNHASHED;
841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
Ingo Molnar341b4462006-04-11 13:57:45 +0200843
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700844/*
845 * pipefs_dname() is called from d_path().
846 */
847static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
848{
849 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
850 dentry->d_inode->i_ino);
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static struct dentry_operations pipefs_dentry_operations = {
854 .d_delete = pipefs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700855 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856};
857
858static struct inode * get_pipe_inode(void)
859{
860 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200861 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (!inode)
864 goto fail_inode;
865
Ingo Molnar923f4f22006-04-11 13:53:33 +0200866 pipe = alloc_pipe_info(inode);
867 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200869 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200870
Ingo Molnar923f4f22006-04-11 13:53:33 +0200871 pipe->readers = pipe->writers = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 inode->i_fop = &rdwr_pipe_fops;
873
874 /*
875 * Mark the inode dirty from the very beginning,
876 * that way it will never be moved to the dirty
877 * list because "mark_inode_dirty()" will think
878 * that it already _is_ on the dirty list.
879 */
880 inode->i_state = I_DIRTY;
881 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
882 inode->i_uid = current->fsuid;
883 inode->i_gid = current->fsgid;
884 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return inode;
887
888fail_iput:
889 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891fail_inode:
892 return NULL;
893}
894
Andi Kleend6cbd282006-09-30 23:29:26 -0700895struct file *create_write_pipe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Andi Kleend6cbd282006-09-30 23:29:26 -0700897 int err;
898 struct inode *inode;
899 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700901 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Andi Kleend6cbd282006-09-30 23:29:26 -0700903 f = get_empty_filp();
904 if (!f)
905 return ERR_PTR(-ENFILE);
906 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 inode = get_pipe_inode();
908 if (!inode)
Andi Kleend6cbd282006-09-30 23:29:26 -0700909 goto err_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Andi Kleend6cbd282006-09-30 23:29:26 -0700911 err = -ENOMEM;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700912 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (!dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700914 goto err_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 dentry->d_op = &pipefs_dentry_operations;
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800917 /*
918 * We dont want to publish this dentry into global dentry hash table.
919 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
920 * This permits a working /proc/$pid/fd/XXX on pipes
921 */
922 dentry->d_flags &= ~DCACHE_UNHASHED;
923 d_instantiate(dentry, inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800924 f->f_path.mnt = mntget(pipe_mnt);
925 f->f_path.dentry = dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -0700926 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Andi Kleend6cbd282006-09-30 23:29:26 -0700928 f->f_flags = O_WRONLY;
929 f->f_op = &write_pipe_fops;
930 f->f_mode = FMODE_WRITE;
931 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Andi Kleend6cbd282006-09-30 23:29:26 -0700933 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Andi Kleend6cbd282006-09-30 23:29:26 -0700935 err_inode:
936 free_pipe_info(inode);
937 iput(inode);
938 err_file:
939 put_filp(f);
940 return ERR_PTR(err);
941}
942
943void free_write_pipe(struct file *f)
944{
Al Viro5ccac88e2006-12-18 13:31:18 +0000945 free_pipe_info(f->f_dentry->d_inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800946 dput(f->f_path.dentry);
Al Viro5ccac88e2006-12-18 13:31:18 +0000947 mntput(f->f_path.mnt);
Andi Kleend6cbd282006-09-30 23:29:26 -0700948 put_filp(f);
949}
950
951struct file *create_read_pipe(struct file *wrf)
952{
953 struct file *f = get_empty_filp();
954 if (!f)
955 return ERR_PTR(-ENFILE);
956
957 /* Grab pipe from the writer */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800958 f->f_path.mnt = mntget(wrf->f_path.mnt);
959 f->f_path.dentry = dget(wrf->f_path.dentry);
960 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
Andi Kleend6cbd282006-09-30 23:29:26 -0700961
962 f->f_pos = 0;
963 f->f_flags = O_RDONLY;
964 f->f_op = &read_pipe_fops;
965 f->f_mode = FMODE_READ;
966 f->f_version = 0;
967
968 return f;
969}
970
971int do_pipe(int *fd)
972{
973 struct file *fw, *fr;
974 int error;
975 int fdw, fdr;
976
977 fw = create_write_pipe();
978 if (IS_ERR(fw))
979 return PTR_ERR(fw);
980 fr = create_read_pipe(fw);
981 error = PTR_ERR(fr);
982 if (IS_ERR(fr))
983 goto err_write_pipe;
984
985 error = get_unused_fd();
986 if (error < 0)
987 goto err_read_pipe;
988 fdr = error;
989
990 error = get_unused_fd();
991 if (error < 0)
992 goto err_fdr;
993 fdw = error;
994
Al Virodb349502007-02-07 01:48:00 -0500995 error = audit_fd_pair(fdr, fdw);
996 if (error < 0)
997 goto err_fdw;
998
Andi Kleend6cbd282006-09-30 23:29:26 -0700999 fd_install(fdr, fr);
1000 fd_install(fdw, fw);
1001 fd[0] = fdr;
1002 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005
Al Virodb349502007-02-07 01:48:00 -05001006 err_fdw:
1007 put_unused_fd(fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001008 err_fdr:
1009 put_unused_fd(fdr);
1010 err_read_pipe:
Al Viro5ccac88e2006-12-18 13:31:18 +00001011 dput(fr->f_dentry);
1012 mntput(fr->f_vfsmnt);
Andi Kleend6cbd282006-09-30 23:29:26 -07001013 put_filp(fr);
1014 err_write_pipe:
1015 free_write_pipe(fw);
1016 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/*
1020 * pipefs should _never_ be mounted by userland - too much of security hassle,
1021 * no real gain from having the whole whorehouse mounted. So we don't need
1022 * any operations on the root directory. However, we need a non-trivial
1023 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1024 */
David Howells454e2392006-06-23 02:02:57 -07001025static int pipefs_get_sb(struct file_system_type *fs_type,
1026 int flags, const char *dev_name, void *data,
1027 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
David Howells454e2392006-06-23 02:02:57 -07001029 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032static struct file_system_type pipe_fs_type = {
1033 .name = "pipefs",
1034 .get_sb = pipefs_get_sb,
1035 .kill_sb = kill_anon_super,
1036};
1037
1038static int __init init_pipe_fs(void)
1039{
1040 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!err) {
1043 pipe_mnt = kern_mount(&pipe_fs_type);
1044 if (IS_ERR(pipe_mnt)) {
1045 err = PTR_ERR(pipe_mnt);
1046 unregister_filesystem(&pipe_fs_type);
1047 }
1048 }
1049 return err;
1050}
1051
1052static void __exit exit_pipe_fs(void)
1053{
1054 unregister_filesystem(&pipe_fs_type);
1055 mntput(pipe_mnt);
1056}
1057
1058fs_initcall(init_pipe_fs);
1059module_exit(exit_pipe_fs);