blob: 2a297bce381f53afbda0ae46c97dbaa1ff4c5bd7 [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 */
Matthew Wilcox0311ff82019-04-05 14:02:10 -0700192bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200193{
Matthew Wilcox0311ff82019-04-05 14:02:10 -0700194 return try_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
Jann Horn2af926f2019-01-23 15:19:17 +0100237static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
238 .can_merge = 0,
239 .confirm = generic_pipe_buf_confirm,
240 .release = anon_pipe_buf_release,
241 .steal = anon_pipe_buf_steal,
242 .get = generic_pipe_buf_get,
243};
244
Linus Torvalds98830352012-04-29 13:12:42 -0700245static const struct pipe_buf_operations packet_pipe_buf_ops = {
246 .can_merge = 0,
Linus Torvalds98830352012-04-29 13:12:42 -0700247 .confirm = generic_pipe_buf_confirm,
248 .release = anon_pipe_buf_release,
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700249 .steal = anon_pipe_buf_steal,
Linus Torvalds98830352012-04-29 13:12:42 -0700250 .get = generic_pipe_buf_get,
251};
252
Jann Horn2af926f2019-01-23 15:19:17 +0100253void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
254{
255 if (buf->ops == &anon_pipe_buf_ops)
256 buf->ops = &anon_pipe_buf_nomerge_ops;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static ssize_t
Al Virofb9096a2014-04-02 19:56:54 -0400260pipe_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Al Virofb9096a2014-04-02 19:56:54 -0400262 size_t total_len = iov_iter_count(to);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700263 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400264 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int do_wakeup;
266 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* Null read succeeds. */
269 if (unlikely(total_len == 0))
270 return 0;
271
272 do_wakeup = 0;
273 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400274 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200276 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200278 int curbuf = pipe->curbuf;
279 struct pipe_buffer *buf = pipe->bufs + curbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500281 size_t written;
282 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (chars > total_len)
285 chars = total_len;
286
Miklos Szeredifba597d2016-09-27 10:45:12 +0200287 error = pipe_buf_confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200288 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200289 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200290 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200291 break;
292 }
Jens Axboef84d7512006-05-01 19:59:03 +0200293
Al Virofb9096a2014-04-02 19:56:54 -0400294 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
Al Viro637b58c2014-02-03 19:11:42 -0500295 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200296 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500297 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
299 }
300 ret += chars;
301 buf->offset += chars;
302 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700303
304 /* Was it a packet buffer? Clean up and exit */
305 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
306 total_len = chars;
307 buf->len = 0;
308 }
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!buf->len) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200311 pipe_buf_release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200312 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200313 pipe->curbuf = curbuf;
314 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 do_wakeup = 1;
316 }
317 total_len -= chars;
318 if (!total_len)
319 break; /* common path: read succeeded */
320 }
321 if (bufs) /* More to do? */
322 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200323 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200325 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* syscall merging: Usually we must not sleep
327 * if O_NONBLOCK is set, or if we got some data.
328 * But if a writer sleeps in kernel space, then
329 * we can wait for that data without violating POSIX.
330 */
331 if (ret)
332 break;
333 if (filp->f_flags & O_NONBLOCK) {
334 ret = -EAGAIN;
335 break;
336 }
337 }
338 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200339 if (!ret)
340 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 break;
342 }
343 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800344 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200345 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200347 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Al Viroebec73f2013-03-21 12:24:01 -0400349 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200350
351 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800353 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200354 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 if (ret > 0)
357 file_accessed(filp);
358 return ret;
359}
360
Linus Torvalds98830352012-04-29 13:12:42 -0700361static inline int is_packetized(struct file *file)
362{
363 return (file->f_flags & O_DIRECT) != 0;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static ssize_t
Al Virof0d1bec2014-04-03 15:05:18 -0400367pipe_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700369 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400370 struct pipe_inode_info *pipe = filp->private_data;
Al Virof0d1bec2014-04-03 15:05:18 -0400371 ssize_t ret = 0;
372 int do_wakeup = 0;
373 size_t total_len = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ssize_t chars;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* Null write succeeds. */
377 if (unlikely(total_len == 0))
378 return 0;
379
Al Viroebec73f2013-03-21 12:24:01 -0400380 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Ingo Molnar923f4f22006-04-11 13:53:33 +0200382 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 send_sig(SIGPIPE, current, 0);
384 ret = -EPIPE;
385 goto out;
386 }
387
388 /* We try to merge small writes */
389 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200390 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200391 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200392 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200393 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200395
Miklos Szeredifba597d2016-09-27 10:45:12 +0200396 if (buf->ops->can_merge && offset + chars <= PAGE_SIZE) {
397 ret = pipe_buf_confirm(pipe, buf);
Eric Biggers6ae08062015-10-17 16:26:09 -0500398 if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200399 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200400
Al Virof0d1bec2014-04-03 15:05:18 -0400401 ret = copy_page_from_iter(buf->page, offset, chars, from);
402 if (unlikely(ret < chars)) {
Eric Biggers6ae08062015-10-17 16:26:09 -0500403 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200405 }
Al Virof0d1bec2014-04-03 15:05:18 -0400406 do_wakeup = 1;
Eric Biggers6ae08062015-10-17 16:26:09 -0500407 buf->len += ret;
Al Virof0d1bec2014-04-03 15:05:18 -0400408 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto out;
410 }
411 }
412
413 for (;;) {
414 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200415
Ingo Molnar923f4f22006-04-11 13:53:33 +0200416 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200418 if (!ret)
419 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
421 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200422 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200423 if (bufs < pipe->buffers) {
424 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200425 struct pipe_buffer *buf = pipe->bufs + newbuf;
426 struct page *page = pipe->tmp_page;
Al Virof0d1bec2014-04-03 15:05:18 -0400427 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (!page) {
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700430 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (unlikely(!page)) {
432 ret = ret ? : -ENOMEM;
433 break;
434 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200435 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200437 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * we lock up (O_NONBLOCK-)readers that sleep due to
439 * syscall merging.
440 * FIXME! Is this really true?
441 */
442 do_wakeup = 1;
Al Virof0d1bec2014-04-03 15:05:18 -0400443 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
444 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200445 if (!ret)
Al Virof0d1bec2014-04-03 15:05:18 -0400446 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 }
Al Virof0d1bec2014-04-03 15:05:18 -0400449 ret += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* Insert it into the buffer array */
452 buf->page = page;
453 buf->ops = &anon_pipe_buf_ops;
454 buf->offset = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400455 buf->len = copied;
Linus Torvalds98830352012-04-29 13:12:42 -0700456 buf->flags = 0;
457 if (is_packetized(filp)) {
458 buf->ops = &packet_pipe_buf_ops;
459 buf->flags = PIPE_BUF_FLAG_PACKET;
460 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200461 pipe->nrbufs = ++bufs;
462 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Al Virof0d1bec2014-04-03 15:05:18 -0400464 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200467 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 continue;
469 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200470 if (!ret)
471 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 }
474 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200475 if (!ret)
476 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 }
479 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800480 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200481 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 do_wakeup = 0;
483 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200484 pipe->waiting_writers++;
485 pipe_wait(pipe);
486 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488out:
Al Viroebec73f2013-03-21 12:24:01 -0400489 __pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800491 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200492 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800494 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400495 int err = file_update_time(filp);
496 if (err)
497 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800498 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return ret;
501}
502
Andi Kleend59d0b12008-02-08 04:21:23 -0800503static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Al Virode32ec42013-03-21 11:16:56 -0400505 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int count, buf, nrbufs;
507
508 switch (cmd) {
509 case FIONREAD:
Al Viroebec73f2013-03-21 12:24:01 -0400510 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200512 buf = pipe->curbuf;
513 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200515 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200516 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Al Viroebec73f2013-03-21 12:24:01 -0400518 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return put_user(count, (int __user *)arg);
521 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100522 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524}
525
Christoph Hellwigdd670812017-12-31 16:42:12 +0100526/* No kernel lock held - fine */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700527static __poll_t
528pipe_poll(struct file *filp, poll_table *wait)
Christoph Hellwigdd670812017-12-31 16:42:12 +0100529{
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700530 __poll_t mask;
Christoph Hellwigdd670812017-12-31 16:42:12 +0100531 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700532 int nrbufs;
533
534 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* Reading only -- no need for acquiring the semaphore. */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700537 nrbufs = pipe->nrbufs;
538 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (filp->f_mode & FMODE_READ) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800540 mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200541 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800542 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
545 if (filp->f_mode & FMODE_WRITE) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800546 mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700547 /*
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800548 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700549 * behave exactly like pipes for poll().
550 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200551 if (!pipe->readers)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800552 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
555 return mask;
556}
557
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800558static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
559{
560 int kill = 0;
561
562 spin_lock(&inode->i_lock);
563 if (!--pipe->files) {
564 inode->i_pipe = NULL;
565 kill = 1;
566 }
567 spin_unlock(&inode->i_lock);
568
569 if (kill)
570 free_pipe_info(pipe);
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static int
Al Viro599a0ac2013-03-12 09:58:10 -0400574pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800576 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200577
Al Viroebec73f2013-03-21 12:24:01 -0400578 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400579 if (file->f_mode & FMODE_READ)
580 pipe->readers--;
581 if (file->f_mode & FMODE_WRITE)
582 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200583
Al Viroba5bb142013-03-21 02:21:19 -0400584 if (pipe->readers || pipe->writers) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800585 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200586 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
587 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Al Viroebec73f2013-03-21 12:24:01 -0400589 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400590
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800591 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593}
594
595static int
Al Viro599a0ac2013-03-12 09:58:10 -0400596pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Al Virode32ec42013-03-21 11:16:56 -0400598 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400599 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Al Viroebec73f2013-03-21 12:24:01 -0400601 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400602 if (filp->f_mode & FMODE_READ)
603 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
604 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200605 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400606 if (retval < 0 && (filp->f_mode & FMODE_READ))
607 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700608 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
609 }
Al Viroebec73f2013-03-21 12:24:01 -0400610 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700611 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700614static unsigned long account_pipe_buffers(struct user_struct *user,
Willy Tarreau759c0112016-01-18 16:36:09 +0100615 unsigned long old, unsigned long new)
616{
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700617 return atomic_long_add_return(new - old, &user->pipe_bufs);
Willy Tarreau759c0112016-01-18 16:36:09 +0100618}
619
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700620static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100621{
Eric Biggersf7340762018-02-06 15:42:08 -0800622 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
623
624 return soft_limit && user_bufs > soft_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100625}
626
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700627static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100628{
Eric Biggersf7340762018-02-06 15:42:08 -0800629 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
630
631 return hard_limit && user_bufs > hard_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100632}
633
Eric Biggers85c2dd52018-02-06 15:41:53 -0800634static bool is_unprivileged_user(void)
635{
636 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
637}
638
Al Viro7bee1302013-03-21 11:04:15 -0400639struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200640{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200641 struct pipe_inode_info *pipe;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700642 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
643 struct user_struct *user = get_current_user();
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700644 unsigned long user_bufs;
Eric Biggersf7340762018-02-06 15:42:08 -0800645 unsigned int max_size = READ_ONCE(pipe_max_size);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200646
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700647 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700648 if (pipe == NULL)
649 goto out_free_uid;
Willy Tarreau759c0112016-01-18 16:36:09 +0100650
Eric Biggersf7340762018-02-06 15:42:08 -0800651 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
652 pipe_bufs = max_size >> PAGE_SHIFT;
Michael Kerrisk (man-pages)086e7742016-10-11 13:53:43 -0700653
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700654 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700655
Eric Biggers85c2dd52018-02-06 15:41:53 -0800656 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700657 user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700658 pipe_bufs = 1;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200659 }
660
Eric Biggers85c2dd52018-02-06 15:41:53 -0800661 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700662 goto out_revert_acct;
663
664 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
665 GFP_KERNEL_ACCOUNT);
666
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700667 if (pipe->bufs) {
668 init_waitqueue_head(&pipe->wait);
669 pipe->r_counter = pipe->w_counter = 1;
670 pipe->buffers = pipe_bufs;
671 pipe->user = user;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700672 mutex_init(&pipe->mutex);
673 return pipe;
674 }
675
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700676out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700677 (void) account_pipe_buffers(user, pipe_bufs, 0);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700678 kfree(pipe);
679out_free_uid:
680 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200681 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200682}
683
Al Viro4b8a8f12013-03-21 11:06:46 -0400684void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700688 (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
Willy Tarreau759c0112016-01-18 16:36:09 +0100689 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200690 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200691 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (buf->ops)
Miklos Szeredia7796382016-09-27 10:45:12 +0200693 pipe_buf_release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200695 if (pipe->tmp_page)
696 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200697 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200698 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800701static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200702
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700703/*
704 * pipefs_dname() is called from d_path().
705 */
706static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
707{
708 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
David Howells75c3cfa2015-03-17 22:26:12 +0000709 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700710}
711
Al Viro3ba13d12009-02-20 06:02:22 +0000712static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700713 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
716static struct inode * get_pipe_inode(void)
717{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200718 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200719 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (!inode)
722 goto fail_inode;
723
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400724 inode->i_ino = get_next_ino();
725
Al Viro7bee1302013-03-21 11:04:15 -0400726 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200727 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200729
Al Viroba5bb142013-03-21 02:21:19 -0400730 inode->i_pipe = pipe;
731 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200732 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400733 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /*
736 * Mark the inode dirty from the very beginning,
737 * that way it will never be moved to the dirty
738 * list because "mark_inode_dirty()" will think
739 * that it already _is_ on the dirty list.
740 */
741 inode->i_state = I_DIRTY;
742 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100743 inode->i_uid = current_fsuid();
744 inode->i_gid = current_fsgid();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700745 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return inode;
748
749fail_iput:
750 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752fail_inode:
753 return NULL;
754}
755
Al Viroe4fad8e2012-07-21 15:33:25 +0400756int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Al Viroe4fad8e2012-07-21 15:33:25 +0400758 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700759 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400762 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Al Viro152b6372018-06-09 10:05:18 -0400764 f = alloc_file_pseudo(inode, pipe_mnt, "",
765 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
766 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500767 if (IS_ERR(f)) {
Al Viro152b6372018-06-09 10:05:18 -0400768 free_pipe_info(inode->i_pipe);
769 iput(inode);
770 return PTR_ERR(f);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Al Virode32ec42013-03-21 11:16:56 -0400773 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Al Viro183266f2018-06-17 14:15:10 -0400775 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
776 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500777 if (IS_ERR(res[0])) {
Al Virob10a4a92018-07-09 02:29:58 -0400778 put_pipe_info(inode, inode->i_pipe);
779 fput(f);
780 return PTR_ERR(res[0]);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500781 }
Al Virode32ec42013-03-21 11:16:56 -0400782 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400783 res[1] = f;
784 return 0;
Andi Kleend6cbd282006-09-30 23:29:26 -0700785}
786
Al Viro5b249b12012-08-19 12:17:29 -0400787static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700788{
Andi Kleend6cbd282006-09-30 23:29:26 -0700789 int error;
790 int fdw, fdr;
791
Linus Torvalds98830352012-04-29 13:12:42 -0700792 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700793 return -EINVAL;
794
Al Viroe4fad8e2012-07-21 15:33:25 +0400795 error = create_pipe_files(files, flags);
796 if (error)
797 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700798
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700799 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700800 if (error < 0)
801 goto err_read_pipe;
802 fdr = error;
803
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700804 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700805 if (error < 0)
806 goto err_fdr;
807 fdw = error;
808
Al Viro157cf642008-12-14 04:57:47 -0500809 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700810 fd[0] = fdr;
811 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 0;
813
Andi Kleend6cbd282006-09-30 23:29:26 -0700814 err_fdr:
815 put_unused_fd(fdr);
816 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400817 fput(files[0]);
818 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700819 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Al Viro5b249b12012-08-19 12:17:29 -0400822int do_pipe_flags(int *fd, int flags)
823{
824 struct file *files[2];
825 int error = __do_pipe_flags(fd, files, flags);
826 if (!error) {
827 fd_install(fd[0], files[0]);
828 fd_install(fd[1], files[1]);
829 }
830 return error;
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400834 * sys_pipe() is the normal C calling standard for creating
835 * a pipe. It's not the way Unix traditionally does this, though.
836 */
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100837static int do_pipe2(int __user *fildes, int flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400838{
Al Viro5b249b12012-08-19 12:17:29 -0400839 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400840 int fd[2];
841 int error;
842
Al Viro5b249b12012-08-19 12:17:29 -0400843 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400844 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400845 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
846 fput(files[0]);
847 fput(files[1]);
848 put_unused_fd(fd[0]);
849 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400850 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400851 } else {
852 fd_install(fd[0], files[0]);
853 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700854 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400855 }
856 return error;
857}
858
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100859SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
860{
861 return do_pipe2(fildes, flags);
862}
863
Heiko Carstens2b664212009-01-14 14:14:35 +0100864SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700865{
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100866 return do_pipe2(fildes, 0);
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700867}
868
Al Virofc7478a2013-03-21 02:07:59 -0400869static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400870{
871 int cur = *cnt;
872
873 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400874 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400875 if (signal_pending(current))
876 break;
877 }
878 return cur == *cnt ? -ERESTARTSYS : 0;
879}
880
Al Virofc7478a2013-03-21 02:07:59 -0400881static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400882{
Al Virofc7478a2013-03-21 02:07:59 -0400883 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400884}
885
886static int fifo_open(struct inode *inode, struct file *filp)
887{
888 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400889 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400890 int ret;
891
Al Viroba5bb142013-03-21 02:21:19 -0400892 filp->f_version = 0;
893
894 spin_lock(&inode->i_lock);
895 if (inode->i_pipe) {
896 pipe = inode->i_pipe;
897 pipe->files++;
898 spin_unlock(&inode->i_lock);
899 } else {
900 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400901 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400902 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400903 return -ENOMEM;
904 pipe->files = 1;
905 spin_lock(&inode->i_lock);
906 if (unlikely(inode->i_pipe)) {
907 inode->i_pipe->files++;
908 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400909 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400910 pipe = inode->i_pipe;
911 } else {
912 inode->i_pipe = pipe;
913 spin_unlock(&inode->i_lock);
914 }
Al Virof776c732013-03-12 09:46:27 -0400915 }
Al Virode32ec42013-03-21 11:16:56 -0400916 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400917 /* OK, we have a pipe and it's pinned down */
918
Al Viroebec73f2013-03-21 12:24:01 -0400919 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400920
921 /* We can only do regular read/write on fifos */
922 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
923
924 switch (filp->f_mode) {
925 case FMODE_READ:
926 /*
927 * O_RDONLY
928 * POSIX.1 says that O_NONBLOCK means return with the FIFO
929 * opened, even when there is no process writing the FIFO.
930 */
Al Virof776c732013-03-12 09:46:27 -0400931 pipe->r_counter++;
932 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400933 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400934
Al Viro599a0ac2013-03-12 09:58:10 -0400935 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400936 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800937 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -0400938 * seen a writer */
939 filp->f_version = pipe->w_counter;
940 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400941 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400942 goto err_rd;
943 }
944 }
945 break;
946
947 case FMODE_WRITE:
948 /*
949 * O_WRONLY
950 * POSIX.1 says that O_NONBLOCK means return -1 with
951 * errno=ENXIO when there is no process reading the FIFO.
952 */
953 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400954 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400955 goto err;
956
Al Virof776c732013-03-12 09:46:27 -0400957 pipe->w_counter++;
958 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400959 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400960
Al Viro599a0ac2013-03-12 09:58:10 -0400961 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400962 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400963 goto err_wr;
964 }
965 break;
966
967 case FMODE_READ | FMODE_WRITE:
968 /*
969 * O_RDWR
970 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
971 * This implementation will NEVER block on a O_RDWR open, since
972 * the process can at least talk to itself.
973 */
Al Virof776c732013-03-12 09:46:27 -0400974
975 pipe->readers++;
976 pipe->writers++;
977 pipe->r_counter++;
978 pipe->w_counter++;
979 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400980 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400981 break;
982
983 default:
984 ret = -EINVAL;
985 goto err;
986 }
987
988 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -0400989 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400990 return 0;
991
992err_rd:
993 if (!--pipe->readers)
994 wake_up_interruptible(&pipe->wait);
995 ret = -ERESTARTSYS;
996 goto err;
997
998err_wr:
999 if (!--pipe->writers)
1000 wake_up_interruptible(&pipe->wait);
1001 ret = -ERESTARTSYS;
1002 goto err;
1003
1004err:
Al Viroebec73f2013-03-21 12:24:01 -04001005 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -08001006
1007 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -04001008 return ret;
1009}
1010
Al Viro599a0ac2013-03-12 09:58:10 -04001011const struct file_operations pipefifo_fops = {
1012 .open = fifo_open,
1013 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001014 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001015 .write_iter = pipe_write,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001016 .poll = pipe_poll,
Al Viro599a0ac2013-03-12 09:58:10 -04001017 .unlocked_ioctl = pipe_ioctl,
1018 .release = pipe_release,
1019 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001020};
1021
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001022/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001023 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001024 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001025 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001026unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001027{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001028 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001029 return 0;
1030
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001031 /* Minimum pipe size, as required by POSIX */
1032 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001033 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001034
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001035 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001036}
1037
1038/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001039 * Allocate a new array of pipe buffers and copy the info over. Returns the
1040 * pipe size if successful, or return -ERROR on error.
1041 */
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001042static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
Jens Axboe35f3d142010-05-20 10:43:18 +02001043{
1044 struct pipe_buffer *bufs;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001045 unsigned int size, nr_pages;
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001046 unsigned long user_bufs;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001047 long ret = 0;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001048
1049 size = round_pipe_size(arg);
1050 nr_pages = size >> PAGE_SHIFT;
1051
1052 if (!nr_pages)
1053 return -EINVAL;
1054
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001055 /*
1056 * If trying to increase the pipe capacity, check that an
1057 * unprivileged user is not trying to exceed various limits
1058 * (soft limit check here, hard limit check just below).
1059 * Decreasing the pipe capacity is always permitted, even
1060 * if the user is currently over a limit.
1061 */
1062 if (nr_pages > pipe->buffers &&
1063 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001064 return -EPERM;
1065
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001066 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001067
1068 if (nr_pages > pipe->buffers &&
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001069 (too_many_pipe_buffers_hard(user_bufs) ||
1070 too_many_pipe_buffers_soft(user_bufs)) &&
Eric Biggers85c2dd52018-02-06 15:41:53 -08001071 is_unprivileged_user()) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001072 ret = -EPERM;
1073 goto out_revert_acct;
1074 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001075
1076 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001077 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1078 * expect a lot of shrink+grow operations, just free and allocate
1079 * again like we would do for growing. If the pipe currently
1080 * contains more buffers than arg, then return busy.
1081 */
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001082 if (nr_pages < pipe->nrbufs) {
1083 ret = -EBUSY;
1084 goto out_revert_acct;
1085 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001086
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001087 bufs = kcalloc(nr_pages, sizeof(*bufs),
1088 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001089 if (unlikely(!bufs)) {
1090 ret = -ENOMEM;
1091 goto out_revert_acct;
1092 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001093
1094 /*
1095 * The pipe array wraps around, so just start the new one at zero
1096 * and adjust the indexes.
1097 */
1098 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001099 unsigned int tail;
1100 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001101
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001102 tail = pipe->curbuf + pipe->nrbufs;
1103 if (tail < pipe->buffers)
1104 tail = 0;
1105 else
1106 tail &= (pipe->buffers - 1);
1107
1108 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001109 if (head)
1110 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1111 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001112 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001113 }
1114
1115 pipe->curbuf = 0;
1116 kfree(pipe->bufs);
1117 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001118 pipe->buffers = nr_pages;
1119 return nr_pages * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001120
1121out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001122 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001123 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001124}
1125
Jens Axboeff9da692010-06-03 14:54:39 +02001126/*
Linus Torvalds72083642010-11-28 16:27:19 -08001127 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1128 * location, so checking ->i_pipe is not enough to verify that this is a
1129 * pipe.
1130 */
1131struct pipe_inode_info *get_pipe_info(struct file *file)
1132{
Al Virode32ec42013-03-21 11:16:56 -04001133 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001134}
1135
Jens Axboe35f3d142010-05-20 10:43:18 +02001136long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1137{
1138 struct pipe_inode_info *pipe;
1139 long ret;
1140
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001141 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001142 if (!pipe)
1143 return -EBADF;
1144
Al Viroebec73f2013-03-21 12:24:01 -04001145 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001146
1147 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001148 case F_SETPIPE_SZ:
1149 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001150 break;
1151 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001152 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001153 break;
1154 default:
1155 ret = -EINVAL;
1156 break;
1157 }
1158
Al Viroebec73f2013-03-21 12:24:01 -04001159 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001160 return ret;
1161}
1162
Nick Pigginff0c7d12011-01-07 17:49:50 +11001163static const struct super_operations pipefs_ops = {
1164 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001165 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001166};
1167
Jens Axboe35f3d142010-05-20 10:43:18 +02001168/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * pipefs should _never_ be mounted by userland - too much of security hassle,
1170 * no real gain from having the whole whorehouse mounted. So we don't need
1171 * any operations on the root directory. However, we need a non-trivial
1172 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1173 */
Al Viro51139ad2010-07-25 23:47:46 +04001174static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1175 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Al Viroc74a1cb2011-01-12 16:59:34 -05001177 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1178 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181static struct file_system_type pipe_fs_type = {
1182 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001183 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 .kill_sb = kill_anon_super,
1185};
1186
1187static int __init init_pipe_fs(void)
1188{
1189 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (!err) {
1192 pipe_mnt = kern_mount(&pipe_fs_type);
1193 if (IS_ERR(pipe_mnt)) {
1194 err = PTR_ERR(pipe_mnt);
1195 unregister_filesystem(&pipe_fs_type);
1196 }
1197 }
1198 return err;
1199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201fs_initcall(init_pipe_fs);