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