blob: bb0840e234f3bc176d2af120d6ed94ee3720aad0 [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
Christoph Hellwigdd670812017-12-31 16:42:12 +0100512static struct wait_queue_head *
513pipe_get_poll_head(struct file *filp, __poll_t events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Al Virode32ec42013-03-21 11:16:56 -0400515 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Christoph Hellwigdd670812017-12-31 16:42:12 +0100517 return &pipe->wait;
518}
519
520/* No kernel lock held - fine */
521static __poll_t pipe_poll_mask(struct file *filp, __poll_t events)
522{
523 struct pipe_inode_info *pipe = filp->private_data;
524 int nrbufs = pipe->nrbufs;
525 __poll_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /* Reading only -- no need for acquiring the semaphore. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (filp->f_mode & FMODE_READ) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800529 mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200530 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800531 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
534 if (filp->f_mode & FMODE_WRITE) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800535 mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700536 /*
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800537 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700538 * behave exactly like pipes for poll().
539 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200540 if (!pipe->readers)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800541 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 return mask;
545}
546
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800547static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
548{
549 int kill = 0;
550
551 spin_lock(&inode->i_lock);
552 if (!--pipe->files) {
553 inode->i_pipe = NULL;
554 kill = 1;
555 }
556 spin_unlock(&inode->i_lock);
557
558 if (kill)
559 free_pipe_info(pipe);
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static int
Al Viro599a0ac2013-03-12 09:58:10 -0400563pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800565 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200566
Al Viroebec73f2013-03-21 12:24:01 -0400567 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400568 if (file->f_mode & FMODE_READ)
569 pipe->readers--;
570 if (file->f_mode & FMODE_WRITE)
571 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200572
Al Viroba5bb142013-03-21 02:21:19 -0400573 if (pipe->readers || pipe->writers) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800574 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200575 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
576 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Al Viroebec73f2013-03-21 12:24:01 -0400578 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400579
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800580 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582}
583
584static int
Al Viro599a0ac2013-03-12 09:58:10 -0400585pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Al Virode32ec42013-03-21 11:16:56 -0400587 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400588 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Al Viroebec73f2013-03-21 12:24:01 -0400590 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400591 if (filp->f_mode & FMODE_READ)
592 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
593 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200594 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400595 if (retval < 0 && (filp->f_mode & FMODE_READ))
596 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700597 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
598 }
Al Viroebec73f2013-03-21 12:24:01 -0400599 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700600 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700603static unsigned long account_pipe_buffers(struct user_struct *user,
Willy Tarreau759c0112016-01-18 16:36:09 +0100604 unsigned long old, unsigned long new)
605{
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700606 return atomic_long_add_return(new - old, &user->pipe_bufs);
Willy Tarreau759c0112016-01-18 16:36:09 +0100607}
608
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700609static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100610{
Eric Biggersf7340762018-02-06 15:42:08 -0800611 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
612
613 return soft_limit && user_bufs > soft_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100614}
615
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700616static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100617{
Eric Biggersf7340762018-02-06 15:42:08 -0800618 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
619
620 return hard_limit && user_bufs > hard_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100621}
622
Eric Biggers85c2dd52018-02-06 15:41:53 -0800623static bool is_unprivileged_user(void)
624{
625 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
626}
627
Al Viro7bee1302013-03-21 11:04:15 -0400628struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200629{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200630 struct pipe_inode_info *pipe;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700631 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
632 struct user_struct *user = get_current_user();
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700633 unsigned long user_bufs;
Eric Biggersf7340762018-02-06 15:42:08 -0800634 unsigned int max_size = READ_ONCE(pipe_max_size);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200635
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700636 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700637 if (pipe == NULL)
638 goto out_free_uid;
Willy Tarreau759c0112016-01-18 16:36:09 +0100639
Eric Biggersf7340762018-02-06 15:42:08 -0800640 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
641 pipe_bufs = max_size >> PAGE_SHIFT;
Michael Kerrisk (man-pages)086e7742016-10-11 13:53:43 -0700642
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700643 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700644
Eric Biggers85c2dd52018-02-06 15:41:53 -0800645 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700646 user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700647 pipe_bufs = 1;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200648 }
649
Eric Biggers85c2dd52018-02-06 15:41:53 -0800650 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700651 goto out_revert_acct;
652
653 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
654 GFP_KERNEL_ACCOUNT);
655
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700656 if (pipe->bufs) {
657 init_waitqueue_head(&pipe->wait);
658 pipe->r_counter = pipe->w_counter = 1;
659 pipe->buffers = pipe_bufs;
660 pipe->user = user;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700661 mutex_init(&pipe->mutex);
662 return pipe;
663 }
664
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700665out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700666 (void) account_pipe_buffers(user, pipe_bufs, 0);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700667 kfree(pipe);
668out_free_uid:
669 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200670 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200671}
672
Al Viro4b8a8f12013-03-21 11:06:46 -0400673void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700677 (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
Willy Tarreau759c0112016-01-18 16:36:09 +0100678 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200679 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200680 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (buf->ops)
Miklos Szeredia7796382016-09-27 10:45:12 +0200682 pipe_buf_release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200684 if (pipe->tmp_page)
685 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200686 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200687 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800690static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200691
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700692/*
693 * pipefs_dname() is called from d_path().
694 */
695static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
696{
697 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
David Howells75c3cfa2015-03-17 22:26:12 +0000698 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700699}
700
Al Viro3ba13d12009-02-20 06:02:22 +0000701static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700702 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};
704
705static struct inode * get_pipe_inode(void)
706{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200707 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200708 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (!inode)
711 goto fail_inode;
712
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400713 inode->i_ino = get_next_ino();
714
Al Viro7bee1302013-03-21 11:04:15 -0400715 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200716 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200718
Al Viroba5bb142013-03-21 02:21:19 -0400719 inode->i_pipe = pipe;
720 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200721 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400722 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /*
725 * Mark the inode dirty from the very beginning,
726 * that way it will never be moved to the dirty
727 * list because "mark_inode_dirty()" will think
728 * that it already _is_ on the dirty list.
729 */
730 inode->i_state = I_DIRTY;
731 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100732 inode->i_uid = current_fsuid();
733 inode->i_gid = current_fsgid();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700734 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return inode;
737
738fail_iput:
739 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741fail_inode:
742 return NULL;
743}
744
Al Viroe4fad8e2012-07-21 15:33:25 +0400745int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Andi Kleend6cbd282006-09-30 23:29:26 -0700747 int err;
Al Viroe4fad8e2012-07-21 15:33:25 +0400748 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700749 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +0400750 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400753 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Andi Kleend6cbd282006-09-30 23:29:26 -0700755 err = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +0100756 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &empty_name);
Al Viro2c48b9c2009-08-09 00:52:35 +0400757 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700758 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +0400759 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +0200760
Al Viro2c48b9c2009-08-09 00:52:35 +0400761 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800762
Al Viro599a0ac2013-03-12 09:58:10 -0400763 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500764 if (IS_ERR(f)) {
765 err = PTR_ERR(f);
Dave Hansen430e2852008-02-15 14:37:26 -0800766 goto err_dentry;
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvalds98830352012-04-29 13:12:42 -0700769 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Al Virode32ec42013-03-21 11:16:56 -0400770 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Al Viro599a0ac2013-03-12 09:58:10 -0400772 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500773 if (IS_ERR(res[0])) {
774 err = PTR_ERR(res[0]);
Al Viroe4fad8e2012-07-21 15:33:25 +0400775 goto err_file;
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Al Viroe4fad8e2012-07-21 15:33:25 +0400778 path_get(&path);
Al Virode32ec42013-03-21 11:16:56 -0400779 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400780 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
781 res[1] = f;
782 return 0;
783
784err_file:
785 put_filp(f);
786err_dentry:
Al Viro4b8a8f12013-03-21 11:06:46 -0400787 free_pipe_info(inode->i_pipe);
Al Viro2c48b9c2009-08-09 00:52:35 +0400788 path_put(&path);
Al Viroe4fad8e2012-07-21 15:33:25 +0400789 return err;
Al Viroed152432008-04-22 19:51:27 -0400790
Al Viroe4fad8e2012-07-21 15:33:25 +0400791err_inode:
Al Viro4b8a8f12013-03-21 11:06:46 -0400792 free_pipe_info(inode->i_pipe);
Andi Kleend6cbd282006-09-30 23:29:26 -0700793 iput(inode);
Al Viroe4fad8e2012-07-21 15:33:25 +0400794 return err;
Andi Kleend6cbd282006-09-30 23:29:26 -0700795}
796
Al Viro5b249b12012-08-19 12:17:29 -0400797static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700798{
Andi Kleend6cbd282006-09-30 23:29:26 -0700799 int error;
800 int fdw, fdr;
801
Linus Torvalds98830352012-04-29 13:12:42 -0700802 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700803 return -EINVAL;
804
Al Viroe4fad8e2012-07-21 15:33:25 +0400805 error = create_pipe_files(files, flags);
806 if (error)
807 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700808
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700809 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700810 if (error < 0)
811 goto err_read_pipe;
812 fdr = error;
813
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700814 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700815 if (error < 0)
816 goto err_fdr;
817 fdw = error;
818
Al Viro157cf642008-12-14 04:57:47 -0500819 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700820 fd[0] = fdr;
821 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823
Andi Kleend6cbd282006-09-30 23:29:26 -0700824 err_fdr:
825 put_unused_fd(fdr);
826 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400827 fput(files[0]);
828 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700829 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Al Viro5b249b12012-08-19 12:17:29 -0400832int do_pipe_flags(int *fd, int flags)
833{
834 struct file *files[2];
835 int error = __do_pipe_flags(fd, files, flags);
836 if (!error) {
837 fd_install(fd[0], files[0]);
838 fd_install(fd[1], files[1]);
839 }
840 return error;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400844 * sys_pipe() is the normal C calling standard for creating
845 * a pipe. It's not the way Unix traditionally does this, though.
846 */
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100847static int do_pipe2(int __user *fildes, int flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400848{
Al Viro5b249b12012-08-19 12:17:29 -0400849 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400850 int fd[2];
851 int error;
852
Al Viro5b249b12012-08-19 12:17:29 -0400853 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400854 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400855 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
856 fput(files[0]);
857 fput(files[1]);
858 put_unused_fd(fd[0]);
859 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400860 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400861 } else {
862 fd_install(fd[0], files[0]);
863 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700864 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400865 }
866 return error;
867}
868
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100869SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
870{
871 return do_pipe2(fildes, flags);
872}
873
Heiko Carstens2b664212009-01-14 14:14:35 +0100874SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700875{
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100876 return do_pipe2(fildes, 0);
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700877}
878
Al Virofc7478a2013-03-21 02:07:59 -0400879static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400880{
881 int cur = *cnt;
882
883 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400884 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400885 if (signal_pending(current))
886 break;
887 }
888 return cur == *cnt ? -ERESTARTSYS : 0;
889}
890
Al Virofc7478a2013-03-21 02:07:59 -0400891static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400892{
Al Virofc7478a2013-03-21 02:07:59 -0400893 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400894}
895
896static int fifo_open(struct inode *inode, struct file *filp)
897{
898 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400899 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400900 int ret;
901
Al Viroba5bb142013-03-21 02:21:19 -0400902 filp->f_version = 0;
903
904 spin_lock(&inode->i_lock);
905 if (inode->i_pipe) {
906 pipe = inode->i_pipe;
907 pipe->files++;
908 spin_unlock(&inode->i_lock);
909 } else {
910 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400911 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400912 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400913 return -ENOMEM;
914 pipe->files = 1;
915 spin_lock(&inode->i_lock);
916 if (unlikely(inode->i_pipe)) {
917 inode->i_pipe->files++;
918 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400919 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400920 pipe = inode->i_pipe;
921 } else {
922 inode->i_pipe = pipe;
923 spin_unlock(&inode->i_lock);
924 }
Al Virof776c732013-03-12 09:46:27 -0400925 }
Al Virode32ec42013-03-21 11:16:56 -0400926 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400927 /* OK, we have a pipe and it's pinned down */
928
Al Viroebec73f2013-03-21 12:24:01 -0400929 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400930
931 /* We can only do regular read/write on fifos */
932 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
933
934 switch (filp->f_mode) {
935 case FMODE_READ:
936 /*
937 * O_RDONLY
938 * POSIX.1 says that O_NONBLOCK means return with the FIFO
939 * opened, even when there is no process writing the FIFO.
940 */
Al Virof776c732013-03-12 09:46:27 -0400941 pipe->r_counter++;
942 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400943 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400944
Al Viro599a0ac2013-03-12 09:58:10 -0400945 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400946 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800947 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -0400948 * seen a writer */
949 filp->f_version = pipe->w_counter;
950 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400951 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400952 goto err_rd;
953 }
954 }
955 break;
956
957 case FMODE_WRITE:
958 /*
959 * O_WRONLY
960 * POSIX.1 says that O_NONBLOCK means return -1 with
961 * errno=ENXIO when there is no process reading the FIFO.
962 */
963 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400964 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400965 goto err;
966
Al Virof776c732013-03-12 09:46:27 -0400967 pipe->w_counter++;
968 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400969 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400970
Al Viro599a0ac2013-03-12 09:58:10 -0400971 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400972 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400973 goto err_wr;
974 }
975 break;
976
977 case FMODE_READ | FMODE_WRITE:
978 /*
979 * O_RDWR
980 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
981 * This implementation will NEVER block on a O_RDWR open, since
982 * the process can at least talk to itself.
983 */
Al Virof776c732013-03-12 09:46:27 -0400984
985 pipe->readers++;
986 pipe->writers++;
987 pipe->r_counter++;
988 pipe->w_counter++;
989 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400990 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400991 break;
992
993 default:
994 ret = -EINVAL;
995 goto err;
996 }
997
998 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -0400999 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001000 return 0;
1001
1002err_rd:
1003 if (!--pipe->readers)
1004 wake_up_interruptible(&pipe->wait);
1005 ret = -ERESTARTSYS;
1006 goto err;
1007
1008err_wr:
1009 if (!--pipe->writers)
1010 wake_up_interruptible(&pipe->wait);
1011 ret = -ERESTARTSYS;
1012 goto err;
1013
1014err:
Al Viroebec73f2013-03-21 12:24:01 -04001015 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -08001016
1017 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -04001018 return ret;
1019}
1020
Al Viro599a0ac2013-03-12 09:58:10 -04001021const struct file_operations pipefifo_fops = {
1022 .open = fifo_open,
1023 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001024 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001025 .write_iter = pipe_write,
Christoph Hellwigdd670812017-12-31 16:42:12 +01001026 .get_poll_head = pipe_get_poll_head,
1027 .poll_mask = pipe_poll_mask,
Al Viro599a0ac2013-03-12 09:58:10 -04001028 .unlocked_ioctl = pipe_ioctl,
1029 .release = pipe_release,
1030 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001031};
1032
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001033/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001034 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001035 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001036 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001037unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001038{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001039 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001040 return 0;
1041
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001042 /* Minimum pipe size, as required by POSIX */
1043 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001044 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001045
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001046 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001047}
1048
1049/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001050 * Allocate a new array of pipe buffers and copy the info over. Returns the
1051 * pipe size if successful, or return -ERROR on error.
1052 */
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001053static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
Jens Axboe35f3d142010-05-20 10:43:18 +02001054{
1055 struct pipe_buffer *bufs;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001056 unsigned int size, nr_pages;
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001057 unsigned long user_bufs;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001058 long ret = 0;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001059
1060 size = round_pipe_size(arg);
1061 nr_pages = size >> PAGE_SHIFT;
1062
1063 if (!nr_pages)
1064 return -EINVAL;
1065
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001066 /*
1067 * If trying to increase the pipe capacity, check that an
1068 * unprivileged user is not trying to exceed various limits
1069 * (soft limit check here, hard limit check just below).
1070 * Decreasing the pipe capacity is always permitted, even
1071 * if the user is currently over a limit.
1072 */
1073 if (nr_pages > pipe->buffers &&
1074 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001075 return -EPERM;
1076
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001077 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001078
1079 if (nr_pages > pipe->buffers &&
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001080 (too_many_pipe_buffers_hard(user_bufs) ||
1081 too_many_pipe_buffers_soft(user_bufs)) &&
Eric Biggers85c2dd52018-02-06 15:41:53 -08001082 is_unprivileged_user()) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001083 ret = -EPERM;
1084 goto out_revert_acct;
1085 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001086
1087 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001088 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1089 * expect a lot of shrink+grow operations, just free and allocate
1090 * again like we would do for growing. If the pipe currently
1091 * contains more buffers than arg, then return busy.
1092 */
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001093 if (nr_pages < pipe->nrbufs) {
1094 ret = -EBUSY;
1095 goto out_revert_acct;
1096 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001097
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001098 bufs = kcalloc(nr_pages, sizeof(*bufs),
1099 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001100 if (unlikely(!bufs)) {
1101 ret = -ENOMEM;
1102 goto out_revert_acct;
1103 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001104
1105 /*
1106 * The pipe array wraps around, so just start the new one at zero
1107 * and adjust the indexes.
1108 */
1109 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001110 unsigned int tail;
1111 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001112
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001113 tail = pipe->curbuf + pipe->nrbufs;
1114 if (tail < pipe->buffers)
1115 tail = 0;
1116 else
1117 tail &= (pipe->buffers - 1);
1118
1119 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001120 if (head)
1121 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1122 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001123 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001124 }
1125
1126 pipe->curbuf = 0;
1127 kfree(pipe->bufs);
1128 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001129 pipe->buffers = nr_pages;
1130 return nr_pages * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001131
1132out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001133 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001134 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001135}
1136
Jens Axboeff9da692010-06-03 14:54:39 +02001137/*
Linus Torvalds72083642010-11-28 16:27:19 -08001138 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1139 * location, so checking ->i_pipe is not enough to verify that this is a
1140 * pipe.
1141 */
1142struct pipe_inode_info *get_pipe_info(struct file *file)
1143{
Al Virode32ec42013-03-21 11:16:56 -04001144 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001145}
1146
Jens Axboe35f3d142010-05-20 10:43:18 +02001147long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1148{
1149 struct pipe_inode_info *pipe;
1150 long ret;
1151
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001152 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001153 if (!pipe)
1154 return -EBADF;
1155
Al Viroebec73f2013-03-21 12:24:01 -04001156 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001157
1158 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001159 case F_SETPIPE_SZ:
1160 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001161 break;
1162 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001163 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001164 break;
1165 default:
1166 ret = -EINVAL;
1167 break;
1168 }
1169
Al Viroebec73f2013-03-21 12:24:01 -04001170 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001171 return ret;
1172}
1173
Nick Pigginff0c7d12011-01-07 17:49:50 +11001174static const struct super_operations pipefs_ops = {
1175 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001176 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001177};
1178
Jens Axboe35f3d142010-05-20 10:43:18 +02001179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * pipefs should _never_ be mounted by userland - too much of security hassle,
1181 * no real gain from having the whole whorehouse mounted. So we don't need
1182 * any operations on the root directory. However, we need a non-trivial
1183 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1184 */
Al Viro51139ad2010-07-25 23:47:46 +04001185static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1186 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Al Viroc74a1cb2011-01-12 16:59:34 -05001188 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1189 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static struct file_system_type pipe_fs_type = {
1193 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001194 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 .kill_sb = kill_anon_super,
1196};
1197
1198static int __init init_pipe_fs(void)
1199{
1200 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!err) {
1203 pipe_mnt = kern_mount(&pipe_fs_type);
1204 if (IS_ERR(pipe_mnt)) {
1205 err = PTR_ERR(pipe_mnt);
1206 unregister_filesystem(&pipe_fs_type);
1207 }
1208 }
1209 return err;
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212fs_initcall(init_pipe_fs);