blob: ec228bc9f8824128ea99c5cb24abc743063e00bb [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>
Ulrich Drepperba719ba2008-05-06 20:42:38 -070020#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/uaccess.h>
23#include <asm/ioctls.h>
24
25/*
26 * We use a start+len construction, which provides full use of the
27 * allocated memory.
28 * -- Florian Coosmann (FGC)
29 *
30 * Reads with count = 0 should always return 0.
31 * -- Julian Bradfield 1999-06-07.
32 *
33 * FIFOs and Pipes now generate SIGIO for both readers and writers.
34 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
35 *
36 * pipe_read & write cleanup
37 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
38 */
39
40/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020041void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 DEFINE_WAIT(wait);
44
Ingo Molnard79fc0f2005-09-10 00:26:12 -070045 /*
46 * Pipes are system-local resources, so sleeping on them
47 * is considered a noninteractive wait:
48 */
Mike Galbraithaf927232007-10-15 17:00:13 +020049 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
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 Axboe08457182007-06-12 20:51:32 +0200167/**
168 * generic_pipe_buf_map - virtually map a pipe buffer
169 * @pipe: the pipe that the buffer belongs to
170 * @buf: the buffer that should be mapped
171 * @atomic: whether to use an atomic map
172 *
173 * Description:
174 * This function returns a kernel virtual address mapping for the
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800175 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
Jens Axboe08457182007-06-12 20:51:32 +0200176 * and the caller has to be careful not to fault before calling
177 * the unmap function.
178 *
179 * Note that this function occupies KM_USER0 if @atomic != 0.
180 */
Jens Axboef84d7512006-05-01 19:59:03 +0200181void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200182 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jens Axboef6762b72006-05-01 20:02:05 +0200184 if (atomic) {
185 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
186 return kmap_atomic(buf->page, KM_USER0);
187 }
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return kmap(buf->page);
190}
191
Jens Axboe08457182007-06-12 20:51:32 +0200192/**
193 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
194 * @pipe: the pipe that the buffer belongs to
195 * @buf: the buffer that should be unmapped
196 * @map_data: the data that the mapping function returned
197 *
198 * Description:
199 * This function undoes the mapping that ->map() provided.
200 */
Jens Axboef84d7512006-05-01 19:59:03 +0200201void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200202 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Jens Axboef6762b72006-05-01 20:02:05 +0200204 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
205 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
206 kunmap_atomic(map_data, KM_USER0);
207 } else
208 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Jens Axboe08457182007-06-12 20:51:32 +0200211/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800212 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200213 * @pipe: the pipe that the buffer belongs to
214 * @buf: the buffer to attempt to steal
215 *
216 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800217 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200218 * @buf. If successful, this function returns 0 and returns with
219 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800220 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200221 * page cache.
222 */
Jens Axboe330ab712006-05-02 15:29:57 +0200223int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
224 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200225{
Jens Axboe46e678c2006-04-30 16:36:32 +0200226 struct page *page = buf->page;
227
Jens Axboe08457182007-06-12 20:51:32 +0200228 /*
229 * A reference of one is golden, that means that the owner of this
230 * page is the only one holding a reference to it. lock the page
231 * and return OK.
232 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200233 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200234 lock_page(page);
235 return 0;
236 }
237
238 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200239}
240
Jens Axboe08457182007-06-12 20:51:32 +0200241/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800242 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200243 * @pipe: the pipe that the buffer belongs to
244 * @buf: the buffer to get a reference to
245 *
246 * Description:
247 * This function grabs an extra reference to @buf. It's used in
248 * in the tee() system call, when we duplicate the buffers in one
249 * pipe into another.
250 */
251void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200252{
253 page_cache_get(buf->page);
254}
255
Jens Axboe08457182007-06-12 20:51:32 +0200256/**
257 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200258 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200259 * @buf: the buffer to confirm
260 *
261 * Description:
262 * This function does nothing, because the generic pipe code uses
263 * pages that are always good when inserted into the pipe.
264 */
Jens Axboecac36bb2007-06-14 13:10:48 +0200265int generic_pipe_buf_confirm(struct pipe_inode_info *info,
266 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200267{
268 return 0;
269}
270
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800271static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200273 .map = generic_pipe_buf_map,
274 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb2007-06-14 13:10:48 +0200275 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200277 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200278 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
281static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700282pipe_read(struct kiocb *iocb, const struct iovec *_iov,
283 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700285 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800286 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200287 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int do_wakeup;
289 ssize_t ret;
290 struct iovec *iov = (struct iovec *)_iov;
291 size_t total_len;
292
293 total_len = iov_length(iov, nr_segs);
294 /* Null read succeeds. */
295 if (unlikely(total_len == 0))
296 return 0;
297
298 do_wakeup = 0;
299 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200300 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200301 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200303 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200305 int curbuf = pipe->curbuf;
306 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800307 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 void *addr;
309 size_t chars = buf->len;
Jens Axboef6762b72006-05-01 20:02:05 +0200310 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (chars > total_len)
313 chars = total_len;
314
Jens Axboecac36bb2007-06-14 13:10:48 +0200315 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200316 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200317 if (!ret)
Jens Axboef84d7512006-05-01 19:59:03 +0200318 error = ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200319 break;
320 }
Jens Axboef84d7512006-05-01 19:59:03 +0200321
Jens Axboef6762b72006-05-01 20:02:05 +0200322 atomic = !iov_fault_in_pages_write(iov, chars);
323redo:
324 addr = ops->map(pipe, buf, atomic);
325 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
326 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200328 /*
329 * Just retry with the slow path if we failed.
330 */
331 if (atomic) {
332 atomic = 0;
333 goto redo;
334 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200335 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200336 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
338 }
339 ret += chars;
340 buf->offset += chars;
341 buf->len -= chars;
342 if (!buf->len) {
343 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200344 ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200346 pipe->curbuf = curbuf;
347 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 do_wakeup = 1;
349 }
350 total_len -= chars;
351 if (!total_len)
352 break; /* common path: read succeeded */
353 }
354 if (bufs) /* More to do? */
355 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200356 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200358 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* syscall merging: Usually we must not sleep
360 * if O_NONBLOCK is set, or if we got some data.
361 * But if a writer sleeps in kernel space, then
362 * we can wait for that data without violating POSIX.
363 */
364 if (ret)
365 break;
366 if (filp->f_flags & O_NONBLOCK) {
367 ret = -EAGAIN;
368 break;
369 }
370 }
371 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200372 if (!ret)
373 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375 }
376 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200377 wake_up_interruptible_sync(&pipe->wait);
378 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200380 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200382 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200383
384 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (do_wakeup) {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200386 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200387 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 if (ret > 0)
390 file_accessed(filp);
391 return ret;
392}
393
394static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700395pipe_write(struct kiocb *iocb, const struct iovec *_iov,
396 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700398 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800399 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200400 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ssize_t ret;
402 int do_wakeup;
403 struct iovec *iov = (struct iovec *)_iov;
404 size_t total_len;
405 ssize_t chars;
406
407 total_len = iov_length(iov, nr_segs);
408 /* Null write succeeds. */
409 if (unlikely(total_len == 0))
410 return 0;
411
412 do_wakeup = 0;
413 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200414 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200415 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Ingo Molnar923f4f22006-04-11 13:53:33 +0200417 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 send_sig(SIGPIPE, current, 0);
419 ret = -EPIPE;
420 goto out;
421 }
422
423 /* We try to merge small writes */
424 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200425 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200426 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
427 (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200428 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800429 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200433 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200434 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200435
Jens Axboecac36bb2007-06-14 13:10:48 +0200436 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200437 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200438 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200439
Jens Axboef6762b72006-05-01 20:02:05 +0200440 iov_fault_in_pages_read(iov, chars);
441redo1:
442 addr = ops->map(pipe, buf, atomic);
Jens Axboe5274f052006-03-30 15:15:30 +0200443 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200444 chars, atomic);
445 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ret = error;
447 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200448 if (error) {
449 if (atomic) {
450 atomic = 0;
451 goto redo1;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 buf->len += chars;
456 total_len -= chars;
457 ret = chars;
458 if (!total_len)
459 goto out;
460 }
461 }
462
463 for (;;) {
464 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200465
Ingo Molnar923f4f22006-04-11 13:53:33 +0200466 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200468 if (!ret)
469 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200472 bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (bufs < PIPE_BUFFERS) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200474 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
475 struct pipe_buffer *buf = pipe->bufs + newbuf;
476 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200477 char *src;
478 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (!page) {
481 page = alloc_page(GFP_HIGHUSER);
482 if (unlikely(!page)) {
483 ret = ret ? : -ENOMEM;
484 break;
485 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200486 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200488 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * we lock up (O_NONBLOCK-)readers that sleep due to
490 * syscall merging.
491 * FIXME! Is this really true?
492 */
493 do_wakeup = 1;
494 chars = PAGE_SIZE;
495 if (chars > total_len)
496 chars = total_len;
497
Jens Axboef6762b72006-05-01 20:02:05 +0200498 iov_fault_in_pages_read(iov, chars);
499redo2:
500 if (atomic)
501 src = kmap_atomic(page, KM_USER0);
502 else
503 src = kmap(page);
504
505 error = pipe_iov_copy_from_user(src, iov, chars,
506 atomic);
507 if (atomic)
508 kunmap_atomic(src, KM_USER0);
509 else
510 kunmap(page);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200513 if (atomic) {
514 atomic = 0;
515 goto redo2;
516 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200517 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200518 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520 }
521 ret += chars;
522
523 /* Insert it into the buffer array */
524 buf->page = page;
525 buf->ops = &anon_pipe_buf_ops;
526 buf->offset = 0;
527 buf->len = chars;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200528 pipe->nrbufs = ++bufs;
529 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 total_len -= chars;
532 if (!total_len)
533 break;
534 }
535 if (bufs < PIPE_BUFFERS)
536 continue;
537 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200538 if (!ret)
539 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
541 }
542 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200543 if (!ret)
544 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
546 }
547 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200548 wake_up_interruptible_sync(&pipe->wait);
549 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 do_wakeup = 0;
551 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200552 pipe->waiting_writers++;
553 pipe_wait(pipe);
554 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200557 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (do_wakeup) {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200559 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200560 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800563 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return ret;
565}
566
567static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
569{
570 return -EBADF;
571}
572
573static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200574bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
575 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 return -EBADF;
578}
579
Andi Kleend59d0b12008-02-08 04:21:23 -0800580static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800582 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200583 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int count, buf, nrbufs;
585
586 switch (cmd) {
587 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200588 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200589 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200591 buf = pipe->curbuf;
592 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200594 count += pipe->bufs[buf].len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 buf = (buf+1) & (PIPE_BUFFERS-1);
596 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200597 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return put_user(count, (int __user *)arg);
600 default:
601 return -EINVAL;
602 }
603}
604
605/* No kernel lock held - fine */
606static unsigned int
607pipe_poll(struct file *filp, poll_table *wait)
608{
609 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800610 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200611 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int nrbufs;
613
Ingo Molnar923f4f22006-04-11 13:53:33 +0200614 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200617 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 mask = 0;
619 if (filp->f_mode & FMODE_READ) {
620 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200621 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 mask |= POLLHUP;
623 }
624
625 if (filp->f_mode & FMODE_WRITE) {
626 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700627 /*
628 * Most Unices do not set POLLERR for FIFOs but on Linux they
629 * behave exactly like pipes for poll().
630 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200631 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 mask |= POLLERR;
633 }
634
635 return mask;
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static int
639pipe_release(struct inode *inode, int decr, int decw)
640{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200641 struct pipe_inode_info *pipe;
642
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200643 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200644 pipe = inode->i_pipe;
645 pipe->readers -= decr;
646 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200647
Ingo Molnar923f4f22006-04-11 13:53:33 +0200648 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 free_pipe_info(inode);
650 } else {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200651 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200652 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
653 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200655 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 return 0;
658}
659
660static int
661pipe_read_fasync(int fd, struct file *filp, int on)
662{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800663 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int retval;
665
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200666 mutex_lock(&inode->i_mutex);
667 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
668 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (retval < 0)
671 return retval;
672
673 return 0;
674}
675
676
677static int
678pipe_write_fasync(int fd, struct file *filp, int on)
679{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800680 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int retval;
682
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200683 mutex_lock(&inode->i_mutex);
684 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
685 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (retval < 0)
688 return retval;
689
690 return 0;
691}
692
693
694static int
695pipe_rdwr_fasync(int fd, struct file *filp, int on)
696{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800697 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200698 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int retval;
700
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200701 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Ingo Molnar341b4462006-04-11 13:57:45 +0200703 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (retval >= 0)
Ingo Molnar341b4462006-04-11 13:57:45 +0200706 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200708 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (retval < 0)
711 return retval;
712
713 return 0;
714}
715
716
717static int
718pipe_read_release(struct inode *inode, struct file *filp)
719{
720 pipe_read_fasync(-1, filp, 0);
721 return pipe_release(inode, 1, 0);
722}
723
724static int
725pipe_write_release(struct inode *inode, struct file *filp)
726{
727 pipe_write_fasync(-1, filp, 0);
728 return pipe_release(inode, 0, 1);
729}
730
731static int
732pipe_rdwr_release(struct inode *inode, struct file *filp)
733{
734 int decr, decw;
735
736 pipe_rdwr_fasync(-1, filp, 0);
737 decr = (filp->f_mode & FMODE_READ) != 0;
738 decw = (filp->f_mode & FMODE_WRITE) != 0;
739 return pipe_release(inode, decr, decw);
740}
741
742static int
743pipe_read_open(struct inode *inode, struct file *filp)
744{
745 /* We could have perhaps used atomic_t, but this and friends
746 below are the only places. So it doesn't seem worthwhile. */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200747 mutex_lock(&inode->i_mutex);
748 inode->i_pipe->readers++;
749 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 return 0;
752}
753
754static int
755pipe_write_open(struct inode *inode, struct file *filp)
756{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200757 mutex_lock(&inode->i_mutex);
758 inode->i_pipe->writers++;
759 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 return 0;
762}
763
764static int
765pipe_rdwr_open(struct inode *inode, struct file *filp)
766{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200767 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (filp->f_mode & FMODE_READ)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200769 inode->i_pipe->readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (filp->f_mode & FMODE_WRITE)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200771 inode->i_pipe->writers++;
772 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 return 0;
775}
776
777/*
778 * The file_operations structs are not static because they
779 * are also used in linux/fs/fifo.c to do operations on FIFOs.
780 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800781const struct file_operations read_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700783 .read = do_sync_read,
784 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700786 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800787 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .open = pipe_read_open,
789 .release = pipe_read_release,
790 .fasync = pipe_read_fasync,
791};
792
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800793const struct file_operations write_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .llseek = no_llseek,
795 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700796 .write = do_sync_write,
797 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700798 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800799 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .open = pipe_write_open,
801 .release = pipe_write_release,
802 .fasync = pipe_write_fasync,
803};
804
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800805const struct file_operations rdwr_fifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700807 .read = do_sync_read,
808 .aio_read = pipe_read,
809 .write = do_sync_write,
810 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700811 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800812 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .open = pipe_rdwr_open,
814 .release = pipe_rdwr_release,
815 .fasync = pipe_rdwr_fasync,
816};
817
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800818static const struct file_operations read_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700820 .read = do_sync_read,
821 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .write = bad_pipe_w,
823 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800824 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .open = pipe_read_open,
826 .release = pipe_read_release,
827 .fasync = pipe_read_fasync,
828};
829
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800830static const struct file_operations write_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .llseek = no_llseek,
832 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700833 .write = do_sync_write,
834 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800836 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 .open = pipe_write_open,
838 .release = pipe_write_release,
839 .fasync = pipe_write_fasync,
840};
841
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800842static const struct file_operations rdwr_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700844 .read = do_sync_read,
845 .aio_read = pipe_read,
846 .write = do_sync_write,
847 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800849 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .open = pipe_rdwr_open,
851 .release = pipe_rdwr_release,
852 .fasync = pipe_rdwr_fasync,
853};
854
Ingo Molnar3a326a22006-04-10 15:18:35 +0200855struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
856{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200857 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200858
Ingo Molnar923f4f22006-04-11 13:53:33 +0200859 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
860 if (pipe) {
861 init_waitqueue_head(&pipe->wait);
862 pipe->r_counter = pipe->w_counter = 1;
863 pipe->inode = inode;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200864 }
865
Ingo Molnar923f4f22006-04-11 13:53:33 +0200866 return pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200867}
868
Ingo Molnar923f4f22006-04-11 13:53:33 +0200869void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 for (i = 0; i < PIPE_BUFFERS; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200874 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200876 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200878 if (pipe->tmp_page)
879 __free_page(pipe->tmp_page);
880 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Jens Axboeb92ce552006-04-11 13:52:07 +0200883void free_pipe_info(struct inode *inode)
884{
885 __free_pipe_info(inode->i_pipe);
886 inode->i_pipe = NULL;
887}
888
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800889static struct vfsmount *pipe_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890static int pipefs_delete_dentry(struct dentry *dentry)
891{
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800892 /*
893 * At creation time, we pretended this dentry was hashed
894 * (by clearing DCACHE_UNHASHED bit in d_flags)
895 * At delete time, we restore the truth : not hashed.
896 * (so that dput() can proceed correctly)
897 */
898 dentry->d_flags |= DCACHE_UNHASHED;
899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
Ingo Molnar341b4462006-04-11 13:57:45 +0200901
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700902/*
903 * pipefs_dname() is called from d_path().
904 */
905static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
906{
907 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
908 dentry->d_inode->i_ino);
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static struct dentry_operations pipefs_dentry_operations = {
912 .d_delete = pipefs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700913 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914};
915
916static struct inode * get_pipe_inode(void)
917{
918 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200919 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!inode)
922 goto fail_inode;
923
Ingo Molnar923f4f22006-04-11 13:53:33 +0200924 pipe = alloc_pipe_info(inode);
925 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200927 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200928
Ingo Molnar923f4f22006-04-11 13:53:33 +0200929 pipe->readers = pipe->writers = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 inode->i_fop = &rdwr_pipe_fops;
931
932 /*
933 * Mark the inode dirty from the very beginning,
934 * that way it will never be moved to the dirty
935 * list because "mark_inode_dirty()" will think
936 * that it already _is_ on the dirty list.
937 */
938 inode->i_state = I_DIRTY;
939 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
940 inode->i_uid = current->fsuid;
941 inode->i_gid = current->fsgid;
942 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return inode;
945
946fail_iput:
947 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949fail_inode:
950 return NULL;
951}
952
Andi Kleend6cbd282006-09-30 23:29:26 -0700953struct file *create_write_pipe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Andi Kleend6cbd282006-09-30 23:29:26 -0700955 int err;
956 struct inode *inode;
957 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700959 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Andi Kleend6cbd282006-09-30 23:29:26 -0700961 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 inode = get_pipe_inode();
963 if (!inode)
Dave Hansen430e2852008-02-15 14:37:26 -0800964 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Andi Kleend6cbd282006-09-30 23:29:26 -0700966 err = -ENOMEM;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700967 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700969 goto err_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 dentry->d_op = &pipefs_dentry_operations;
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800972 /*
973 * We dont want to publish this dentry into global dentry hash table.
974 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
975 * This permits a working /proc/$pid/fd/XXX on pipes
976 */
977 dentry->d_flags &= ~DCACHE_UNHASHED;
978 d_instantiate(dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800979
980 err = -ENFILE;
981 f = alloc_file(pipe_mnt, dentry, FMODE_WRITE, &write_pipe_fops);
982 if (!f)
983 goto err_dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -0700984 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Andi Kleend6cbd282006-09-30 23:29:26 -0700986 f->f_flags = O_WRONLY;
Andi Kleend6cbd282006-09-30 23:29:26 -0700987 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Andi Kleend6cbd282006-09-30 23:29:26 -0700989 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dave Hansen430e2852008-02-15 14:37:26 -0800991 err_dentry:
Al Viroed152432008-04-22 19:51:27 -0400992 free_pipe_info(inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800993 dput(dentry);
Al Viroed152432008-04-22 19:51:27 -0400994 return ERR_PTR(err);
995
Andi Kleend6cbd282006-09-30 23:29:26 -0700996 err_inode:
997 free_pipe_info(inode);
998 iput(inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800999 err:
Andi Kleend6cbd282006-09-30 23:29:26 -07001000 return ERR_PTR(err);
1001}
1002
1003void free_write_pipe(struct file *f)
1004{
Al Viro5ccac882006-12-18 13:31:18 +00001005 free_pipe_info(f->f_dentry->d_inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001006 dput(f->f_path.dentry);
Al Viro5ccac882006-12-18 13:31:18 +00001007 mntput(f->f_path.mnt);
Andi Kleend6cbd282006-09-30 23:29:26 -07001008 put_filp(f);
1009}
1010
1011struct file *create_read_pipe(struct file *wrf)
1012{
1013 struct file *f = get_empty_filp();
1014 if (!f)
1015 return ERR_PTR(-ENFILE);
1016
1017 /* Grab pipe from the writer */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001018 f->f_path.mnt = mntget(wrf->f_path.mnt);
1019 f->f_path.dentry = dget(wrf->f_path.dentry);
1020 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
Andi Kleend6cbd282006-09-30 23:29:26 -07001021
1022 f->f_pos = 0;
1023 f->f_flags = O_RDONLY;
1024 f->f_op = &read_pipe_fops;
1025 f->f_mode = FMODE_READ;
1026 f->f_version = 0;
1027
1028 return f;
1029}
1030
1031int do_pipe(int *fd)
1032{
1033 struct file *fw, *fr;
1034 int error;
1035 int fdw, fdr;
1036
1037 fw = create_write_pipe();
1038 if (IS_ERR(fw))
1039 return PTR_ERR(fw);
1040 fr = create_read_pipe(fw);
1041 error = PTR_ERR(fr);
1042 if (IS_ERR(fr))
1043 goto err_write_pipe;
1044
1045 error = get_unused_fd();
1046 if (error < 0)
1047 goto err_read_pipe;
1048 fdr = error;
1049
1050 error = get_unused_fd();
1051 if (error < 0)
1052 goto err_fdr;
1053 fdw = error;
1054
Al Virodb349502007-02-07 01:48:00 -05001055 error = audit_fd_pair(fdr, fdw);
1056 if (error < 0)
1057 goto err_fdw;
1058
Andi Kleend6cbd282006-09-30 23:29:26 -07001059 fd_install(fdr, fr);
1060 fd_install(fdw, fw);
1061 fd[0] = fdr;
1062 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065
Al Virodb349502007-02-07 01:48:00 -05001066 err_fdw:
1067 put_unused_fd(fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001068 err_fdr:
1069 put_unused_fd(fdr);
1070 err_read_pipe:
Al Viro5ccac882006-12-18 13:31:18 +00001071 dput(fr->f_dentry);
1072 mntput(fr->f_vfsmnt);
Andi Kleend6cbd282006-09-30 23:29:26 -07001073 put_filp(fr);
1074 err_write_pipe:
1075 free_write_pipe(fw);
1076 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001080 * sys_pipe() is the normal C calling standard for creating
1081 * a pipe. It's not the way Unix traditionally does this, though.
1082 */
1083asmlinkage long __weak sys_pipe(int __user *fildes)
1084{
1085 int fd[2];
1086 int error;
1087
1088 error = do_pipe(fd);
1089 if (!error) {
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001090 if (copy_to_user(fildes, fd, sizeof(fd))) {
1091 sys_close(fd[0]);
1092 sys_close(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001093 error = -EFAULT;
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001094 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001095 }
1096 return error;
1097}
1098
1099/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 * pipefs should _never_ be mounted by userland - too much of security hassle,
1101 * no real gain from having the whole whorehouse mounted. So we don't need
1102 * any operations on the root directory. However, we need a non-trivial
1103 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1104 */
David Howells454e2392006-06-23 02:02:57 -07001105static int pipefs_get_sb(struct file_system_type *fs_type,
1106 int flags, const char *dev_name, void *data,
1107 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
David Howells454e2392006-06-23 02:02:57 -07001109 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112static struct file_system_type pipe_fs_type = {
1113 .name = "pipefs",
1114 .get_sb = pipefs_get_sb,
1115 .kill_sb = kill_anon_super,
1116};
1117
1118static int __init init_pipe_fs(void)
1119{
1120 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!err) {
1123 pipe_mnt = kern_mount(&pipe_fs_type);
1124 if (IS_ERR(pipe_mnt)) {
1125 err = PTR_ERR(pipe_mnt);
1126 unregister_filesystem(&pipe_fs_type);
1127 }
1128 }
1129 return err;
1130}
1131
1132static void __exit exit_pipe_fs(void)
1133{
1134 unregister_filesystem(&pipe_fs_type);
1135 mntput(pipe_mnt);
1136}
1137
1138fs_initcall(init_pipe_fs);
1139module_exit(exit_pipe_fs);