blob: 41065901106b09d4365ebc13ee6cfa7b6465339b [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) {
Shakeel Butt60cd4bc2019-03-05 15:43:13 -0800143 memcg_kmem_uncharge(page, 0);
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700144 __SetPageLocked(page);
145 return 0;
146 }
147 return 1;
148}
149
Jens Axboe08457182007-06-12 20:51:32 +0200150/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800151 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200152 * @pipe: the pipe that the buffer belongs to
153 * @buf: the buffer to attempt to steal
154 *
155 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800156 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200157 * @buf. If successful, this function returns 0 and returns with
158 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800159 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200160 * page cache.
161 */
Jens Axboe330ab712006-05-02 15:29:57 +0200162int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
163 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200164{
Jens Axboe46e678c2006-04-30 16:36:32 +0200165 struct page *page = buf->page;
166
Jens Axboe08457182007-06-12 20:51:32 +0200167 /*
168 * A reference of one is golden, that means that the owner of this
169 * page is the only one holding a reference to it. lock the page
170 * and return OK.
171 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200172 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200173 lock_page(page);
174 return 0;
175 }
176
177 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200178}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200179EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200180
Jens Axboe08457182007-06-12 20:51:32 +0200181/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800182 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200183 * @pipe: the pipe that the buffer belongs to
184 * @buf: the buffer to get a reference to
185 *
186 * Description:
187 * This function grabs an extra reference to @buf. It's used in
188 * in the tee() system call, when we duplicate the buffers in one
189 * pipe into another.
190 */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700191bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200192{
Matthew Wilcox15fab632019-04-05 14:02:10 -0700193 return try_get_page(buf->page);
Jens Axboe70524492006-04-11 15:51:17 +0200194}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200195EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200196
Jens Axboe08457182007-06-12 20:51:32 +0200197/**
198 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200199 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200200 * @buf: the buffer to confirm
201 *
202 * Description:
203 * This function does nothing, because the generic pipe code uses
204 * pages that are always good when inserted into the pipe.
205 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200206int generic_pipe_buf_confirm(struct pipe_inode_info *info,
207 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200208{
209 return 0;
210}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200211EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200212
Miklos Szeredi68181732009-05-07 15:37:36 +0200213/**
214 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
215 * @pipe: the pipe that the buffer belongs to
216 * @buf: the buffer to put a reference to
217 *
218 * Description:
219 * This function releases a reference to @buf.
220 */
221void generic_pipe_buf_release(struct pipe_inode_info *pipe,
222 struct pipe_buffer *buf)
223{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300224 put_page(buf->page);
Miklos Szeredi68181732009-05-07 15:37:36 +0200225}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200226EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200227
Jann Horn01e71872019-01-23 15:19:18 +0100228/* New data written to a pipe may be appended to a buffer with this type. */
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800229static const struct pipe_buf_operations anon_pipe_buf_ops = {
Jens Axboecac36bb02007-06-14 13:10:48 +0200230 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .release = anon_pipe_buf_release,
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700232 .steal = anon_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200233 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Jann Horna0ce2f02019-01-23 15:19:17 +0100236static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .confirm = generic_pipe_buf_confirm,
238 .release = anon_pipe_buf_release,
239 .steal = anon_pipe_buf_steal,
240 .get = generic_pipe_buf_get,
Ingo Molnar923f4f22006-04-11 13:53:33 +0200241};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds98830352012-04-29 13:12:42 -0700243static const struct pipe_buf_operations packet_pipe_buf_ops = {
Linus Torvalds98830352012-04-29 13:12:42 -0700244 .confirm = generic_pipe_buf_confirm,
245 .release = anon_pipe_buf_release,
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700246 .steal = anon_pipe_buf_steal,
Linus Torvalds98830352012-04-29 13:12:42 -0700247 .get = generic_pipe_buf_get,
248};
249
Jann Horn01e71872019-01-23 15:19:18 +0100250/**
251 * pipe_buf_mark_unmergeable - mark a &struct pipe_buffer as unmergeable
252 * @buf: the buffer to mark
253 *
254 * Description:
255 * This function ensures that no future writes will be merged into the
256 * given &struct pipe_buffer. This is necessary when multiple pipe buffers
257 * share the same backing page.
258 */
Jann Horna0ce2f02019-01-23 15:19:17 +0100259void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
260{
261 if (buf->ops == &anon_pipe_buf_ops)
262 buf->ops = &anon_pipe_buf_nomerge_ops;
263}
264
Jann Horn01e71872019-01-23 15:19:18 +0100265static bool pipe_buf_can_merge(struct pipe_buffer *buf)
266{
267 return buf->ops == &anon_pipe_buf_ops;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static ssize_t
Al Virofb9096a2014-04-02 19:56:54 -0400271pipe_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Al Virofb9096a2014-04-02 19:56:54 -0400273 size_t total_len = iov_iter_count(to);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700274 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400275 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int do_wakeup;
277 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* Null read succeeds. */
280 if (unlikely(total_len == 0))
281 return 0;
282
283 do_wakeup = 0;
284 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400285 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200287 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200289 int curbuf = pipe->curbuf;
290 struct pipe_buffer *buf = pipe->bufs + curbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500292 size_t written;
293 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (chars > total_len)
296 chars = total_len;
297
Miklos Szeredifba597d2016-09-27 10:45:12 +0200298 error = pipe_buf_confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200299 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200300 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200301 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200302 break;
303 }
Jens Axboef84d7512006-05-01 19:59:03 +0200304
Al Virofb9096a2014-04-02 19:56:54 -0400305 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
Al Viro637b58c2014-02-03 19:11:42 -0500306 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200307 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500308 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 break;
310 }
311 ret += chars;
312 buf->offset += chars;
313 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700314
315 /* Was it a packet buffer? Clean up and exit */
316 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
317 total_len = chars;
318 buf->len = 0;
319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!buf->len) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200322 pipe_buf_release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200323 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200324 pipe->curbuf = curbuf;
325 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 do_wakeup = 1;
327 }
328 total_len -= chars;
329 if (!total_len)
330 break; /* common path: read succeeded */
331 }
332 if (bufs) /* More to do? */
333 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200334 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200336 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* syscall merging: Usually we must not sleep
338 * if O_NONBLOCK is set, or if we got some data.
339 * But if a writer sleeps in kernel space, then
340 * we can wait for that data without violating POSIX.
341 */
342 if (ret)
343 break;
344 if (filp->f_flags & O_NONBLOCK) {
345 ret = -EAGAIN;
346 break;
347 }
348 }
349 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200350 if (!ret)
351 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 }
354 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800355 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200356 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200358 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Al Viroebec73f2013-03-21 12:24:01 -0400360 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200361
362 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800364 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200365 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 if (ret > 0)
368 file_accessed(filp);
369 return ret;
370}
371
Linus Torvalds98830352012-04-29 13:12:42 -0700372static inline int is_packetized(struct file *file)
373{
374 return (file->f_flags & O_DIRECT) != 0;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static ssize_t
Al Virof0d1bec2014-04-03 15:05:18 -0400378pipe_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700380 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400381 struct pipe_inode_info *pipe = filp->private_data;
Al Virof0d1bec2014-04-03 15:05:18 -0400382 ssize_t ret = 0;
383 int do_wakeup = 0;
384 size_t total_len = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ssize_t chars;
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* Null write succeeds. */
388 if (unlikely(total_len == 0))
389 return 0;
390
Al Viroebec73f2013-03-21 12:24:01 -0400391 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Ingo Molnar923f4f22006-04-11 13:53:33 +0200393 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 send_sig(SIGPIPE, current, 0);
395 ret = -EPIPE;
396 goto out;
397 }
398
399 /* We try to merge small writes */
400 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200401 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200402 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200403 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200404 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200406
Jann Horn01e71872019-01-23 15:19:18 +0100407 if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200408 ret = pipe_buf_confirm(pipe, buf);
Eric Biggers6ae08062015-10-17 16:26:09 -0500409 if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200410 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200411
Al Virof0d1bec2014-04-03 15:05:18 -0400412 ret = copy_page_from_iter(buf->page, offset, chars, from);
413 if (unlikely(ret < chars)) {
Eric Biggers6ae08062015-10-17 16:26:09 -0500414 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200416 }
Al Virof0d1bec2014-04-03 15:05:18 -0400417 do_wakeup = 1;
Eric Biggers6ae08062015-10-17 16:26:09 -0500418 buf->len += ret;
Al Virof0d1bec2014-04-03 15:05:18 -0400419 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto out;
421 }
422 }
423
424 for (;;) {
425 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200426
Ingo Molnar923f4f22006-04-11 13:53:33 +0200427 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200429 if (!ret)
430 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200433 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200434 if (bufs < pipe->buffers) {
435 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200436 struct pipe_buffer *buf = pipe->bufs + newbuf;
437 struct page *page = pipe->tmp_page;
Al Virof0d1bec2014-04-03 15:05:18 -0400438 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (!page) {
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700441 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (unlikely(!page)) {
443 ret = ret ? : -ENOMEM;
444 break;
445 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200446 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200448 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * we lock up (O_NONBLOCK-)readers that sleep due to
450 * syscall merging.
451 * FIXME! Is this really true?
452 */
453 do_wakeup = 1;
Al Virof0d1bec2014-04-03 15:05:18 -0400454 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
455 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200456 if (!ret)
Al Virof0d1bec2014-04-03 15:05:18 -0400457 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
459 }
Al Virof0d1bec2014-04-03 15:05:18 -0400460 ret += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* Insert it into the buffer array */
463 buf->page = page;
464 buf->ops = &anon_pipe_buf_ops;
465 buf->offset = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400466 buf->len = copied;
Linus Torvalds98830352012-04-29 13:12:42 -0700467 buf->flags = 0;
468 if (is_packetized(filp)) {
469 buf->ops = &packet_pipe_buf_ops;
470 buf->flags = PIPE_BUF_FLAG_PACKET;
471 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200472 pipe->nrbufs = ++bufs;
473 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Al Virof0d1bec2014-04-03 15:05:18 -0400475 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200478 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 continue;
480 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200481 if (!ret)
482 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 }
485 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200486 if (!ret)
487 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 }
490 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 do_wakeup = 0;
494 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200495 pipe->waiting_writers++;
496 pipe_wait(pipe);
497 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499out:
Al Viroebec73f2013-03-21 12:24:01 -0400500 __pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800502 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200503 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800505 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400506 int err = file_update_time(filp);
507 if (err)
508 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800509 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return ret;
512}
513
Andi Kleend59d0b12008-02-08 04:21:23 -0800514static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Al Virode32ec42013-03-21 11:16:56 -0400516 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int count, buf, nrbufs;
518
519 switch (cmd) {
520 case FIONREAD:
Al Viroebec73f2013-03-21 12:24:01 -0400521 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200523 buf = pipe->curbuf;
524 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200526 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200527 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Al Viroebec73f2013-03-21 12:24:01 -0400529 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return put_user(count, (int __user *)arg);
532 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100533 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535}
536
Christoph Hellwigdd670812017-12-31 16:42:12 +0100537/* No kernel lock held - fine */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700538static __poll_t
539pipe_poll(struct file *filp, poll_table *wait)
Christoph Hellwigdd670812017-12-31 16:42:12 +0100540{
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700541 __poll_t mask;
Christoph Hellwigdd670812017-12-31 16:42:12 +0100542 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700543 int nrbufs;
544
545 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* Reading only -- no need for acquiring the semaphore. */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700548 nrbufs = pipe->nrbufs;
549 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (filp->f_mode & FMODE_READ) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800551 mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200552 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800553 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
556 if (filp->f_mode & FMODE_WRITE) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800557 mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700558 /*
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800559 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700560 * behave exactly like pipes for poll().
561 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200562 if (!pipe->readers)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800563 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 return mask;
567}
568
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800569static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
570{
571 int kill = 0;
572
573 spin_lock(&inode->i_lock);
574 if (!--pipe->files) {
575 inode->i_pipe = NULL;
576 kill = 1;
577 }
578 spin_unlock(&inode->i_lock);
579
580 if (kill)
581 free_pipe_info(pipe);
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static int
Al Viro599a0ac2013-03-12 09:58:10 -0400585pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800587 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200588
Al Viroebec73f2013-03-21 12:24:01 -0400589 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400590 if (file->f_mode & FMODE_READ)
591 pipe->readers--;
592 if (file->f_mode & FMODE_WRITE)
593 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200594
Al Viroba5bb142013-03-21 02:21:19 -0400595 if (pipe->readers || pipe->writers) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800596 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200597 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
598 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Al Viroebec73f2013-03-21 12:24:01 -0400600 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400601
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800602 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
606static int
Al Viro599a0ac2013-03-12 09:58:10 -0400607pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Al Virode32ec42013-03-21 11:16:56 -0400609 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400610 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Al Viroebec73f2013-03-21 12:24:01 -0400612 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400613 if (filp->f_mode & FMODE_READ)
614 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
615 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200616 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400617 if (retval < 0 && (filp->f_mode & FMODE_READ))
618 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700619 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
620 }
Al Viroebec73f2013-03-21 12:24:01 -0400621 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700622 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700625static unsigned long account_pipe_buffers(struct user_struct *user,
Willy Tarreau759c0112016-01-18 16:36:09 +0100626 unsigned long old, unsigned long new)
627{
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700628 return atomic_long_add_return(new - old, &user->pipe_bufs);
Willy Tarreau759c0112016-01-18 16:36:09 +0100629}
630
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700631static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100632{
Eric Biggersf7340762018-02-06 15:42:08 -0800633 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
634
635 return soft_limit && user_bufs > soft_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100636}
637
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700638static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100639{
Eric Biggersf7340762018-02-06 15:42:08 -0800640 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
641
642 return hard_limit && user_bufs > hard_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100643}
644
Eric Biggers85c2dd52018-02-06 15:41:53 -0800645static bool is_unprivileged_user(void)
646{
647 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
648}
649
Al Viro7bee1302013-03-21 11:04:15 -0400650struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200651{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200652 struct pipe_inode_info *pipe;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700653 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
654 struct user_struct *user = get_current_user();
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700655 unsigned long user_bufs;
Eric Biggersf7340762018-02-06 15:42:08 -0800656 unsigned int max_size = READ_ONCE(pipe_max_size);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200657
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700658 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700659 if (pipe == NULL)
660 goto out_free_uid;
Willy Tarreau759c0112016-01-18 16:36:09 +0100661
Eric Biggersf7340762018-02-06 15:42:08 -0800662 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
663 pipe_bufs = max_size >> PAGE_SHIFT;
Michael Kerrisk (man-pages)086e7742016-10-11 13:53:43 -0700664
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700665 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700666
Eric Biggers85c2dd52018-02-06 15:41:53 -0800667 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700668 user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700669 pipe_bufs = 1;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200670 }
671
Eric Biggers85c2dd52018-02-06 15:41:53 -0800672 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700673 goto out_revert_acct;
674
675 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
676 GFP_KERNEL_ACCOUNT);
677
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700678 if (pipe->bufs) {
679 init_waitqueue_head(&pipe->wait);
680 pipe->r_counter = pipe->w_counter = 1;
681 pipe->buffers = pipe_bufs;
682 pipe->user = user;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700683 mutex_init(&pipe->mutex);
684 return pipe;
685 }
686
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700687out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700688 (void) account_pipe_buffers(user, pipe_bufs, 0);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700689 kfree(pipe);
690out_free_uid:
691 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200692 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200693}
694
Al Viro4b8a8f12013-03-21 11:06:46 -0400695void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700699 (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
Willy Tarreau759c0112016-01-18 16:36:09 +0100700 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200701 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200702 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (buf->ops)
Miklos Szeredia7796382016-09-27 10:45:12 +0200704 pipe_buf_release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200706 if (pipe->tmp_page)
707 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200708 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200709 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800712static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200713
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700714/*
715 * pipefs_dname() is called from d_path().
716 */
717static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
718{
719 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
David Howells75c3cfa2015-03-17 22:26:12 +0000720 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700721}
722
Al Viro3ba13d12009-02-20 06:02:22 +0000723static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700724 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725};
726
727static struct inode * get_pipe_inode(void)
728{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200729 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200730 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (!inode)
733 goto fail_inode;
734
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400735 inode->i_ino = get_next_ino();
736
Al Viro7bee1302013-03-21 11:04:15 -0400737 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200738 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200740
Al Viroba5bb142013-03-21 02:21:19 -0400741 inode->i_pipe = pipe;
742 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200743 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400744 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /*
747 * Mark the inode dirty from the very beginning,
748 * that way it will never be moved to the dirty
749 * list because "mark_inode_dirty()" will think
750 * that it already _is_ on the dirty list.
751 */
752 inode->i_state = I_DIRTY;
753 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100754 inode->i_uid = current_fsuid();
755 inode->i_gid = current_fsgid();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700756 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return inode;
759
760fail_iput:
761 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763fail_inode:
764 return NULL;
765}
766
Al Viroe4fad8e2012-07-21 15:33:25 +0400767int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Al Viroe4fad8e2012-07-21 15:33:25 +0400769 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700770 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400773 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Al Viro152b6372018-06-09 10:05:18 -0400775 f = alloc_file_pseudo(inode, pipe_mnt, "",
776 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
777 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500778 if (IS_ERR(f)) {
Al Viro152b6372018-06-09 10:05:18 -0400779 free_pipe_info(inode->i_pipe);
780 iput(inode);
781 return PTR_ERR(f);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Al Virode32ec42013-03-21 11:16:56 -0400784 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Al Viro183266f2018-06-17 14:15:10 -0400786 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
787 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500788 if (IS_ERR(res[0])) {
Al Virob10a4a92018-07-09 02:29:58 -0400789 put_pipe_info(inode, inode->i_pipe);
790 fput(f);
791 return PTR_ERR(res[0]);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500792 }
Al Virode32ec42013-03-21 11:16:56 -0400793 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400794 res[1] = f;
795 return 0;
Andi Kleend6cbd282006-09-30 23:29:26 -0700796}
797
Al Viro5b249b12012-08-19 12:17:29 -0400798static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700799{
Andi Kleend6cbd282006-09-30 23:29:26 -0700800 int error;
801 int fdw, fdr;
802
Linus Torvalds98830352012-04-29 13:12:42 -0700803 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700804 return -EINVAL;
805
Al Viroe4fad8e2012-07-21 15:33:25 +0400806 error = create_pipe_files(files, flags);
807 if (error)
808 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700809
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700810 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700811 if (error < 0)
812 goto err_read_pipe;
813 fdr = error;
814
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700815 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700816 if (error < 0)
817 goto err_fdr;
818 fdw = error;
819
Al Viro157cf642008-12-14 04:57:47 -0500820 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700821 fd[0] = fdr;
822 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824
Andi Kleend6cbd282006-09-30 23:29:26 -0700825 err_fdr:
826 put_unused_fd(fdr);
827 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400828 fput(files[0]);
829 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700830 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Al Viro5b249b12012-08-19 12:17:29 -0400833int do_pipe_flags(int *fd, int flags)
834{
835 struct file *files[2];
836 int error = __do_pipe_flags(fd, files, flags);
837 if (!error) {
838 fd_install(fd[0], files[0]);
839 fd_install(fd[1], files[1]);
840 }
841 return error;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400845 * sys_pipe() is the normal C calling standard for creating
846 * a pipe. It's not the way Unix traditionally does this, though.
847 */
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100848static int do_pipe2(int __user *fildes, int flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400849{
Al Viro5b249b12012-08-19 12:17:29 -0400850 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400851 int fd[2];
852 int error;
853
Al Viro5b249b12012-08-19 12:17:29 -0400854 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400855 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400856 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
857 fput(files[0]);
858 fput(files[1]);
859 put_unused_fd(fd[0]);
860 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400861 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400862 } else {
863 fd_install(fd[0], files[0]);
864 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700865 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400866 }
867 return error;
868}
869
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100870SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
871{
872 return do_pipe2(fildes, flags);
873}
874
Heiko Carstens2b664212009-01-14 14:14:35 +0100875SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700876{
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100877 return do_pipe2(fildes, 0);
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700878}
879
Al Virofc7478a2013-03-21 02:07:59 -0400880static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400881{
882 int cur = *cnt;
883
884 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400885 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400886 if (signal_pending(current))
887 break;
888 }
889 return cur == *cnt ? -ERESTARTSYS : 0;
890}
891
Al Virofc7478a2013-03-21 02:07:59 -0400892static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400893{
Al Virofc7478a2013-03-21 02:07:59 -0400894 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400895}
896
897static int fifo_open(struct inode *inode, struct file *filp)
898{
899 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400900 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400901 int ret;
902
Al Viroba5bb142013-03-21 02:21:19 -0400903 filp->f_version = 0;
904
905 spin_lock(&inode->i_lock);
906 if (inode->i_pipe) {
907 pipe = inode->i_pipe;
908 pipe->files++;
909 spin_unlock(&inode->i_lock);
910 } else {
911 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400912 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400913 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400914 return -ENOMEM;
915 pipe->files = 1;
916 spin_lock(&inode->i_lock);
917 if (unlikely(inode->i_pipe)) {
918 inode->i_pipe->files++;
919 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400920 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400921 pipe = inode->i_pipe;
922 } else {
923 inode->i_pipe = pipe;
924 spin_unlock(&inode->i_lock);
925 }
Al Virof776c732013-03-12 09:46:27 -0400926 }
Al Virode32ec42013-03-21 11:16:56 -0400927 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400928 /* OK, we have a pipe and it's pinned down */
929
Al Viroebec73f2013-03-21 12:24:01 -0400930 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400931
932 /* We can only do regular read/write on fifos */
933 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
934
935 switch (filp->f_mode) {
936 case FMODE_READ:
937 /*
938 * O_RDONLY
939 * POSIX.1 says that O_NONBLOCK means return with the FIFO
940 * opened, even when there is no process writing the FIFO.
941 */
Al Virof776c732013-03-12 09:46:27 -0400942 pipe->r_counter++;
943 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400944 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400945
Al Viro599a0ac2013-03-12 09:58:10 -0400946 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400947 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800948 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -0400949 * seen a writer */
950 filp->f_version = pipe->w_counter;
951 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400952 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400953 goto err_rd;
954 }
955 }
956 break;
957
958 case FMODE_WRITE:
959 /*
960 * O_WRONLY
961 * POSIX.1 says that O_NONBLOCK means return -1 with
962 * errno=ENXIO when there is no process reading the FIFO.
963 */
964 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400965 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400966 goto err;
967
Al Virof776c732013-03-12 09:46:27 -0400968 pipe->w_counter++;
969 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400970 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400971
Al Viro599a0ac2013-03-12 09:58:10 -0400972 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400973 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400974 goto err_wr;
975 }
976 break;
977
978 case FMODE_READ | FMODE_WRITE:
979 /*
980 * O_RDWR
981 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
982 * This implementation will NEVER block on a O_RDWR open, since
983 * the process can at least talk to itself.
984 */
Al Virof776c732013-03-12 09:46:27 -0400985
986 pipe->readers++;
987 pipe->writers++;
988 pipe->r_counter++;
989 pipe->w_counter++;
990 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400991 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400992 break;
993
994 default:
995 ret = -EINVAL;
996 goto err;
997 }
998
999 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -04001000 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001001 return 0;
1002
1003err_rd:
1004 if (!--pipe->readers)
1005 wake_up_interruptible(&pipe->wait);
1006 ret = -ERESTARTSYS;
1007 goto err;
1008
1009err_wr:
1010 if (!--pipe->writers)
1011 wake_up_interruptible(&pipe->wait);
1012 ret = -ERESTARTSYS;
1013 goto err;
1014
1015err:
Al Viroebec73f2013-03-21 12:24:01 -04001016 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -08001017
1018 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -04001019 return ret;
1020}
1021
Al Viro599a0ac2013-03-12 09:58:10 -04001022const struct file_operations pipefifo_fops = {
1023 .open = fifo_open,
1024 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001025 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001026 .write_iter = pipe_write,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001027 .poll = pipe_poll,
Al Viro599a0ac2013-03-12 09:58:10 -04001028 .unlocked_ioctl = pipe_ioctl,
1029 .release = pipe_release,
1030 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001031};
1032
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001033/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001034 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001035 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001036 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001037unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001038{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001039 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001040 return 0;
1041
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001042 /* Minimum pipe size, as required by POSIX */
1043 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001044 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001045
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001046 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001047}
1048
1049/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001050 * Allocate a new array of pipe buffers and copy the info over. Returns the
1051 * pipe size if successful, or return -ERROR on error.
1052 */
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001053static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
Jens Axboe35f3d142010-05-20 10:43:18 +02001054{
1055 struct pipe_buffer *bufs;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001056 unsigned int size, nr_pages;
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001057 unsigned long user_bufs;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001058 long ret = 0;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001059
1060 size = round_pipe_size(arg);
1061 nr_pages = size >> PAGE_SHIFT;
1062
1063 if (!nr_pages)
1064 return -EINVAL;
1065
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001066 /*
1067 * If trying to increase the pipe capacity, check that an
1068 * unprivileged user is not trying to exceed various limits
1069 * (soft limit check here, hard limit check just below).
1070 * Decreasing the pipe capacity is always permitted, even
1071 * if the user is currently over a limit.
1072 */
1073 if (nr_pages > pipe->buffers &&
1074 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001075 return -EPERM;
1076
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001077 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001078
1079 if (nr_pages > pipe->buffers &&
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001080 (too_many_pipe_buffers_hard(user_bufs) ||
1081 too_many_pipe_buffers_soft(user_bufs)) &&
Eric Biggers85c2dd52018-02-06 15:41:53 -08001082 is_unprivileged_user()) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001083 ret = -EPERM;
1084 goto out_revert_acct;
1085 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001086
1087 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001088 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1089 * expect a lot of shrink+grow operations, just free and allocate
1090 * again like we would do for growing. If the pipe currently
1091 * contains more buffers than arg, then return busy.
1092 */
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001093 if (nr_pages < pipe->nrbufs) {
1094 ret = -EBUSY;
1095 goto out_revert_acct;
1096 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001097
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001098 bufs = kcalloc(nr_pages, sizeof(*bufs),
1099 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001100 if (unlikely(!bufs)) {
1101 ret = -ENOMEM;
1102 goto out_revert_acct;
1103 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001104
1105 /*
1106 * The pipe array wraps around, so just start the new one at zero
1107 * and adjust the indexes.
1108 */
1109 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001110 unsigned int tail;
1111 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001112
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001113 tail = pipe->curbuf + pipe->nrbufs;
1114 if (tail < pipe->buffers)
1115 tail = 0;
1116 else
1117 tail &= (pipe->buffers - 1);
1118
1119 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001120 if (head)
1121 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1122 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001123 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001124 }
1125
1126 pipe->curbuf = 0;
1127 kfree(pipe->bufs);
1128 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001129 pipe->buffers = nr_pages;
1130 return nr_pages * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001131
1132out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001133 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001134 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001135}
1136
Jens Axboeff9da692010-06-03 14:54:39 +02001137/*
Linus Torvalds72083642010-11-28 16:27:19 -08001138 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1139 * location, so checking ->i_pipe is not enough to verify that this is a
1140 * pipe.
1141 */
1142struct pipe_inode_info *get_pipe_info(struct file *file)
1143{
Al Virode32ec42013-03-21 11:16:56 -04001144 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001145}
1146
Jens Axboe35f3d142010-05-20 10:43:18 +02001147long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1148{
1149 struct pipe_inode_info *pipe;
1150 long ret;
1151
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001152 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001153 if (!pipe)
1154 return -EBADF;
1155
Al Viroebec73f2013-03-21 12:24:01 -04001156 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001157
1158 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001159 case F_SETPIPE_SZ:
1160 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001161 break;
1162 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001163 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001164 break;
1165 default:
1166 ret = -EINVAL;
1167 break;
1168 }
1169
Al Viroebec73f2013-03-21 12:24:01 -04001170 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001171 return ret;
1172}
1173
Nick Pigginff0c7d12011-01-07 17:49:50 +11001174static const struct super_operations pipefs_ops = {
1175 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001176 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001177};
1178
Jens Axboe35f3d142010-05-20 10:43:18 +02001179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * pipefs should _never_ be mounted by userland - too much of security hassle,
1181 * no real gain from having the whole whorehouse mounted. So we don't need
1182 * any operations on the root directory. However, we need a non-trivial
1183 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1184 */
Al Viro51139ad2010-07-25 23:47:46 +04001185static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1186 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Al Viroc74a1cb2011-01-12 16:59:34 -05001188 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1189 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static struct file_system_type pipe_fs_type = {
1193 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001194 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 .kill_sb = kill_anon_super,
1196};
1197
1198static int __init init_pipe_fs(void)
1199{
1200 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!err) {
1203 pipe_mnt = kern_mount(&pipe_fs_type);
1204 if (IS_ERR(pipe_mnt)) {
1205 err = PTR_ERR(pipe_mnt);
1206 unregister_filesystem(&pipe_fs_type);
1207 }
1208 }
1209 return err;
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212fs_initcall(init_pipe_fs);