blob: 3694af10dd2c22423945a6ec1ef011a41563882e [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 Axboecac36bb02007-06-14 13:10:48 +0200206int generic_pipe_buf_confirm(struct pipe_inode_info *info,
207 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200208{
209 return 0;
210}
211
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800212static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200214 .map = generic_pipe_buf_map,
215 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb02007-06-14 13:10:48 +0200216 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200218 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200219 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
222static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700223pipe_read(struct kiocb *iocb, const struct iovec *_iov,
224 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700226 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800227 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200228 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int do_wakeup;
230 ssize_t ret;
231 struct iovec *iov = (struct iovec *)_iov;
232 size_t total_len;
233
234 total_len = iov_length(iov, nr_segs);
235 /* Null read succeeds. */
236 if (unlikely(total_len == 0))
237 return 0;
238
239 do_wakeup = 0;
240 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200241 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200242 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200244 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200246 int curbuf = pipe->curbuf;
247 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800248 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 void *addr;
250 size_t chars = buf->len;
Jens Axboef6762b72006-05-01 20:02:05 +0200251 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (chars > total_len)
254 chars = total_len;
255
Jens Axboecac36bb02007-06-14 13:10:48 +0200256 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200257 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200258 if (!ret)
Jens Axboef84d7512006-05-01 19:59:03 +0200259 error = ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200260 break;
261 }
Jens Axboef84d7512006-05-01 19:59:03 +0200262
Jens Axboef6762b72006-05-01 20:02:05 +0200263 atomic = !iov_fault_in_pages_write(iov, chars);
264redo:
265 addr = ops->map(pipe, buf, atomic);
266 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
267 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200269 /*
270 * Just retry with the slow path if we failed.
271 */
272 if (atomic) {
273 atomic = 0;
274 goto redo;
275 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200276 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200277 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279 }
280 ret += chars;
281 buf->offset += chars;
282 buf->len -= chars;
283 if (!buf->len) {
284 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200285 ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200287 pipe->curbuf = curbuf;
288 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 do_wakeup = 1;
290 }
291 total_len -= chars;
292 if (!total_len)
293 break; /* common path: read succeeded */
294 }
295 if (bufs) /* More to do? */
296 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200297 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200299 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* syscall merging: Usually we must not sleep
301 * if O_NONBLOCK is set, or if we got some data.
302 * But if a writer sleeps in kernel space, then
303 * we can wait for that data without violating POSIX.
304 */
305 if (ret)
306 break;
307 if (filp->f_flags & O_NONBLOCK) {
308 ret = -EAGAIN;
309 break;
310 }
311 }
312 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200313 if (!ret)
314 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
316 }
317 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200318 wake_up_interruptible_sync(&pipe->wait);
319 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200321 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200323 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200324
325 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200327 wake_up_interruptible(&pipe->wait);
328 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 if (ret > 0)
331 file_accessed(filp);
332 return ret;
333}
334
335static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700336pipe_write(struct kiocb *iocb, const struct iovec *_iov,
337 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700339 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800340 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200341 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 ssize_t ret;
343 int do_wakeup;
344 struct iovec *iov = (struct iovec *)_iov;
345 size_t total_len;
346 ssize_t chars;
347
348 total_len = iov_length(iov, nr_segs);
349 /* Null write succeeds. */
350 if (unlikely(total_len == 0))
351 return 0;
352
353 do_wakeup = 0;
354 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200355 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200356 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Ingo Molnar923f4f22006-04-11 13:53:33 +0200358 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 send_sig(SIGPIPE, current, 0);
360 ret = -EPIPE;
361 goto out;
362 }
363
364 /* We try to merge small writes */
365 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200366 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200367 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
368 (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200369 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800370 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200374 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200375 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200376
Jens Axboecac36bb02007-06-14 13:10:48 +0200377 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200378 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200379 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200380
Jens Axboef6762b72006-05-01 20:02:05 +0200381 iov_fault_in_pages_read(iov, chars);
382redo1:
383 addr = ops->map(pipe, buf, atomic);
Jens Axboe5274f052006-03-30 15:15:30 +0200384 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200385 chars, atomic);
386 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ret = error;
388 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200389 if (error) {
390 if (atomic) {
391 atomic = 0;
392 goto redo1;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 buf->len += chars;
397 total_len -= chars;
398 ret = chars;
399 if (!total_len)
400 goto out;
401 }
402 }
403
404 for (;;) {
405 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200406
Ingo Molnar923f4f22006-04-11 13:53:33 +0200407 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200409 if (!ret)
410 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200413 bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (bufs < PIPE_BUFFERS) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200415 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
416 struct pipe_buffer *buf = pipe->bufs + newbuf;
417 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200418 char *src;
419 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (!page) {
422 page = alloc_page(GFP_HIGHUSER);
423 if (unlikely(!page)) {
424 ret = ret ? : -ENOMEM;
425 break;
426 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200427 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200429 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * we lock up (O_NONBLOCK-)readers that sleep due to
431 * syscall merging.
432 * FIXME! Is this really true?
433 */
434 do_wakeup = 1;
435 chars = PAGE_SIZE;
436 if (chars > total_len)
437 chars = total_len;
438
Jens Axboef6762b72006-05-01 20:02:05 +0200439 iov_fault_in_pages_read(iov, chars);
440redo2:
441 if (atomic)
442 src = kmap_atomic(page, KM_USER0);
443 else
444 src = kmap(page);
445
446 error = pipe_iov_copy_from_user(src, iov, chars,
447 atomic);
448 if (atomic)
449 kunmap_atomic(src, KM_USER0);
450 else
451 kunmap(page);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200454 if (atomic) {
455 atomic = 0;
456 goto redo2;
457 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200458 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200459 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461 }
462 ret += chars;
463
464 /* Insert it into the buffer array */
465 buf->page = page;
466 buf->ops = &anon_pipe_buf_ops;
467 buf->offset = 0;
468 buf->len = chars;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200469 pipe->nrbufs = ++bufs;
470 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 total_len -= chars;
473 if (!total_len)
474 break;
475 }
476 if (bufs < PIPE_BUFFERS)
477 continue;
478 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200479 if (!ret)
480 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482 }
483 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200484 if (!ret)
485 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 }
488 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200489 wake_up_interruptible_sync(&pipe->wait);
490 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 do_wakeup = 0;
492 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200493 pipe->waiting_writers++;
494 pipe_wait(pipe);
495 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200498 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200500 wake_up_interruptible(&pipe->wait);
501 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800504 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return ret;
506}
507
508static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
510{
511 return -EBADF;
512}
513
514static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200515bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
516 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 return -EBADF;
519}
520
521static int
522pipe_ioctl(struct inode *pino, struct file *filp,
523 unsigned int cmd, unsigned long arg)
524{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800525 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200526 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int count, buf, nrbufs;
528
529 switch (cmd) {
530 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200531 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200532 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200534 buf = pipe->curbuf;
535 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200537 count += pipe->bufs[buf].len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 buf = (buf+1) & (PIPE_BUFFERS-1);
539 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200540 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return put_user(count, (int __user *)arg);
543 default:
544 return -EINVAL;
545 }
546}
547
548/* No kernel lock held - fine */
549static unsigned int
550pipe_poll(struct file *filp, poll_table *wait)
551{
552 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800553 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200554 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int nrbufs;
556
Ingo Molnar923f4f22006-04-11 13:53:33 +0200557 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200560 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 mask = 0;
562 if (filp->f_mode & FMODE_READ) {
563 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200564 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 mask |= POLLHUP;
566 }
567
568 if (filp->f_mode & FMODE_WRITE) {
569 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700570 /*
571 * Most Unices do not set POLLERR for FIFOs but on Linux they
572 * behave exactly like pipes for poll().
573 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200574 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mask |= POLLERR;
576 }
577
578 return mask;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static int
582pipe_release(struct inode *inode, int decr, int decw)
583{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200584 struct pipe_inode_info *pipe;
585
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200586 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200587 pipe = inode->i_pipe;
588 pipe->readers -= decr;
589 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200590
Ingo Molnar923f4f22006-04-11 13:53:33 +0200591 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 free_pipe_info(inode);
593 } else {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200594 wake_up_interruptible(&pipe->wait);
595 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
596 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200598 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 return 0;
601}
602
603static int
604pipe_read_fasync(int fd, struct file *filp, int on)
605{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800606 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int retval;
608
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200609 mutex_lock(&inode->i_mutex);
610 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
611 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (retval < 0)
614 return retval;
615
616 return 0;
617}
618
619
620static int
621pipe_write_fasync(int fd, struct file *filp, int on)
622{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800623 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int retval;
625
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200626 mutex_lock(&inode->i_mutex);
627 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
628 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (retval < 0)
631 return retval;
632
633 return 0;
634}
635
636
637static int
638pipe_rdwr_fasync(int fd, struct file *filp, int on)
639{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800640 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200641 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int retval;
643
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200644 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Ingo Molnar341b4462006-04-11 13:57:45 +0200646 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (retval >= 0)
Ingo Molnar341b4462006-04-11 13:57:45 +0200649 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200651 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (retval < 0)
654 return retval;
655
656 return 0;
657}
658
659
660static int
661pipe_read_release(struct inode *inode, struct file *filp)
662{
663 pipe_read_fasync(-1, filp, 0);
664 return pipe_release(inode, 1, 0);
665}
666
667static int
668pipe_write_release(struct inode *inode, struct file *filp)
669{
670 pipe_write_fasync(-1, filp, 0);
671 return pipe_release(inode, 0, 1);
672}
673
674static int
675pipe_rdwr_release(struct inode *inode, struct file *filp)
676{
677 int decr, decw;
678
679 pipe_rdwr_fasync(-1, filp, 0);
680 decr = (filp->f_mode & FMODE_READ) != 0;
681 decw = (filp->f_mode & FMODE_WRITE) != 0;
682 return pipe_release(inode, decr, decw);
683}
684
685static int
686pipe_read_open(struct inode *inode, struct file *filp)
687{
688 /* We could have perhaps used atomic_t, but this and friends
689 below are the only places. So it doesn't seem worthwhile. */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200690 mutex_lock(&inode->i_mutex);
691 inode->i_pipe->readers++;
692 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 return 0;
695}
696
697static int
698pipe_write_open(struct inode *inode, struct file *filp)
699{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200700 mutex_lock(&inode->i_mutex);
701 inode->i_pipe->writers++;
702 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return 0;
705}
706
707static int
708pipe_rdwr_open(struct inode *inode, struct file *filp)
709{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200710 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (filp->f_mode & FMODE_READ)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200712 inode->i_pipe->readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (filp->f_mode & FMODE_WRITE)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200714 inode->i_pipe->writers++;
715 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return 0;
718}
719
720/*
721 * The file_operations structs are not static because they
722 * are also used in linux/fs/fifo.c to do operations on FIFOs.
723 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800724const struct file_operations read_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700726 .read = do_sync_read,
727 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700729 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 .ioctl = pipe_ioctl,
731 .open = pipe_read_open,
732 .release = pipe_read_release,
733 .fasync = pipe_read_fasync,
734};
735
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800736const struct file_operations write_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 .llseek = no_llseek,
738 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700739 .write = do_sync_write,
740 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700741 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .ioctl = pipe_ioctl,
743 .open = pipe_write_open,
744 .release = pipe_write_release,
745 .fasync = pipe_write_fasync,
746};
747
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800748const struct file_operations rdwr_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700750 .read = do_sync_read,
751 .aio_read = pipe_read,
752 .write = do_sync_write,
753 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700754 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .ioctl = pipe_ioctl,
756 .open = pipe_rdwr_open,
757 .release = pipe_rdwr_release,
758 .fasync = pipe_rdwr_fasync,
759};
760
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800761static const struct file_operations read_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700763 .read = do_sync_read,
764 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .write = bad_pipe_w,
766 .poll = pipe_poll,
767 .ioctl = pipe_ioctl,
768 .open = pipe_read_open,
769 .release = pipe_read_release,
770 .fasync = pipe_read_fasync,
771};
772
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800773static const struct file_operations write_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 .llseek = no_llseek,
775 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700776 .write = do_sync_write,
777 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .poll = pipe_poll,
779 .ioctl = pipe_ioctl,
780 .open = pipe_write_open,
781 .release = pipe_write_release,
782 .fasync = pipe_write_fasync,
783};
784
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800785static const struct file_operations rdwr_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700787 .read = do_sync_read,
788 .aio_read = pipe_read,
789 .write = do_sync_write,
790 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .poll = pipe_poll,
792 .ioctl = pipe_ioctl,
793 .open = pipe_rdwr_open,
794 .release = pipe_rdwr_release,
795 .fasync = pipe_rdwr_fasync,
796};
797
Ingo Molnar3a326a22006-04-10 15:18:35 +0200798struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
799{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200800 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200801
Ingo Molnar923f4f22006-04-11 13:53:33 +0200802 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
803 if (pipe) {
804 init_waitqueue_head(&pipe->wait);
805 pipe->r_counter = pipe->w_counter = 1;
806 pipe->inode = inode;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200807 }
808
Ingo Molnar923f4f22006-04-11 13:53:33 +0200809 return pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200810}
811
Ingo Molnar923f4f22006-04-11 13:53:33 +0200812void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 for (i = 0; i < PIPE_BUFFERS; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200817 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200819 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200821 if (pipe->tmp_page)
822 __free_page(pipe->tmp_page);
823 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Jens Axboeb92ce552006-04-11 13:52:07 +0200826void free_pipe_info(struct inode *inode)
827{
828 __free_pipe_info(inode->i_pipe);
829 inode->i_pipe = NULL;
830}
831
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800832static struct vfsmount *pipe_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int pipefs_delete_dentry(struct dentry *dentry)
834{
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800835 /*
836 * At creation time, we pretended this dentry was hashed
837 * (by clearing DCACHE_UNHASHED bit in d_flags)
838 * At delete time, we restore the truth : not hashed.
839 * (so that dput() can proceed correctly)
840 */
841 dentry->d_flags |= DCACHE_UNHASHED;
842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
Ingo Molnar341b4462006-04-11 13:57:45 +0200844
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700845/*
846 * pipefs_dname() is called from d_path().
847 */
848static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
849{
850 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
851 dentry->d_inode->i_ino);
852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static struct dentry_operations pipefs_dentry_operations = {
855 .d_delete = pipefs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700856 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858
859static struct inode * get_pipe_inode(void)
860{
861 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200862 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (!inode)
865 goto fail_inode;
866
Ingo Molnar923f4f22006-04-11 13:53:33 +0200867 pipe = alloc_pipe_info(inode);
868 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200870 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200871
Ingo Molnar923f4f22006-04-11 13:53:33 +0200872 pipe->readers = pipe->writers = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 inode->i_fop = &rdwr_pipe_fops;
874
875 /*
876 * Mark the inode dirty from the very beginning,
877 * that way it will never be moved to the dirty
878 * list because "mark_inode_dirty()" will think
879 * that it already _is_ on the dirty list.
880 */
881 inode->i_state = I_DIRTY;
882 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
883 inode->i_uid = current->fsuid;
884 inode->i_gid = current->fsgid;
885 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return inode;
888
889fail_iput:
890 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892fail_inode:
893 return NULL;
894}
895
Andi Kleend6cbd282006-09-30 23:29:26 -0700896struct file *create_write_pipe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Andi Kleend6cbd282006-09-30 23:29:26 -0700898 int err;
899 struct inode *inode;
900 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700902 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Andi Kleend6cbd282006-09-30 23:29:26 -0700904 f = get_empty_filp();
905 if (!f)
906 return ERR_PTR(-ENFILE);
907 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 inode = get_pipe_inode();
909 if (!inode)
Andi Kleend6cbd282006-09-30 23:29:26 -0700910 goto err_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Andi Kleend6cbd282006-09-30 23:29:26 -0700912 err = -ENOMEM;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700913 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700915 goto err_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 dentry->d_op = &pipefs_dentry_operations;
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800918 /*
919 * We dont want to publish this dentry into global dentry hash table.
920 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
921 * This permits a working /proc/$pid/fd/XXX on pipes
922 */
923 dentry->d_flags &= ~DCACHE_UNHASHED;
924 d_instantiate(dentry, inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800925 f->f_path.mnt = mntget(pipe_mnt);
926 f->f_path.dentry = dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -0700927 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Andi Kleend6cbd282006-09-30 23:29:26 -0700929 f->f_flags = O_WRONLY;
930 f->f_op = &write_pipe_fops;
931 f->f_mode = FMODE_WRITE;
932 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Andi Kleend6cbd282006-09-30 23:29:26 -0700934 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Andi Kleend6cbd282006-09-30 23:29:26 -0700936 err_inode:
937 free_pipe_info(inode);
938 iput(inode);
939 err_file:
940 put_filp(f);
941 return ERR_PTR(err);
942}
943
944void free_write_pipe(struct file *f)
945{
Al Viro5ccac88e2006-12-18 13:31:18 +0000946 free_pipe_info(f->f_dentry->d_inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800947 dput(f->f_path.dentry);
Al Viro5ccac88e2006-12-18 13:31:18 +0000948 mntput(f->f_path.mnt);
Andi Kleend6cbd282006-09-30 23:29:26 -0700949 put_filp(f);
950}
951
952struct file *create_read_pipe(struct file *wrf)
953{
954 struct file *f = get_empty_filp();
955 if (!f)
956 return ERR_PTR(-ENFILE);
957
958 /* Grab pipe from the writer */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800959 f->f_path.mnt = mntget(wrf->f_path.mnt);
960 f->f_path.dentry = dget(wrf->f_path.dentry);
961 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
Andi Kleend6cbd282006-09-30 23:29:26 -0700962
963 f->f_pos = 0;
964 f->f_flags = O_RDONLY;
965 f->f_op = &read_pipe_fops;
966 f->f_mode = FMODE_READ;
967 f->f_version = 0;
968
969 return f;
970}
971
972int do_pipe(int *fd)
973{
974 struct file *fw, *fr;
975 int error;
976 int fdw, fdr;
977
978 fw = create_write_pipe();
979 if (IS_ERR(fw))
980 return PTR_ERR(fw);
981 fr = create_read_pipe(fw);
982 error = PTR_ERR(fr);
983 if (IS_ERR(fr))
984 goto err_write_pipe;
985
986 error = get_unused_fd();
987 if (error < 0)
988 goto err_read_pipe;
989 fdr = error;
990
991 error = get_unused_fd();
992 if (error < 0)
993 goto err_fdr;
994 fdw = error;
995
Al Virodb349502007-02-07 01:48:00 -0500996 error = audit_fd_pair(fdr, fdw);
997 if (error < 0)
998 goto err_fdw;
999
Andi Kleend6cbd282006-09-30 23:29:26 -07001000 fd_install(fdr, fr);
1001 fd_install(fdw, fw);
1002 fd[0] = fdr;
1003 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006
Al Virodb349502007-02-07 01:48:00 -05001007 err_fdw:
1008 put_unused_fd(fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001009 err_fdr:
1010 put_unused_fd(fdr);
1011 err_read_pipe:
Al Viro5ccac88e2006-12-18 13:31:18 +00001012 dput(fr->f_dentry);
1013 mntput(fr->f_vfsmnt);
Andi Kleend6cbd282006-09-30 23:29:26 -07001014 put_filp(fr);
1015 err_write_pipe:
1016 free_write_pipe(fw);
1017 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020/*
1021 * pipefs should _never_ be mounted by userland - too much of security hassle,
1022 * no real gain from having the whole whorehouse mounted. So we don't need
1023 * any operations on the root directory. However, we need a non-trivial
1024 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1025 */
David Howells454e2392006-06-23 02:02:57 -07001026static int pipefs_get_sb(struct file_system_type *fs_type,
1027 int flags, const char *dev_name, void *data,
1028 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
David Howells454e2392006-06-23 02:02:57 -07001030 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static struct file_system_type pipe_fs_type = {
1034 .name = "pipefs",
1035 .get_sb = pipefs_get_sb,
1036 .kill_sb = kill_anon_super,
1037};
1038
1039static int __init init_pipe_fs(void)
1040{
1041 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!err) {
1044 pipe_mnt = kern_mount(&pipe_fs_type);
1045 if (IS_ERR(pipe_mnt)) {
1046 err = PTR_ERR(pipe_mnt);
1047 unregister_filesystem(&pipe_fs_type);
1048 }
1049 }
1050 return err;
1051}
1052
1053static void __exit exit_pipe_fs(void)
1054{
1055 unregister_filesystem(&pipe_fs_type);
1056 mntput(pipe_mnt);
1057}
1058
1059fs_initcall(init_pipe_fs);
1060module_exit(exit_pipe_fs);