]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/pipe.c
Rationalize fasync return values
[mirror_ubuntu-artful-kernel.git] / fs / pipe.c
CommitLineData
1da177e4
LT
1/*
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>
5274f052 18#include <linux/pagemap.h>
db349509 19#include <linux/audit.h>
ba719bae 20#include <linux/syscalls.h>
1da177e4
LT
21
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 */
3a326a2c 41void pipe_wait(struct pipe_inode_info *pipe)
1da177e4
LT
42{
43 DEFINE_WAIT(wait);
44
d79fc0fc
IM
45 /*
46 * Pipes are system-local resources, so sleeping on them
47 * is considered a noninteractive wait:
48 */
af927232 49 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
3a326a2c
IM
50 if (pipe->inode)
51 mutex_unlock(&pipe->inode->i_mutex);
1da177e4 52 schedule();
3a326a2c
IM
53 finish_wait(&pipe->wait, &wait);
54 if (pipe->inode)
55 mutex_lock(&pipe->inode->i_mutex);
1da177e4
LT
56}
57
858119e1 58static int
f6762b7a
JA
59pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
60 int atomic)
1da177e4
LT
61{
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
f6762b7a
JA
69 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 }
1da177e4
LT
76 to += copy;
77 len -= copy;
78 iov->iov_base += copy;
79 iov->iov_len -= copy;
80 }
81 return 0;
82}
83
858119e1 84static int
f6762b7a
JA
85pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
86 int atomic)
1da177e4
LT
87{
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
f6762b7a
JA
95 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 }
1da177e4
LT
102 from += copy;
103 len -= copy;
104 iov->iov_base += copy;
105 iov->iov_len -= copy;
106 }
107 return 0;
108}
109
f6762b7a
JA
110/*
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
341b446b
IM
151static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
152 struct pipe_buffer *buf)
1da177e4
LT
153{
154 struct page *page = buf->page;
155
5274f052
JA
156 /*
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
341b446b 159 * allocation cache. (Otherwise just release our reference to it)
5274f052 160 */
341b446b 161 if (page_count(page) == 1 && !pipe->tmp_page)
923f4f23 162 pipe->tmp_page = page;
341b446b
IM
163 else
164 page_cache_release(page);
1da177e4
LT
165}
166
0845718d
JA
167/**
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
b51d63c6 175 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
0845718d
JA
176 * 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 */
f84d7519 181void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
f6762b7a 182 struct pipe_buffer *buf, int atomic)
1da177e4 183{
f6762b7a
JA
184 if (atomic) {
185 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
186 return kmap_atomic(buf->page, KM_USER0);
187 }
188
1da177e4
LT
189 return kmap(buf->page);
190}
191
0845718d
JA
192/**
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 */
f84d7519 201void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
f6762b7a 202 struct pipe_buffer *buf, void *map_data)
1da177e4 203{
f6762b7a
JA
204 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);
1da177e4
LT
209}
210
0845718d 211/**
b51d63c6 212 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
0845718d
JA
213 * @pipe: the pipe that the buffer belongs to
214 * @buf: the buffer to attempt to steal
215 *
216 * Description:
b51d63c6 217 * This function attempts to steal the &struct page attached to
0845718d
JA
218 * @buf. If successful, this function returns 0 and returns with
219 * the page locked. The caller may then reuse the page for whatever
b51d63c6 220 * he wishes; the typical use is insertion into a different file
0845718d
JA
221 * page cache.
222 */
330ab716
JA
223int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
224 struct pipe_buffer *buf)
5abc97aa 225{
46e678c9
JA
226 struct page *page = buf->page;
227
0845718d
JA
228 /*
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 */
46e678c9 233 if (page_count(page) == 1) {
46e678c9
JA
234 lock_page(page);
235 return 0;
236 }
237
238 return 1;
5abc97aa
JA
239}
240
0845718d 241/**
b51d63c6 242 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
0845718d
JA
243 * @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)
70524490
JA
252{
253 page_cache_get(buf->page);
254}
255
0845718d
JA
256/**
257 * generic_pipe_buf_confirm - verify contents of the pipe buffer
79685b8d 258 * @info: the pipe that the buffer belongs to
0845718d
JA
259 * @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 */
cac36bb0
JA
265int generic_pipe_buf_confirm(struct pipe_inode_info *info,
266 struct pipe_buffer *buf)
f84d7519
JA
267{
268 return 0;
269}
270
d4c3cca9 271static const struct pipe_buf_operations anon_pipe_buf_ops = {
1da177e4 272 .can_merge = 1,
f84d7519
JA
273 .map = generic_pipe_buf_map,
274 .unmap = generic_pipe_buf_unmap,
cac36bb0 275 .confirm = generic_pipe_buf_confirm,
1da177e4 276 .release = anon_pipe_buf_release,
330ab716 277 .steal = generic_pipe_buf_steal,
f84d7519 278 .get = generic_pipe_buf_get,
1da177e4
LT
279};
280
281static ssize_t
ee0b3e67
BP
282pipe_read(struct kiocb *iocb, const struct iovec *_iov,
283 unsigned long nr_segs, loff_t pos)
1da177e4 284{
ee0b3e67 285 struct file *filp = iocb->ki_filp;
0f7fc9e4 286 struct inode *inode = filp->f_path.dentry->d_inode;
923f4f23 287 struct pipe_inode_info *pipe;
1da177e4
LT
288 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;
9aeedfc4 300 mutex_lock(&inode->i_mutex);
923f4f23 301 pipe = inode->i_pipe;
1da177e4 302 for (;;) {
923f4f23 303 int bufs = pipe->nrbufs;
1da177e4 304 if (bufs) {
923f4f23
IM
305 int curbuf = pipe->curbuf;
306 struct pipe_buffer *buf = pipe->bufs + curbuf;
d4c3cca9 307 const struct pipe_buf_operations *ops = buf->ops;
1da177e4
LT
308 void *addr;
309 size_t chars = buf->len;
f6762b7a 310 int error, atomic;
1da177e4
LT
311
312 if (chars > total_len)
313 chars = total_len;
314
cac36bb0 315 error = ops->confirm(pipe, buf);
f84d7519 316 if (error) {
5274f052 317 if (!ret)
f84d7519 318 error = ret;
5274f052
JA
319 break;
320 }
f84d7519 321
f6762b7a
JA
322 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);
1da177e4 327 if (unlikely(error)) {
f6762b7a
JA
328 /*
329 * Just retry with the slow path if we failed.
330 */
331 if (atomic) {
332 atomic = 0;
333 goto redo;
334 }
341b446b 335 if (!ret)
f6762b7a 336 ret = error;
1da177e4
LT
337 break;
338 }
339 ret += chars;
340 buf->offset += chars;
341 buf->len -= chars;
342 if (!buf->len) {
343 buf->ops = NULL;
923f4f23 344 ops->release(pipe, buf);
1da177e4 345 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
923f4f23
IM
346 pipe->curbuf = curbuf;
347 pipe->nrbufs = --bufs;
1da177e4
LT
348 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;
923f4f23 356 if (!pipe->writers)
1da177e4 357 break;
923f4f23 358 if (!pipe->waiting_writers) {
1da177e4
LT
359 /* 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)) {
341b446b
IM
372 if (!ret)
373 ret = -ERESTARTSYS;
1da177e4
LT
374 break;
375 }
376 if (do_wakeup) {
923f4f23
IM
377 wake_up_interruptible_sync(&pipe->wait);
378 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4 379 }
923f4f23 380 pipe_wait(pipe);
1da177e4 381 }
9aeedfc4 382 mutex_unlock(&inode->i_mutex);
341b446b
IM
383
384 /* Signal writers asynchronously that there is more room. */
1da177e4 385 if (do_wakeup) {
71e20f18 386 wake_up_interruptible_sync(&pipe->wait);
923f4f23 387 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4
LT
388 }
389 if (ret > 0)
390 file_accessed(filp);
391 return ret;
392}
393
394static ssize_t
ee0b3e67
BP
395pipe_write(struct kiocb *iocb, const struct iovec *_iov,
396 unsigned long nr_segs, loff_t ppos)
1da177e4 397{
ee0b3e67 398 struct file *filp = iocb->ki_filp;
0f7fc9e4 399 struct inode *inode = filp->f_path.dentry->d_inode;
923f4f23 400 struct pipe_inode_info *pipe;
1da177e4
LT
401 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;
9aeedfc4 414 mutex_lock(&inode->i_mutex);
923f4f23 415 pipe = inode->i_pipe;
1da177e4 416
923f4f23 417 if (!pipe->readers) {
1da177e4
LT
418 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 */
923f4f23 425 if (pipe->nrbufs && chars != 0) {
341b446b
IM
426 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
427 (PIPE_BUFFERS-1);
923f4f23 428 struct pipe_buffer *buf = pipe->bufs + lastbuf;
d4c3cca9 429 const struct pipe_buf_operations *ops = buf->ops;
1da177e4 430 int offset = buf->offset + buf->len;
341b446b 431
1da177e4 432 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
f6762b7a 433 int error, atomic = 1;
5274f052 434 void *addr;
5274f052 435
cac36bb0 436 error = ops->confirm(pipe, buf);
f84d7519 437 if (error)
5274f052 438 goto out;
f84d7519 439
f6762b7a
JA
440 iov_fault_in_pages_read(iov, chars);
441redo1:
442 addr = ops->map(pipe, buf, atomic);
5274f052 443 error = pipe_iov_copy_from_user(offset + addr, iov,
f6762b7a
JA
444 chars, atomic);
445 ops->unmap(pipe, buf, addr);
1da177e4
LT
446 ret = error;
447 do_wakeup = 1;
f6762b7a
JA
448 if (error) {
449 if (atomic) {
450 atomic = 0;
451 goto redo1;
452 }
1da177e4 453 goto out;
f6762b7a 454 }
1da177e4
LT
455 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;
341b446b 465
923f4f23 466 if (!pipe->readers) {
1da177e4 467 send_sig(SIGPIPE, current, 0);
341b446b
IM
468 if (!ret)
469 ret = -EPIPE;
1da177e4
LT
470 break;
471 }
923f4f23 472 bufs = pipe->nrbufs;
1da177e4 473 if (bufs < PIPE_BUFFERS) {
923f4f23
IM
474 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
475 struct pipe_buffer *buf = pipe->bufs + newbuf;
476 struct page *page = pipe->tmp_page;
f6762b7a
JA
477 char *src;
478 int error, atomic = 1;
1da177e4
LT
479
480 if (!page) {
481 page = alloc_page(GFP_HIGHUSER);
482 if (unlikely(!page)) {
483 ret = ret ? : -ENOMEM;
484 break;
485 }
923f4f23 486 pipe->tmp_page = page;
1da177e4 487 }
341b446b 488 /* Always wake up, even if the copy fails. Otherwise
1da177e4
LT
489 * 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
f6762b7a
JA
498 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
1da177e4 512 if (unlikely(error)) {
f6762b7a
JA
513 if (atomic) {
514 atomic = 0;
515 goto redo2;
516 }
341b446b 517 if (!ret)
f6762b7a 518 ret = error;
1da177e4
LT
519 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;
923f4f23
IM
528 pipe->nrbufs = ++bufs;
529 pipe->tmp_page = NULL;
1da177e4
LT
530
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) {
341b446b
IM
538 if (!ret)
539 ret = -EAGAIN;
1da177e4
LT
540 break;
541 }
542 if (signal_pending(current)) {
341b446b
IM
543 if (!ret)
544 ret = -ERESTARTSYS;
1da177e4
LT
545 break;
546 }
547 if (do_wakeup) {
923f4f23
IM
548 wake_up_interruptible_sync(&pipe->wait);
549 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4
LT
550 do_wakeup = 0;
551 }
923f4f23
IM
552 pipe->waiting_writers++;
553 pipe_wait(pipe);
554 pipe->waiting_writers--;
1da177e4
LT
555 }
556out:
9aeedfc4 557 mutex_unlock(&inode->i_mutex);
1da177e4 558 if (do_wakeup) {
71e20f18 559 wake_up_interruptible_sync(&pipe->wait);
923f4f23 560 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4
LT
561 }
562 if (ret > 0)
870f4817 563 file_update_time(filp);
1da177e4
LT
564 return ret;
565}
566
1da177e4
LT
567static ssize_t
568bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
569{
570 return -EBADF;
571}
572
573static ssize_t
341b446b
IM
574bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
575 loff_t *ppos)
1da177e4
LT
576{
577 return -EBADF;
578}
579
d59d0b1b 580static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4 581{
0f7fc9e4 582 struct inode *inode = filp->f_path.dentry->d_inode;
923f4f23 583 struct pipe_inode_info *pipe;
1da177e4
LT
584 int count, buf, nrbufs;
585
586 switch (cmd) {
587 case FIONREAD:
9aeedfc4 588 mutex_lock(&inode->i_mutex);
923f4f23 589 pipe = inode->i_pipe;
1da177e4 590 count = 0;
923f4f23
IM
591 buf = pipe->curbuf;
592 nrbufs = pipe->nrbufs;
1da177e4 593 while (--nrbufs >= 0) {
923f4f23 594 count += pipe->bufs[buf].len;
1da177e4
LT
595 buf = (buf+1) & (PIPE_BUFFERS-1);
596 }
9aeedfc4 597 mutex_unlock(&inode->i_mutex);
923f4f23 598
1da177e4
LT
599 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;
0f7fc9e4 610 struct inode *inode = filp->f_path.dentry->d_inode;
923f4f23 611 struct pipe_inode_info *pipe = inode->i_pipe;
1da177e4
LT
612 int nrbufs;
613
923f4f23 614 poll_wait(filp, &pipe->wait, wait);
1da177e4
LT
615
616 /* Reading only -- no need for acquiring the semaphore. */
923f4f23 617 nrbufs = pipe->nrbufs;
1da177e4
LT
618 mask = 0;
619 if (filp->f_mode & FMODE_READ) {
620 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
923f4f23 621 if (!pipe->writers && filp->f_version != pipe->w_counter)
1da177e4
LT
622 mask |= POLLHUP;
623 }
624
625 if (filp->f_mode & FMODE_WRITE) {
626 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
5e5d7a22
PE
627 /*
628 * Most Unices do not set POLLERR for FIFOs but on Linux they
629 * behave exactly like pipes for poll().
630 */
923f4f23 631 if (!pipe->readers)
1da177e4
LT
632 mask |= POLLERR;
633 }
634
635 return mask;
636}
637
1da177e4
LT
638static int
639pipe_release(struct inode *inode, int decr, int decw)
640{
923f4f23
IM
641 struct pipe_inode_info *pipe;
642
9aeedfc4 643 mutex_lock(&inode->i_mutex);
923f4f23
IM
644 pipe = inode->i_pipe;
645 pipe->readers -= decr;
646 pipe->writers -= decw;
341b446b 647
923f4f23 648 if (!pipe->readers && !pipe->writers) {
1da177e4
LT
649 free_pipe_info(inode);
650 } else {
71e20f18 651 wake_up_interruptible_sync(&pipe->wait);
923f4f23
IM
652 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
653 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4 654 }
9aeedfc4 655 mutex_unlock(&inode->i_mutex);
1da177e4
LT
656
657 return 0;
658}
659
660static int
661pipe_read_fasync(int fd, struct file *filp, int on)
662{
0f7fc9e4 663 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4
LT
664 int retval;
665
9aeedfc4
IM
666 mutex_lock(&inode->i_mutex);
667 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
668 mutex_unlock(&inode->i_mutex);
1da177e4 669
60aa4924 670 return retval;
1da177e4
LT
671}
672
673
674static int
675pipe_write_fasync(int fd, struct file *filp, int on)
676{
0f7fc9e4 677 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4
LT
678 int retval;
679
9aeedfc4
IM
680 mutex_lock(&inode->i_mutex);
681 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
682 mutex_unlock(&inode->i_mutex);
1da177e4 683
60aa4924 684 return retval;
1da177e4
LT
685}
686
687
688static int
689pipe_rdwr_fasync(int fd, struct file *filp, int on)
690{
0f7fc9e4 691 struct inode *inode = filp->f_path.dentry->d_inode;
341b446b 692 struct pipe_inode_info *pipe = inode->i_pipe;
1da177e4
LT
693 int retval;
694
9aeedfc4 695 mutex_lock(&inode->i_mutex);
341b446b 696 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
e5bc49ba 697 if (retval >= 0) {
341b446b 698 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
e5bc49ba
ON
699 if (retval < 0) /* this can happen only if on == T */
700 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
701 }
9aeedfc4 702 mutex_unlock(&inode->i_mutex);
60aa4924 703 return retval;
1da177e4
LT
704}
705
706
707static int
708pipe_read_release(struct inode *inode, struct file *filp)
709{
1da177e4
LT
710 return pipe_release(inode, 1, 0);
711}
712
713static int
714pipe_write_release(struct inode *inode, struct file *filp)
715{
1da177e4
LT
716 return pipe_release(inode, 0, 1);
717}
718
719static int
720pipe_rdwr_release(struct inode *inode, struct file *filp)
721{
722 int decr, decw;
723
1da177e4
LT
724 decr = (filp->f_mode & FMODE_READ) != 0;
725 decw = (filp->f_mode & FMODE_WRITE) != 0;
726 return pipe_release(inode, decr, decw);
727}
728
729static int
730pipe_read_open(struct inode *inode, struct file *filp)
731{
732 /* We could have perhaps used atomic_t, but this and friends
733 below are the only places. So it doesn't seem worthwhile. */
9aeedfc4
IM
734 mutex_lock(&inode->i_mutex);
735 inode->i_pipe->readers++;
736 mutex_unlock(&inode->i_mutex);
1da177e4
LT
737
738 return 0;
739}
740
741static int
742pipe_write_open(struct inode *inode, struct file *filp)
743{
9aeedfc4
IM
744 mutex_lock(&inode->i_mutex);
745 inode->i_pipe->writers++;
746 mutex_unlock(&inode->i_mutex);
1da177e4
LT
747
748 return 0;
749}
750
751static int
752pipe_rdwr_open(struct inode *inode, struct file *filp)
753{
9aeedfc4 754 mutex_lock(&inode->i_mutex);
1da177e4 755 if (filp->f_mode & FMODE_READ)
9aeedfc4 756 inode->i_pipe->readers++;
1da177e4 757 if (filp->f_mode & FMODE_WRITE)
9aeedfc4
IM
758 inode->i_pipe->writers++;
759 mutex_unlock(&inode->i_mutex);
1da177e4
LT
760
761 return 0;
762}
763
764/*
765 * The file_operations structs are not static because they
766 * are also used in linux/fs/fifo.c to do operations on FIFOs.
d2d9648e
DV
767 *
768 * Pipes reuse fifos' file_operations structs.
1da177e4 769 */
d2d9648e 770const struct file_operations read_pipefifo_fops = {
1da177e4 771 .llseek = no_llseek,
ee0b3e67
BP
772 .read = do_sync_read,
773 .aio_read = pipe_read,
1da177e4
LT
774 .write = bad_pipe_w,
775 .poll = pipe_poll,
d59d0b1b 776 .unlocked_ioctl = pipe_ioctl,
1da177e4
LT
777 .open = pipe_read_open,
778 .release = pipe_read_release,
779 .fasync = pipe_read_fasync,
780};
781
d2d9648e 782const struct file_operations write_pipefifo_fops = {
1da177e4
LT
783 .llseek = no_llseek,
784 .read = bad_pipe_r,
ee0b3e67
BP
785 .write = do_sync_write,
786 .aio_write = pipe_write,
1da177e4 787 .poll = pipe_poll,
d59d0b1b 788 .unlocked_ioctl = pipe_ioctl,
1da177e4
LT
789 .open = pipe_write_open,
790 .release = pipe_write_release,
791 .fasync = pipe_write_fasync,
792};
793
d2d9648e 794const struct file_operations rdwr_pipefifo_fops = {
1da177e4 795 .llseek = no_llseek,
ee0b3e67
BP
796 .read = do_sync_read,
797 .aio_read = pipe_read,
798 .write = do_sync_write,
799 .aio_write = pipe_write,
1da177e4 800 .poll = pipe_poll,
d59d0b1b 801 .unlocked_ioctl = pipe_ioctl,
1da177e4
LT
802 .open = pipe_rdwr_open,
803 .release = pipe_rdwr_release,
804 .fasync = pipe_rdwr_fasync,
805};
806
3a326a2c
IM
807struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
808{
923f4f23 809 struct pipe_inode_info *pipe;
3a326a2c 810
923f4f23
IM
811 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
812 if (pipe) {
813 init_waitqueue_head(&pipe->wait);
814 pipe->r_counter = pipe->w_counter = 1;
815 pipe->inode = inode;
3a326a2c
IM
816 }
817
923f4f23 818 return pipe;
3a326a2c
IM
819}
820
923f4f23 821void __free_pipe_info(struct pipe_inode_info *pipe)
1da177e4
LT
822{
823 int i;
1da177e4 824
1da177e4 825 for (i = 0; i < PIPE_BUFFERS; i++) {
923f4f23 826 struct pipe_buffer *buf = pipe->bufs + i;
1da177e4 827 if (buf->ops)
923f4f23 828 buf->ops->release(pipe, buf);
1da177e4 829 }
923f4f23
IM
830 if (pipe->tmp_page)
831 __free_page(pipe->tmp_page);
832 kfree(pipe);
1da177e4
LT
833}
834
b92ce558
JA
835void free_pipe_info(struct inode *inode)
836{
837 __free_pipe_info(inode->i_pipe);
838 inode->i_pipe = NULL;
839}
840
fa3536cc 841static struct vfsmount *pipe_mnt __read_mostly;
1da177e4
LT
842static int pipefs_delete_dentry(struct dentry *dentry)
843{
d18de5a2
ED
844 /*
845 * At creation time, we pretended this dentry was hashed
846 * (by clearing DCACHE_UNHASHED bit in d_flags)
847 * At delete time, we restore the truth : not hashed.
848 * (so that dput() can proceed correctly)
849 */
850 dentry->d_flags |= DCACHE_UNHASHED;
851 return 0;
1da177e4 852}
341b446b 853
c23fbb6b
ED
854/*
855 * pipefs_dname() is called from d_path().
856 */
857static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
858{
859 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
860 dentry->d_inode->i_ino);
861}
862
1da177e4
LT
863static struct dentry_operations pipefs_dentry_operations = {
864 .d_delete = pipefs_delete_dentry,
c23fbb6b 865 .d_dname = pipefs_dname,
1da177e4
LT
866};
867
868static struct inode * get_pipe_inode(void)
869{
870 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
923f4f23 871 struct pipe_inode_info *pipe;
1da177e4
LT
872
873 if (!inode)
874 goto fail_inode;
875
923f4f23
IM
876 pipe = alloc_pipe_info(inode);
877 if (!pipe)
1da177e4 878 goto fail_iput;
923f4f23 879 inode->i_pipe = pipe;
3a326a2c 880
923f4f23 881 pipe->readers = pipe->writers = 1;
d2d9648e 882 inode->i_fop = &rdwr_pipefifo_fops;
1da177e4
LT
883
884 /*
885 * Mark the inode dirty from the very beginning,
886 * that way it will never be moved to the dirty
887 * list because "mark_inode_dirty()" will think
888 * that it already _is_ on the dirty list.
889 */
890 inode->i_state = I_DIRTY;
891 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
da9592ed
DH
892 inode->i_uid = current_fsuid();
893 inode->i_gid = current_fsgid();
1da177e4 894 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
923f4f23 895
1da177e4
LT
896 return inode;
897
898fail_iput:
899 iput(inode);
341b446b 900
1da177e4
LT
901fail_inode:
902 return NULL;
903}
904
be61a86d 905struct file *create_write_pipe(int flags)
1da177e4 906{
d6cbd281
AK
907 int err;
908 struct inode *inode;
909 struct file *f;
1da177e4 910 struct dentry *dentry;
c23fbb6b 911 struct qstr name = { .name = "" };
1da177e4 912
d6cbd281 913 err = -ENFILE;
1da177e4
LT
914 inode = get_pipe_inode();
915 if (!inode)
430e285e 916 goto err;
1da177e4 917
d6cbd281 918 err = -ENOMEM;
c23fbb6b 919 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
1da177e4 920 if (!dentry)
d6cbd281 921 goto err_inode;
341b446b 922
1da177e4 923 dentry->d_op = &pipefs_dentry_operations;
d18de5a2
ED
924 /*
925 * We dont want to publish this dentry into global dentry hash table.
926 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
927 * This permits a working /proc/$pid/fd/XXX on pipes
928 */
929 dentry->d_flags &= ~DCACHE_UNHASHED;
930 d_instantiate(dentry, inode);
430e285e
DH
931
932 err = -ENFILE;
d2d9648e 933 f = alloc_file(pipe_mnt, dentry, FMODE_WRITE, &write_pipefifo_fops);
430e285e
DH
934 if (!f)
935 goto err_dentry;
d6cbd281 936 f->f_mapping = inode->i_mapping;
341b446b 937
be61a86d 938 f->f_flags = O_WRONLY | (flags & O_NONBLOCK);
d6cbd281
AK
939 f->f_version = 0;
940
941 return f;
1da177e4 942
430e285e 943 err_dentry:
ed152437 944 free_pipe_info(inode);
430e285e 945 dput(dentry);
ed152437
AV
946 return ERR_PTR(err);
947
d6cbd281 948 err_inode:
1da177e4
LT
949 free_pipe_info(inode);
950 iput(inode);
430e285e 951 err:
d6cbd281
AK
952 return ERR_PTR(err);
953}
954
955void free_write_pipe(struct file *f)
956{
5ccac88e 957 free_pipe_info(f->f_dentry->d_inode);
c8e7f449 958 path_put(&f->f_path);
d6cbd281
AK
959 put_filp(f);
960}
961
be61a86d 962struct file *create_read_pipe(struct file *wrf, int flags)
d6cbd281
AK
963{
964 struct file *f = get_empty_filp();
965 if (!f)
966 return ERR_PTR(-ENFILE);
967
968 /* Grab pipe from the writer */
c8e7f449
JB
969 f->f_path = wrf->f_path;
970 path_get(&wrf->f_path);
0f7fc9e4 971 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
d6cbd281
AK
972
973 f->f_pos = 0;
be61a86d 974 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
d2d9648e 975 f->f_op = &read_pipefifo_fops;
d6cbd281
AK
976 f->f_mode = FMODE_READ;
977 f->f_version = 0;
978
979 return f;
980}
981
ed8cae8b 982int do_pipe_flags(int *fd, int flags)
d6cbd281
AK
983{
984 struct file *fw, *fr;
985 int error;
986 int fdw, fdr;
987
be61a86d 988 if (flags & ~(O_CLOEXEC | O_NONBLOCK))
ed8cae8b
UD
989 return -EINVAL;
990
be61a86d 991 fw = create_write_pipe(flags);
d6cbd281
AK
992 if (IS_ERR(fw))
993 return PTR_ERR(fw);
be61a86d 994 fr = create_read_pipe(fw, flags);
d6cbd281
AK
995 error = PTR_ERR(fr);
996 if (IS_ERR(fr))
997 goto err_write_pipe;
998
ed8cae8b 999 error = get_unused_fd_flags(flags);
d6cbd281
AK
1000 if (error < 0)
1001 goto err_read_pipe;
1002 fdr = error;
1003
ed8cae8b 1004 error = get_unused_fd_flags(flags);
d6cbd281
AK
1005 if (error < 0)
1006 goto err_fdr;
1007 fdw = error;
1008
157cf649 1009 audit_fd_pair(fdr, fdw);
d6cbd281
AK
1010 fd_install(fdr, fr);
1011 fd_install(fdw, fw);
1012 fd[0] = fdr;
1013 fd[1] = fdw;
1014
1015 return 0;
1016
1017 err_fdr:
1018 put_unused_fd(fdr);
1019 err_read_pipe:
c8e7f449 1020 path_put(&fr->f_path);
d6cbd281
AK
1021 put_filp(fr);
1022 err_write_pipe:
1023 free_write_pipe(fw);
1024 return error;
1da177e4
LT
1025}
1026
ed8cae8b
UD
1027int do_pipe(int *fd)
1028{
1029 return do_pipe_flags(fd, 0);
1030}
1031
d35c7b0e
UD
1032/*
1033 * sys_pipe() is the normal C calling standard for creating
1034 * a pipe. It's not the way Unix traditionally does this, though.
1035 */
d4e82042 1036SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
d35c7b0e
UD
1037{
1038 int fd[2];
1039 int error;
1040
ed8cae8b 1041 error = do_pipe_flags(fd, flags);
d35c7b0e 1042 if (!error) {
ba719bae
UD
1043 if (copy_to_user(fildes, fd, sizeof(fd))) {
1044 sys_close(fd[0]);
1045 sys_close(fd[1]);
d35c7b0e 1046 error = -EFAULT;
ba719bae 1047 }
d35c7b0e
UD
1048 }
1049 return error;
1050}
1051
2b664219 1052SYSCALL_DEFINE1(pipe, int __user *, fildes)
ed8cae8b
UD
1053{
1054 return sys_pipe2(fildes, 0);
1055}
1056
1da177e4
LT
1057/*
1058 * pipefs should _never_ be mounted by userland - too much of security hassle,
1059 * no real gain from having the whole whorehouse mounted. So we don't need
1060 * any operations on the root directory. However, we need a non-trivial
1061 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1062 */
454e2398
DH
1063static int pipefs_get_sb(struct file_system_type *fs_type,
1064 int flags, const char *dev_name, void *data,
1065 struct vfsmount *mnt)
1da177e4 1066{
454e2398 1067 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
1da177e4
LT
1068}
1069
1070static struct file_system_type pipe_fs_type = {
1071 .name = "pipefs",
1072 .get_sb = pipefs_get_sb,
1073 .kill_sb = kill_anon_super,
1074};
1075
1076static int __init init_pipe_fs(void)
1077{
1078 int err = register_filesystem(&pipe_fs_type);
341b446b 1079
1da177e4
LT
1080 if (!err) {
1081 pipe_mnt = kern_mount(&pipe_fs_type);
1082 if (IS_ERR(pipe_mnt)) {
1083 err = PTR_ERR(pipe_mnt);
1084 unregister_filesystem(&pipe_fs_type);
1085 }
1086 }
1087 return err;
1088}
1089
1090static void __exit exit_pipe_fs(void)
1091{
1092 unregister_filesystem(&pipe_fs_type);
1093 mntput(pipe_mnt);
1094}
1095
1096fs_initcall(init_pipe_fs);
1097module_exit(exit_pipe_fs);