blob: 5f50070774bcead11930e19f87bf574d0ab42131 [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{
Al Viroe4fad8e2012-07-21 15:33:25 +0400747 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700748 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400751 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Al Viro152b6372018-06-09 10:05:18 -0400753 f = alloc_file_pseudo(inode, pipe_mnt, "",
754 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
755 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500756 if (IS_ERR(f)) {
Al Viro152b6372018-06-09 10:05:18 -0400757 free_pipe_info(inode->i_pipe);
758 iput(inode);
759 return PTR_ERR(f);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Al Virode32ec42013-03-21 11:16:56 -0400762 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Al Viro183266f2018-06-17 14:15:10 -0400764 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
765 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500766 if (IS_ERR(res[0])) {
Al Virob10a4a92018-07-09 02:29:58 -0400767 put_pipe_info(inode, inode->i_pipe);
768 fput(f);
769 return PTR_ERR(res[0]);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500770 }
Al Virode32ec42013-03-21 11:16:56 -0400771 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400772 res[1] = f;
773 return 0;
Andi Kleend6cbd282006-09-30 23:29:26 -0700774}
775
Al Viro5b249b12012-08-19 12:17:29 -0400776static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700777{
Andi Kleend6cbd282006-09-30 23:29:26 -0700778 int error;
779 int fdw, fdr;
780
Linus Torvalds98830352012-04-29 13:12:42 -0700781 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700782 return -EINVAL;
783
Al Viroe4fad8e2012-07-21 15:33:25 +0400784 error = create_pipe_files(files, flags);
785 if (error)
786 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700787
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700788 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700789 if (error < 0)
790 goto err_read_pipe;
791 fdr = error;
792
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700793 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700794 if (error < 0)
795 goto err_fdr;
796 fdw = error;
797
Al Viro157cf642008-12-14 04:57:47 -0500798 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700799 fd[0] = fdr;
800 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802
Andi Kleend6cbd282006-09-30 23:29:26 -0700803 err_fdr:
804 put_unused_fd(fdr);
805 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400806 fput(files[0]);
807 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700808 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Al Viro5b249b12012-08-19 12:17:29 -0400811int do_pipe_flags(int *fd, int flags)
812{
813 struct file *files[2];
814 int error = __do_pipe_flags(fd, files, flags);
815 if (!error) {
816 fd_install(fd[0], files[0]);
817 fd_install(fd[1], files[1]);
818 }
819 return error;
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400823 * sys_pipe() is the normal C calling standard for creating
824 * a pipe. It's not the way Unix traditionally does this, though.
825 */
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100826static int do_pipe2(int __user *fildes, int flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400827{
Al Viro5b249b12012-08-19 12:17:29 -0400828 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400829 int fd[2];
830 int error;
831
Al Viro5b249b12012-08-19 12:17:29 -0400832 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400833 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400834 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
835 fput(files[0]);
836 fput(files[1]);
837 put_unused_fd(fd[0]);
838 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400839 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400840 } else {
841 fd_install(fd[0], files[0]);
842 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700843 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400844 }
845 return error;
846}
847
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100848SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
849{
850 return do_pipe2(fildes, flags);
851}
852
Heiko Carstens2b664212009-01-14 14:14:35 +0100853SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700854{
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100855 return do_pipe2(fildes, 0);
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700856}
857
Al Virofc7478a2013-03-21 02:07:59 -0400858static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400859{
860 int cur = *cnt;
861
862 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400863 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400864 if (signal_pending(current))
865 break;
866 }
867 return cur == *cnt ? -ERESTARTSYS : 0;
868}
869
Al Virofc7478a2013-03-21 02:07:59 -0400870static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400871{
Al Virofc7478a2013-03-21 02:07:59 -0400872 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400873}
874
875static int fifo_open(struct inode *inode, struct file *filp)
876{
877 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400878 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400879 int ret;
880
Al Viroba5bb142013-03-21 02:21:19 -0400881 filp->f_version = 0;
882
883 spin_lock(&inode->i_lock);
884 if (inode->i_pipe) {
885 pipe = inode->i_pipe;
886 pipe->files++;
887 spin_unlock(&inode->i_lock);
888 } else {
889 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400890 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400891 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400892 return -ENOMEM;
893 pipe->files = 1;
894 spin_lock(&inode->i_lock);
895 if (unlikely(inode->i_pipe)) {
896 inode->i_pipe->files++;
897 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400898 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400899 pipe = inode->i_pipe;
900 } else {
901 inode->i_pipe = pipe;
902 spin_unlock(&inode->i_lock);
903 }
Al Virof776c732013-03-12 09:46:27 -0400904 }
Al Virode32ec42013-03-21 11:16:56 -0400905 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400906 /* OK, we have a pipe and it's pinned down */
907
Al Viroebec73f2013-03-21 12:24:01 -0400908 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400909
910 /* We can only do regular read/write on fifos */
911 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
912
913 switch (filp->f_mode) {
914 case FMODE_READ:
915 /*
916 * O_RDONLY
917 * POSIX.1 says that O_NONBLOCK means return with the FIFO
918 * opened, even when there is no process writing the FIFO.
919 */
Al Virof776c732013-03-12 09:46:27 -0400920 pipe->r_counter++;
921 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400922 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400923
Al Viro599a0ac2013-03-12 09:58:10 -0400924 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400925 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800926 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -0400927 * seen a writer */
928 filp->f_version = pipe->w_counter;
929 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400930 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400931 goto err_rd;
932 }
933 }
934 break;
935
936 case FMODE_WRITE:
937 /*
938 * O_WRONLY
939 * POSIX.1 says that O_NONBLOCK means return -1 with
940 * errno=ENXIO when there is no process reading the FIFO.
941 */
942 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400943 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400944 goto err;
945
Al Virof776c732013-03-12 09:46:27 -0400946 pipe->w_counter++;
947 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400948 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400949
Al Viro599a0ac2013-03-12 09:58:10 -0400950 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400951 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400952 goto err_wr;
953 }
954 break;
955
956 case FMODE_READ | FMODE_WRITE:
957 /*
958 * O_RDWR
959 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
960 * This implementation will NEVER block on a O_RDWR open, since
961 * the process can at least talk to itself.
962 */
Al Virof776c732013-03-12 09:46:27 -0400963
964 pipe->readers++;
965 pipe->writers++;
966 pipe->r_counter++;
967 pipe->w_counter++;
968 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400969 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400970 break;
971
972 default:
973 ret = -EINVAL;
974 goto err;
975 }
976
977 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -0400978 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400979 return 0;
980
981err_rd:
982 if (!--pipe->readers)
983 wake_up_interruptible(&pipe->wait);
984 ret = -ERESTARTSYS;
985 goto err;
986
987err_wr:
988 if (!--pipe->writers)
989 wake_up_interruptible(&pipe->wait);
990 ret = -ERESTARTSYS;
991 goto err;
992
993err:
Al Viroebec73f2013-03-21 12:24:01 -0400994 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800995
996 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -0400997 return ret;
998}
999
Al Viro599a0ac2013-03-12 09:58:10 -04001000const struct file_operations pipefifo_fops = {
1001 .open = fifo_open,
1002 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001003 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001004 .write_iter = pipe_write,
Christoph Hellwigdd670812017-12-31 16:42:12 +01001005 .get_poll_head = pipe_get_poll_head,
1006 .poll_mask = pipe_poll_mask,
Al Viro599a0ac2013-03-12 09:58:10 -04001007 .unlocked_ioctl = pipe_ioctl,
1008 .release = pipe_release,
1009 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001010};
1011
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001012/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001013 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001014 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001015 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001016unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001017{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001018 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001019 return 0;
1020
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001021 /* Minimum pipe size, as required by POSIX */
1022 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001023 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001024
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001025 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001026}
1027
1028/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001029 * Allocate a new array of pipe buffers and copy the info over. Returns the
1030 * pipe size if successful, or return -ERROR on error.
1031 */
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001032static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
Jens Axboe35f3d142010-05-20 10:43:18 +02001033{
1034 struct pipe_buffer *bufs;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001035 unsigned int size, nr_pages;
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001036 unsigned long user_bufs;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001037 long ret = 0;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001038
1039 size = round_pipe_size(arg);
1040 nr_pages = size >> PAGE_SHIFT;
1041
1042 if (!nr_pages)
1043 return -EINVAL;
1044
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001045 /*
1046 * If trying to increase the pipe capacity, check that an
1047 * unprivileged user is not trying to exceed various limits
1048 * (soft limit check here, hard limit check just below).
1049 * Decreasing the pipe capacity is always permitted, even
1050 * if the user is currently over a limit.
1051 */
1052 if (nr_pages > pipe->buffers &&
1053 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001054 return -EPERM;
1055
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001056 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001057
1058 if (nr_pages > pipe->buffers &&
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001059 (too_many_pipe_buffers_hard(user_bufs) ||
1060 too_many_pipe_buffers_soft(user_bufs)) &&
Eric Biggers85c2dd52018-02-06 15:41:53 -08001061 is_unprivileged_user()) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001062 ret = -EPERM;
1063 goto out_revert_acct;
1064 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001065
1066 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001067 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1068 * expect a lot of shrink+grow operations, just free and allocate
1069 * again like we would do for growing. If the pipe currently
1070 * contains more buffers than arg, then return busy.
1071 */
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001072 if (nr_pages < pipe->nrbufs) {
1073 ret = -EBUSY;
1074 goto out_revert_acct;
1075 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001076
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001077 bufs = kcalloc(nr_pages, sizeof(*bufs),
1078 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001079 if (unlikely(!bufs)) {
1080 ret = -ENOMEM;
1081 goto out_revert_acct;
1082 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001083
1084 /*
1085 * The pipe array wraps around, so just start the new one at zero
1086 * and adjust the indexes.
1087 */
1088 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001089 unsigned int tail;
1090 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001091
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001092 tail = pipe->curbuf + pipe->nrbufs;
1093 if (tail < pipe->buffers)
1094 tail = 0;
1095 else
1096 tail &= (pipe->buffers - 1);
1097
1098 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001099 if (head)
1100 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1101 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001102 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001103 }
1104
1105 pipe->curbuf = 0;
1106 kfree(pipe->bufs);
1107 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001108 pipe->buffers = nr_pages;
1109 return nr_pages * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001110
1111out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001112 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001113 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001114}
1115
Jens Axboeff9da692010-06-03 14:54:39 +02001116/*
Linus Torvalds72083642010-11-28 16:27:19 -08001117 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1118 * location, so checking ->i_pipe is not enough to verify that this is a
1119 * pipe.
1120 */
1121struct pipe_inode_info *get_pipe_info(struct file *file)
1122{
Al Virode32ec42013-03-21 11:16:56 -04001123 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001124}
1125
Jens Axboe35f3d142010-05-20 10:43:18 +02001126long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1127{
1128 struct pipe_inode_info *pipe;
1129 long ret;
1130
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001131 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001132 if (!pipe)
1133 return -EBADF;
1134
Al Viroebec73f2013-03-21 12:24:01 -04001135 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001136
1137 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001138 case F_SETPIPE_SZ:
1139 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001140 break;
1141 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001142 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001143 break;
1144 default:
1145 ret = -EINVAL;
1146 break;
1147 }
1148
Al Viroebec73f2013-03-21 12:24:01 -04001149 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001150 return ret;
1151}
1152
Nick Pigginff0c7d12011-01-07 17:49:50 +11001153static const struct super_operations pipefs_ops = {
1154 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001155 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001156};
1157
Jens Axboe35f3d142010-05-20 10:43:18 +02001158/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * pipefs should _never_ be mounted by userland - too much of security hassle,
1160 * no real gain from having the whole whorehouse mounted. So we don't need
1161 * any operations on the root directory. However, we need a non-trivial
1162 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1163 */
Al Viro51139ad2010-07-25 23:47:46 +04001164static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1165 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Al Viroc74a1cb2011-01-12 16:59:34 -05001167 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1168 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171static struct file_system_type pipe_fs_type = {
1172 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001173 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 .kill_sb = kill_anon_super,
1175};
1176
1177static int __init init_pipe_fs(void)
1178{
1179 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!err) {
1182 pipe_mnt = kern_mount(&pipe_fs_type);
1183 if (IS_ERR(pipe_mnt)) {
1184 err = PTR_ERR(pipe_mnt);
1185 unregister_filesystem(&pipe_fs_type);
1186 }
1187 }
1188 return err;
1189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191fs_initcall(init_pipe_fs);