]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - fs/pipe.c
pipe: don't use ->i_mutex
[thirdparty/kernel/stable.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>
35f3d14d 14#include <linux/log2.h>
1da177e4 15#include <linux/mount.h>
b502bd11 16#include <linux/magic.h>
1da177e4
LT
17#include <linux/pipe_fs_i.h>
18#include <linux/uio.h>
19#include <linux/highmem.h>
5274f052 20#include <linux/pagemap.h>
db349509 21#include <linux/audit.h>
ba719bae 22#include <linux/syscalls.h>
b492e95b 23#include <linux/fcntl.h>
1da177e4
LT
24
25#include <asm/uaccess.h>
26#include <asm/ioctls.h>
27
599a0ac1
AV
28#include "internal.h"
29
b492e95b
JA
30/*
31 * The max size that a non-root user is allowed to grow the pipe. Can
ff9da691 32 * be set by root in /proc/sys/fs/pipe-max-size
b492e95b 33 */
ff9da691
JA
34unsigned int pipe_max_size = 1048576;
35
36/*
37 * Minimum pipe size, as required by POSIX
38 */
39unsigned int pipe_min_size = PAGE_SIZE;
b492e95b 40
1da177e4
LT
41/*
42 * We use a start+len construction, which provides full use of the
43 * allocated memory.
44 * -- Florian Coosmann (FGC)
45 *
46 * Reads with count = 0 should always return 0.
47 * -- Julian Bradfield 1999-06-07.
48 *
49 * FIFOs and Pipes now generate SIGIO for both readers and writers.
50 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
51 *
52 * pipe_read & write cleanup
53 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
54 */
55
61e0d47c
MS
56static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
57{
58 if (pipe->inode)
72b0d9aa 59 mutex_lock_nested(&pipe->mutex, subclass);
61e0d47c
MS
60}
61
62void pipe_lock(struct pipe_inode_info *pipe)
63{
64 /*
65 * pipe_lock() nests non-pipe inode locks (for writing to a file)
66 */
67 pipe_lock_nested(pipe, I_MUTEX_PARENT);
68}
69EXPORT_SYMBOL(pipe_lock);
70
71void pipe_unlock(struct pipe_inode_info *pipe)
72{
73 if (pipe->inode)
72b0d9aa 74 mutex_unlock(&pipe->mutex);
61e0d47c
MS
75}
76EXPORT_SYMBOL(pipe_unlock);
77
78void pipe_double_lock(struct pipe_inode_info *pipe1,
79 struct pipe_inode_info *pipe2)
80{
81 BUG_ON(pipe1 == pipe2);
82
83 if (pipe1 < pipe2) {
84 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
85 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
86 } else {
023d43c7
PZ
87 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
88 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
61e0d47c
MS
89 }
90}
91
1da177e4 92/* Drop the inode semaphore and wait for a pipe event, atomically */
3a326a2c 93void pipe_wait(struct pipe_inode_info *pipe)
1da177e4
LT
94{
95 DEFINE_WAIT(wait);
96
d79fc0fc
IM
97 /*
98 * Pipes are system-local resources, so sleeping on them
99 * is considered a noninteractive wait:
100 */
af927232 101 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
61e0d47c 102 pipe_unlock(pipe);
1da177e4 103 schedule();
3a326a2c 104 finish_wait(&pipe->wait, &wait);
61e0d47c 105 pipe_lock(pipe);
1da177e4
LT
106}
107
858119e1 108static int
f6762b7a
JA
109pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
110 int atomic)
1da177e4
LT
111{
112 unsigned long copy;
113
114 while (len > 0) {
115 while (!iov->iov_len)
116 iov++;
117 copy = min_t(unsigned long, len, iov->iov_len);
118
f6762b7a
JA
119 if (atomic) {
120 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
121 return -EFAULT;
122 } else {
123 if (copy_from_user(to, iov->iov_base, copy))
124 return -EFAULT;
125 }
1da177e4
LT
126 to += copy;
127 len -= copy;
128 iov->iov_base += copy;
129 iov->iov_len -= copy;
130 }
131 return 0;
132}
133
858119e1 134static int
f6762b7a
JA
135pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
136 int atomic)
1da177e4
LT
137{
138 unsigned long copy;
139
140 while (len > 0) {
141 while (!iov->iov_len)
142 iov++;
143 copy = min_t(unsigned long, len, iov->iov_len);
144
f6762b7a
JA
145 if (atomic) {
146 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
147 return -EFAULT;
148 } else {
149 if (copy_to_user(iov->iov_base, from, copy))
150 return -EFAULT;
151 }
1da177e4
LT
152 from += copy;
153 len -= copy;
154 iov->iov_base += copy;
155 iov->iov_len -= copy;
156 }
157 return 0;
158}
159
f6762b7a
JA
160/*
161 * Attempt to pre-fault in the user memory, so we can use atomic copies.
162 * Returns the number of bytes not faulted in.
163 */
164static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
165{
166 while (!iov->iov_len)
167 iov++;
168
169 while (len > 0) {
170 unsigned long this_len;
171
172 this_len = min_t(unsigned long, len, iov->iov_len);
173 if (fault_in_pages_writeable(iov->iov_base, this_len))
174 break;
175
176 len -= this_len;
177 iov++;
178 }
179
180 return len;
181}
182
183/*
184 * Pre-fault in the user memory, so we can use atomic copies.
185 */
186static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
187{
188 while (!iov->iov_len)
189 iov++;
190
191 while (len > 0) {
192 unsigned long this_len;
193
194 this_len = min_t(unsigned long, len, iov->iov_len);
195 fault_in_pages_readable(iov->iov_base, this_len);
196 len -= this_len;
197 iov++;
198 }
199}
200
341b446b
IM
201static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
202 struct pipe_buffer *buf)
1da177e4
LT
203{
204 struct page *page = buf->page;
205
5274f052
JA
206 /*
207 * If nobody else uses this page, and we don't already have a
208 * temporary page, let's keep track of it as a one-deep
341b446b 209 * allocation cache. (Otherwise just release our reference to it)
5274f052 210 */
341b446b 211 if (page_count(page) == 1 && !pipe->tmp_page)
923f4f23 212 pipe->tmp_page = page;
341b446b
IM
213 else
214 page_cache_release(page);
1da177e4
LT
215}
216
0845718d
JA
217/**
218 * generic_pipe_buf_map - virtually map a pipe buffer
219 * @pipe: the pipe that the buffer belongs to
220 * @buf: the buffer that should be mapped
221 * @atomic: whether to use an atomic map
222 *
223 * Description:
224 * This function returns a kernel virtual address mapping for the
b51d63c6 225 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
0845718d
JA
226 * and the caller has to be careful not to fault before calling
227 * the unmap function.
228 *
2164d334 229 * Note that this function calls kmap_atomic() if @atomic != 0.
0845718d 230 */
f84d7519 231void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
f6762b7a 232 struct pipe_buffer *buf, int atomic)
1da177e4 233{
f6762b7a
JA
234 if (atomic) {
235 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
e8e3c3d6 236 return kmap_atomic(buf->page);
f6762b7a
JA
237 }
238
1da177e4
LT
239 return kmap(buf->page);
240}
51921cb7 241EXPORT_SYMBOL(generic_pipe_buf_map);
1da177e4 242
0845718d
JA
243/**
244 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
245 * @pipe: the pipe that the buffer belongs to
246 * @buf: the buffer that should be unmapped
247 * @map_data: the data that the mapping function returned
248 *
249 * Description:
250 * This function undoes the mapping that ->map() provided.
251 */
f84d7519 252void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
f6762b7a 253 struct pipe_buffer *buf, void *map_data)
1da177e4 254{
f6762b7a
JA
255 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
256 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
e8e3c3d6 257 kunmap_atomic(map_data);
f6762b7a
JA
258 } else
259 kunmap(buf->page);
1da177e4 260}
51921cb7 261EXPORT_SYMBOL(generic_pipe_buf_unmap);
1da177e4 262
0845718d 263/**
b51d63c6 264 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
0845718d
JA
265 * @pipe: the pipe that the buffer belongs to
266 * @buf: the buffer to attempt to steal
267 *
268 * Description:
b51d63c6 269 * This function attempts to steal the &struct page attached to
0845718d
JA
270 * @buf. If successful, this function returns 0 and returns with
271 * the page locked. The caller may then reuse the page for whatever
b51d63c6 272 * he wishes; the typical use is insertion into a different file
0845718d
JA
273 * page cache.
274 */
330ab716
JA
275int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
276 struct pipe_buffer *buf)
5abc97aa 277{
46e678c9
JA
278 struct page *page = buf->page;
279
0845718d
JA
280 /*
281 * A reference of one is golden, that means that the owner of this
282 * page is the only one holding a reference to it. lock the page
283 * and return OK.
284 */
46e678c9 285 if (page_count(page) == 1) {
46e678c9
JA
286 lock_page(page);
287 return 0;
288 }
289
290 return 1;
5abc97aa 291}
51921cb7 292EXPORT_SYMBOL(generic_pipe_buf_steal);
5abc97aa 293
0845718d 294/**
b51d63c6 295 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
0845718d
JA
296 * @pipe: the pipe that the buffer belongs to
297 * @buf: the buffer to get a reference to
298 *
299 * Description:
300 * This function grabs an extra reference to @buf. It's used in
301 * in the tee() system call, when we duplicate the buffers in one
302 * pipe into another.
303 */
304void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
70524490
JA
305{
306 page_cache_get(buf->page);
307}
51921cb7 308EXPORT_SYMBOL(generic_pipe_buf_get);
70524490 309
0845718d
JA
310/**
311 * generic_pipe_buf_confirm - verify contents of the pipe buffer
79685b8d 312 * @info: the pipe that the buffer belongs to
0845718d
JA
313 * @buf: the buffer to confirm
314 *
315 * Description:
316 * This function does nothing, because the generic pipe code uses
317 * pages that are always good when inserted into the pipe.
318 */
cac36bb0
JA
319int generic_pipe_buf_confirm(struct pipe_inode_info *info,
320 struct pipe_buffer *buf)
f84d7519
JA
321{
322 return 0;
323}
51921cb7 324EXPORT_SYMBOL(generic_pipe_buf_confirm);
f84d7519 325
6818173b
MS
326/**
327 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
328 * @pipe: the pipe that the buffer belongs to
329 * @buf: the buffer to put a reference to
330 *
331 * Description:
332 * This function releases a reference to @buf.
333 */
334void generic_pipe_buf_release(struct pipe_inode_info *pipe,
335 struct pipe_buffer *buf)
336{
337 page_cache_release(buf->page);
338}
51921cb7 339EXPORT_SYMBOL(generic_pipe_buf_release);
6818173b 340
d4c3cca9 341static const struct pipe_buf_operations anon_pipe_buf_ops = {
1da177e4 342 .can_merge = 1,
f84d7519
JA
343 .map = generic_pipe_buf_map,
344 .unmap = generic_pipe_buf_unmap,
cac36bb0 345 .confirm = generic_pipe_buf_confirm,
1da177e4 346 .release = anon_pipe_buf_release,
330ab716 347 .steal = generic_pipe_buf_steal,
f84d7519 348 .get = generic_pipe_buf_get,
1da177e4
LT
349};
350
9883035a
LT
351static const struct pipe_buf_operations packet_pipe_buf_ops = {
352 .can_merge = 0,
353 .map = generic_pipe_buf_map,
354 .unmap = generic_pipe_buf_unmap,
355 .confirm = generic_pipe_buf_confirm,
356 .release = anon_pipe_buf_release,
357 .steal = generic_pipe_buf_steal,
358 .get = generic_pipe_buf_get,
359};
360
1da177e4 361static ssize_t
ee0b3e67
BP
362pipe_read(struct kiocb *iocb, const struct iovec *_iov,
363 unsigned long nr_segs, loff_t pos)
1da177e4 364{
ee0b3e67 365 struct file *filp = iocb->ki_filp;
18c03cfd 366 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
1da177e4
LT
367 int do_wakeup;
368 ssize_t ret;
369 struct iovec *iov = (struct iovec *)_iov;
370 size_t total_len;
371
372 total_len = iov_length(iov, nr_segs);
373 /* Null read succeeds. */
374 if (unlikely(total_len == 0))
375 return 0;
376
377 do_wakeup = 0;
378 ret = 0;
18c03cfd 379 pipe_lock(pipe);
1da177e4 380 for (;;) {
923f4f23 381 int bufs = pipe->nrbufs;
1da177e4 382 if (bufs) {
923f4f23
IM
383 int curbuf = pipe->curbuf;
384 struct pipe_buffer *buf = pipe->bufs + curbuf;
d4c3cca9 385 const struct pipe_buf_operations *ops = buf->ops;
1da177e4
LT
386 void *addr;
387 size_t chars = buf->len;
f6762b7a 388 int error, atomic;
1da177e4
LT
389
390 if (chars > total_len)
391 chars = total_len;
392
cac36bb0 393 error = ops->confirm(pipe, buf);
f84d7519 394 if (error) {
5274f052 395 if (!ret)
e5953cbd 396 ret = error;
5274f052
JA
397 break;
398 }
f84d7519 399
f6762b7a
JA
400 atomic = !iov_fault_in_pages_write(iov, chars);
401redo:
402 addr = ops->map(pipe, buf, atomic);
403 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
404 ops->unmap(pipe, buf, addr);
1da177e4 405 if (unlikely(error)) {
f6762b7a
JA
406 /*
407 * Just retry with the slow path if we failed.
408 */
409 if (atomic) {
410 atomic = 0;
411 goto redo;
412 }
341b446b 413 if (!ret)
f6762b7a 414 ret = error;
1da177e4
LT
415 break;
416 }
417 ret += chars;
418 buf->offset += chars;
419 buf->len -= chars;
9883035a
LT
420
421 /* Was it a packet buffer? Clean up and exit */
422 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
423 total_len = chars;
424 buf->len = 0;
425 }
426
1da177e4
LT
427 if (!buf->len) {
428 buf->ops = NULL;
923f4f23 429 ops->release(pipe, buf);
35f3d14d 430 curbuf = (curbuf + 1) & (pipe->buffers - 1);
923f4f23
IM
431 pipe->curbuf = curbuf;
432 pipe->nrbufs = --bufs;
1da177e4
LT
433 do_wakeup = 1;
434 }
435 total_len -= chars;
436 if (!total_len)
437 break; /* common path: read succeeded */
438 }
439 if (bufs) /* More to do? */
440 continue;
923f4f23 441 if (!pipe->writers)
1da177e4 442 break;
923f4f23 443 if (!pipe->waiting_writers) {
1da177e4
LT
444 /* syscall merging: Usually we must not sleep
445 * if O_NONBLOCK is set, or if we got some data.
446 * But if a writer sleeps in kernel space, then
447 * we can wait for that data without violating POSIX.
448 */
449 if (ret)
450 break;
451 if (filp->f_flags & O_NONBLOCK) {
452 ret = -EAGAIN;
453 break;
454 }
455 }
456 if (signal_pending(current)) {
341b446b
IM
457 if (!ret)
458 ret = -ERESTARTSYS;
1da177e4
LT
459 break;
460 }
461 if (do_wakeup) {
28e58ee8 462 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
923f4f23 463 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4 464 }
923f4f23 465 pipe_wait(pipe);
1da177e4 466 }
18c03cfd 467 pipe_unlock(pipe);
341b446b
IM
468
469 /* Signal writers asynchronously that there is more room. */
1da177e4 470 if (do_wakeup) {
28e58ee8 471 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
923f4f23 472 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4
LT
473 }
474 if (ret > 0)
475 file_accessed(filp);
476 return ret;
477}
478
9883035a
LT
479static inline int is_packetized(struct file *file)
480{
481 return (file->f_flags & O_DIRECT) != 0;
482}
483
1da177e4 484static ssize_t
ee0b3e67
BP
485pipe_write(struct kiocb *iocb, const struct iovec *_iov,
486 unsigned long nr_segs, loff_t ppos)
1da177e4 487{
ee0b3e67 488 struct file *filp = iocb->ki_filp;
18c03cfd 489 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
1da177e4
LT
490 ssize_t ret;
491 int do_wakeup;
492 struct iovec *iov = (struct iovec *)_iov;
493 size_t total_len;
494 ssize_t chars;
495
496 total_len = iov_length(iov, nr_segs);
497 /* Null write succeeds. */
498 if (unlikely(total_len == 0))
499 return 0;
500
501 do_wakeup = 0;
502 ret = 0;
18c03cfd 503 pipe_lock(pipe);
1da177e4 504
923f4f23 505 if (!pipe->readers) {
1da177e4
LT
506 send_sig(SIGPIPE, current, 0);
507 ret = -EPIPE;
508 goto out;
509 }
510
511 /* We try to merge small writes */
512 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
923f4f23 513 if (pipe->nrbufs && chars != 0) {
341b446b 514 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
35f3d14d 515 (pipe->buffers - 1);
923f4f23 516 struct pipe_buffer *buf = pipe->bufs + lastbuf;
d4c3cca9 517 const struct pipe_buf_operations *ops = buf->ops;
1da177e4 518 int offset = buf->offset + buf->len;
341b446b 519
1da177e4 520 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
f6762b7a 521 int error, atomic = 1;
5274f052 522 void *addr;
5274f052 523
cac36bb0 524 error = ops->confirm(pipe, buf);
f84d7519 525 if (error)
5274f052 526 goto out;
f84d7519 527
f6762b7a
JA
528 iov_fault_in_pages_read(iov, chars);
529redo1:
530 addr = ops->map(pipe, buf, atomic);
5274f052 531 error = pipe_iov_copy_from_user(offset + addr, iov,
f6762b7a
JA
532 chars, atomic);
533 ops->unmap(pipe, buf, addr);
1da177e4
LT
534 ret = error;
535 do_wakeup = 1;
f6762b7a
JA
536 if (error) {
537 if (atomic) {
538 atomic = 0;
539 goto redo1;
540 }
1da177e4 541 goto out;
f6762b7a 542 }
1da177e4
LT
543 buf->len += chars;
544 total_len -= chars;
545 ret = chars;
546 if (!total_len)
547 goto out;
548 }
549 }
550
551 for (;;) {
552 int bufs;
341b446b 553
923f4f23 554 if (!pipe->readers) {
1da177e4 555 send_sig(SIGPIPE, current, 0);
341b446b
IM
556 if (!ret)
557 ret = -EPIPE;
1da177e4
LT
558 break;
559 }
923f4f23 560 bufs = pipe->nrbufs;
35f3d14d
JA
561 if (bufs < pipe->buffers) {
562 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
923f4f23
IM
563 struct pipe_buffer *buf = pipe->bufs + newbuf;
564 struct page *page = pipe->tmp_page;
f6762b7a
JA
565 char *src;
566 int error, atomic = 1;
1da177e4
LT
567
568 if (!page) {
569 page = alloc_page(GFP_HIGHUSER);
570 if (unlikely(!page)) {
571 ret = ret ? : -ENOMEM;
572 break;
573 }
923f4f23 574 pipe->tmp_page = page;
1da177e4 575 }
341b446b 576 /* Always wake up, even if the copy fails. Otherwise
1da177e4
LT
577 * we lock up (O_NONBLOCK-)readers that sleep due to
578 * syscall merging.
579 * FIXME! Is this really true?
580 */
581 do_wakeup = 1;
582 chars = PAGE_SIZE;
583 if (chars > total_len)
584 chars = total_len;
585
f6762b7a
JA
586 iov_fault_in_pages_read(iov, chars);
587redo2:
588 if (atomic)
e8e3c3d6 589 src = kmap_atomic(page);
f6762b7a
JA
590 else
591 src = kmap(page);
592
593 error = pipe_iov_copy_from_user(src, iov, chars,
594 atomic);
595 if (atomic)
e8e3c3d6 596 kunmap_atomic(src);
f6762b7a
JA
597 else
598 kunmap(page);
599
1da177e4 600 if (unlikely(error)) {
f6762b7a
JA
601 if (atomic) {
602 atomic = 0;
603 goto redo2;
604 }
341b446b 605 if (!ret)
f6762b7a 606 ret = error;
1da177e4
LT
607 break;
608 }
609 ret += chars;
610
611 /* Insert it into the buffer array */
612 buf->page = page;
613 buf->ops = &anon_pipe_buf_ops;
614 buf->offset = 0;
615 buf->len = chars;
9883035a
LT
616 buf->flags = 0;
617 if (is_packetized(filp)) {
618 buf->ops = &packet_pipe_buf_ops;
619 buf->flags = PIPE_BUF_FLAG_PACKET;
620 }
923f4f23
IM
621 pipe->nrbufs = ++bufs;
622 pipe->tmp_page = NULL;
1da177e4
LT
623
624 total_len -= chars;
625 if (!total_len)
626 break;
627 }
35f3d14d 628 if (bufs < pipe->buffers)
1da177e4
LT
629 continue;
630 if (filp->f_flags & O_NONBLOCK) {
341b446b
IM
631 if (!ret)
632 ret = -EAGAIN;
1da177e4
LT
633 break;
634 }
635 if (signal_pending(current)) {
341b446b
IM
636 if (!ret)
637 ret = -ERESTARTSYS;
1da177e4
LT
638 break;
639 }
640 if (do_wakeup) {
28e58ee8 641 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
923f4f23 642 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4
LT
643 do_wakeup = 0;
644 }
923f4f23
IM
645 pipe->waiting_writers++;
646 pipe_wait(pipe);
647 pipe->waiting_writers--;
1da177e4
LT
648 }
649out:
18c03cfd 650 pipe_unlock(pipe);
1da177e4 651 if (do_wakeup) {
28e58ee8 652 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
923f4f23 653 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4 654 }
c3b2da31
JB
655 if (ret > 0) {
656 int err = file_update_time(filp);
657 if (err)
658 ret = err;
659 }
1da177e4
LT
660 return ret;
661}
662
d59d0b1b 663static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4 664{
18c03cfd 665 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
1da177e4
LT
666 int count, buf, nrbufs;
667
668 switch (cmd) {
669 case FIONREAD:
18c03cfd 670 pipe_lock(pipe);
1da177e4 671 count = 0;
923f4f23
IM
672 buf = pipe->curbuf;
673 nrbufs = pipe->nrbufs;
1da177e4 674 while (--nrbufs >= 0) {
923f4f23 675 count += pipe->bufs[buf].len;
35f3d14d 676 buf = (buf+1) & (pipe->buffers - 1);
1da177e4 677 }
18c03cfd 678 pipe_unlock(pipe);
923f4f23 679
1da177e4
LT
680 return put_user(count, (int __user *)arg);
681 default:
46ce341b 682 return -ENOIOCTLCMD;
1da177e4
LT
683 }
684}
685
686/* No kernel lock held - fine */
687static unsigned int
688pipe_poll(struct file *filp, poll_table *wait)
689{
690 unsigned int mask;
18c03cfd 691 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
1da177e4
LT
692 int nrbufs;
693
923f4f23 694 poll_wait(filp, &pipe->wait, wait);
1da177e4
LT
695
696 /* Reading only -- no need for acquiring the semaphore. */
923f4f23 697 nrbufs = pipe->nrbufs;
1da177e4
LT
698 mask = 0;
699 if (filp->f_mode & FMODE_READ) {
700 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
923f4f23 701 if (!pipe->writers && filp->f_version != pipe->w_counter)
1da177e4
LT
702 mask |= POLLHUP;
703 }
704
705 if (filp->f_mode & FMODE_WRITE) {
35f3d14d 706 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
5e5d7a22
PE
707 /*
708 * Most Unices do not set POLLERR for FIFOs but on Linux they
709 * behave exactly like pipes for poll().
710 */
923f4f23 711 if (!pipe->readers)
1da177e4
LT
712 mask |= POLLERR;
713 }
714
715 return mask;
716}
717
1da177e4 718static int
599a0ac1 719pipe_release(struct inode *inode, struct file *file)
1da177e4 720{
ba5bb147
AV
721 struct pipe_inode_info *pipe = inode->i_pipe;
722 int kill = 0;
923f4f23 723
ba5bb147 724 pipe_lock(pipe);
599a0ac1
AV
725 if (file->f_mode & FMODE_READ)
726 pipe->readers--;
727 if (file->f_mode & FMODE_WRITE)
728 pipe->writers--;
341b446b 729
ba5bb147 730 if (pipe->readers || pipe->writers) {
28e58ee8 731 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
923f4f23
IM
732 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
733 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4 734 }
ba5bb147
AV
735 spin_lock(&inode->i_lock);
736 if (!--pipe->files) {
737 inode->i_pipe = NULL;
738 kill = 1;
739 }
740 spin_unlock(&inode->i_lock);
741 pipe_unlock(pipe);
742
743 if (kill)
744 __free_pipe_info(pipe);
1da177e4
LT
745
746 return 0;
747}
748
749static int
599a0ac1 750pipe_fasync(int fd, struct file *filp, int on)
1da177e4 751{
18c03cfd 752 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
599a0ac1 753 int retval = 0;
1da177e4 754
18c03cfd 755 pipe_lock(pipe);
599a0ac1
AV
756 if (filp->f_mode & FMODE_READ)
757 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
758 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
341b446b 759 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
599a0ac1
AV
760 if (retval < 0 && (filp->f_mode & FMODE_READ))
761 /* this can happen only if on == T */
e5bc49ba
ON
762 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
763 }
18c03cfd 764 pipe_unlock(pipe);
60aa4924 765 return retval;
1da177e4
LT
766}
767
3a326a2c
IM
768struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
769{
923f4f23 770 struct pipe_inode_info *pipe;
3a326a2c 771
923f4f23
IM
772 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
773 if (pipe) {
35f3d14d
JA
774 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
775 if (pipe->bufs) {
776 init_waitqueue_head(&pipe->wait);
777 pipe->r_counter = pipe->w_counter = 1;
778 pipe->inode = inode;
779 pipe->buffers = PIPE_DEF_BUFFERS;
72b0d9aa 780 mutex_init(&pipe->mutex);
35f3d14d
JA
781 return pipe;
782 }
783 kfree(pipe);
3a326a2c
IM
784 }
785
35f3d14d 786 return NULL;
3a326a2c
IM
787}
788
923f4f23 789void __free_pipe_info(struct pipe_inode_info *pipe)
1da177e4
LT
790{
791 int i;
1da177e4 792
35f3d14d 793 for (i = 0; i < pipe->buffers; i++) {
923f4f23 794 struct pipe_buffer *buf = pipe->bufs + i;
1da177e4 795 if (buf->ops)
923f4f23 796 buf->ops->release(pipe, buf);
1da177e4 797 }
923f4f23
IM
798 if (pipe->tmp_page)
799 __free_page(pipe->tmp_page);
35f3d14d 800 kfree(pipe->bufs);
923f4f23 801 kfree(pipe);
1da177e4
LT
802}
803
b92ce558
JA
804void free_pipe_info(struct inode *inode)
805{
806 __free_pipe_info(inode->i_pipe);
807 inode->i_pipe = NULL;
808}
809
fa3536cc 810static struct vfsmount *pipe_mnt __read_mostly;
341b446b 811
c23fbb6b
ED
812/*
813 * pipefs_dname() is called from d_path().
814 */
815static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
816{
817 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
818 dentry->d_inode->i_ino);
819}
820
3ba13d17 821static const struct dentry_operations pipefs_dentry_operations = {
c23fbb6b 822 .d_dname = pipefs_dname,
1da177e4
LT
823};
824
825static struct inode * get_pipe_inode(void)
826{
a209dfc7 827 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
923f4f23 828 struct pipe_inode_info *pipe;
1da177e4
LT
829
830 if (!inode)
831 goto fail_inode;
832
85fe4025
CH
833 inode->i_ino = get_next_ino();
834
923f4f23
IM
835 pipe = alloc_pipe_info(inode);
836 if (!pipe)
1da177e4 837 goto fail_iput;
3a326a2c 838
ba5bb147
AV
839 inode->i_pipe = pipe;
840 pipe->files = 2;
923f4f23 841 pipe->readers = pipe->writers = 1;
599a0ac1 842 inode->i_fop = &pipefifo_fops;
1da177e4
LT
843
844 /*
845 * Mark the inode dirty from the very beginning,
846 * that way it will never be moved to the dirty
847 * list because "mark_inode_dirty()" will think
848 * that it already _is_ on the dirty list.
849 */
850 inode->i_state = I_DIRTY;
851 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
da9592ed
DH
852 inode->i_uid = current_fsuid();
853 inode->i_gid = current_fsgid();
1da177e4 854 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
923f4f23 855
1da177e4
LT
856 return inode;
857
858fail_iput:
859 iput(inode);
341b446b 860
1da177e4
LT
861fail_inode:
862 return NULL;
863}
864
e4fad8e5 865int create_pipe_files(struct file **res, int flags)
1da177e4 866{
d6cbd281 867 int err;
e4fad8e5 868 struct inode *inode = get_pipe_inode();
d6cbd281 869 struct file *f;
2c48b9c4 870 struct path path;
e4fad8e5 871 static struct qstr name = { .name = "" };
1da177e4 872
1da177e4 873 if (!inode)
e4fad8e5 874 return -ENFILE;
1da177e4 875
d6cbd281 876 err = -ENOMEM;
4b936885 877 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
2c48b9c4 878 if (!path.dentry)
d6cbd281 879 goto err_inode;
2c48b9c4 880 path.mnt = mntget(pipe_mnt);
341b446b 881
2c48b9c4 882 d_instantiate(path.dentry, inode);
430e285e
DH
883
884 err = -ENFILE;
599a0ac1 885 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
39b65252 886 if (IS_ERR(f))
430e285e 887 goto err_dentry;
341b446b 888
9883035a 889 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
d6cbd281 890
599a0ac1 891 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
39b65252 892 if (IS_ERR(res[0]))
e4fad8e5
AV
893 goto err_file;
894
895 path_get(&path);
896 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
897 res[1] = f;
898 return 0;
1da177e4 899
e4fad8e5
AV
900err_file:
901 put_filp(f);
902err_dentry:
ed152437 903 free_pipe_info(inode);
2c48b9c4 904 path_put(&path);
e4fad8e5 905 return err;
ed152437 906
e4fad8e5 907err_inode:
1da177e4
LT
908 free_pipe_info(inode);
909 iput(inode);
e4fad8e5 910 return err;
d6cbd281
AK
911}
912
5b249b1b 913static int __do_pipe_flags(int *fd, struct file **files, int flags)
d6cbd281 914{
d6cbd281
AK
915 int error;
916 int fdw, fdr;
917
9883035a 918 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
ed8cae8b
UD
919 return -EINVAL;
920
e4fad8e5
AV
921 error = create_pipe_files(files, flags);
922 if (error)
923 return error;
d6cbd281 924
ed8cae8b 925 error = get_unused_fd_flags(flags);
d6cbd281
AK
926 if (error < 0)
927 goto err_read_pipe;
928 fdr = error;
929
ed8cae8b 930 error = get_unused_fd_flags(flags);
d6cbd281
AK
931 if (error < 0)
932 goto err_fdr;
933 fdw = error;
934
157cf649 935 audit_fd_pair(fdr, fdw);
d6cbd281
AK
936 fd[0] = fdr;
937 fd[1] = fdw;
d6cbd281
AK
938 return 0;
939
940 err_fdr:
941 put_unused_fd(fdr);
942 err_read_pipe:
e4fad8e5
AV
943 fput(files[0]);
944 fput(files[1]);
d6cbd281 945 return error;
1da177e4
LT
946}
947
5b249b1b
AV
948int do_pipe_flags(int *fd, int flags)
949{
950 struct file *files[2];
951 int error = __do_pipe_flags(fd, files, flags);
952 if (!error) {
953 fd_install(fd[0], files[0]);
954 fd_install(fd[1], files[1]);
955 }
956 return error;
957}
958
d35c7b0e
UD
959/*
960 * sys_pipe() is the normal C calling standard for creating
961 * a pipe. It's not the way Unix traditionally does this, though.
962 */
d4e82042 963SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
d35c7b0e 964{
5b249b1b 965 struct file *files[2];
d35c7b0e
UD
966 int fd[2];
967 int error;
968
5b249b1b 969 error = __do_pipe_flags(fd, files, flags);
d35c7b0e 970 if (!error) {
5b249b1b
AV
971 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
972 fput(files[0]);
973 fput(files[1]);
974 put_unused_fd(fd[0]);
975 put_unused_fd(fd[1]);
d35c7b0e 976 error = -EFAULT;
5b249b1b
AV
977 } else {
978 fd_install(fd[0], files[0]);
979 fd_install(fd[1], files[1]);
ba719bae 980 }
d35c7b0e
UD
981 }
982 return error;
983}
984
2b664219 985SYSCALL_DEFINE1(pipe, int __user *, fildes)
ed8cae8b
UD
986{
987 return sys_pipe2(fildes, 0);
988}
989
fc7478a2 990static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
f776c738
AV
991{
992 int cur = *cnt;
993
994 while (cur == *cnt) {
fc7478a2 995 pipe_wait(pipe);
f776c738
AV
996 if (signal_pending(current))
997 break;
998 }
999 return cur == *cnt ? -ERESTARTSYS : 0;
1000}
1001
fc7478a2 1002static void wake_up_partner(struct pipe_inode_info *pipe)
f776c738 1003{
fc7478a2 1004 wake_up_interruptible(&pipe->wait);
f776c738
AV
1005}
1006
1007static int fifo_open(struct inode *inode, struct file *filp)
1008{
1009 struct pipe_inode_info *pipe;
599a0ac1 1010 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
ba5bb147 1011 int kill = 0;
f776c738
AV
1012 int ret;
1013
ba5bb147
AV
1014 filp->f_version = 0;
1015
1016 spin_lock(&inode->i_lock);
1017 if (inode->i_pipe) {
1018 pipe = inode->i_pipe;
1019 pipe->files++;
1020 spin_unlock(&inode->i_lock);
1021 } else {
1022 spin_unlock(&inode->i_lock);
f776c738
AV
1023 pipe = alloc_pipe_info(inode);
1024 if (!pipe)
ba5bb147
AV
1025 return -ENOMEM;
1026 pipe->files = 1;
1027 spin_lock(&inode->i_lock);
1028 if (unlikely(inode->i_pipe)) {
1029 inode->i_pipe->files++;
1030 spin_unlock(&inode->i_lock);
1031 __free_pipe_info(pipe);
1032 pipe = inode->i_pipe;
1033 } else {
1034 inode->i_pipe = pipe;
1035 spin_unlock(&inode->i_lock);
1036 }
f776c738 1037 }
ba5bb147
AV
1038 /* OK, we have a pipe and it's pinned down */
1039
1040 pipe_lock(pipe);
f776c738
AV
1041
1042 /* We can only do regular read/write on fifos */
1043 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
1044
1045 switch (filp->f_mode) {
1046 case FMODE_READ:
1047 /*
1048 * O_RDONLY
1049 * POSIX.1 says that O_NONBLOCK means return with the FIFO
1050 * opened, even when there is no process writing the FIFO.
1051 */
f776c738
AV
1052 pipe->r_counter++;
1053 if (pipe->readers++ == 0)
fc7478a2 1054 wake_up_partner(pipe);
f776c738 1055
599a0ac1 1056 if (!is_pipe && !pipe->writers) {
f776c738
AV
1057 if ((filp->f_flags & O_NONBLOCK)) {
1058 /* suppress POLLHUP until we have
1059 * seen a writer */
1060 filp->f_version = pipe->w_counter;
1061 } else {
fc7478a2 1062 if (wait_for_partner(pipe, &pipe->w_counter))
f776c738
AV
1063 goto err_rd;
1064 }
1065 }
1066 break;
1067
1068 case FMODE_WRITE:
1069 /*
1070 * O_WRONLY
1071 * POSIX.1 says that O_NONBLOCK means return -1 with
1072 * errno=ENXIO when there is no process reading the FIFO.
1073 */
1074 ret = -ENXIO;
599a0ac1 1075 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
f776c738
AV
1076 goto err;
1077
f776c738
AV
1078 pipe->w_counter++;
1079 if (!pipe->writers++)
fc7478a2 1080 wake_up_partner(pipe);
f776c738 1081
599a0ac1 1082 if (!is_pipe && !pipe->readers) {
fc7478a2 1083 if (wait_for_partner(pipe, &pipe->r_counter))
f776c738
AV
1084 goto err_wr;
1085 }
1086 break;
1087
1088 case FMODE_READ | FMODE_WRITE:
1089 /*
1090 * O_RDWR
1091 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1092 * This implementation will NEVER block on a O_RDWR open, since
1093 * the process can at least talk to itself.
1094 */
f776c738
AV
1095
1096 pipe->readers++;
1097 pipe->writers++;
1098 pipe->r_counter++;
1099 pipe->w_counter++;
1100 if (pipe->readers == 1 || pipe->writers == 1)
fc7478a2 1101 wake_up_partner(pipe);
f776c738
AV
1102 break;
1103
1104 default:
1105 ret = -EINVAL;
1106 goto err;
1107 }
1108
1109 /* Ok! */
ba5bb147 1110 pipe_unlock(pipe);
f776c738
AV
1111 return 0;
1112
1113err_rd:
1114 if (!--pipe->readers)
1115 wake_up_interruptible(&pipe->wait);
1116 ret = -ERESTARTSYS;
1117 goto err;
1118
1119err_wr:
1120 if (!--pipe->writers)
1121 wake_up_interruptible(&pipe->wait);
1122 ret = -ERESTARTSYS;
1123 goto err;
1124
1125err:
ba5bb147
AV
1126 spin_lock(&inode->i_lock);
1127 if (!--pipe->files) {
1128 inode->i_pipe = NULL;
1129 kill = 1;
1130 }
1131 spin_unlock(&inode->i_lock);
1132 pipe_unlock(pipe);
1133 if (kill)
1134 __free_pipe_info(pipe);
f776c738
AV
1135 return ret;
1136}
1137
599a0ac1
AV
1138const struct file_operations pipefifo_fops = {
1139 .open = fifo_open,
1140 .llseek = no_llseek,
1141 .read = do_sync_read,
1142 .aio_read = pipe_read,
1143 .write = do_sync_write,
1144 .aio_write = pipe_write,
1145 .poll = pipe_poll,
1146 .unlocked_ioctl = pipe_ioctl,
1147 .release = pipe_release,
1148 .fasync = pipe_fasync,
f776c738
AV
1149};
1150
35f3d14d
JA
1151/*
1152 * Allocate a new array of pipe buffers and copy the info over. Returns the
1153 * pipe size if successful, or return -ERROR on error.
1154 */
b9598db3 1155static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
35f3d14d
JA
1156{
1157 struct pipe_buffer *bufs;
1158
35f3d14d
JA
1159 /*
1160 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1161 * expect a lot of shrink+grow operations, just free and allocate
1162 * again like we would do for growing. If the pipe currently
1163 * contains more buffers than arg, then return busy.
1164 */
b9598db3 1165 if (nr_pages < pipe->nrbufs)
35f3d14d
JA
1166 return -EBUSY;
1167
2ccd4f4d 1168 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
35f3d14d
JA
1169 if (unlikely(!bufs))
1170 return -ENOMEM;
1171
1172 /*
1173 * The pipe array wraps around, so just start the new one at zero
1174 * and adjust the indexes.
1175 */
1176 if (pipe->nrbufs) {
1d862f41
MS
1177 unsigned int tail;
1178 unsigned int head;
35f3d14d 1179
1d862f41
MS
1180 tail = pipe->curbuf + pipe->nrbufs;
1181 if (tail < pipe->buffers)
1182 tail = 0;
1183 else
1184 tail &= (pipe->buffers - 1);
1185
1186 head = pipe->nrbufs - tail;
35f3d14d
JA
1187 if (head)
1188 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1189 if (tail)
1d862f41 1190 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
35f3d14d
JA
1191 }
1192
1193 pipe->curbuf = 0;
1194 kfree(pipe->bufs);
1195 pipe->bufs = bufs;
b9598db3
JA
1196 pipe->buffers = nr_pages;
1197 return nr_pages * PAGE_SIZE;
35f3d14d
JA
1198}
1199
ff9da691
JA
1200/*
1201 * Currently we rely on the pipe array holding a power-of-2 number
1202 * of pages.
1203 */
1204static inline unsigned int round_pipe_size(unsigned int size)
1205{
1206 unsigned long nr_pages;
1207
1208 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1209 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1210}
1211
1212/*
1213 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1214 * will return an error.
1215 */
1216int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1217 size_t *lenp, loff_t *ppos)
1218{
1219 int ret;
1220
1221 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1222 if (ret < 0 || !write)
1223 return ret;
1224
1225 pipe_max_size = round_pipe_size(pipe_max_size);
1226 return ret;
1227}
1228
72083646
LT
1229/*
1230 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1231 * location, so checking ->i_pipe is not enough to verify that this is a
1232 * pipe.
1233 */
1234struct pipe_inode_info *get_pipe_info(struct file *file)
1235{
496ad9aa 1236 struct inode *i = file_inode(file);
72083646
LT
1237
1238 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1239}
1240
35f3d14d
JA
1241long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1242{
1243 struct pipe_inode_info *pipe;
1244 long ret;
1245
c66fb347 1246 pipe = get_pipe_info(file);
35f3d14d
JA
1247 if (!pipe)
1248 return -EBADF;
1249
18c03cfd 1250 pipe_lock(pipe);
35f3d14d
JA
1251
1252 switch (cmd) {
b9598db3 1253 case F_SETPIPE_SZ: {
ff9da691 1254 unsigned int size, nr_pages;
b9598db3 1255
ff9da691
JA
1256 size = round_pipe_size(arg);
1257 nr_pages = size >> PAGE_SHIFT;
b9598db3 1258
6db40cf0
MS
1259 ret = -EINVAL;
1260 if (!nr_pages)
1261 goto out;
1262
ff9da691 1263 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
b4ca7615 1264 ret = -EPERM;
cc967be5 1265 goto out;
cc967be5 1266 }
ff9da691 1267 ret = pipe_set_size(pipe, nr_pages);
35f3d14d 1268 break;
b9598db3 1269 }
35f3d14d 1270 case F_GETPIPE_SZ:
b9598db3 1271 ret = pipe->buffers * PAGE_SIZE;
35f3d14d
JA
1272 break;
1273 default:
1274 ret = -EINVAL;
1275 break;
1276 }
1277
cc967be5 1278out:
18c03cfd 1279 pipe_unlock(pipe);
35f3d14d
JA
1280 return ret;
1281}
1282
ff0c7d15
NP
1283static const struct super_operations pipefs_ops = {
1284 .destroy_inode = free_inode_nonrcu,
d70ef97b 1285 .statfs = simple_statfs,
ff0c7d15
NP
1286};
1287
1da177e4
LT
1288/*
1289 * pipefs should _never_ be mounted by userland - too much of security hassle,
1290 * no real gain from having the whole whorehouse mounted. So we don't need
1291 * any operations on the root directory. However, we need a non-trivial
1292 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1293 */
51139ada
AV
1294static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1295 int flags, const char *dev_name, void *data)
1da177e4 1296{
c74a1cbb
AV
1297 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1298 &pipefs_dentry_operations, PIPEFS_MAGIC);
1da177e4
LT
1299}
1300
1301static struct file_system_type pipe_fs_type = {
1302 .name = "pipefs",
51139ada 1303 .mount = pipefs_mount,
1da177e4
LT
1304 .kill_sb = kill_anon_super,
1305};
1306
1307static int __init init_pipe_fs(void)
1308{
1309 int err = register_filesystem(&pipe_fs_type);
341b446b 1310
1da177e4
LT
1311 if (!err) {
1312 pipe_mnt = kern_mount(&pipe_fs_type);
1313 if (IS_ERR(pipe_mnt)) {
1314 err = PTR_ERR(pipe_mnt);
1315 unregister_filesystem(&pipe_fs_type);
1316 }
1317 }
1318 return err;
1319}
1320
1da177e4 1321fs_initcall(init_pipe_fs);