blob: 7b1954caf3885d1c24458aa3a1843802c0506a09 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Jens Axboe35f3d142010-05-20 10:43:18 +020015#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mount.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070017#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/pipe_fs_i.h>
19#include <linux/uio.h>
20#include <linux/highmem.h>
Jens Axboe5274f052006-03-30 15:15:30 +020021#include <linux/pagemap.h>
Al Virodb349502007-02-07 01:48:00 -050022#include <linux/audit.h>
Ulrich Drepperba719ba2008-05-06 20:42:38 -070023#include <linux/syscalls.h>
Jens Axboeb492e952010-05-19 21:03:16 +020024#include <linux/fcntl.h>
Vladimir Davydovd86133b2016-07-26 15:24:33 -070025#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/ioctls.h>
29
Al Viro599a0ac2013-03-12 09:58:10 -040030#include "internal.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
Jens Axboeb492e952010-05-19 21:03:16 +020033 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020034 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020035 */
Jens Axboeff9da692010-06-03 14:54:39 +020036unsigned int pipe_max_size = 1048576;
37
Willy Tarreau759c0112016-01-18 16:36:09 +010038/* Maximum allocatable pages per user. Hard limit is unset by default, soft
39 * matches default values.
40 */
41unsigned long pipe_user_pages_hard;
42unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
43
Jens Axboeb492e952010-05-19 21:03:16 +020044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * We use a start+len construction, which provides full use of the
46 * allocated memory.
47 * -- Florian Coosmann (FGC)
48 *
49 * Reads with count = 0 should always return 0.
50 * -- Julian Bradfield 1999-06-07.
51 *
52 * FIFOs and Pipes now generate SIGIO for both readers and writers.
53 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
54 *
55 * pipe_read & write cleanup
56 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
57 */
58
Miklos Szeredi61e0d472009-04-14 19:48:41 +020059static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
60{
Al Viro6447a3c2013-03-21 11:01:38 -040061 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040062 mutex_lock_nested(&pipe->mutex, subclass);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020063}
64
65void pipe_lock(struct pipe_inode_info *pipe)
66{
67 /*
68 * pipe_lock() nests non-pipe inode locks (for writing to a file)
69 */
70 pipe_lock_nested(pipe, I_MUTEX_PARENT);
71}
72EXPORT_SYMBOL(pipe_lock);
73
74void pipe_unlock(struct pipe_inode_info *pipe)
75{
Al Viro6447a3c2013-03-21 11:01:38 -040076 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040077 mutex_unlock(&pipe->mutex);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020078}
79EXPORT_SYMBOL(pipe_unlock);
80
Al Viroebec73f2013-03-21 12:24:01 -040081static inline void __pipe_lock(struct pipe_inode_info *pipe)
82{
83 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
84}
85
86static inline void __pipe_unlock(struct pipe_inode_info *pipe)
87{
88 mutex_unlock(&pipe->mutex);
89}
90
Miklos Szeredi61e0d472009-04-14 19:48:41 +020091void pipe_double_lock(struct pipe_inode_info *pipe1,
92 struct pipe_inode_info *pipe2)
93{
94 BUG_ON(pipe1 == pipe2);
95
96 if (pipe1 < pipe2) {
97 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
98 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
99 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +0200100 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
101 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200102 }
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200106void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 DEFINE_WAIT(wait);
109
Ingo Molnard79fc0f2005-09-10 00:26:12 -0700110 /*
111 * Pipes are system-local resources, so sleeping on them
112 * is considered a noninteractive wait:
113 */
Mike Galbraithaf927232007-10-15 17:00:13 +0200114 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200115 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200117 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200118 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Ingo Molnar341b4462006-04-11 13:57:45 +0200121static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
122 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct page *page = buf->page;
125
Jens Axboe5274f052006-03-30 15:15:30 +0200126 /*
127 * If nobody else uses this page, and we don't already have a
128 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200129 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200130 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200131 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200132 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200133 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300134 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700137static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
138 struct pipe_buffer *buf)
139{
140 struct page *page = buf->page;
141
142 if (page_count(page) == 1) {
Vladimir Davydovc4159a72016-08-08 23:03:12 +0300143 if (memcg_kmem_enabled())
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700144 memcg_kmem_uncharge(page, 0);
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700145 __SetPageLocked(page);
146 return 0;
147 }
148 return 1;
149}
150
Jens Axboe08457182007-06-12 20:51:32 +0200151/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800152 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200153 * @pipe: the pipe that the buffer belongs to
154 * @buf: the buffer to attempt to steal
155 *
156 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800157 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200158 * @buf. If successful, this function returns 0 and returns with
159 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800160 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200161 * page cache.
162 */
Jens Axboe330ab712006-05-02 15:29:57 +0200163int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
164 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200165{
Jens Axboe46e678c2006-04-30 16:36:32 +0200166 struct page *page = buf->page;
167
Jens Axboe08457182007-06-12 20:51:32 +0200168 /*
169 * A reference of one is golden, that means that the owner of this
170 * page is the only one holding a reference to it. lock the page
171 * and return OK.
172 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200173 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200174 lock_page(page);
175 return 0;
176 }
177
178 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200179}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200180EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200181
Jens Axboe08457182007-06-12 20:51:32 +0200182/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800183 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200184 * @pipe: the pipe that the buffer belongs to
185 * @buf: the buffer to get a reference to
186 *
187 * Description:
188 * This function grabs an extra reference to @buf. It's used in
189 * in the tee() system call, when we duplicate the buffers in one
190 * pipe into another.
191 */
192void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200193{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300194 get_page(buf->page);
Jens Axboe70524492006-04-11 15:51:17 +0200195}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200196EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200197
Jens Axboe08457182007-06-12 20:51:32 +0200198/**
199 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200200 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200201 * @buf: the buffer to confirm
202 *
203 * Description:
204 * This function does nothing, because the generic pipe code uses
205 * pages that are always good when inserted into the pipe.
206 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200207int generic_pipe_buf_confirm(struct pipe_inode_info *info,
208 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200209{
210 return 0;
211}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200212EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200213
Miklos Szeredi68181732009-05-07 15:37:36 +0200214/**
215 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
216 * @pipe: the pipe that the buffer belongs to
217 * @buf: the buffer to put a reference to
218 *
219 * Description:
220 * This function releases a reference to @buf.
221 */
222void generic_pipe_buf_release(struct pipe_inode_info *pipe,
223 struct pipe_buffer *buf)
224{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300225 put_page(buf->page);
Miklos Szeredi68181732009-05-07 15:37:36 +0200226}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200227EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200228
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800229static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .can_merge = 1,
Jens Axboecac36bb02007-06-14 13:10:48 +0200231 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .release = anon_pipe_buf_release,
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700233 .steal = anon_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200234 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
Linus Torvalds98830352012-04-29 13:12:42 -0700237static const struct pipe_buf_operations packet_pipe_buf_ops = {
238 .can_merge = 0,
Linus Torvalds98830352012-04-29 13:12:42 -0700239 .confirm = generic_pipe_buf_confirm,
240 .release = anon_pipe_buf_release,
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700241 .steal = anon_pipe_buf_steal,
Linus Torvalds98830352012-04-29 13:12:42 -0700242 .get = generic_pipe_buf_get,
243};
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static ssize_t
Al Virofb9096a2014-04-02 19:56:54 -0400246pipe_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Al Virofb9096a2014-04-02 19:56:54 -0400248 size_t total_len = iov_iter_count(to);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700249 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400250 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int do_wakeup;
252 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Null read succeeds. */
255 if (unlikely(total_len == 0))
256 return 0;
257
258 do_wakeup = 0;
259 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400260 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200262 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200264 int curbuf = pipe->curbuf;
265 struct pipe_buffer *buf = pipe->bufs + curbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500267 size_t written;
268 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (chars > total_len)
271 chars = total_len;
272
Miklos Szeredifba597d2016-09-27 10:45:12 +0200273 error = pipe_buf_confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200274 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200275 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200276 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200277 break;
278 }
Jens Axboef84d7512006-05-01 19:59:03 +0200279
Al Virofb9096a2014-04-02 19:56:54 -0400280 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
Al Viro637b58c2014-02-03 19:11:42 -0500281 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200282 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500283 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 }
286 ret += chars;
287 buf->offset += chars;
288 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700289
290 /* Was it a packet buffer? Clean up and exit */
291 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
292 total_len = chars;
293 buf->len = 0;
294 }
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!buf->len) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200297 pipe_buf_release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200298 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200299 pipe->curbuf = curbuf;
300 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 do_wakeup = 1;
302 }
303 total_len -= chars;
304 if (!total_len)
305 break; /* common path: read succeeded */
306 }
307 if (bufs) /* More to do? */
308 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200309 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200311 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* syscall merging: Usually we must not sleep
313 * if O_NONBLOCK is set, or if we got some data.
314 * But if a writer sleeps in kernel space, then
315 * we can wait for that data without violating POSIX.
316 */
317 if (ret)
318 break;
319 if (filp->f_flags & O_NONBLOCK) {
320 ret = -EAGAIN;
321 break;
322 }
323 }
324 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200325 if (!ret)
326 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 }
329 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800330 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200331 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200333 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Al Viroebec73f2013-03-21 12:24:01 -0400335 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200336
337 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800339 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200340 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 if (ret > 0)
343 file_accessed(filp);
344 return ret;
345}
346
Linus Torvalds98830352012-04-29 13:12:42 -0700347static inline int is_packetized(struct file *file)
348{
349 return (file->f_flags & O_DIRECT) != 0;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static ssize_t
Al Virof0d1bec2014-04-03 15:05:18 -0400353pipe_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700355 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400356 struct pipe_inode_info *pipe = filp->private_data;
Al Virof0d1bec2014-04-03 15:05:18 -0400357 ssize_t ret = 0;
358 int do_wakeup = 0;
359 size_t total_len = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 ssize_t chars;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Null write succeeds. */
363 if (unlikely(total_len == 0))
364 return 0;
365
Al Viroebec73f2013-03-21 12:24:01 -0400366 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Ingo Molnar923f4f22006-04-11 13:53:33 +0200368 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 send_sig(SIGPIPE, current, 0);
370 ret = -EPIPE;
371 goto out;
372 }
373
374 /* We try to merge small writes */
375 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200376 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200377 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200378 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200379 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200381
Miklos Szeredifba597d2016-09-27 10:45:12 +0200382 if (buf->ops->can_merge && offset + chars <= PAGE_SIZE) {
383 ret = pipe_buf_confirm(pipe, buf);
Eric Biggers6ae08062015-10-17 16:26:09 -0500384 if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200385 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200386
Al Virof0d1bec2014-04-03 15:05:18 -0400387 ret = copy_page_from_iter(buf->page, offset, chars, from);
388 if (unlikely(ret < chars)) {
Eric Biggers6ae08062015-10-17 16:26:09 -0500389 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200391 }
Al Virof0d1bec2014-04-03 15:05:18 -0400392 do_wakeup = 1;
Eric Biggers6ae08062015-10-17 16:26:09 -0500393 buf->len += ret;
Al Virof0d1bec2014-04-03 15:05:18 -0400394 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto out;
396 }
397 }
398
399 for (;;) {
400 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200401
Ingo Molnar923f4f22006-04-11 13:53:33 +0200402 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200404 if (!ret)
405 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 break;
407 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200408 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200409 if (bufs < pipe->buffers) {
410 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200411 struct pipe_buffer *buf = pipe->bufs + newbuf;
412 struct page *page = pipe->tmp_page;
Al Virof0d1bec2014-04-03 15:05:18 -0400413 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 if (!page) {
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700416 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (unlikely(!page)) {
418 ret = ret ? : -ENOMEM;
419 break;
420 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200421 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200423 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * we lock up (O_NONBLOCK-)readers that sleep due to
425 * syscall merging.
426 * FIXME! Is this really true?
427 */
428 do_wakeup = 1;
Al Virof0d1bec2014-04-03 15:05:18 -0400429 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
430 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200431 if (!ret)
Al Virof0d1bec2014-04-03 15:05:18 -0400432 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 }
Al Virof0d1bec2014-04-03 15:05:18 -0400435 ret += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* Insert it into the buffer array */
438 buf->page = page;
439 buf->ops = &anon_pipe_buf_ops;
440 buf->offset = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400441 buf->len = copied;
Linus Torvalds98830352012-04-29 13:12:42 -0700442 buf->flags = 0;
443 if (is_packetized(filp)) {
444 buf->ops = &packet_pipe_buf_ops;
445 buf->flags = PIPE_BUF_FLAG_PACKET;
446 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200447 pipe->nrbufs = ++bufs;
448 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Al Virof0d1bec2014-04-03 15:05:18 -0400450 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200453 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 continue;
455 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200456 if (!ret)
457 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
459 }
460 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200461 if (!ret)
462 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 }
465 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800466 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200467 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 do_wakeup = 0;
469 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200470 pipe->waiting_writers++;
471 pipe_wait(pipe);
472 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474out:
Al Viroebec73f2013-03-21 12:24:01 -0400475 __pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800477 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200478 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800480 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400481 int err = file_update_time(filp);
482 if (err)
483 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800484 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return ret;
487}
488
Andi Kleend59d0b12008-02-08 04:21:23 -0800489static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Al Virode32ec42013-03-21 11:16:56 -0400491 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int count, buf, nrbufs;
493
494 switch (cmd) {
495 case FIONREAD:
Al Viroebec73f2013-03-21 12:24:01 -0400496 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200498 buf = pipe->curbuf;
499 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200501 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200502 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Al Viroebec73f2013-03-21 12:24:01 -0400504 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return put_user(count, (int __user *)arg);
507 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100508 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510}
511
512/* No kernel lock held - fine */
Al Viro076ccb72017-07-03 01:02:18 -0400513static __poll_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514pipe_poll(struct file *filp, poll_table *wait)
515{
Al Viro076ccb72017-07-03 01:02:18 -0400516 __poll_t mask;
Al Virode32ec42013-03-21 11:16:56 -0400517 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int nrbufs;
519
Ingo Molnar923f4f22006-04-11 13:53:33 +0200520 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200523 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 mask = 0;
525 if (filp->f_mode & FMODE_READ) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800526 mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200527 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800528 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 if (filp->f_mode & FMODE_WRITE) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800532 mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700533 /*
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800534 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700535 * behave exactly like pipes for poll().
536 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200537 if (!pipe->readers)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800538 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 return mask;
542}
543
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800544static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
545{
546 int kill = 0;
547
548 spin_lock(&inode->i_lock);
549 if (!--pipe->files) {
550 inode->i_pipe = NULL;
551 kill = 1;
552 }
553 spin_unlock(&inode->i_lock);
554
555 if (kill)
556 free_pipe_info(pipe);
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static int
Al Viro599a0ac2013-03-12 09:58:10 -0400560pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800562 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200563
Al Viroebec73f2013-03-21 12:24:01 -0400564 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400565 if (file->f_mode & FMODE_READ)
566 pipe->readers--;
567 if (file->f_mode & FMODE_WRITE)
568 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200569
Al Viroba5bb142013-03-21 02:21:19 -0400570 if (pipe->readers || pipe->writers) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800571 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200572 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
573 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Al Viroebec73f2013-03-21 12:24:01 -0400575 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400576
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800577 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579}
580
581static int
Al Viro599a0ac2013-03-12 09:58:10 -0400582pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Al Virode32ec42013-03-21 11:16:56 -0400584 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400585 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Al Viroebec73f2013-03-21 12:24:01 -0400587 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400588 if (filp->f_mode & FMODE_READ)
589 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
590 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200591 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400592 if (retval < 0 && (filp->f_mode & FMODE_READ))
593 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700594 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
595 }
Al Viroebec73f2013-03-21 12:24:01 -0400596 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700597 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700600static unsigned long account_pipe_buffers(struct user_struct *user,
Willy Tarreau759c0112016-01-18 16:36:09 +0100601 unsigned long old, unsigned long new)
602{
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700603 return atomic_long_add_return(new - old, &user->pipe_bufs);
Willy Tarreau759c0112016-01-18 16:36:09 +0100604}
605
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700606static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100607{
Eric Biggersf7340762018-02-06 15:42:08 -0800608 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
609
610 return soft_limit && user_bufs > soft_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100611}
612
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700613static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100614{
Eric Biggersf7340762018-02-06 15:42:08 -0800615 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
616
617 return hard_limit && user_bufs > hard_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100618}
619
Eric Biggers85c2dd52018-02-06 15:41:53 -0800620static bool is_unprivileged_user(void)
621{
622 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
623}
624
Al Viro7bee1302013-03-21 11:04:15 -0400625struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200626{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200627 struct pipe_inode_info *pipe;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700628 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
629 struct user_struct *user = get_current_user();
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700630 unsigned long user_bufs;
Eric Biggersf7340762018-02-06 15:42:08 -0800631 unsigned int max_size = READ_ONCE(pipe_max_size);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200632
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700633 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700634 if (pipe == NULL)
635 goto out_free_uid;
Willy Tarreau759c0112016-01-18 16:36:09 +0100636
Eric Biggersf7340762018-02-06 15:42:08 -0800637 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
638 pipe_bufs = max_size >> PAGE_SHIFT;
Michael Kerrisk (man-pages)086e7742016-10-11 13:53:43 -0700639
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700640 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700641
Eric Biggers85c2dd52018-02-06 15:41:53 -0800642 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700643 user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700644 pipe_bufs = 1;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200645 }
646
Eric Biggers85c2dd52018-02-06 15:41:53 -0800647 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700648 goto out_revert_acct;
649
650 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
651 GFP_KERNEL_ACCOUNT);
652
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700653 if (pipe->bufs) {
654 init_waitqueue_head(&pipe->wait);
655 pipe->r_counter = pipe->w_counter = 1;
656 pipe->buffers = pipe_bufs;
657 pipe->user = user;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700658 mutex_init(&pipe->mutex);
659 return pipe;
660 }
661
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700662out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700663 (void) account_pipe_buffers(user, pipe_bufs, 0);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700664 kfree(pipe);
665out_free_uid:
666 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200667 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200668}
669
Al Viro4b8a8f12013-03-21 11:06:46 -0400670void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700674 (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
Willy Tarreau759c0112016-01-18 16:36:09 +0100675 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200676 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200677 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (buf->ops)
Miklos Szeredia7796382016-09-27 10:45:12 +0200679 pipe_buf_release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200681 if (pipe->tmp_page)
682 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200683 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200684 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800687static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200688
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700689/*
690 * pipefs_dname() is called from d_path().
691 */
692static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
693{
694 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
David Howells75c3cfa2015-03-17 22:26:12 +0000695 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700696}
697
Al Viro3ba13d12009-02-20 06:02:22 +0000698static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700699 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700};
701
702static struct inode * get_pipe_inode(void)
703{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200704 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200705 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (!inode)
708 goto fail_inode;
709
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400710 inode->i_ino = get_next_ino();
711
Al Viro7bee1302013-03-21 11:04:15 -0400712 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200713 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200715
Al Viroba5bb142013-03-21 02:21:19 -0400716 inode->i_pipe = pipe;
717 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200718 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400719 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /*
722 * Mark the inode dirty from the very beginning,
723 * that way it will never be moved to the dirty
724 * list because "mark_inode_dirty()" will think
725 * that it already _is_ on the dirty list.
726 */
727 inode->i_state = I_DIRTY;
728 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100729 inode->i_uid = current_fsuid();
730 inode->i_gid = current_fsgid();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700731 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return inode;
734
735fail_iput:
736 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738fail_inode:
739 return NULL;
740}
741
Al Viroe4fad8e2012-07-21 15:33:25 +0400742int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Andi Kleend6cbd282006-09-30 23:29:26 -0700744 int err;
Al Viroe4fad8e2012-07-21 15:33:25 +0400745 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700746 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +0400747 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400750 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Andi Kleend6cbd282006-09-30 23:29:26 -0700752 err = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +0100753 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &empty_name);
Al Viro2c48b9c2009-08-09 00:52:35 +0400754 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700755 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +0400756 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +0200757
Al Viro2c48b9c2009-08-09 00:52:35 +0400758 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800759
Al Viro599a0ac2013-03-12 09:58:10 -0400760 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500761 if (IS_ERR(f)) {
762 err = PTR_ERR(f);
Dave Hansen430e2852008-02-15 14:37:26 -0800763 goto err_dentry;
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvalds98830352012-04-29 13:12:42 -0700766 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Al Virode32ec42013-03-21 11:16:56 -0400767 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Al Viro599a0ac2013-03-12 09:58:10 -0400769 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500770 if (IS_ERR(res[0])) {
771 err = PTR_ERR(res[0]);
Al Viroe4fad8e2012-07-21 15:33:25 +0400772 goto err_file;
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Al Viroe4fad8e2012-07-21 15:33:25 +0400775 path_get(&path);
Al Virode32ec42013-03-21 11:16:56 -0400776 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400777 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
778 res[1] = f;
779 return 0;
780
781err_file:
782 put_filp(f);
783err_dentry:
Al Viro4b8a8f12013-03-21 11:06:46 -0400784 free_pipe_info(inode->i_pipe);
Al Viro2c48b9c2009-08-09 00:52:35 +0400785 path_put(&path);
Al Viroe4fad8e2012-07-21 15:33:25 +0400786 return err;
Al Viroed152432008-04-22 19:51:27 -0400787
Al Viroe4fad8e2012-07-21 15:33:25 +0400788err_inode:
Al Viro4b8a8f12013-03-21 11:06:46 -0400789 free_pipe_info(inode->i_pipe);
Andi Kleend6cbd282006-09-30 23:29:26 -0700790 iput(inode);
Al Viroe4fad8e2012-07-21 15:33:25 +0400791 return err;
Andi Kleend6cbd282006-09-30 23:29:26 -0700792}
793
Al Viro5b249b12012-08-19 12:17:29 -0400794static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700795{
Andi Kleend6cbd282006-09-30 23:29:26 -0700796 int error;
797 int fdw, fdr;
798
Linus Torvalds98830352012-04-29 13:12:42 -0700799 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700800 return -EINVAL;
801
Al Viroe4fad8e2012-07-21 15:33:25 +0400802 error = create_pipe_files(files, flags);
803 if (error)
804 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700805
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700806 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700807 if (error < 0)
808 goto err_read_pipe;
809 fdr = error;
810
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700811 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700812 if (error < 0)
813 goto err_fdr;
814 fdw = error;
815
Al Viro157cf642008-12-14 04:57:47 -0500816 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700817 fd[0] = fdr;
818 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820
Andi Kleend6cbd282006-09-30 23:29:26 -0700821 err_fdr:
822 put_unused_fd(fdr);
823 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400824 fput(files[0]);
825 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700826 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Al Viro5b249b12012-08-19 12:17:29 -0400829int do_pipe_flags(int *fd, int flags)
830{
831 struct file *files[2];
832 int error = __do_pipe_flags(fd, files, flags);
833 if (!error) {
834 fd_install(fd[0], files[0]);
835 fd_install(fd[1], files[1]);
836 }
837 return error;
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400841 * sys_pipe() is the normal C calling standard for creating
842 * a pipe. It's not the way Unix traditionally does this, though.
843 */
Heiko Carstensd4e82042009-01-14 14:14:34 +0100844SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400845{
Al Viro5b249b12012-08-19 12:17:29 -0400846 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400847 int fd[2];
848 int error;
849
Al Viro5b249b12012-08-19 12:17:29 -0400850 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400851 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400852 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
853 fput(files[0]);
854 fput(files[1]);
855 put_unused_fd(fd[0]);
856 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400857 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400858 } else {
859 fd_install(fd[0], files[0]);
860 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700861 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400862 }
863 return error;
864}
865
Heiko Carstens2b664212009-01-14 14:14:35 +0100866SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700867{
868 return sys_pipe2(fildes, 0);
869}
870
Al Virofc7478a2013-03-21 02:07:59 -0400871static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400872{
873 int cur = *cnt;
874
875 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400876 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400877 if (signal_pending(current))
878 break;
879 }
880 return cur == *cnt ? -ERESTARTSYS : 0;
881}
882
Al Virofc7478a2013-03-21 02:07:59 -0400883static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400884{
Al Virofc7478a2013-03-21 02:07:59 -0400885 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400886}
887
888static int fifo_open(struct inode *inode, struct file *filp)
889{
890 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400891 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400892 int ret;
893
Al Viroba5bb142013-03-21 02:21:19 -0400894 filp->f_version = 0;
895
896 spin_lock(&inode->i_lock);
897 if (inode->i_pipe) {
898 pipe = inode->i_pipe;
899 pipe->files++;
900 spin_unlock(&inode->i_lock);
901 } else {
902 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400903 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400904 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400905 return -ENOMEM;
906 pipe->files = 1;
907 spin_lock(&inode->i_lock);
908 if (unlikely(inode->i_pipe)) {
909 inode->i_pipe->files++;
910 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400911 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400912 pipe = inode->i_pipe;
913 } else {
914 inode->i_pipe = pipe;
915 spin_unlock(&inode->i_lock);
916 }
Al Virof776c732013-03-12 09:46:27 -0400917 }
Al Virode32ec42013-03-21 11:16:56 -0400918 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400919 /* OK, we have a pipe and it's pinned down */
920
Al Viroebec73f2013-03-21 12:24:01 -0400921 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400922
923 /* We can only do regular read/write on fifos */
924 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
925
926 switch (filp->f_mode) {
927 case FMODE_READ:
928 /*
929 * O_RDONLY
930 * POSIX.1 says that O_NONBLOCK means return with the FIFO
931 * opened, even when there is no process writing the FIFO.
932 */
Al Virof776c732013-03-12 09:46:27 -0400933 pipe->r_counter++;
934 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400935 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400936
Al Viro599a0ac2013-03-12 09:58:10 -0400937 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400938 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800939 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -0400940 * seen a writer */
941 filp->f_version = pipe->w_counter;
942 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400943 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400944 goto err_rd;
945 }
946 }
947 break;
948
949 case FMODE_WRITE:
950 /*
951 * O_WRONLY
952 * POSIX.1 says that O_NONBLOCK means return -1 with
953 * errno=ENXIO when there is no process reading the FIFO.
954 */
955 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400956 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400957 goto err;
958
Al Virof776c732013-03-12 09:46:27 -0400959 pipe->w_counter++;
960 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400961 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400962
Al Viro599a0ac2013-03-12 09:58:10 -0400963 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400964 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400965 goto err_wr;
966 }
967 break;
968
969 case FMODE_READ | FMODE_WRITE:
970 /*
971 * O_RDWR
972 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
973 * This implementation will NEVER block on a O_RDWR open, since
974 * the process can at least talk to itself.
975 */
Al Virof776c732013-03-12 09:46:27 -0400976
977 pipe->readers++;
978 pipe->writers++;
979 pipe->r_counter++;
980 pipe->w_counter++;
981 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400982 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400983 break;
984
985 default:
986 ret = -EINVAL;
987 goto err;
988 }
989
990 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -0400991 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400992 return 0;
993
994err_rd:
995 if (!--pipe->readers)
996 wake_up_interruptible(&pipe->wait);
997 ret = -ERESTARTSYS;
998 goto err;
999
1000err_wr:
1001 if (!--pipe->writers)
1002 wake_up_interruptible(&pipe->wait);
1003 ret = -ERESTARTSYS;
1004 goto err;
1005
1006err:
Al Viroebec73f2013-03-21 12:24:01 -04001007 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -08001008
1009 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -04001010 return ret;
1011}
1012
Al Viro599a0ac2013-03-12 09:58:10 -04001013const struct file_operations pipefifo_fops = {
1014 .open = fifo_open,
1015 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001016 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001017 .write_iter = pipe_write,
Al Viro599a0ac2013-03-12 09:58:10 -04001018 .poll = pipe_poll,
1019 .unlocked_ioctl = pipe_ioctl,
1020 .release = pipe_release,
1021 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001022};
1023
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001024/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001025 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001026 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001027 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001028unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001029{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001030 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001031 return 0;
1032
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001033 /* Minimum pipe size, as required by POSIX */
1034 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001035 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001036
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001037 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001038}
1039
1040/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001041 * Allocate a new array of pipe buffers and copy the info over. Returns the
1042 * pipe size if successful, or return -ERROR on error.
1043 */
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001044static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
Jens Axboe35f3d142010-05-20 10:43:18 +02001045{
1046 struct pipe_buffer *bufs;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001047 unsigned int size, nr_pages;
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001048 unsigned long user_bufs;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001049 long ret = 0;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001050
1051 size = round_pipe_size(arg);
1052 nr_pages = size >> PAGE_SHIFT;
1053
1054 if (!nr_pages)
1055 return -EINVAL;
1056
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001057 /*
1058 * If trying to increase the pipe capacity, check that an
1059 * unprivileged user is not trying to exceed various limits
1060 * (soft limit check here, hard limit check just below).
1061 * Decreasing the pipe capacity is always permitted, even
1062 * if the user is currently over a limit.
1063 */
1064 if (nr_pages > pipe->buffers &&
1065 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001066 return -EPERM;
1067
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001068 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001069
1070 if (nr_pages > pipe->buffers &&
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001071 (too_many_pipe_buffers_hard(user_bufs) ||
1072 too_many_pipe_buffers_soft(user_bufs)) &&
Eric Biggers85c2dd52018-02-06 15:41:53 -08001073 is_unprivileged_user()) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001074 ret = -EPERM;
1075 goto out_revert_acct;
1076 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001077
1078 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001079 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1080 * expect a lot of shrink+grow operations, just free and allocate
1081 * again like we would do for growing. If the pipe currently
1082 * contains more buffers than arg, then return busy.
1083 */
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001084 if (nr_pages < pipe->nrbufs) {
1085 ret = -EBUSY;
1086 goto out_revert_acct;
1087 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001088
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001089 bufs = kcalloc(nr_pages, sizeof(*bufs),
1090 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001091 if (unlikely(!bufs)) {
1092 ret = -ENOMEM;
1093 goto out_revert_acct;
1094 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001095
1096 /*
1097 * The pipe array wraps around, so just start the new one at zero
1098 * and adjust the indexes.
1099 */
1100 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001101 unsigned int tail;
1102 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001103
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001104 tail = pipe->curbuf + pipe->nrbufs;
1105 if (tail < pipe->buffers)
1106 tail = 0;
1107 else
1108 tail &= (pipe->buffers - 1);
1109
1110 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001111 if (head)
1112 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1113 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001114 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001115 }
1116
1117 pipe->curbuf = 0;
1118 kfree(pipe->bufs);
1119 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001120 pipe->buffers = nr_pages;
1121 return nr_pages * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001122
1123out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001124 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001125 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001126}
1127
Jens Axboeff9da692010-06-03 14:54:39 +02001128/*
Linus Torvalds72083642010-11-28 16:27:19 -08001129 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1130 * location, so checking ->i_pipe is not enough to verify that this is a
1131 * pipe.
1132 */
1133struct pipe_inode_info *get_pipe_info(struct file *file)
1134{
Al Virode32ec42013-03-21 11:16:56 -04001135 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001136}
1137
Jens Axboe35f3d142010-05-20 10:43:18 +02001138long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1139{
1140 struct pipe_inode_info *pipe;
1141 long ret;
1142
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001143 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001144 if (!pipe)
1145 return -EBADF;
1146
Al Viroebec73f2013-03-21 12:24:01 -04001147 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001148
1149 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001150 case F_SETPIPE_SZ:
1151 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001152 break;
1153 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001154 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001155 break;
1156 default:
1157 ret = -EINVAL;
1158 break;
1159 }
1160
Al Viroebec73f2013-03-21 12:24:01 -04001161 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001162 return ret;
1163}
1164
Nick Pigginff0c7d12011-01-07 17:49:50 +11001165static const struct super_operations pipefs_ops = {
1166 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001167 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001168};
1169
Jens Axboe35f3d142010-05-20 10:43:18 +02001170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * pipefs should _never_ be mounted by userland - too much of security hassle,
1172 * no real gain from having the whole whorehouse mounted. So we don't need
1173 * any operations on the root directory. However, we need a non-trivial
1174 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1175 */
Al Viro51139ad2010-07-25 23:47:46 +04001176static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1177 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Al Viroc74a1cb2011-01-12 16:59:34 -05001179 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1180 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183static struct file_system_type pipe_fs_type = {
1184 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001185 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 .kill_sb = kill_anon_super,
1187};
1188
1189static int __init init_pipe_fs(void)
1190{
1191 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (!err) {
1194 pipe_mnt = kern_mount(&pipe_fs_type);
1195 if (IS_ERR(pipe_mnt)) {
1196 err = PTR_ERR(pipe_mnt);
1197 unregister_filesystem(&pipe_fs_type);
1198 }
1199 }
1200 return err;
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203fs_initcall(init_pipe_fs);