blob: e66ec48e95d8f44223ba960e5fe82a9c7acd962d [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 */
Mike Galbraithaf927232007-10-15 17:00:13 +020048 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
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
Jens Axboef6762b72006-05-01 20:02:05 +020058pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
59 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 unsigned long copy;
62
63 while (len > 0) {
64 while (!iov->iov_len)
65 iov++;
66 copy = min_t(unsigned long, len, iov->iov_len);
67
Jens Axboef6762b72006-05-01 20:02:05 +020068 if (atomic) {
69 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
70 return -EFAULT;
71 } else {
72 if (copy_from_user(to, iov->iov_base, copy))
73 return -EFAULT;
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 to += copy;
76 len -= copy;
77 iov->iov_base += copy;
78 iov->iov_len -= copy;
79 }
80 return 0;
81}
82
Arjan van de Ven858119e2006-01-14 13:20:43 -080083static int
Jens Axboef6762b72006-05-01 20:02:05 +020084pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
85 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 unsigned long copy;
88
89 while (len > 0) {
90 while (!iov->iov_len)
91 iov++;
92 copy = min_t(unsigned long, len, iov->iov_len);
93
Jens Axboef6762b72006-05-01 20:02:05 +020094 if (atomic) {
95 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
96 return -EFAULT;
97 } else {
98 if (copy_to_user(iov->iov_base, from, copy))
99 return -EFAULT;
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 from += copy;
102 len -= copy;
103 iov->iov_base += copy;
104 iov->iov_len -= copy;
105 }
106 return 0;
107}
108
Jens Axboef6762b72006-05-01 20:02:05 +0200109/*
110 * Attempt to pre-fault in the user memory, so we can use atomic copies.
111 * Returns the number of bytes not faulted in.
112 */
113static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
114{
115 while (!iov->iov_len)
116 iov++;
117
118 while (len > 0) {
119 unsigned long this_len;
120
121 this_len = min_t(unsigned long, len, iov->iov_len);
122 if (fault_in_pages_writeable(iov->iov_base, this_len))
123 break;
124
125 len -= this_len;
126 iov++;
127 }
128
129 return len;
130}
131
132/*
133 * Pre-fault in the user memory, so we can use atomic copies.
134 */
135static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
136{
137 while (!iov->iov_len)
138 iov++;
139
140 while (len > 0) {
141 unsigned long this_len;
142
143 this_len = min_t(unsigned long, len, iov->iov_len);
144 fault_in_pages_readable(iov->iov_base, this_len);
145 len -= this_len;
146 iov++;
147 }
148}
149
Ingo Molnar341b4462006-04-11 13:57:45 +0200150static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
151 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct page *page = buf->page;
154
Jens Axboe5274f052006-03-30 15:15:30 +0200155 /*
156 * If nobody else uses this page, and we don't already have a
157 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200158 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200159 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200160 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200161 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200162 else
163 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jens Axboe08457182007-06-12 20:51:32 +0200166/**
167 * generic_pipe_buf_map - virtually map a pipe buffer
168 * @pipe: the pipe that the buffer belongs to
169 * @buf: the buffer that should be mapped
170 * @atomic: whether to use an atomic map
171 *
172 * Description:
173 * This function returns a kernel virtual address mapping for the
174 * passed in @pipe_buffer. If @atomic is set, an atomic map is provided
175 * and the caller has to be careful not to fault before calling
176 * the unmap function.
177 *
178 * Note that this function occupies KM_USER0 if @atomic != 0.
179 */
Jens Axboef84d7512006-05-01 19:59:03 +0200180void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200181 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Jens Axboef6762b72006-05-01 20:02:05 +0200183 if (atomic) {
184 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
185 return kmap_atomic(buf->page, KM_USER0);
186 }
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return kmap(buf->page);
189}
190
Jens Axboe08457182007-06-12 20:51:32 +0200191/**
192 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
193 * @pipe: the pipe that the buffer belongs to
194 * @buf: the buffer that should be unmapped
195 * @map_data: the data that the mapping function returned
196 *
197 * Description:
198 * This function undoes the mapping that ->map() provided.
199 */
Jens Axboef84d7512006-05-01 19:59:03 +0200200void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200201 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Jens Axboef6762b72006-05-01 20:02:05 +0200203 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
204 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
205 kunmap_atomic(map_data, KM_USER0);
206 } else
207 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Jens Axboe08457182007-06-12 20:51:32 +0200210/**
211 * generic_pipe_buf_steal - attempt to take ownership of a @pipe_buffer
212 * @pipe: the pipe that the buffer belongs to
213 * @buf: the buffer to attempt to steal
214 *
215 * Description:
216 * This function attempts to steal the @struct page attached to
217 * @buf. If successful, this function returns 0 and returns with
218 * the page locked. The caller may then reuse the page for whatever
219 * he wishes, the typical use is insertion into a different file
220 * page cache.
221 */
Jens Axboe330ab712006-05-02 15:29:57 +0200222int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
223 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200224{
Jens Axboe46e678c2006-04-30 16:36:32 +0200225 struct page *page = buf->page;
226
Jens Axboe08457182007-06-12 20:51:32 +0200227 /*
228 * A reference of one is golden, that means that the owner of this
229 * page is the only one holding a reference to it. lock the page
230 * and return OK.
231 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200232 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200233 lock_page(page);
234 return 0;
235 }
236
237 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200238}
239
Jens Axboe08457182007-06-12 20:51:32 +0200240/**
241 * generic_pipe_buf_get - get a reference to a @struct pipe_buffer
242 * @pipe: the pipe that the buffer belongs to
243 * @buf: the buffer to get a reference to
244 *
245 * Description:
246 * This function grabs an extra reference to @buf. It's used in
247 * in the tee() system call, when we duplicate the buffers in one
248 * pipe into another.
249 */
250void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200251{
252 page_cache_get(buf->page);
253}
254
Jens Axboe08457182007-06-12 20:51:32 +0200255/**
256 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200257 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200258 * @buf: the buffer to confirm
259 *
260 * Description:
261 * This function does nothing, because the generic pipe code uses
262 * pages that are always good when inserted into the pipe.
263 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200264int generic_pipe_buf_confirm(struct pipe_inode_info *info,
265 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200266{
267 return 0;
268}
269
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800270static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200272 .map = generic_pipe_buf_map,
273 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb02007-06-14 13:10:48 +0200274 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200276 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200277 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
280static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700281pipe_read(struct kiocb *iocb, const struct iovec *_iov,
282 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700284 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800285 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200286 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int do_wakeup;
288 ssize_t ret;
289 struct iovec *iov = (struct iovec *)_iov;
290 size_t total_len;
291
292 total_len = iov_length(iov, nr_segs);
293 /* Null read succeeds. */
294 if (unlikely(total_len == 0))
295 return 0;
296
297 do_wakeup = 0;
298 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200299 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200300 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200302 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200304 int curbuf = pipe->curbuf;
305 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800306 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 void *addr;
308 size_t chars = buf->len;
Jens Axboef6762b72006-05-01 20:02:05 +0200309 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 if (chars > total_len)
312 chars = total_len;
313
Jens Axboecac36bb02007-06-14 13:10:48 +0200314 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200315 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200316 if (!ret)
Jens Axboef84d7512006-05-01 19:59:03 +0200317 error = ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200318 break;
319 }
Jens Axboef84d7512006-05-01 19:59:03 +0200320
Jens Axboef6762b72006-05-01 20:02:05 +0200321 atomic = !iov_fault_in_pages_write(iov, chars);
322redo:
323 addr = ops->map(pipe, buf, atomic);
324 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
325 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200327 /*
328 * Just retry with the slow path if we failed.
329 */
330 if (atomic) {
331 atomic = 0;
332 goto redo;
333 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200334 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200335 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
337 }
338 ret += chars;
339 buf->offset += chars;
340 buf->len -= chars;
341 if (!buf->len) {
342 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200343 ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200345 pipe->curbuf = curbuf;
346 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 do_wakeup = 1;
348 }
349 total_len -= chars;
350 if (!total_len)
351 break; /* common path: read succeeded */
352 }
353 if (bufs) /* More to do? */
354 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200355 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200357 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* syscall merging: Usually we must not sleep
359 * if O_NONBLOCK is set, or if we got some data.
360 * But if a writer sleeps in kernel space, then
361 * we can wait for that data without violating POSIX.
362 */
363 if (ret)
364 break;
365 if (filp->f_flags & O_NONBLOCK) {
366 ret = -EAGAIN;
367 break;
368 }
369 }
370 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200371 if (!ret)
372 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374 }
375 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200376 wake_up_interruptible_sync(&pipe->wait);
377 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200379 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200381 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200382
383 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (do_wakeup) {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200385 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200386 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 if (ret > 0)
389 file_accessed(filp);
390 return ret;
391}
392
393static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700394pipe_write(struct kiocb *iocb, const struct iovec *_iov,
395 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700397 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800398 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200399 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 ssize_t ret;
401 int do_wakeup;
402 struct iovec *iov = (struct iovec *)_iov;
403 size_t total_len;
404 ssize_t chars;
405
406 total_len = iov_length(iov, nr_segs);
407 /* Null write succeeds. */
408 if (unlikely(total_len == 0))
409 return 0;
410
411 do_wakeup = 0;
412 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200413 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200414 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Ingo Molnar923f4f22006-04-11 13:53:33 +0200416 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 send_sig(SIGPIPE, current, 0);
418 ret = -EPIPE;
419 goto out;
420 }
421
422 /* We try to merge small writes */
423 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200424 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200425 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
426 (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200427 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800428 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200432 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200433 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200434
Jens Axboecac36bb02007-06-14 13:10:48 +0200435 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200436 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200437 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200438
Jens Axboef6762b72006-05-01 20:02:05 +0200439 iov_fault_in_pages_read(iov, chars);
440redo1:
441 addr = ops->map(pipe, buf, atomic);
Jens Axboe5274f052006-03-30 15:15:30 +0200442 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200443 chars, atomic);
444 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 ret = error;
446 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200447 if (error) {
448 if (atomic) {
449 atomic = 0;
450 goto redo1;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 buf->len += chars;
455 total_len -= chars;
456 ret = chars;
457 if (!total_len)
458 goto out;
459 }
460 }
461
462 for (;;) {
463 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200464
Ingo Molnar923f4f22006-04-11 13:53:33 +0200465 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200467 if (!ret)
468 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200471 bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (bufs < PIPE_BUFFERS) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200473 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
474 struct pipe_buffer *buf = pipe->bufs + newbuf;
475 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200476 char *src;
477 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (!page) {
480 page = alloc_page(GFP_HIGHUSER);
481 if (unlikely(!page)) {
482 ret = ret ? : -ENOMEM;
483 break;
484 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200485 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200487 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * we lock up (O_NONBLOCK-)readers that sleep due to
489 * syscall merging.
490 * FIXME! Is this really true?
491 */
492 do_wakeup = 1;
493 chars = PAGE_SIZE;
494 if (chars > total_len)
495 chars = total_len;
496
Jens Axboef6762b72006-05-01 20:02:05 +0200497 iov_fault_in_pages_read(iov, chars);
498redo2:
499 if (atomic)
500 src = kmap_atomic(page, KM_USER0);
501 else
502 src = kmap(page);
503
504 error = pipe_iov_copy_from_user(src, iov, chars,
505 atomic);
506 if (atomic)
507 kunmap_atomic(src, KM_USER0);
508 else
509 kunmap(page);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200512 if (atomic) {
513 atomic = 0;
514 goto redo2;
515 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200516 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200517 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
519 }
520 ret += chars;
521
522 /* Insert it into the buffer array */
523 buf->page = page;
524 buf->ops = &anon_pipe_buf_ops;
525 buf->offset = 0;
526 buf->len = chars;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200527 pipe->nrbufs = ++bufs;
528 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 total_len -= chars;
531 if (!total_len)
532 break;
533 }
534 if (bufs < PIPE_BUFFERS)
535 continue;
536 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200537 if (!ret)
538 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
540 }
541 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200542 if (!ret)
543 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
546 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200547 wake_up_interruptible_sync(&pipe->wait);
548 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 do_wakeup = 0;
550 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200551 pipe->waiting_writers++;
552 pipe_wait(pipe);
553 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200556 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (do_wakeup) {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200558 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200559 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800562 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return ret;
564}
565
566static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
568{
569 return -EBADF;
570}
571
572static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200573bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
574 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 return -EBADF;
577}
578
579static int
580pipe_ioctl(struct inode *pino, struct file *filp,
581 unsigned int cmd, unsigned long arg)
582{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800583 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200584 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int count, buf, nrbufs;
586
587 switch (cmd) {
588 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200589 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200590 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200592 buf = pipe->curbuf;
593 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200595 count += pipe->bufs[buf].len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 buf = (buf+1) & (PIPE_BUFFERS-1);
597 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200598 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return put_user(count, (int __user *)arg);
601 default:
602 return -EINVAL;
603 }
604}
605
606/* No kernel lock held - fine */
607static unsigned int
608pipe_poll(struct file *filp, poll_table *wait)
609{
610 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800611 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200612 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int nrbufs;
614
Ingo Molnar923f4f22006-04-11 13:53:33 +0200615 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200618 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 mask = 0;
620 if (filp->f_mode & FMODE_READ) {
621 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200622 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 mask |= POLLHUP;
624 }
625
626 if (filp->f_mode & FMODE_WRITE) {
627 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700628 /*
629 * Most Unices do not set POLLERR for FIFOs but on Linux they
630 * behave exactly like pipes for poll().
631 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200632 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 mask |= POLLERR;
634 }
635
636 return mask;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static int
640pipe_release(struct inode *inode, int decr, int decw)
641{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200642 struct pipe_inode_info *pipe;
643
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200644 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200645 pipe = inode->i_pipe;
646 pipe->readers -= decr;
647 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200648
Ingo Molnar923f4f22006-04-11 13:53:33 +0200649 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 free_pipe_info(inode);
651 } else {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200652 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200653 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
654 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200656 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 return 0;
659}
660
661static int
662pipe_read_fasync(int fd, struct file *filp, int on)
663{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800664 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int retval;
666
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200667 mutex_lock(&inode->i_mutex);
668 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
669 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (retval < 0)
672 return retval;
673
674 return 0;
675}
676
677
678static int
679pipe_write_fasync(int fd, struct file *filp, int on)
680{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800681 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int retval;
683
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200684 mutex_lock(&inode->i_mutex);
685 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
686 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (retval < 0)
689 return retval;
690
691 return 0;
692}
693
694
695static int
696pipe_rdwr_fasync(int fd, struct file *filp, int on)
697{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800698 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200699 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int retval;
701
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200702 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Ingo Molnar341b4462006-04-11 13:57:45 +0200704 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (retval >= 0)
Ingo Molnar341b4462006-04-11 13:57:45 +0200707 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200709 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 if (retval < 0)
712 return retval;
713
714 return 0;
715}
716
717
718static int
719pipe_read_release(struct inode *inode, struct file *filp)
720{
721 pipe_read_fasync(-1, filp, 0);
722 return pipe_release(inode, 1, 0);
723}
724
725static int
726pipe_write_release(struct inode *inode, struct file *filp)
727{
728 pipe_write_fasync(-1, filp, 0);
729 return pipe_release(inode, 0, 1);
730}
731
732static int
733pipe_rdwr_release(struct inode *inode, struct file *filp)
734{
735 int decr, decw;
736
737 pipe_rdwr_fasync(-1, filp, 0);
738 decr = (filp->f_mode & FMODE_READ) != 0;
739 decw = (filp->f_mode & FMODE_WRITE) != 0;
740 return pipe_release(inode, decr, decw);
741}
742
743static int
744pipe_read_open(struct inode *inode, struct file *filp)
745{
746 /* We could have perhaps used atomic_t, but this and friends
747 below are the only places. So it doesn't seem worthwhile. */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200748 mutex_lock(&inode->i_mutex);
749 inode->i_pipe->readers++;
750 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 return 0;
753}
754
755static int
756pipe_write_open(struct inode *inode, struct file *filp)
757{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200758 mutex_lock(&inode->i_mutex);
759 inode->i_pipe->writers++;
760 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 return 0;
763}
764
765static int
766pipe_rdwr_open(struct inode *inode, struct file *filp)
767{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200768 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (filp->f_mode & FMODE_READ)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200770 inode->i_pipe->readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (filp->f_mode & FMODE_WRITE)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200772 inode->i_pipe->writers++;
773 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 return 0;
776}
777
778/*
779 * The file_operations structs are not static because they
780 * are also used in linux/fs/fifo.c to do operations on FIFOs.
781 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800782const struct file_operations read_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700784 .read = do_sync_read,
785 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700787 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .ioctl = pipe_ioctl,
789 .open = pipe_read_open,
790 .release = pipe_read_release,
791 .fasync = pipe_read_fasync,
792};
793
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800794const struct file_operations write_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .llseek = no_llseek,
796 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700797 .write = do_sync_write,
798 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700799 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .ioctl = pipe_ioctl,
801 .open = pipe_write_open,
802 .release = pipe_write_release,
803 .fasync = pipe_write_fasync,
804};
805
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800806const struct file_operations rdwr_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700808 .read = do_sync_read,
809 .aio_read = pipe_read,
810 .write = do_sync_write,
811 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700812 .poll = pipe_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .ioctl = pipe_ioctl,
814 .open = pipe_rdwr_open,
815 .release = pipe_rdwr_release,
816 .fasync = pipe_rdwr_fasync,
817};
818
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800819static const struct file_operations read_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700821 .read = do_sync_read,
822 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .write = bad_pipe_w,
824 .poll = pipe_poll,
825 .ioctl = pipe_ioctl,
826 .open = pipe_read_open,
827 .release = pipe_read_release,
828 .fasync = pipe_read_fasync,
829};
830
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800831static const struct file_operations write_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .llseek = no_llseek,
833 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700834 .write = do_sync_write,
835 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .poll = pipe_poll,
837 .ioctl = pipe_ioctl,
838 .open = pipe_write_open,
839 .release = pipe_write_release,
840 .fasync = pipe_write_fasync,
841};
842
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800843static const struct file_operations rdwr_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700845 .read = do_sync_read,
846 .aio_read = pipe_read,
847 .write = do_sync_write,
848 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .poll = pipe_poll,
850 .ioctl = pipe_ioctl,
851 .open = pipe_rdwr_open,
852 .release = pipe_rdwr_release,
853 .fasync = pipe_rdwr_fasync,
854};
855
Ingo Molnar3a326a22006-04-10 15:18:35 +0200856struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
857{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200858 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200859
Ingo Molnar923f4f22006-04-11 13:53:33 +0200860 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
861 if (pipe) {
862 init_waitqueue_head(&pipe->wait);
863 pipe->r_counter = pipe->w_counter = 1;
864 pipe->inode = inode;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200865 }
866
Ingo Molnar923f4f22006-04-11 13:53:33 +0200867 return pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200868}
869
Ingo Molnar923f4f22006-04-11 13:53:33 +0200870void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 for (i = 0; i < PIPE_BUFFERS; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200875 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200877 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200879 if (pipe->tmp_page)
880 __free_page(pipe->tmp_page);
881 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Jens Axboeb92ce552006-04-11 13:52:07 +0200884void free_pipe_info(struct inode *inode)
885{
886 __free_pipe_info(inode->i_pipe);
887 inode->i_pipe = NULL;
888}
889
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800890static struct vfsmount *pipe_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891static int pipefs_delete_dentry(struct dentry *dentry)
892{
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800893 /*
894 * At creation time, we pretended this dentry was hashed
895 * (by clearing DCACHE_UNHASHED bit in d_flags)
896 * At delete time, we restore the truth : not hashed.
897 * (so that dput() can proceed correctly)
898 */
899 dentry->d_flags |= DCACHE_UNHASHED;
900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Ingo Molnar341b4462006-04-11 13:57:45 +0200902
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700903/*
904 * pipefs_dname() is called from d_path().
905 */
906static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
907{
908 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
909 dentry->d_inode->i_ino);
910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static struct dentry_operations pipefs_dentry_operations = {
913 .d_delete = pipefs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700914 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915};
916
917static struct inode * get_pipe_inode(void)
918{
919 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200920 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (!inode)
923 goto fail_inode;
924
Ingo Molnar923f4f22006-04-11 13:53:33 +0200925 pipe = alloc_pipe_info(inode);
926 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200928 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200929
Ingo Molnar923f4f22006-04-11 13:53:33 +0200930 pipe->readers = pipe->writers = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 inode->i_fop = &rdwr_pipe_fops;
932
933 /*
934 * Mark the inode dirty from the very beginning,
935 * that way it will never be moved to the dirty
936 * list because "mark_inode_dirty()" will think
937 * that it already _is_ on the dirty list.
938 */
939 inode->i_state = I_DIRTY;
940 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
941 inode->i_uid = current->fsuid;
942 inode->i_gid = current->fsgid;
943 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return inode;
946
947fail_iput:
948 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950fail_inode:
951 return NULL;
952}
953
Andi Kleend6cbd282006-09-30 23:29:26 -0700954struct file *create_write_pipe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Andi Kleend6cbd282006-09-30 23:29:26 -0700956 int err;
957 struct inode *inode;
958 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700960 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Andi Kleend6cbd282006-09-30 23:29:26 -0700962 f = get_empty_filp();
963 if (!f)
964 return ERR_PTR(-ENFILE);
965 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 inode = get_pipe_inode();
967 if (!inode)
Andi Kleend6cbd282006-09-30 23:29:26 -0700968 goto err_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Andi Kleend6cbd282006-09-30 23:29:26 -0700970 err = -ENOMEM;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700971 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700973 goto err_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 dentry->d_op = &pipefs_dentry_operations;
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800976 /*
977 * We dont want to publish this dentry into global dentry hash table.
978 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
979 * This permits a working /proc/$pid/fd/XXX on pipes
980 */
981 dentry->d_flags &= ~DCACHE_UNHASHED;
982 d_instantiate(dentry, inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800983 f->f_path.mnt = mntget(pipe_mnt);
984 f->f_path.dentry = dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -0700985 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Andi Kleend6cbd282006-09-30 23:29:26 -0700987 f->f_flags = O_WRONLY;
988 f->f_op = &write_pipe_fops;
989 f->f_mode = FMODE_WRITE;
990 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Andi Kleend6cbd282006-09-30 23:29:26 -0700992 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Andi Kleend6cbd282006-09-30 23:29:26 -0700994 err_inode:
995 free_pipe_info(inode);
996 iput(inode);
997 err_file:
998 put_filp(f);
999 return ERR_PTR(err);
1000}
1001
1002void free_write_pipe(struct file *f)
1003{
Al Viro5ccac88e2006-12-18 13:31:18 +00001004 free_pipe_info(f->f_dentry->d_inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001005 dput(f->f_path.dentry);
Al Viro5ccac88e2006-12-18 13:31:18 +00001006 mntput(f->f_path.mnt);
Andi Kleend6cbd282006-09-30 23:29:26 -07001007 put_filp(f);
1008}
1009
1010struct file *create_read_pipe(struct file *wrf)
1011{
1012 struct file *f = get_empty_filp();
1013 if (!f)
1014 return ERR_PTR(-ENFILE);
1015
1016 /* Grab pipe from the writer */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001017 f->f_path.mnt = mntget(wrf->f_path.mnt);
1018 f->f_path.dentry = dget(wrf->f_path.dentry);
1019 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
Andi Kleend6cbd282006-09-30 23:29:26 -07001020
1021 f->f_pos = 0;
1022 f->f_flags = O_RDONLY;
1023 f->f_op = &read_pipe_fops;
1024 f->f_mode = FMODE_READ;
1025 f->f_version = 0;
1026
1027 return f;
1028}
1029
1030int do_pipe(int *fd)
1031{
1032 struct file *fw, *fr;
1033 int error;
1034 int fdw, fdr;
1035
1036 fw = create_write_pipe();
1037 if (IS_ERR(fw))
1038 return PTR_ERR(fw);
1039 fr = create_read_pipe(fw);
1040 error = PTR_ERR(fr);
1041 if (IS_ERR(fr))
1042 goto err_write_pipe;
1043
1044 error = get_unused_fd();
1045 if (error < 0)
1046 goto err_read_pipe;
1047 fdr = error;
1048
1049 error = get_unused_fd();
1050 if (error < 0)
1051 goto err_fdr;
1052 fdw = error;
1053
Al Virodb349502007-02-07 01:48:00 -05001054 error = audit_fd_pair(fdr, fdw);
1055 if (error < 0)
1056 goto err_fdw;
1057
Andi Kleend6cbd282006-09-30 23:29:26 -07001058 fd_install(fdr, fr);
1059 fd_install(fdw, fw);
1060 fd[0] = fdr;
1061 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064
Al Virodb349502007-02-07 01:48:00 -05001065 err_fdw:
1066 put_unused_fd(fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001067 err_fdr:
1068 put_unused_fd(fdr);
1069 err_read_pipe:
Al Viro5ccac88e2006-12-18 13:31:18 +00001070 dput(fr->f_dentry);
1071 mntput(fr->f_vfsmnt);
Andi Kleend6cbd282006-09-30 23:29:26 -07001072 put_filp(fr);
1073 err_write_pipe:
1074 free_write_pipe(fw);
1075 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078/*
1079 * pipefs should _never_ be mounted by userland - too much of security hassle,
1080 * no real gain from having the whole whorehouse mounted. So we don't need
1081 * any operations on the root directory. However, we need a non-trivial
1082 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1083 */
David Howells454e2392006-06-23 02:02:57 -07001084static int pipefs_get_sb(struct file_system_type *fs_type,
1085 int flags, const char *dev_name, void *data,
1086 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
David Howells454e2392006-06-23 02:02:57 -07001088 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091static struct file_system_type pipe_fs_type = {
1092 .name = "pipefs",
1093 .get_sb = pipefs_get_sb,
1094 .kill_sb = kill_anon_super,
1095};
1096
1097static int __init init_pipe_fs(void)
1098{
1099 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (!err) {
1102 pipe_mnt = kern_mount(&pipe_fs_type);
1103 if (IS_ERR(pipe_mnt)) {
1104 err = PTR_ERR(pipe_mnt);
1105 unregister_filesystem(&pipe_fs_type);
1106 }
1107 }
1108 return err;
1109}
1110
1111static void __exit exit_pipe_fs(void)
1112{
1113 unregister_filesystem(&pipe_fs_type);
1114 mntput(pipe_mnt);
1115}
1116
1117fs_initcall(init_pipe_fs);
1118module_exit(exit_pipe_fs);