blob: bdc5d3c0977d09b37d5eb80abfb92c3a336f6c92 [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 +0100512/* No kernel lock held - fine */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700513static __poll_t
514pipe_poll(struct file *filp, poll_table *wait)
Christoph Hellwigdd670812017-12-31 16:42:12 +0100515{
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700516 __poll_t mask;
Christoph Hellwigdd670812017-12-31 16:42:12 +0100517 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700518 int nrbufs;
519
520 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* Reading only -- no need for acquiring the semaphore. */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700523 nrbufs = pipe->nrbufs;
524 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 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{
Al Viroe4fad8e2012-07-21 15:33:25 +0400744 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700745 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400748 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Al Viro152b6372018-06-09 10:05:18 -0400750 f = alloc_file_pseudo(inode, pipe_mnt, "",
751 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
752 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500753 if (IS_ERR(f)) {
Al Viro152b6372018-06-09 10:05:18 -0400754 free_pipe_info(inode->i_pipe);
755 iput(inode);
756 return PTR_ERR(f);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Al Virode32ec42013-03-21 11:16:56 -0400759 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Al Viro183266f2018-06-17 14:15:10 -0400761 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
762 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500763 if (IS_ERR(res[0])) {
Al Virob10a4a92018-07-09 02:29:58 -0400764 put_pipe_info(inode, inode->i_pipe);
765 fput(f);
766 return PTR_ERR(res[0]);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500767 }
Al Virode32ec42013-03-21 11:16:56 -0400768 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400769 res[1] = f;
770 return 0;
Andi Kleend6cbd282006-09-30 23:29:26 -0700771}
772
Al Viro5b249b12012-08-19 12:17:29 -0400773static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700774{
Andi Kleend6cbd282006-09-30 23:29:26 -0700775 int error;
776 int fdw, fdr;
777
Linus Torvalds98830352012-04-29 13:12:42 -0700778 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700779 return -EINVAL;
780
Al Viroe4fad8e2012-07-21 15:33:25 +0400781 error = create_pipe_files(files, flags);
782 if (error)
783 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700784
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700785 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700786 if (error < 0)
787 goto err_read_pipe;
788 fdr = error;
789
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700790 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700791 if (error < 0)
792 goto err_fdr;
793 fdw = error;
794
Al Viro157cf642008-12-14 04:57:47 -0500795 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700796 fd[0] = fdr;
797 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799
Andi Kleend6cbd282006-09-30 23:29:26 -0700800 err_fdr:
801 put_unused_fd(fdr);
802 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400803 fput(files[0]);
804 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700805 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Al Viro5b249b12012-08-19 12:17:29 -0400808int do_pipe_flags(int *fd, int flags)
809{
810 struct file *files[2];
811 int error = __do_pipe_flags(fd, files, flags);
812 if (!error) {
813 fd_install(fd[0], files[0]);
814 fd_install(fd[1], files[1]);
815 }
816 return error;
817}
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400820 * sys_pipe() is the normal C calling standard for creating
821 * a pipe. It's not the way Unix traditionally does this, though.
822 */
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100823static int do_pipe2(int __user *fildes, int flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400824{
Al Viro5b249b12012-08-19 12:17:29 -0400825 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400826 int fd[2];
827 int error;
828
Al Viro5b249b12012-08-19 12:17:29 -0400829 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400830 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400831 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
832 fput(files[0]);
833 fput(files[1]);
834 put_unused_fd(fd[0]);
835 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400836 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400837 } else {
838 fd_install(fd[0], files[0]);
839 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700840 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400841 }
842 return error;
843}
844
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100845SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
846{
847 return do_pipe2(fildes, flags);
848}
849
Heiko Carstens2b664212009-01-14 14:14:35 +0100850SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700851{
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100852 return do_pipe2(fildes, 0);
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700853}
854
Al Virofc7478a2013-03-21 02:07:59 -0400855static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400856{
857 int cur = *cnt;
858
859 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400860 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400861 if (signal_pending(current))
862 break;
863 }
864 return cur == *cnt ? -ERESTARTSYS : 0;
865}
866
Al Virofc7478a2013-03-21 02:07:59 -0400867static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400868{
Al Virofc7478a2013-03-21 02:07:59 -0400869 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400870}
871
872static int fifo_open(struct inode *inode, struct file *filp)
873{
874 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400875 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400876 int ret;
877
Al Viroba5bb142013-03-21 02:21:19 -0400878 filp->f_version = 0;
879
880 spin_lock(&inode->i_lock);
881 if (inode->i_pipe) {
882 pipe = inode->i_pipe;
883 pipe->files++;
884 spin_unlock(&inode->i_lock);
885 } else {
886 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400887 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400888 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400889 return -ENOMEM;
890 pipe->files = 1;
891 spin_lock(&inode->i_lock);
892 if (unlikely(inode->i_pipe)) {
893 inode->i_pipe->files++;
894 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400895 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400896 pipe = inode->i_pipe;
897 } else {
898 inode->i_pipe = pipe;
899 spin_unlock(&inode->i_lock);
900 }
Al Virof776c732013-03-12 09:46:27 -0400901 }
Al Virode32ec42013-03-21 11:16:56 -0400902 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400903 /* OK, we have a pipe and it's pinned down */
904
Al Viroebec73f2013-03-21 12:24:01 -0400905 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400906
907 /* We can only do regular read/write on fifos */
908 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
909
910 switch (filp->f_mode) {
911 case FMODE_READ:
912 /*
913 * O_RDONLY
914 * POSIX.1 says that O_NONBLOCK means return with the FIFO
915 * opened, even when there is no process writing the FIFO.
916 */
Al Virof776c732013-03-12 09:46:27 -0400917 pipe->r_counter++;
918 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400919 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400920
Al Viro599a0ac2013-03-12 09:58:10 -0400921 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400922 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800923 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -0400924 * seen a writer */
925 filp->f_version = pipe->w_counter;
926 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400927 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400928 goto err_rd;
929 }
930 }
931 break;
932
933 case FMODE_WRITE:
934 /*
935 * O_WRONLY
936 * POSIX.1 says that O_NONBLOCK means return -1 with
937 * errno=ENXIO when there is no process reading the FIFO.
938 */
939 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400940 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400941 goto err;
942
Al Virof776c732013-03-12 09:46:27 -0400943 pipe->w_counter++;
944 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400945 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400946
Al Viro599a0ac2013-03-12 09:58:10 -0400947 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400948 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400949 goto err_wr;
950 }
951 break;
952
953 case FMODE_READ | FMODE_WRITE:
954 /*
955 * O_RDWR
956 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
957 * This implementation will NEVER block on a O_RDWR open, since
958 * the process can at least talk to itself.
959 */
Al Virof776c732013-03-12 09:46:27 -0400960
961 pipe->readers++;
962 pipe->writers++;
963 pipe->r_counter++;
964 pipe->w_counter++;
965 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400966 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400967 break;
968
969 default:
970 ret = -EINVAL;
971 goto err;
972 }
973
974 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -0400975 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400976 return 0;
977
978err_rd:
979 if (!--pipe->readers)
980 wake_up_interruptible(&pipe->wait);
981 ret = -ERESTARTSYS;
982 goto err;
983
984err_wr:
985 if (!--pipe->writers)
986 wake_up_interruptible(&pipe->wait);
987 ret = -ERESTARTSYS;
988 goto err;
989
990err:
Al Viroebec73f2013-03-21 12:24:01 -0400991 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800992
993 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -0400994 return ret;
995}
996
Al Viro599a0ac2013-03-12 09:58:10 -0400997const struct file_operations pipefifo_fops = {
998 .open = fifo_open,
999 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001000 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001001 .write_iter = pipe_write,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001002 .poll = pipe_poll,
Al Viro599a0ac2013-03-12 09:58:10 -04001003 .unlocked_ioctl = pipe_ioctl,
1004 .release = pipe_release,
1005 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001006};
1007
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001008/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001009 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001010 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001011 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001012unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001013{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001014 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001015 return 0;
1016
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001017 /* Minimum pipe size, as required by POSIX */
1018 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001019 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001020
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001021 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001022}
1023
1024/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001025 * Allocate a new array of pipe buffers and copy the info over. Returns the
1026 * pipe size if successful, or return -ERROR on error.
1027 */
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001028static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
Jens Axboe35f3d142010-05-20 10:43:18 +02001029{
1030 struct pipe_buffer *bufs;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001031 unsigned int size, nr_pages;
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001032 unsigned long user_bufs;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001033 long ret = 0;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001034
1035 size = round_pipe_size(arg);
1036 nr_pages = size >> PAGE_SHIFT;
1037
1038 if (!nr_pages)
1039 return -EINVAL;
1040
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001041 /*
1042 * If trying to increase the pipe capacity, check that an
1043 * unprivileged user is not trying to exceed various limits
1044 * (soft limit check here, hard limit check just below).
1045 * Decreasing the pipe capacity is always permitted, even
1046 * if the user is currently over a limit.
1047 */
1048 if (nr_pages > pipe->buffers &&
1049 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001050 return -EPERM;
1051
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001052 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001053
1054 if (nr_pages > pipe->buffers &&
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001055 (too_many_pipe_buffers_hard(user_bufs) ||
1056 too_many_pipe_buffers_soft(user_bufs)) &&
Eric Biggers85c2dd52018-02-06 15:41:53 -08001057 is_unprivileged_user()) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001058 ret = -EPERM;
1059 goto out_revert_acct;
1060 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001061
1062 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001063 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1064 * expect a lot of shrink+grow operations, just free and allocate
1065 * again like we would do for growing. If the pipe currently
1066 * contains more buffers than arg, then return busy.
1067 */
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001068 if (nr_pages < pipe->nrbufs) {
1069 ret = -EBUSY;
1070 goto out_revert_acct;
1071 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001072
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001073 bufs = kcalloc(nr_pages, sizeof(*bufs),
1074 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001075 if (unlikely(!bufs)) {
1076 ret = -ENOMEM;
1077 goto out_revert_acct;
1078 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001079
1080 /*
1081 * The pipe array wraps around, so just start the new one at zero
1082 * and adjust the indexes.
1083 */
1084 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001085 unsigned int tail;
1086 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001087
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001088 tail = pipe->curbuf + pipe->nrbufs;
1089 if (tail < pipe->buffers)
1090 tail = 0;
1091 else
1092 tail &= (pipe->buffers - 1);
1093
1094 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001095 if (head)
1096 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1097 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001098 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001099 }
1100
1101 pipe->curbuf = 0;
1102 kfree(pipe->bufs);
1103 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001104 pipe->buffers = nr_pages;
1105 return nr_pages * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001106
1107out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001108 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001109 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001110}
1111
Jens Axboeff9da692010-06-03 14:54:39 +02001112/*
Linus Torvalds72083642010-11-28 16:27:19 -08001113 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1114 * location, so checking ->i_pipe is not enough to verify that this is a
1115 * pipe.
1116 */
1117struct pipe_inode_info *get_pipe_info(struct file *file)
1118{
Al Virode32ec42013-03-21 11:16:56 -04001119 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001120}
1121
Jens Axboe35f3d142010-05-20 10:43:18 +02001122long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1123{
1124 struct pipe_inode_info *pipe;
1125 long ret;
1126
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001127 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001128 if (!pipe)
1129 return -EBADF;
1130
Al Viroebec73f2013-03-21 12:24:01 -04001131 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001132
1133 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001134 case F_SETPIPE_SZ:
1135 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001136 break;
1137 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001138 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001139 break;
1140 default:
1141 ret = -EINVAL;
1142 break;
1143 }
1144
Al Viroebec73f2013-03-21 12:24:01 -04001145 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001146 return ret;
1147}
1148
Nick Pigginff0c7d12011-01-07 17:49:50 +11001149static const struct super_operations pipefs_ops = {
1150 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001151 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001152};
1153
Jens Axboe35f3d142010-05-20 10:43:18 +02001154/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * pipefs should _never_ be mounted by userland - too much of security hassle,
1156 * no real gain from having the whole whorehouse mounted. So we don't need
1157 * any operations on the root directory. However, we need a non-trivial
1158 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1159 */
Al Viro51139ad2010-07-25 23:47:46 +04001160static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1161 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Al Viroc74a1cb2011-01-12 16:59:34 -05001163 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1164 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static struct file_system_type pipe_fs_type = {
1168 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001169 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 .kill_sb = kill_anon_super,
1171};
1172
1173static int __init init_pipe_fs(void)
1174{
1175 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (!err) {
1178 pipe_mnt = kern_mount(&pipe_fs_type);
1179 if (IS_ERR(pipe_mnt)) {
1180 err = PTR_ERR(pipe_mnt);
1181 unregister_filesystem(&pipe_fs_type);
1182 }
1183 }
1184 return err;
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187fs_initcall(init_pipe_fs);