blob: 5916c19dbb0247d872101c21252596a406b169d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/pipe.c
3 *
4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
5 */
6
7#include <linux/mm.h>
8#include <linux/file.h>
9#include <linux/poll.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/fs.h>
Jens Axboe35f3d142010-05-20 10:43:18 +020014#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mount.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070016#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/pipe_fs_i.h>
18#include <linux/uio.h>
19#include <linux/highmem.h>
Jens Axboe5274f052006-03-30 15:15:30 +020020#include <linux/pagemap.h>
Al Virodb349502007-02-07 01:48:00 -050021#include <linux/audit.h>
Ulrich Drepperba719ba2008-05-06 20:42:38 -070022#include <linux/syscalls.h>
Jens Axboeb492e952010-05-19 21:03:16 +020023#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26#include <asm/ioctls.h>
27
Al Viro599a0ac2013-03-12 09:58:10 -040028#include "internal.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
Jens Axboeb492e952010-05-19 21:03:16 +020031 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020032 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020033 */
Jens Axboeff9da692010-06-03 14:54:39 +020034unsigned int pipe_max_size = 1048576;
35
36/*
37 * Minimum pipe size, as required by POSIX
38 */
39unsigned int pipe_min_size = PAGE_SIZE;
Jens Axboeb492e952010-05-19 21:03:16 +020040
Willy Tarreau2612a942016-01-18 16:36:09 +010041/* Maximum allocatable pages per user. Hard limit is unset by default, soft
42 * matches default values.
43 */
44unsigned long pipe_user_pages_hard;
45unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
46
Jens Axboeb492e952010-05-19 21:03:16 +020047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * We use a start+len construction, which provides full use of the
49 * allocated memory.
50 * -- Florian Coosmann (FGC)
51 *
52 * Reads with count = 0 should always return 0.
53 * -- Julian Bradfield 1999-06-07.
54 *
55 * FIFOs and Pipes now generate SIGIO for both readers and writers.
56 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
57 *
58 * pipe_read & write cleanup
59 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
60 */
61
Miklos Szeredi61e0d472009-04-14 19:48:41 +020062static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
63{
Al Viro6447a3c2013-03-21 11:01:38 -040064 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040065 mutex_lock_nested(&pipe->mutex, subclass);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020066}
67
68void pipe_lock(struct pipe_inode_info *pipe)
69{
70 /*
71 * pipe_lock() nests non-pipe inode locks (for writing to a file)
72 */
73 pipe_lock_nested(pipe, I_MUTEX_PARENT);
74}
75EXPORT_SYMBOL(pipe_lock);
76
77void pipe_unlock(struct pipe_inode_info *pipe)
78{
Al Viro6447a3c2013-03-21 11:01:38 -040079 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040080 mutex_unlock(&pipe->mutex);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020081}
82EXPORT_SYMBOL(pipe_unlock);
83
Al Viroebec73f2013-03-21 12:24:01 -040084static inline void __pipe_lock(struct pipe_inode_info *pipe)
85{
86 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
87}
88
89static inline void __pipe_unlock(struct pipe_inode_info *pipe)
90{
91 mutex_unlock(&pipe->mutex);
92}
93
Miklos Szeredi61e0d472009-04-14 19:48:41 +020094void pipe_double_lock(struct pipe_inode_info *pipe1,
95 struct pipe_inode_info *pipe2)
96{
97 BUG_ON(pipe1 == pipe2);
98
99 if (pipe1 < pipe2) {
100 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
101 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
102 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +0200103 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
104 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200105 }
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200109void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 DEFINE_WAIT(wait);
112
Ingo Molnard79fc0f2005-09-10 00:26:12 -0700113 /*
114 * Pipes are system-local resources, so sleeping on them
115 * is considered a noninteractive wait:
116 */
Mike Galbraithaf927232007-10-15 17:00:13 +0200117 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200118 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200120 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200121 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Ingo Molnar341b4462006-04-11 13:57:45 +0200124static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
125 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct page *page = buf->page;
128
Jens Axboe5274f052006-03-30 15:15:30 +0200129 /*
130 * If nobody else uses this page, and we don't already have a
131 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200132 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200133 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200134 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200135 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200136 else
137 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Jens Axboe08457182007-06-12 20:51:32 +0200140/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800141 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200142 * @pipe: the pipe that the buffer belongs to
143 * @buf: the buffer to attempt to steal
144 *
145 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800146 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200147 * @buf. If successful, this function returns 0 and returns with
148 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800149 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200150 * page cache.
151 */
Jens Axboe330ab712006-05-02 15:29:57 +0200152int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
153 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200154{
Jens Axboe46e678c2006-04-30 16:36:32 +0200155 struct page *page = buf->page;
156
Jens Axboe08457182007-06-12 20:51:32 +0200157 /*
158 * A reference of one is golden, that means that the owner of this
159 * page is the only one holding a reference to it. lock the page
160 * and return OK.
161 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200162 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200163 lock_page(page);
164 return 0;
165 }
166
167 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200168}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200169EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200170
Jens Axboe08457182007-06-12 20:51:32 +0200171/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800172 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200173 * @pipe: the pipe that the buffer belongs to
174 * @buf: the buffer to get a reference to
175 *
176 * Description:
177 * This function grabs an extra reference to @buf. It's used in
178 * in the tee() system call, when we duplicate the buffers in one
179 * pipe into another.
180 */
181void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200182{
183 page_cache_get(buf->page);
184}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200185EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200186
Jens Axboe08457182007-06-12 20:51:32 +0200187/**
188 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200189 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200190 * @buf: the buffer to confirm
191 *
192 * Description:
193 * This function does nothing, because the generic pipe code uses
194 * pages that are always good when inserted into the pipe.
195 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200196int generic_pipe_buf_confirm(struct pipe_inode_info *info,
197 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200198{
199 return 0;
200}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200201EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200202
Miklos Szeredi68181732009-05-07 15:37:36 +0200203/**
204 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
205 * @pipe: the pipe that the buffer belongs to
206 * @buf: the buffer to put a reference to
207 *
208 * Description:
209 * This function releases a reference to @buf.
210 */
211void generic_pipe_buf_release(struct pipe_inode_info *pipe,
212 struct pipe_buffer *buf)
213{
214 page_cache_release(buf->page);
215}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200216EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200217
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800218static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .can_merge = 1,
Jens Axboecac36bb02007-06-14 13:10:48 +0200220 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200222 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200223 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Linus Torvalds98830352012-04-29 13:12:42 -0700226static const struct pipe_buf_operations packet_pipe_buf_ops = {
227 .can_merge = 0,
Linus Torvalds98830352012-04-29 13:12:42 -0700228 .confirm = generic_pipe_buf_confirm,
229 .release = anon_pipe_buf_release,
230 .steal = generic_pipe_buf_steal,
231 .get = generic_pipe_buf_get,
232};
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static ssize_t
Al Virofb9096a2014-04-02 19:56:54 -0400235pipe_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Al Virofb9096a2014-04-02 19:56:54 -0400237 size_t total_len = iov_iter_count(to);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700238 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400239 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int do_wakeup;
241 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* Null read succeeds. */
244 if (unlikely(total_len == 0))
245 return 0;
246
247 do_wakeup = 0;
248 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400249 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200251 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200253 int curbuf = pipe->curbuf;
254 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800255 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500257 size_t written;
258 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (chars > total_len)
261 chars = total_len;
262
Jens Axboecac36bb02007-06-14 13:10:48 +0200263 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200264 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200265 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200266 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200267 break;
268 }
Jens Axboef84d7512006-05-01 19:59:03 +0200269
Al Virofb9096a2014-04-02 19:56:54 -0400270 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
Al Viro637b58c2014-02-03 19:11:42 -0500271 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200272 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500273 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 }
276 ret += chars;
277 buf->offset += chars;
278 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700279
280 /* Was it a packet buffer? Clean up and exit */
281 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
282 total_len = chars;
283 buf->len = 0;
284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!buf->len) {
287 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200288 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200289 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200290 pipe->curbuf = curbuf;
291 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 do_wakeup = 1;
293 }
294 total_len -= chars;
295 if (!total_len)
296 break; /* common path: read succeeded */
297 }
298 if (bufs) /* More to do? */
299 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200300 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200302 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* syscall merging: Usually we must not sleep
304 * if O_NONBLOCK is set, or if we got some data.
305 * But if a writer sleeps in kernel space, then
306 * we can wait for that data without violating POSIX.
307 */
308 if (ret)
309 break;
310 if (filp->f_flags & O_NONBLOCK) {
311 ret = -EAGAIN;
312 break;
313 }
314 }
315 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200316 if (!ret)
317 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 }
320 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800321 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200322 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200324 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Al Viroebec73f2013-03-21 12:24:01 -0400326 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200327
328 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800330 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200331 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 if (ret > 0)
334 file_accessed(filp);
335 return ret;
336}
337
Linus Torvalds98830352012-04-29 13:12:42 -0700338static inline int is_packetized(struct file *file)
339{
340 return (file->f_flags & O_DIRECT) != 0;
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static ssize_t
Al Virof0d1bec2014-04-03 15:05:18 -0400344pipe_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700346 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400347 struct pipe_inode_info *pipe = filp->private_data;
Al Virof0d1bec2014-04-03 15:05:18 -0400348 ssize_t ret = 0;
349 int do_wakeup = 0;
350 size_t total_len = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ssize_t chars;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Null write succeeds. */
354 if (unlikely(total_len == 0))
355 return 0;
356
Al Viroebec73f2013-03-21 12:24:01 -0400357 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Ingo Molnar923f4f22006-04-11 13:53:33 +0200359 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 send_sig(SIGPIPE, current, 0);
361 ret = -EPIPE;
362 goto out;
363 }
364
365 /* We try to merge small writes */
366 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200367 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200368 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200369 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200370 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800371 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Al Virof0d1bec2014-04-03 15:05:18 -0400375 int error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200376 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200377 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200378
Al Virof0d1bec2014-04-03 15:05:18 -0400379 ret = copy_page_from_iter(buf->page, offset, chars, from);
380 if (unlikely(ret < chars)) {
381 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200383 }
Al Virof0d1bec2014-04-03 15:05:18 -0400384 do_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 buf->len += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 ret = chars;
Al Virof0d1bec2014-04-03 15:05:18 -0400387 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 goto out;
389 }
390 }
391
392 for (;;) {
393 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200394
Ingo Molnar923f4f22006-04-11 13:53:33 +0200395 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200397 if (!ret)
398 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
400 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200401 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200402 if (bufs < pipe->buffers) {
403 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200404 struct pipe_buffer *buf = pipe->bufs + newbuf;
405 struct page *page = pipe->tmp_page;
Al Virof0d1bec2014-04-03 15:05:18 -0400406 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (!page) {
409 page = alloc_page(GFP_HIGHUSER);
410 if (unlikely(!page)) {
411 ret = ret ? : -ENOMEM;
412 break;
413 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200414 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200416 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * we lock up (O_NONBLOCK-)readers that sleep due to
418 * syscall merging.
419 * FIXME! Is this really true?
420 */
421 do_wakeup = 1;
Al Virof0d1bec2014-04-03 15:05:18 -0400422 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
423 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200424 if (!ret)
Al Virof0d1bec2014-04-03 15:05:18 -0400425 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427 }
Al Virof0d1bec2014-04-03 15:05:18 -0400428 ret += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* Insert it into the buffer array */
431 buf->page = page;
432 buf->ops = &anon_pipe_buf_ops;
433 buf->offset = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400434 buf->len = copied;
Linus Torvalds98830352012-04-29 13:12:42 -0700435 buf->flags = 0;
436 if (is_packetized(filp)) {
437 buf->ops = &packet_pipe_buf_ops;
438 buf->flags = PIPE_BUF_FLAG_PACKET;
439 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200440 pipe->nrbufs = ++bufs;
441 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Al Virof0d1bec2014-04-03 15:05:18 -0400443 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
445 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200446 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 continue;
448 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200449 if (!ret)
450 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 }
453 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200454 if (!ret)
455 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 }
458 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800459 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200460 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 do_wakeup = 0;
462 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200463 pipe->waiting_writers++;
464 pipe_wait(pipe);
465 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467out:
Al Viroebec73f2013-03-21 12:24:01 -0400468 __pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800470 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200471 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800473 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400474 int err = file_update_time(filp);
475 if (err)
476 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800477 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return ret;
480}
481
Andi Kleend59d0b12008-02-08 04:21:23 -0800482static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Al Virode32ec42013-03-21 11:16:56 -0400484 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int count, buf, nrbufs;
486
487 switch (cmd) {
488 case FIONREAD:
Al Viroebec73f2013-03-21 12:24:01 -0400489 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200491 buf = pipe->curbuf;
492 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200494 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200495 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Al Viroebec73f2013-03-21 12:24:01 -0400497 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return put_user(count, (int __user *)arg);
500 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100501 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503}
504
505/* No kernel lock held - fine */
506static unsigned int
507pipe_poll(struct file *filp, poll_table *wait)
508{
509 unsigned int mask;
Al Virode32ec42013-03-21 11:16:56 -0400510 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int nrbufs;
512
Ingo Molnar923f4f22006-04-11 13:53:33 +0200513 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200516 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 mask = 0;
518 if (filp->f_mode & FMODE_READ) {
519 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200520 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 mask |= POLLHUP;
522 }
523
524 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200525 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700526 /*
527 * Most Unices do not set POLLERR for FIFOs but on Linux they
528 * behave exactly like pipes for poll().
529 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200530 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 mask |= POLLERR;
532 }
533
534 return mask;
535}
536
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800537static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
538{
539 int kill = 0;
540
541 spin_lock(&inode->i_lock);
542 if (!--pipe->files) {
543 inode->i_pipe = NULL;
544 kill = 1;
545 }
546 spin_unlock(&inode->i_lock);
547
548 if (kill)
549 free_pipe_info(pipe);
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static int
Al Viro599a0ac2013-03-12 09:58:10 -0400553pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800555 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200556
Al Viroebec73f2013-03-21 12:24:01 -0400557 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400558 if (file->f_mode & FMODE_READ)
559 pipe->readers--;
560 if (file->f_mode & FMODE_WRITE)
561 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200562
Al Viroba5bb142013-03-21 02:21:19 -0400563 if (pipe->readers || pipe->writers) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800564 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200565 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
566 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Al Viroebec73f2013-03-21 12:24:01 -0400568 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400569
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800570 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 0;
572}
573
574static int
Al Viro599a0ac2013-03-12 09:58:10 -0400575pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Al Virode32ec42013-03-21 11:16:56 -0400577 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400578 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Al Viroebec73f2013-03-21 12:24:01 -0400580 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400581 if (filp->f_mode & FMODE_READ)
582 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
583 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200584 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400585 if (retval < 0 && (filp->f_mode & FMODE_READ))
586 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700587 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
588 }
Al Viroebec73f2013-03-21 12:24:01 -0400589 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700590 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Willy Tarreau2612a942016-01-18 16:36:09 +0100593static void account_pipe_buffers(struct pipe_inode_info *pipe,
594 unsigned long old, unsigned long new)
595{
596 atomic_long_add(new - old, &pipe->user->pipe_bufs);
597}
598
599static bool too_many_pipe_buffers_soft(struct user_struct *user)
600{
601 return pipe_user_pages_soft &&
602 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
603}
604
605static bool too_many_pipe_buffers_hard(struct user_struct *user)
606{
607 return pipe_user_pages_hard &&
608 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
609}
610
Al Viro7bee1302013-03-21 11:04:15 -0400611struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200612{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200613 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200614
Ingo Molnar923f4f22006-04-11 13:53:33 +0200615 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
616 if (pipe) {
Willy Tarreau2612a942016-01-18 16:36:09 +0100617 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
618 struct user_struct *user = get_current_user();
619
620 if (!too_many_pipe_buffers_hard(user)) {
621 if (too_many_pipe_buffers_soft(user))
622 pipe_bufs = 1;
623 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
624 }
625
Jens Axboe35f3d142010-05-20 10:43:18 +0200626 if (pipe->bufs) {
627 init_waitqueue_head(&pipe->wait);
628 pipe->r_counter = pipe->w_counter = 1;
Willy Tarreau2612a942016-01-18 16:36:09 +0100629 pipe->buffers = pipe_bufs;
630 pipe->user = user;
631 account_pipe_buffers(pipe, 0, pipe_bufs);
Al Viro72b0d9a2013-03-21 02:32:24 -0400632 mutex_init(&pipe->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +0200633 return pipe;
634 }
Willy Tarreau2612a942016-01-18 16:36:09 +0100635 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200636 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200637 }
638
Jens Axboe35f3d142010-05-20 10:43:18 +0200639 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200640}
641
Al Viro4b8a8f12013-03-21 11:06:46 -0400642void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Willy Tarreau2612a942016-01-18 16:36:09 +0100646 account_pipe_buffers(pipe, pipe->buffers, 0);
647 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200648 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200649 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200651 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200653 if (pipe->tmp_page)
654 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200655 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200656 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800659static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200660
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700661/*
662 * pipefs_dname() is called from d_path().
663 */
664static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
665{
666 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
David Howells75c3cfa2015-03-17 22:26:12 +0000667 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700668}
669
Al Viro3ba13d12009-02-20 06:02:22 +0000670static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700671 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
673
674static struct inode * get_pipe_inode(void)
675{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200676 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200677 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!inode)
680 goto fail_inode;
681
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400682 inode->i_ino = get_next_ino();
683
Al Viro7bee1302013-03-21 11:04:15 -0400684 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200685 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200687
Al Viroba5bb142013-03-21 02:21:19 -0400688 inode->i_pipe = pipe;
689 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200690 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400691 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /*
694 * Mark the inode dirty from the very beginning,
695 * that way it will never be moved to the dirty
696 * list because "mark_inode_dirty()" will think
697 * that it already _is_ on the dirty list.
698 */
699 inode->i_state = I_DIRTY;
700 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100701 inode->i_uid = current_fsuid();
702 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return inode;
706
707fail_iput:
708 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710fail_inode:
711 return NULL;
712}
713
Al Viroe4fad8e2012-07-21 15:33:25 +0400714int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Andi Kleend6cbd282006-09-30 23:29:26 -0700716 int err;
Al Viroe4fad8e2012-07-21 15:33:25 +0400717 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700718 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +0400719 struct path path;
Al Viroe4fad8e2012-07-21 15:33:25 +0400720 static struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400723 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Andi Kleend6cbd282006-09-30 23:29:26 -0700725 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +1100726 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +0400727 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700728 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +0400729 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +0200730
Al Viro2c48b9c2009-08-09 00:52:35 +0400731 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800732
733 err = -ENFILE;
Al Viro599a0ac2013-03-12 09:58:10 -0400734 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700735 if (IS_ERR(f))
Dave Hansen430e2852008-02-15 14:37:26 -0800736 goto err_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds98830352012-04-29 13:12:42 -0700738 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Al Virode32ec42013-03-21 11:16:56 -0400739 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Al Viro599a0ac2013-03-12 09:58:10 -0400741 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700742 if (IS_ERR(res[0]))
Al Viroe4fad8e2012-07-21 15:33:25 +0400743 goto err_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Al Viroe4fad8e2012-07-21 15:33:25 +0400745 path_get(&path);
Al Virode32ec42013-03-21 11:16:56 -0400746 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400747 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
748 res[1] = f;
749 return 0;
750
751err_file:
752 put_filp(f);
753err_dentry:
Al Viro4b8a8f12013-03-21 11:06:46 -0400754 free_pipe_info(inode->i_pipe);
Al Viro2c48b9c2009-08-09 00:52:35 +0400755 path_put(&path);
Al Viroe4fad8e2012-07-21 15:33:25 +0400756 return err;
Al Viroed152432008-04-22 19:51:27 -0400757
Al Viroe4fad8e2012-07-21 15:33:25 +0400758err_inode:
Al Viro4b8a8f12013-03-21 11:06:46 -0400759 free_pipe_info(inode->i_pipe);
Andi Kleend6cbd282006-09-30 23:29:26 -0700760 iput(inode);
Al Viroe4fad8e2012-07-21 15:33:25 +0400761 return err;
Andi Kleend6cbd282006-09-30 23:29:26 -0700762}
763
Al Viro5b249b12012-08-19 12:17:29 -0400764static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700765{
Andi Kleend6cbd282006-09-30 23:29:26 -0700766 int error;
767 int fdw, fdr;
768
Linus Torvalds98830352012-04-29 13:12:42 -0700769 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700770 return -EINVAL;
771
Al Viroe4fad8e2012-07-21 15:33:25 +0400772 error = create_pipe_files(files, flags);
773 if (error)
774 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700775
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700776 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700777 if (error < 0)
778 goto err_read_pipe;
779 fdr = error;
780
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700781 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700782 if (error < 0)
783 goto err_fdr;
784 fdw = error;
785
Al Viro157cf642008-12-14 04:57:47 -0500786 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700787 fd[0] = fdr;
788 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 0;
790
Andi Kleend6cbd282006-09-30 23:29:26 -0700791 err_fdr:
792 put_unused_fd(fdr);
793 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400794 fput(files[0]);
795 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700796 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Al Viro5b249b12012-08-19 12:17:29 -0400799int do_pipe_flags(int *fd, int flags)
800{
801 struct file *files[2];
802 int error = __do_pipe_flags(fd, files, flags);
803 if (!error) {
804 fd_install(fd[0], files[0]);
805 fd_install(fd[1], files[1]);
806 }
807 return error;
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400811 * sys_pipe() is the normal C calling standard for creating
812 * a pipe. It's not the way Unix traditionally does this, though.
813 */
Heiko Carstensd4e82042009-01-14 14:14:34 +0100814SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400815{
Al Viro5b249b12012-08-19 12:17:29 -0400816 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400817 int fd[2];
818 int error;
819
Al Viro5b249b12012-08-19 12:17:29 -0400820 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400821 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400822 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
823 fput(files[0]);
824 fput(files[1]);
825 put_unused_fd(fd[0]);
826 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400827 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400828 } else {
829 fd_install(fd[0], files[0]);
830 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700831 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400832 }
833 return error;
834}
835
Heiko Carstens2b664212009-01-14 14:14:35 +0100836SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700837{
838 return sys_pipe2(fildes, 0);
839}
840
Al Virofc7478a2013-03-21 02:07:59 -0400841static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400842{
843 int cur = *cnt;
844
845 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400846 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400847 if (signal_pending(current))
848 break;
849 }
850 return cur == *cnt ? -ERESTARTSYS : 0;
851}
852
Al Virofc7478a2013-03-21 02:07:59 -0400853static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400854{
Al Virofc7478a2013-03-21 02:07:59 -0400855 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400856}
857
858static int fifo_open(struct inode *inode, struct file *filp)
859{
860 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400861 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400862 int ret;
863
Al Viroba5bb142013-03-21 02:21:19 -0400864 filp->f_version = 0;
865
866 spin_lock(&inode->i_lock);
867 if (inode->i_pipe) {
868 pipe = inode->i_pipe;
869 pipe->files++;
870 spin_unlock(&inode->i_lock);
871 } else {
872 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400873 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400874 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400875 return -ENOMEM;
876 pipe->files = 1;
877 spin_lock(&inode->i_lock);
878 if (unlikely(inode->i_pipe)) {
879 inode->i_pipe->files++;
880 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400881 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400882 pipe = inode->i_pipe;
883 } else {
884 inode->i_pipe = pipe;
885 spin_unlock(&inode->i_lock);
886 }
Al Virof776c732013-03-12 09:46:27 -0400887 }
Al Virode32ec42013-03-21 11:16:56 -0400888 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400889 /* OK, we have a pipe and it's pinned down */
890
Al Viroebec73f2013-03-21 12:24:01 -0400891 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400892
893 /* We can only do regular read/write on fifos */
894 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
895
896 switch (filp->f_mode) {
897 case FMODE_READ:
898 /*
899 * O_RDONLY
900 * POSIX.1 says that O_NONBLOCK means return with the FIFO
901 * opened, even when there is no process writing the FIFO.
902 */
Al Virof776c732013-03-12 09:46:27 -0400903 pipe->r_counter++;
904 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400905 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400906
Al Viro599a0ac2013-03-12 09:58:10 -0400907 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400908 if ((filp->f_flags & O_NONBLOCK)) {
909 /* suppress POLLHUP until we have
910 * seen a writer */
911 filp->f_version = pipe->w_counter;
912 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400913 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400914 goto err_rd;
915 }
916 }
917 break;
918
919 case FMODE_WRITE:
920 /*
921 * O_WRONLY
922 * POSIX.1 says that O_NONBLOCK means return -1 with
923 * errno=ENXIO when there is no process reading the FIFO.
924 */
925 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400926 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400927 goto err;
928
Al Virof776c732013-03-12 09:46:27 -0400929 pipe->w_counter++;
930 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400931 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400932
Al Viro599a0ac2013-03-12 09:58:10 -0400933 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400934 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400935 goto err_wr;
936 }
937 break;
938
939 case FMODE_READ | FMODE_WRITE:
940 /*
941 * O_RDWR
942 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
943 * This implementation will NEVER block on a O_RDWR open, since
944 * the process can at least talk to itself.
945 */
Al Virof776c732013-03-12 09:46:27 -0400946
947 pipe->readers++;
948 pipe->writers++;
949 pipe->r_counter++;
950 pipe->w_counter++;
951 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400952 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400953 break;
954
955 default:
956 ret = -EINVAL;
957 goto err;
958 }
959
960 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -0400961 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400962 return 0;
963
964err_rd:
965 if (!--pipe->readers)
966 wake_up_interruptible(&pipe->wait);
967 ret = -ERESTARTSYS;
968 goto err;
969
970err_wr:
971 if (!--pipe->writers)
972 wake_up_interruptible(&pipe->wait);
973 ret = -ERESTARTSYS;
974 goto err;
975
976err:
Al Viroebec73f2013-03-21 12:24:01 -0400977 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800978
979 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -0400980 return ret;
981}
982
Al Viro599a0ac2013-03-12 09:58:10 -0400983const struct file_operations pipefifo_fops = {
984 .open = fifo_open,
985 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -0400986 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -0400987 .write_iter = pipe_write,
Al Viro599a0ac2013-03-12 09:58:10 -0400988 .poll = pipe_poll,
989 .unlocked_ioctl = pipe_ioctl,
990 .release = pipe_release,
991 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -0400992};
993
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400994/*
Jens Axboe35f3d142010-05-20 10:43:18 +0200995 * Allocate a new array of pipe buffers and copy the info over. Returns the
996 * pipe size if successful, or return -ERROR on error.
997 */
Jens Axboeb9598db2010-05-24 19:34:43 +0200998static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +0200999{
1000 struct pipe_buffer *bufs;
1001
1002 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001003 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1004 * expect a lot of shrink+grow operations, just free and allocate
1005 * again like we would do for growing. If the pipe currently
1006 * contains more buffers than arg, then return busy.
1007 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001008 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001009 return -EBUSY;
1010
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001011 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001012 if (unlikely(!bufs))
1013 return -ENOMEM;
1014
1015 /*
1016 * The pipe array wraps around, so just start the new one at zero
1017 * and adjust the indexes.
1018 */
1019 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001020 unsigned int tail;
1021 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001022
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001023 tail = pipe->curbuf + pipe->nrbufs;
1024 if (tail < pipe->buffers)
1025 tail = 0;
1026 else
1027 tail &= (pipe->buffers - 1);
1028
1029 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001030 if (head)
1031 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1032 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001033 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001034 }
1035
Willy Tarreau2612a942016-01-18 16:36:09 +01001036 account_pipe_buffers(pipe, pipe->buffers, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001037 pipe->curbuf = 0;
1038 kfree(pipe->bufs);
1039 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001040 pipe->buffers = nr_pages;
1041 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001042}
1043
Jens Axboeff9da692010-06-03 14:54:39 +02001044/*
1045 * Currently we rely on the pipe array holding a power-of-2 number
1046 * of pages.
1047 */
1048static inline unsigned int round_pipe_size(unsigned int size)
1049{
1050 unsigned long nr_pages;
1051
1052 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1053 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1054}
1055
1056/*
1057 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1058 * will return an error.
1059 */
1060int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1061 size_t *lenp, loff_t *ppos)
1062{
1063 int ret;
1064
1065 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1066 if (ret < 0 || !write)
1067 return ret;
1068
1069 pipe_max_size = round_pipe_size(pipe_max_size);
1070 return ret;
1071}
1072
Linus Torvalds72083642010-11-28 16:27:19 -08001073/*
1074 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1075 * location, so checking ->i_pipe is not enough to verify that this is a
1076 * pipe.
1077 */
1078struct pipe_inode_info *get_pipe_info(struct file *file)
1079{
Al Virode32ec42013-03-21 11:16:56 -04001080 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001081}
1082
Jens Axboe35f3d142010-05-20 10:43:18 +02001083long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1084{
1085 struct pipe_inode_info *pipe;
1086 long ret;
1087
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001088 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001089 if (!pipe)
1090 return -EBADF;
1091
Al Viroebec73f2013-03-21 12:24:01 -04001092 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001093
1094 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001095 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001096 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001097
Jens Axboeff9da692010-06-03 14:54:39 +02001098 size = round_pipe_size(arg);
1099 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001100
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001101 ret = -EINVAL;
1102 if (!nr_pages)
1103 goto out;
1104
Jens Axboeff9da692010-06-03 14:54:39 +02001105 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001106 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001107 goto out;
Willy Tarreau2612a942016-01-18 16:36:09 +01001108 } else if ((too_many_pipe_buffers_hard(pipe->user) ||
1109 too_many_pipe_buffers_soft(pipe->user)) &&
1110 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
1111 ret = -EPERM;
1112 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001113 }
Jens Axboeff9da692010-06-03 14:54:39 +02001114 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001115 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001116 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001117 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001118 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001119 break;
1120 default:
1121 ret = -EINVAL;
1122 break;
1123 }
1124
Julia Lawallcc967be2010-05-26 17:54:39 +02001125out:
Al Viroebec73f2013-03-21 12:24:01 -04001126 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001127 return ret;
1128}
1129
Nick Pigginff0c7d12011-01-07 17:49:50 +11001130static const struct super_operations pipefs_ops = {
1131 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001132 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001133};
1134
Jens Axboe35f3d142010-05-20 10:43:18 +02001135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * pipefs should _never_ be mounted by userland - too much of security hassle,
1137 * no real gain from having the whole whorehouse mounted. So we don't need
1138 * any operations on the root directory. However, we need a non-trivial
1139 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1140 */
Al Viro51139ad2010-07-25 23:47:46 +04001141static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1142 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Al Viroc74a1cb2011-01-12 16:59:34 -05001144 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1145 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static struct file_system_type pipe_fs_type = {
1149 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001150 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 .kill_sb = kill_anon_super,
1152};
1153
1154static int __init init_pipe_fs(void)
1155{
1156 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (!err) {
1159 pipe_mnt = kern_mount(&pipe_fs_type);
1160 if (IS_ERR(pipe_mnt)) {
1161 err = PTR_ERR(pipe_mnt);
1162 unregister_filesystem(&pipe_fs_type);
1163 }
1164 }
1165 return err;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168fs_initcall(init_pipe_fs);