blob: 13414ec45b8d5b42012d8c5fbd56d54df5a96749 [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
Miklos Szeredi61e0d472009-04-14 19:48:41 +020040static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
41{
42 if (pipe->inode)
43 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
44}
45
46void pipe_lock(struct pipe_inode_info *pipe)
47{
48 /*
49 * pipe_lock() nests non-pipe inode locks (for writing to a file)
50 */
51 pipe_lock_nested(pipe, I_MUTEX_PARENT);
52}
53EXPORT_SYMBOL(pipe_lock);
54
55void pipe_unlock(struct pipe_inode_info *pipe)
56{
57 if (pipe->inode)
58 mutex_unlock(&pipe->inode->i_mutex);
59}
60EXPORT_SYMBOL(pipe_unlock);
61
62void pipe_double_lock(struct pipe_inode_info *pipe1,
63 struct pipe_inode_info *pipe2)
64{
65 BUG_ON(pipe1 == pipe2);
66
67 if (pipe1 < pipe2) {
68 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
69 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
70 } else {
71 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
72 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
73 }
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020077void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 DEFINE_WAIT(wait);
80
Ingo Molnard79fc0f2005-09-10 00:26:12 -070081 /*
82 * Pipes are system-local resources, so sleeping on them
83 * is considered a noninteractive wait:
84 */
Mike Galbraithaf927232007-10-15 17:00:13 +020085 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020086 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +020088 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020089 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Arjan van de Ven858119e2006-01-14 13:20:43 -080092static int
Jens Axboef6762b72006-05-01 20:02:05 +020093pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
94 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 unsigned long copy;
97
98 while (len > 0) {
99 while (!iov->iov_len)
100 iov++;
101 copy = min_t(unsigned long, len, iov->iov_len);
102
Jens Axboef6762b72006-05-01 20:02:05 +0200103 if (atomic) {
104 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
105 return -EFAULT;
106 } else {
107 if (copy_from_user(to, iov->iov_base, copy))
108 return -EFAULT;
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 to += copy;
111 len -= copy;
112 iov->iov_base += copy;
113 iov->iov_len -= copy;
114 }
115 return 0;
116}
117
Arjan van de Ven858119e2006-01-14 13:20:43 -0800118static int
Jens Axboef6762b72006-05-01 20:02:05 +0200119pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
120 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 unsigned long copy;
123
124 while (len > 0) {
125 while (!iov->iov_len)
126 iov++;
127 copy = min_t(unsigned long, len, iov->iov_len);
128
Jens Axboef6762b72006-05-01 20:02:05 +0200129 if (atomic) {
130 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
131 return -EFAULT;
132 } else {
133 if (copy_to_user(iov->iov_base, from, copy))
134 return -EFAULT;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 from += copy;
137 len -= copy;
138 iov->iov_base += copy;
139 iov->iov_len -= copy;
140 }
141 return 0;
142}
143
Jens Axboef6762b72006-05-01 20:02:05 +0200144/*
145 * Attempt to pre-fault in the user memory, so we can use atomic copies.
146 * Returns the number of bytes not faulted in.
147 */
148static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
149{
150 while (!iov->iov_len)
151 iov++;
152
153 while (len > 0) {
154 unsigned long this_len;
155
156 this_len = min_t(unsigned long, len, iov->iov_len);
157 if (fault_in_pages_writeable(iov->iov_base, this_len))
158 break;
159
160 len -= this_len;
161 iov++;
162 }
163
164 return len;
165}
166
167/*
168 * Pre-fault in the user memory, so we can use atomic copies.
169 */
170static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
171{
172 while (!iov->iov_len)
173 iov++;
174
175 while (len > 0) {
176 unsigned long this_len;
177
178 this_len = min_t(unsigned long, len, iov->iov_len);
179 fault_in_pages_readable(iov->iov_base, this_len);
180 len -= this_len;
181 iov++;
182 }
183}
184
Ingo Molnar341b4462006-04-11 13:57:45 +0200185static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
186 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct page *page = buf->page;
189
Jens Axboe5274f052006-03-30 15:15:30 +0200190 /*
191 * If nobody else uses this page, and we don't already have a
192 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200193 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200194 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200195 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200196 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200197 else
198 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Jens Axboe08457182007-06-12 20:51:32 +0200201/**
202 * generic_pipe_buf_map - virtually map a pipe buffer
203 * @pipe: the pipe that the buffer belongs to
204 * @buf: the buffer that should be mapped
205 * @atomic: whether to use an atomic map
206 *
207 * Description:
208 * This function returns a kernel virtual address mapping for the
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800209 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
Jens Axboe08457182007-06-12 20:51:32 +0200210 * and the caller has to be careful not to fault before calling
211 * the unmap function.
212 *
213 * Note that this function occupies KM_USER0 if @atomic != 0.
214 */
Jens Axboef84d7512006-05-01 19:59:03 +0200215void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200216 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Jens Axboef6762b72006-05-01 20:02:05 +0200218 if (atomic) {
219 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
220 return kmap_atomic(buf->page, KM_USER0);
221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return kmap(buf->page);
224}
225
Jens Axboe08457182007-06-12 20:51:32 +0200226/**
227 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
228 * @pipe: the pipe that the buffer belongs to
229 * @buf: the buffer that should be unmapped
230 * @map_data: the data that the mapping function returned
231 *
232 * Description:
233 * This function undoes the mapping that ->map() provided.
234 */
Jens Axboef84d7512006-05-01 19:59:03 +0200235void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200236 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Jens Axboef6762b72006-05-01 20:02:05 +0200238 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
239 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
240 kunmap_atomic(map_data, KM_USER0);
241 } else
242 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Jens Axboe08457182007-06-12 20:51:32 +0200245/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800246 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200247 * @pipe: the pipe that the buffer belongs to
248 * @buf: the buffer to attempt to steal
249 *
250 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800251 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200252 * @buf. If successful, this function returns 0 and returns with
253 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800254 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200255 * page cache.
256 */
Jens Axboe330ab712006-05-02 15:29:57 +0200257int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
258 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200259{
Jens Axboe46e678c2006-04-30 16:36:32 +0200260 struct page *page = buf->page;
261
Jens Axboe08457182007-06-12 20:51:32 +0200262 /*
263 * A reference of one is golden, that means that the owner of this
264 * page is the only one holding a reference to it. lock the page
265 * and return OK.
266 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200267 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200268 lock_page(page);
269 return 0;
270 }
271
272 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200273}
274
Jens Axboe08457182007-06-12 20:51:32 +0200275/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800276 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200277 * @pipe: the pipe that the buffer belongs to
278 * @buf: the buffer to get a reference to
279 *
280 * Description:
281 * This function grabs an extra reference to @buf. It's used in
282 * in the tee() system call, when we duplicate the buffers in one
283 * pipe into another.
284 */
285void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200286{
287 page_cache_get(buf->page);
288}
289
Jens Axboe08457182007-06-12 20:51:32 +0200290/**
291 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200292 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200293 * @buf: the buffer to confirm
294 *
295 * Description:
296 * This function does nothing, because the generic pipe code uses
297 * pages that are always good when inserted into the pipe.
298 */
Jens Axboecac36bb2007-06-14 13:10:48 +0200299int generic_pipe_buf_confirm(struct pipe_inode_info *info,
300 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200301{
302 return 0;
303}
304
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800305static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200307 .map = generic_pipe_buf_map,
308 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb2007-06-14 13:10:48 +0200309 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200311 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200312 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
315static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700316pipe_read(struct kiocb *iocb, const struct iovec *_iov,
317 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700319 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800320 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200321 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int do_wakeup;
323 ssize_t ret;
324 struct iovec *iov = (struct iovec *)_iov;
325 size_t total_len;
326
327 total_len = iov_length(iov, nr_segs);
328 /* Null read succeeds. */
329 if (unlikely(total_len == 0))
330 return 0;
331
332 do_wakeup = 0;
333 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200334 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200335 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200337 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200339 int curbuf = pipe->curbuf;
340 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800341 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 void *addr;
343 size_t chars = buf->len;
Jens Axboef6762b72006-05-01 20:02:05 +0200344 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (chars > total_len)
347 chars = total_len;
348
Jens Axboecac36bb2007-06-14 13:10:48 +0200349 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200350 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200351 if (!ret)
Jens Axboef84d7512006-05-01 19:59:03 +0200352 error = ret;
Jens Axboe5274f052006-03-30 15:15:30 +0200353 break;
354 }
Jens Axboef84d7512006-05-01 19:59:03 +0200355
Jens Axboef6762b72006-05-01 20:02:05 +0200356 atomic = !iov_fault_in_pages_write(iov, chars);
357redo:
358 addr = ops->map(pipe, buf, atomic);
359 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
360 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200362 /*
363 * Just retry with the slow path if we failed.
364 */
365 if (atomic) {
366 atomic = 0;
367 goto redo;
368 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200369 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200370 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 break;
372 }
373 ret += chars;
374 buf->offset += chars;
375 buf->len -= chars;
376 if (!buf->len) {
377 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200378 ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200380 pipe->curbuf = curbuf;
381 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 do_wakeup = 1;
383 }
384 total_len -= chars;
385 if (!total_len)
386 break; /* common path: read succeeded */
387 }
388 if (bufs) /* More to do? */
389 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200390 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200392 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* syscall merging: Usually we must not sleep
394 * if O_NONBLOCK is set, or if we got some data.
395 * But if a writer sleeps in kernel space, then
396 * we can wait for that data without violating POSIX.
397 */
398 if (ret)
399 break;
400 if (filp->f_flags & O_NONBLOCK) {
401 ret = -EAGAIN;
402 break;
403 }
404 }
405 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200406 if (!ret)
407 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
409 }
410 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200411 wake_up_interruptible_sync(&pipe->wait);
412 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200414 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200416 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200417
418 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (do_wakeup) {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200420 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200421 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 if (ret > 0)
424 file_accessed(filp);
425 return ret;
426}
427
428static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700429pipe_write(struct kiocb *iocb, const struct iovec *_iov,
430 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700432 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800433 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200434 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 ssize_t ret;
436 int do_wakeup;
437 struct iovec *iov = (struct iovec *)_iov;
438 size_t total_len;
439 ssize_t chars;
440
441 total_len = iov_length(iov, nr_segs);
442 /* Null write succeeds. */
443 if (unlikely(total_len == 0))
444 return 0;
445
446 do_wakeup = 0;
447 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200448 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200449 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Ingo Molnar923f4f22006-04-11 13:53:33 +0200451 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 send_sig(SIGPIPE, current, 0);
453 ret = -EPIPE;
454 goto out;
455 }
456
457 /* We try to merge small writes */
458 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200459 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200460 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
461 (PIPE_BUFFERS-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200462 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800463 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200467 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200468 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200469
Jens Axboecac36bb2007-06-14 13:10:48 +0200470 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200471 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200472 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200473
Jens Axboef6762b72006-05-01 20:02:05 +0200474 iov_fault_in_pages_read(iov, chars);
475redo1:
476 addr = ops->map(pipe, buf, atomic);
Jens Axboe5274f052006-03-30 15:15:30 +0200477 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200478 chars, atomic);
479 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ret = error;
481 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200482 if (error) {
483 if (atomic) {
484 atomic = 0;
485 goto redo1;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 buf->len += chars;
490 total_len -= chars;
491 ret = chars;
492 if (!total_len)
493 goto out;
494 }
495 }
496
497 for (;;) {
498 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200499
Ingo Molnar923f4f22006-04-11 13:53:33 +0200500 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200502 if (!ret)
503 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200506 bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (bufs < PIPE_BUFFERS) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200508 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
509 struct pipe_buffer *buf = pipe->bufs + newbuf;
510 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200511 char *src;
512 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (!page) {
515 page = alloc_page(GFP_HIGHUSER);
516 if (unlikely(!page)) {
517 ret = ret ? : -ENOMEM;
518 break;
519 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200520 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200522 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * we lock up (O_NONBLOCK-)readers that sleep due to
524 * syscall merging.
525 * FIXME! Is this really true?
526 */
527 do_wakeup = 1;
528 chars = PAGE_SIZE;
529 if (chars > total_len)
530 chars = total_len;
531
Jens Axboef6762b72006-05-01 20:02:05 +0200532 iov_fault_in_pages_read(iov, chars);
533redo2:
534 if (atomic)
535 src = kmap_atomic(page, KM_USER0);
536 else
537 src = kmap(page);
538
539 error = pipe_iov_copy_from_user(src, iov, chars,
540 atomic);
541 if (atomic)
542 kunmap_atomic(src, KM_USER0);
543 else
544 kunmap(page);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200547 if (atomic) {
548 atomic = 0;
549 goto redo2;
550 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200551 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200552 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 }
555 ret += chars;
556
557 /* Insert it into the buffer array */
558 buf->page = page;
559 buf->ops = &anon_pipe_buf_ops;
560 buf->offset = 0;
561 buf->len = chars;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200562 pipe->nrbufs = ++bufs;
563 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 total_len -= chars;
566 if (!total_len)
567 break;
568 }
569 if (bufs < PIPE_BUFFERS)
570 continue;
571 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200572 if (!ret)
573 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575 }
576 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200577 if (!ret)
578 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 }
581 if (do_wakeup) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200582 wake_up_interruptible_sync(&pipe->wait);
583 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 do_wakeup = 0;
585 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200586 pipe->waiting_writers++;
587 pipe_wait(pipe);
588 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200591 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (do_wakeup) {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200593 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200594 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800597 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return ret;
599}
600
601static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
603{
604 return -EBADF;
605}
606
607static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200608bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
609 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 return -EBADF;
612}
613
Andi Kleend59d0b12008-02-08 04:21:23 -0800614static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800616 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200617 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int count, buf, nrbufs;
619
620 switch (cmd) {
621 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200622 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200623 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200625 buf = pipe->curbuf;
626 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200628 count += pipe->bufs[buf].len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 buf = (buf+1) & (PIPE_BUFFERS-1);
630 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200631 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return put_user(count, (int __user *)arg);
634 default:
635 return -EINVAL;
636 }
637}
638
639/* No kernel lock held - fine */
640static unsigned int
641pipe_poll(struct file *filp, poll_table *wait)
642{
643 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800644 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200645 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int nrbufs;
647
Ingo Molnar923f4f22006-04-11 13:53:33 +0200648 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200651 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 mask = 0;
653 if (filp->f_mode & FMODE_READ) {
654 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200655 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 mask |= POLLHUP;
657 }
658
659 if (filp->f_mode & FMODE_WRITE) {
660 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700661 /*
662 * Most Unices do not set POLLERR for FIFOs but on Linux they
663 * behave exactly like pipes for poll().
664 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200665 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 mask |= POLLERR;
667 }
668
669 return mask;
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static int
673pipe_release(struct inode *inode, int decr, int decw)
674{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200675 struct pipe_inode_info *pipe;
676
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200677 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200678 pipe = inode->i_pipe;
679 pipe->readers -= decr;
680 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200681
Ingo Molnar923f4f22006-04-11 13:53:33 +0200682 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 free_pipe_info(inode);
684 } else {
Ingo Molnar71e20f12007-10-15 17:00:19 +0200685 wake_up_interruptible_sync(&pipe->wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200686 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
687 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200689 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 return 0;
692}
693
694static int
695pipe_read_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int retval;
699
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200700 mutex_lock(&inode->i_mutex);
701 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
702 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700704 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707
708static int
709pipe_write_fasync(int fd, struct file *filp, int on)
710{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800711 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int retval;
713
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200714 mutex_lock(&inode->i_mutex);
715 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
716 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700718 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721
722static int
723pipe_rdwr_fasync(int fd, struct file *filp, int on)
724{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800725 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200726 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int retval;
728
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200729 mutex_lock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200730 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700731 if (retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200732 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700733 if (retval < 0) /* this can happen only if on == T */
734 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
735 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200736 mutex_unlock(&inode->i_mutex);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700737 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740
741static int
742pipe_read_release(struct inode *inode, struct file *filp)
743{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return pipe_release(inode, 1, 0);
745}
746
747static int
748pipe_write_release(struct inode *inode, struct file *filp)
749{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return pipe_release(inode, 0, 1);
751}
752
753static int
754pipe_rdwr_release(struct inode *inode, struct file *filp)
755{
756 int decr, decw;
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 decr = (filp->f_mode & FMODE_READ) != 0;
759 decw = (filp->f_mode & FMODE_WRITE) != 0;
760 return pipe_release(inode, decr, decw);
761}
762
763static int
764pipe_read_open(struct inode *inode, struct file *filp)
765{
766 /* We could have perhaps used atomic_t, but this and friends
767 below are the only places. So it doesn't seem worthwhile. */
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200768 mutex_lock(&inode->i_mutex);
769 inode->i_pipe->readers++;
770 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 return 0;
773}
774
775static int
776pipe_write_open(struct inode *inode, struct file *filp)
777{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200778 mutex_lock(&inode->i_mutex);
779 inode->i_pipe->writers++;
780 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 return 0;
783}
784
785static int
786pipe_rdwr_open(struct inode *inode, struct file *filp)
787{
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200788 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (filp->f_mode & FMODE_READ)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200790 inode->i_pipe->readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (filp->f_mode & FMODE_WRITE)
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200792 inode->i_pipe->writers++;
793 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 return 0;
796}
797
798/*
799 * The file_operations structs are not static because they
800 * are also used in linux/fs/fifo.c to do operations on FIFOs.
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200801 *
802 * Pipes reuse fifos' file_operations structs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200804const struct file_operations read_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700806 .read = do_sync_read,
807 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700809 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800810 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .open = pipe_read_open,
812 .release = pipe_read_release,
813 .fasync = pipe_read_fasync,
814};
815
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200816const struct file_operations write_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .llseek = no_llseek,
818 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700819 .write = do_sync_write,
820 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700821 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800822 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .open = pipe_write_open,
824 .release = pipe_write_release,
825 .fasync = pipe_write_fasync,
826};
827
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200828const struct file_operations rdwr_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700830 .read = do_sync_read,
831 .aio_read = pipe_read,
832 .write = do_sync_write,
833 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800835 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .open = pipe_rdwr_open,
837 .release = pipe_rdwr_release,
838 .fasync = pipe_rdwr_fasync,
839};
840
Ingo Molnar3a326a22006-04-10 15:18:35 +0200841struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
842{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200843 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200844
Ingo Molnar923f4f22006-04-11 13:53:33 +0200845 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
846 if (pipe) {
847 init_waitqueue_head(&pipe->wait);
848 pipe->r_counter = pipe->w_counter = 1;
849 pipe->inode = inode;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200850 }
851
Ingo Molnar923f4f22006-04-11 13:53:33 +0200852 return pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200853}
854
Ingo Molnar923f4f22006-04-11 13:53:33 +0200855void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 for (i = 0; i < PIPE_BUFFERS; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200860 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200862 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200864 if (pipe->tmp_page)
865 __free_page(pipe->tmp_page);
866 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Jens Axboeb92ce552006-04-11 13:52:07 +0200869void free_pipe_info(struct inode *inode)
870{
871 __free_pipe_info(inode->i_pipe);
872 inode->i_pipe = NULL;
873}
874
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800875static struct vfsmount *pipe_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876static int pipefs_delete_dentry(struct dentry *dentry)
877{
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800878 /*
879 * At creation time, we pretended this dentry was hashed
880 * (by clearing DCACHE_UNHASHED bit in d_flags)
881 * At delete time, we restore the truth : not hashed.
882 * (so that dput() can proceed correctly)
883 */
884 dentry->d_flags |= DCACHE_UNHASHED;
885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Ingo Molnar341b4462006-04-11 13:57:45 +0200887
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700888/*
889 * pipefs_dname() is called from d_path().
890 */
891static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
892{
893 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
894 dentry->d_inode->i_ino);
895}
896
Al Viro3ba13d12009-02-20 06:02:22 +0000897static const struct dentry_operations pipefs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 .d_delete = pipefs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700899 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900};
901
902static struct inode * get_pipe_inode(void)
903{
904 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200905 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (!inode)
908 goto fail_inode;
909
Ingo Molnar923f4f22006-04-11 13:53:33 +0200910 pipe = alloc_pipe_info(inode);
911 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200913 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200914
Ingo Molnar923f4f22006-04-11 13:53:33 +0200915 pipe->readers = pipe->writers = 1;
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200916 inode->i_fop = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /*
919 * Mark the inode dirty from the very beginning,
920 * that way it will never be moved to the dirty
921 * list because "mark_inode_dirty()" will think
922 * that it already _is_ on the dirty list.
923 */
924 inode->i_state = I_DIRTY;
925 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100926 inode->i_uid = current_fsuid();
927 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return inode;
931
932fail_iput:
933 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935fail_inode:
936 return NULL;
937}
938
Ulrich Drepperbe61a862008-07-23 21:29:40 -0700939struct file *create_write_pipe(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Andi Kleend6cbd282006-09-30 23:29:26 -0700941 int err;
942 struct inode *inode;
943 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700945 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Andi Kleend6cbd282006-09-30 23:29:26 -0700947 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 inode = get_pipe_inode();
949 if (!inode)
Dave Hansen430e2852008-02-15 14:37:26 -0800950 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Andi Kleend6cbd282006-09-30 23:29:26 -0700952 err = -ENOMEM;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700953 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700955 goto err_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 dentry->d_op = &pipefs_dentry_operations;
Eric Dumazetd18de5a2006-12-06 20:38:45 -0800958 /*
959 * We dont want to publish this dentry into global dentry hash table.
960 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
961 * This permits a working /proc/$pid/fd/XXX on pipes
962 */
963 dentry->d_flags &= ~DCACHE_UNHASHED;
964 d_instantiate(dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800965
966 err = -ENFILE;
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200967 f = alloc_file(pipe_mnt, dentry, FMODE_WRITE, &write_pipefifo_fops);
Dave Hansen430e2852008-02-15 14:37:26 -0800968 if (!f)
969 goto err_dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -0700970 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Ulrich Drepperbe61a862008-07-23 21:29:40 -0700972 f->f_flags = O_WRONLY | (flags & O_NONBLOCK);
Andi Kleend6cbd282006-09-30 23:29:26 -0700973 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Andi Kleend6cbd282006-09-30 23:29:26 -0700975 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dave Hansen430e2852008-02-15 14:37:26 -0800977 err_dentry:
Al Viroed152432008-04-22 19:51:27 -0400978 free_pipe_info(inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800979 dput(dentry);
Al Viroed152432008-04-22 19:51:27 -0400980 return ERR_PTR(err);
981
Andi Kleend6cbd282006-09-30 23:29:26 -0700982 err_inode:
983 free_pipe_info(inode);
984 iput(inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800985 err:
Andi Kleend6cbd282006-09-30 23:29:26 -0700986 return ERR_PTR(err);
987}
988
989void free_write_pipe(struct file *f)
990{
Al Viro5ccac882006-12-18 13:31:18 +0000991 free_pipe_info(f->f_dentry->d_inode);
Jan Blunckc8e7f442008-06-09 16:40:35 -0700992 path_put(&f->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -0700993 put_filp(f);
994}
995
Ulrich Drepperbe61a862008-07-23 21:29:40 -0700996struct file *create_read_pipe(struct file *wrf, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700997{
998 struct file *f = get_empty_filp();
999 if (!f)
1000 return ERR_PTR(-ENFILE);
1001
1002 /* Grab pipe from the writer */
Jan Blunckc8e7f442008-06-09 16:40:35 -07001003 f->f_path = wrf->f_path;
1004 path_get(&wrf->f_path);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001005 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
Andi Kleend6cbd282006-09-30 23:29:26 -07001006
1007 f->f_pos = 0;
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001008 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
Denys Vlasenkod2d96482008-07-01 14:16:09 +02001009 f->f_op = &read_pipefifo_fops;
Andi Kleend6cbd282006-09-30 23:29:26 -07001010 f->f_mode = FMODE_READ;
1011 f->f_version = 0;
1012
1013 return f;
1014}
1015
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001016int do_pipe_flags(int *fd, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001017{
1018 struct file *fw, *fr;
1019 int error;
1020 int fdw, fdr;
1021
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001022 if (flags & ~(O_CLOEXEC | O_NONBLOCK))
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001023 return -EINVAL;
1024
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001025 fw = create_write_pipe(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001026 if (IS_ERR(fw))
1027 return PTR_ERR(fw);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001028 fr = create_read_pipe(fw, flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001029 error = PTR_ERR(fr);
1030 if (IS_ERR(fr))
1031 goto err_write_pipe;
1032
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001033 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001034 if (error < 0)
1035 goto err_read_pipe;
1036 fdr = error;
1037
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001038 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001039 if (error < 0)
1040 goto err_fdr;
1041 fdw = error;
1042
Al Viro157cf642008-12-14 04:57:47 -05001043 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001044 fd_install(fdr, fr);
1045 fd_install(fdw, fw);
1046 fd[0] = fdr;
1047 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050
Andi Kleend6cbd282006-09-30 23:29:26 -07001051 err_fdr:
1052 put_unused_fd(fdr);
1053 err_read_pipe:
Jan Blunckc8e7f442008-06-09 16:40:35 -07001054 path_put(&fr->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001055 put_filp(fr);
1056 err_write_pipe:
1057 free_write_pipe(fw);
1058 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001062 * sys_pipe() is the normal C calling standard for creating
1063 * a pipe. It's not the way Unix traditionally does this, though.
1064 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01001065SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001066{
1067 int fd[2];
1068 int error;
1069
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001070 error = do_pipe_flags(fd, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001071 if (!error) {
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001072 if (copy_to_user(fildes, fd, sizeof(fd))) {
1073 sys_close(fd[0]);
1074 sys_close(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001075 error = -EFAULT;
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001076 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001077 }
1078 return error;
1079}
1080
Heiko Carstens2b664212009-01-14 14:14:35 +01001081SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001082{
1083 return sys_pipe2(fildes, 0);
1084}
1085
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001086/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * pipefs should _never_ be mounted by userland - too much of security hassle,
1088 * no real gain from having the whole whorehouse mounted. So we don't need
1089 * any operations on the root directory. However, we need a non-trivial
1090 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1091 */
David Howells454e2392006-06-23 02:02:57 -07001092static int pipefs_get_sb(struct file_system_type *fs_type,
1093 int flags, const char *dev_name, void *data,
1094 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
David Howells454e2392006-06-23 02:02:57 -07001096 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099static struct file_system_type pipe_fs_type = {
1100 .name = "pipefs",
1101 .get_sb = pipefs_get_sb,
1102 .kill_sb = kill_anon_super,
1103};
1104
1105static int __init init_pipe_fs(void)
1106{
1107 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!err) {
1110 pipe_mnt = kern_mount(&pipe_fs_type);
1111 if (IS_ERR(pipe_mnt)) {
1112 err = PTR_ERR(pipe_mnt);
1113 unregister_filesystem(&pipe_fs_type);
1114 }
1115 }
1116 return err;
1117}
1118
1119static void __exit exit_pipe_fs(void)
1120{
1121 unregister_filesystem(&pipe_fs_type);
1122 mntput(pipe_mnt);
1123}
1124
1125fs_initcall(init_pipe_fs);
1126module_exit(exit_pipe_fs);