blob: e3ba6c3a1743024d365b31c0deb7af5c66ec088a [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070024#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/uaccess.h>
27#include <asm/ioctls.h>
28
Al Viro599a0ac2013-03-12 09:58:10 -040029#include "internal.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
Jens Axboeb492e952010-05-19 21:03:16 +020032 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020033 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020034 */
Jens Axboeff9da692010-06-03 14:54:39 +020035unsigned int pipe_max_size = 1048576;
36
37/*
38 * Minimum pipe size, as required by POSIX
39 */
40unsigned int pipe_min_size = PAGE_SIZE;
Jens Axboeb492e952010-05-19 21:03:16 +020041
Willy Tarreaube65d292016-01-18 16:36:09 +010042/* Maximum allocatable pages per user. Hard limit is unset by default, soft
43 * matches default values.
44 */
45unsigned long pipe_user_pages_hard;
46unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
47
Jens Axboeb492e952010-05-19 21:03:16 +020048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * We use a start+len construction, which provides full use of the
50 * allocated memory.
51 * -- Florian Coosmann (FGC)
52 *
53 * Reads with count = 0 should always return 0.
54 * -- Julian Bradfield 1999-06-07.
55 *
56 * FIFOs and Pipes now generate SIGIO for both readers and writers.
57 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
58 *
59 * pipe_read & write cleanup
60 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
61 */
62
Miklos Szeredi61e0d472009-04-14 19:48:41 +020063static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
64{
Al Viro6447a3c2013-03-21 11:01:38 -040065 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040066 mutex_lock_nested(&pipe->mutex, subclass);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020067}
68
69void pipe_lock(struct pipe_inode_info *pipe)
70{
71 /*
72 * pipe_lock() nests non-pipe inode locks (for writing to a file)
73 */
74 pipe_lock_nested(pipe, I_MUTEX_PARENT);
75}
76EXPORT_SYMBOL(pipe_lock);
77
78void pipe_unlock(struct pipe_inode_info *pipe)
79{
Al Viro6447a3c2013-03-21 11:01:38 -040080 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040081 mutex_unlock(&pipe->mutex);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020082}
83EXPORT_SYMBOL(pipe_unlock);
84
Al Viroebec73f2013-03-21 12:24:01 -040085static inline void __pipe_lock(struct pipe_inode_info *pipe)
86{
87 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
88}
89
90static inline void __pipe_unlock(struct pipe_inode_info *pipe)
91{
92 mutex_unlock(&pipe->mutex);
93}
94
Miklos Szeredi61e0d472009-04-14 19:48:41 +020095void pipe_double_lock(struct pipe_inode_info *pipe1,
96 struct pipe_inode_info *pipe2)
97{
98 BUG_ON(pipe1 == pipe2);
99
100 if (pipe1 < pipe2) {
101 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
102 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
103 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +0200104 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
105 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200106 }
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200110void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 DEFINE_WAIT(wait);
113
Ingo Molnard79fc0f2005-09-10 00:26:12 -0700114 /*
115 * Pipes are system-local resources, so sleeping on them
116 * is considered a noninteractive wait:
117 */
Mike Galbraithaf927232007-10-15 17:00:13 +0200118 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200119 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200121 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200122 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Ingo Molnar341b4462006-04-11 13:57:45 +0200125static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
126 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct page *page = buf->page;
129
Jens Axboe5274f052006-03-30 15:15:30 +0200130 /*
131 * If nobody else uses this page, and we don't already have a
132 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200133 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200134 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200135 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200136 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200137 else
138 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Jens Axboe08457182007-06-12 20:51:32 +0200141/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800142 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200143 * @pipe: the pipe that the buffer belongs to
144 * @buf: the buffer to attempt to steal
145 *
146 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800147 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200148 * @buf. If successful, this function returns 0 and returns with
149 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800150 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200151 * page cache.
152 */
Jens Axboe330ab712006-05-02 15:29:57 +0200153int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
154 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200155{
Jens Axboe46e678c2006-04-30 16:36:32 +0200156 struct page *page = buf->page;
157
Jens Axboe08457182007-06-12 20:51:32 +0200158 /*
159 * A reference of one is golden, that means that the owner of this
160 * page is the only one holding a reference to it. lock the page
161 * and return OK.
162 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200163 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200164 lock_page(page);
165 return 0;
166 }
167
168 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200169}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200170EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200171
Jens Axboe08457182007-06-12 20:51:32 +0200172/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800173 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200174 * @pipe: the pipe that the buffer belongs to
175 * @buf: the buffer to get a reference to
176 *
177 * Description:
178 * This function grabs an extra reference to @buf. It's used in
179 * in the tee() system call, when we duplicate the buffers in one
180 * pipe into another.
181 */
182void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200183{
184 page_cache_get(buf->page);
185}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200186EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200187
Jens Axboe08457182007-06-12 20:51:32 +0200188/**
189 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200190 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200191 * @buf: the buffer to confirm
192 *
193 * Description:
194 * This function does nothing, because the generic pipe code uses
195 * pages that are always good when inserted into the pipe.
196 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200197int generic_pipe_buf_confirm(struct pipe_inode_info *info,
198 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200199{
200 return 0;
201}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200202EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200203
Miklos Szeredi68181732009-05-07 15:37:36 +0200204/**
205 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
206 * @pipe: the pipe that the buffer belongs to
207 * @buf: the buffer to put a reference to
208 *
209 * Description:
210 * This function releases a reference to @buf.
211 */
212void generic_pipe_buf_release(struct pipe_inode_info *pipe,
213 struct pipe_buffer *buf)
214{
215 page_cache_release(buf->page);
216}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200217EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200218
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800219static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .can_merge = 1,
Jens Axboecac36bb02007-06-14 13:10:48 +0200221 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200223 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200224 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
Linus Torvalds98830352012-04-29 13:12:42 -0700227static const struct pipe_buf_operations packet_pipe_buf_ops = {
228 .can_merge = 0,
Linus Torvalds98830352012-04-29 13:12:42 -0700229 .confirm = generic_pipe_buf_confirm,
230 .release = anon_pipe_buf_release,
231 .steal = generic_pipe_buf_steal,
232 .get = generic_pipe_buf_get,
233};
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static ssize_t
Al Virofb9096a2014-04-02 19:56:54 -0400236pipe_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Al Virofb9096a2014-04-02 19:56:54 -0400238 size_t total_len = iov_iter_count(to);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700239 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400240 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 int do_wakeup;
242 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Null read succeeds. */
245 if (unlikely(total_len == 0))
246 return 0;
247
248 do_wakeup = 0;
249 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400250 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200252 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200254 int curbuf = pipe->curbuf;
255 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800256 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500258 size_t written;
259 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (chars > total_len)
262 chars = total_len;
263
Jens Axboecac36bb02007-06-14 13:10:48 +0200264 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200265 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200266 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200267 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200268 break;
269 }
Jens Axboef84d7512006-05-01 19:59:03 +0200270
Al Virofb9096a2014-04-02 19:56:54 -0400271 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
Al Viro637b58c2014-02-03 19:11:42 -0500272 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200273 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500274 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 }
277 ret += chars;
278 buf->offset += chars;
279 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700280
281 /* Was it a packet buffer? Clean up and exit */
282 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
283 total_len = chars;
284 buf->len = 0;
285 }
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!buf->len) {
288 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200289 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200290 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200291 pipe->curbuf = curbuf;
292 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 do_wakeup = 1;
294 }
295 total_len -= chars;
296 if (!total_len)
297 break; /* common path: read succeeded */
298 }
299 if (bufs) /* More to do? */
300 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200301 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200303 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* syscall merging: Usually we must not sleep
305 * if O_NONBLOCK is set, or if we got some data.
306 * But if a writer sleeps in kernel space, then
307 * we can wait for that data without violating POSIX.
308 */
309 if (ret)
310 break;
311 if (filp->f_flags & O_NONBLOCK) {
312 ret = -EAGAIN;
313 break;
314 }
315 }
316 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200317 if (!ret)
318 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320 }
321 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800322 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200323 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200325 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Al Viroebec73f2013-03-21 12:24:01 -0400327 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200328
329 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800331 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200332 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 if (ret > 0)
335 file_accessed(filp);
336 return ret;
337}
338
Linus Torvalds98830352012-04-29 13:12:42 -0700339static inline int is_packetized(struct file *file)
340{
341 return (file->f_flags & O_DIRECT) != 0;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static ssize_t
Al Virof0d1bec2014-04-03 15:05:18 -0400345pipe_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700347 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400348 struct pipe_inode_info *pipe = filp->private_data;
Al Virof0d1bec2014-04-03 15:05:18 -0400349 ssize_t ret = 0;
350 int do_wakeup = 0;
351 size_t total_len = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ssize_t chars;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* Null write succeeds. */
355 if (unlikely(total_len == 0))
356 return 0;
357
Al Viroebec73f2013-03-21 12:24:01 -0400358 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Ingo Molnar923f4f22006-04-11 13:53:33 +0200360 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 send_sig(SIGPIPE, current, 0);
362 ret = -EPIPE;
363 goto out;
364 }
365
366 /* We try to merge small writes */
367 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200368 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200369 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200370 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200371 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800372 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Al Virof0d1bec2014-04-03 15:05:18 -0400376 int error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200377 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200378 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200379
Al Virof0d1bec2014-04-03 15:05:18 -0400380 ret = copy_page_from_iter(buf->page, offset, chars, from);
381 if (unlikely(ret < chars)) {
382 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200384 }
Al Virof0d1bec2014-04-03 15:05:18 -0400385 do_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 buf->len += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ret = chars;
Al Virof0d1bec2014-04-03 15:05:18 -0400388 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 goto out;
390 }
391 }
392
393 for (;;) {
394 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200395
Ingo Molnar923f4f22006-04-11 13:53:33 +0200396 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200398 if (!ret)
399 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
401 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200402 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200403 if (bufs < pipe->buffers) {
404 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200405 struct pipe_buffer *buf = pipe->bufs + newbuf;
406 struct page *page = pipe->tmp_page;
Al Virof0d1bec2014-04-03 15:05:18 -0400407 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (!page) {
410 page = alloc_page(GFP_HIGHUSER);
411 if (unlikely(!page)) {
412 ret = ret ? : -ENOMEM;
413 break;
414 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200415 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200417 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * we lock up (O_NONBLOCK-)readers that sleep due to
419 * syscall merging.
420 * FIXME! Is this really true?
421 */
422 do_wakeup = 1;
Al Virof0d1bec2014-04-03 15:05:18 -0400423 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
424 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200425 if (!ret)
Al Virof0d1bec2014-04-03 15:05:18 -0400426 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428 }
Al Virof0d1bec2014-04-03 15:05:18 -0400429 ret += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Insert it into the buffer array */
432 buf->page = page;
433 buf->ops = &anon_pipe_buf_ops;
434 buf->offset = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400435 buf->len = copied;
Linus Torvalds98830352012-04-29 13:12:42 -0700436 buf->flags = 0;
437 if (is_packetized(filp)) {
438 buf->ops = &packet_pipe_buf_ops;
439 buf->flags = PIPE_BUF_FLAG_PACKET;
440 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200441 pipe->nrbufs = ++bufs;
442 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Al Virof0d1bec2014-04-03 15:05:18 -0400444 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200447 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 continue;
449 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200450 if (!ret)
451 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 }
454 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200455 if (!ret)
456 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 }
459 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800460 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200461 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 do_wakeup = 0;
463 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200464 pipe->waiting_writers++;
465 pipe_wait(pipe);
466 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468out:
Al Viroebec73f2013-03-21 12:24:01 -0400469 __pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800471 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200472 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800474 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400475 int err = file_update_time(filp);
476 if (err)
477 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800478 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return ret;
481}
482
Andi Kleend59d0b12008-02-08 04:21:23 -0800483static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Al Virode32ec42013-03-21 11:16:56 -0400485 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int count, buf, nrbufs;
487
488 switch (cmd) {
489 case FIONREAD:
Al Viroebec73f2013-03-21 12:24:01 -0400490 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200492 buf = pipe->curbuf;
493 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200495 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200496 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Al Viroebec73f2013-03-21 12:24:01 -0400498 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return put_user(count, (int __user *)arg);
501 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100502 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504}
505
506/* No kernel lock held - fine */
507static unsigned int
508pipe_poll(struct file *filp, poll_table *wait)
509{
510 unsigned int mask;
Al Virode32ec42013-03-21 11:16:56 -0400511 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int nrbufs;
513
Ingo Molnar923f4f22006-04-11 13:53:33 +0200514 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200517 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 mask = 0;
519 if (filp->f_mode & FMODE_READ) {
520 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200521 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 mask |= POLLHUP;
523 }
524
525 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200526 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700527 /*
528 * Most Unices do not set POLLERR for FIFOs but on Linux they
529 * behave exactly like pipes for poll().
530 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200531 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 mask |= POLLERR;
533 }
534
535 return mask;
536}
537
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800538static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
539{
540 int kill = 0;
541
542 spin_lock(&inode->i_lock);
543 if (!--pipe->files) {
544 inode->i_pipe = NULL;
545 kill = 1;
546 }
547 spin_unlock(&inode->i_lock);
548
549 if (kill)
550 free_pipe_info(pipe);
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int
Al Viro599a0ac2013-03-12 09:58:10 -0400554pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800556 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200557
Al Viroebec73f2013-03-21 12:24:01 -0400558 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400559 if (file->f_mode & FMODE_READ)
560 pipe->readers--;
561 if (file->f_mode & FMODE_WRITE)
562 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200563
Al Viroba5bb142013-03-21 02:21:19 -0400564 if (pipe->readers || pipe->writers) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800565 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200566 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
567 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Al Viroebec73f2013-03-21 12:24:01 -0400569 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400570
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800571 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return 0;
573}
574
575static int
Al Viro599a0ac2013-03-12 09:58:10 -0400576pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Al Virode32ec42013-03-21 11:16:56 -0400578 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400579 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Al Viroebec73f2013-03-21 12:24:01 -0400581 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400582 if (filp->f_mode & FMODE_READ)
583 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
584 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200585 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400586 if (retval < 0 && (filp->f_mode & FMODE_READ))
587 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700588 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
589 }
Al Viroebec73f2013-03-21 12:24:01 -0400590 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700591 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Willy Tarreaube65d292016-01-18 16:36:09 +0100594static void account_pipe_buffers(struct pipe_inode_info *pipe,
595 unsigned long old, unsigned long new)
596{
597 atomic_long_add(new - old, &pipe->user->pipe_bufs);
598}
599
600static bool too_many_pipe_buffers_soft(struct user_struct *user)
601{
602 return pipe_user_pages_soft &&
603 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
604}
605
606static bool too_many_pipe_buffers_hard(struct user_struct *user)
607{
608 return pipe_user_pages_hard &&
609 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
610}
611
Al Viro7bee1302013-03-21 11:04:15 -0400612struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200613{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200614 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200615
Ingo Molnar923f4f22006-04-11 13:53:33 +0200616 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
617 if (pipe) {
Willy Tarreaube65d292016-01-18 16:36:09 +0100618 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
619 struct user_struct *user = get_current_user();
620
621 if (!too_many_pipe_buffers_hard(user)) {
622 if (too_many_pipe_buffers_soft(user))
623 pipe_bufs = 1;
624 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
625 }
626
Jens Axboe35f3d142010-05-20 10:43:18 +0200627 if (pipe->bufs) {
628 init_waitqueue_head(&pipe->wait);
629 pipe->r_counter = pipe->w_counter = 1;
Willy Tarreaube65d292016-01-18 16:36:09 +0100630 pipe->buffers = pipe_bufs;
631 pipe->user = user;
632 account_pipe_buffers(pipe, 0, pipe_bufs);
Al Viro72b0d9a2013-03-21 02:32:24 -0400633 mutex_init(&pipe->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +0200634 return pipe;
635 }
Willy Tarreaube65d292016-01-18 16:36:09 +0100636 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200637 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200638 }
639
Jens Axboe35f3d142010-05-20 10:43:18 +0200640 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200641}
642
Al Viro4b8a8f12013-03-21 11:06:46 -0400643void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Willy Tarreaube65d292016-01-18 16:36:09 +0100647 account_pipe_buffers(pipe, pipe->buffers, 0);
648 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200649 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200650 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200652 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200654 if (pipe->tmp_page)
655 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200656 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200657 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800660static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200661
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700662/*
663 * pipefs_dname() is called from d_path().
664 */
665static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
666{
667 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
668 dentry->d_inode->i_ino);
669}
670
Al Viro3ba13d12009-02-20 06:02:22 +0000671static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700672 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673};
674
675static struct inode * get_pipe_inode(void)
676{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200677 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200678 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (!inode)
681 goto fail_inode;
682
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400683 inode->i_ino = get_next_ino();
684
Al Viro7bee1302013-03-21 11:04:15 -0400685 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200686 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200688
Al Viroba5bb142013-03-21 02:21:19 -0400689 inode->i_pipe = pipe;
690 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200691 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400692 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /*
695 * Mark the inode dirty from the very beginning,
696 * that way it will never be moved to the dirty
697 * list because "mark_inode_dirty()" will think
698 * that it already _is_ on the dirty list.
699 */
700 inode->i_state = I_DIRTY;
701 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100702 inode->i_uid = current_fsuid();
703 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return inode;
707
708fail_iput:
709 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711fail_inode:
712 return NULL;
713}
714
Al Viroe4fad8e2012-07-21 15:33:25 +0400715int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Andi Kleend6cbd282006-09-30 23:29:26 -0700717 int err;
Al Viroe4fad8e2012-07-21 15:33:25 +0400718 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700719 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +0400720 struct path path;
Al Viroe4fad8e2012-07-21 15:33:25 +0400721 static struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400724 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Andi Kleend6cbd282006-09-30 23:29:26 -0700726 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +1100727 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +0400728 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700729 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +0400730 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +0200731
Al Viro2c48b9c2009-08-09 00:52:35 +0400732 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800733
734 err = -ENFILE;
Al Viro599a0ac2013-03-12 09:58:10 -0400735 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700736 if (IS_ERR(f))
Dave Hansen430e2852008-02-15 14:37:26 -0800737 goto err_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds98830352012-04-29 13:12:42 -0700739 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Al Virode32ec42013-03-21 11:16:56 -0400740 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Al Viro599a0ac2013-03-12 09:58:10 -0400742 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700743 if (IS_ERR(res[0]))
Al Viroe4fad8e2012-07-21 15:33:25 +0400744 goto err_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Al Viroe4fad8e2012-07-21 15:33:25 +0400746 path_get(&path);
Al Virode32ec42013-03-21 11:16:56 -0400747 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400748 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
749 res[1] = f;
750 return 0;
751
752err_file:
753 put_filp(f);
754err_dentry:
Al Viro4b8a8f12013-03-21 11:06:46 -0400755 free_pipe_info(inode->i_pipe);
Al Viro2c48b9c2009-08-09 00:52:35 +0400756 path_put(&path);
Al Viroe4fad8e2012-07-21 15:33:25 +0400757 return err;
Al Viroed152432008-04-22 19:51:27 -0400758
Al Viroe4fad8e2012-07-21 15:33:25 +0400759err_inode:
Al Viro4b8a8f12013-03-21 11:06:46 -0400760 free_pipe_info(inode->i_pipe);
Andi Kleend6cbd282006-09-30 23:29:26 -0700761 iput(inode);
Al Viroe4fad8e2012-07-21 15:33:25 +0400762 return err;
Andi Kleend6cbd282006-09-30 23:29:26 -0700763}
764
Al Viro5b249b12012-08-19 12:17:29 -0400765static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700766{
Andi Kleend6cbd282006-09-30 23:29:26 -0700767 int error;
768 int fdw, fdr;
769
Linus Torvalds98830352012-04-29 13:12:42 -0700770 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700771 return -EINVAL;
772
Al Viroe4fad8e2012-07-21 15:33:25 +0400773 error = create_pipe_files(files, flags);
774 if (error)
775 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700776
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700777 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700778 if (error < 0)
779 goto err_read_pipe;
780 fdr = error;
781
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700782 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700783 if (error < 0)
784 goto err_fdr;
785 fdw = error;
786
Al Viro157cf642008-12-14 04:57:47 -0500787 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700788 fd[0] = fdr;
789 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 0;
791
Andi Kleend6cbd282006-09-30 23:29:26 -0700792 err_fdr:
793 put_unused_fd(fdr);
794 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400795 fput(files[0]);
796 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700797 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Al Viro5b249b12012-08-19 12:17:29 -0400800int do_pipe_flags(int *fd, int flags)
801{
802 struct file *files[2];
803 int error = __do_pipe_flags(fd, files, flags);
804 if (!error) {
805 fd_install(fd[0], files[0]);
806 fd_install(fd[1], files[1]);
807 }
808 return error;
809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400812 * sys_pipe() is the normal C calling standard for creating
813 * a pipe. It's not the way Unix traditionally does this, though.
814 */
Heiko Carstensd4e82042009-01-14 14:14:34 +0100815SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400816{
Al Viro5b249b12012-08-19 12:17:29 -0400817 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400818 int fd[2];
819 int error;
820
Al Viro5b249b12012-08-19 12:17:29 -0400821 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400822 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400823 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
824 fput(files[0]);
825 fput(files[1]);
826 put_unused_fd(fd[0]);
827 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400828 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400829 } else {
830 fd_install(fd[0], files[0]);
831 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700832 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400833 }
834 return error;
835}
836
Heiko Carstens2b664212009-01-14 14:14:35 +0100837SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700838{
839 return sys_pipe2(fildes, 0);
840}
841
Al Virofc7478a2013-03-21 02:07:59 -0400842static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400843{
844 int cur = *cnt;
845
846 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400847 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400848 if (signal_pending(current))
849 break;
850 }
851 return cur == *cnt ? -ERESTARTSYS : 0;
852}
853
Al Virofc7478a2013-03-21 02:07:59 -0400854static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400855{
Al Virofc7478a2013-03-21 02:07:59 -0400856 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400857}
858
859static int fifo_open(struct inode *inode, struct file *filp)
860{
861 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400862 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400863 int ret;
864
Al Viroba5bb142013-03-21 02:21:19 -0400865 filp->f_version = 0;
866
867 spin_lock(&inode->i_lock);
868 if (inode->i_pipe) {
869 pipe = inode->i_pipe;
870 pipe->files++;
871 spin_unlock(&inode->i_lock);
872 } else {
873 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400874 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400875 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400876 return -ENOMEM;
877 pipe->files = 1;
878 spin_lock(&inode->i_lock);
879 if (unlikely(inode->i_pipe)) {
880 inode->i_pipe->files++;
881 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400882 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400883 pipe = inode->i_pipe;
884 } else {
885 inode->i_pipe = pipe;
886 spin_unlock(&inode->i_lock);
887 }
Al Virof776c732013-03-12 09:46:27 -0400888 }
Al Virode32ec42013-03-21 11:16:56 -0400889 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400890 /* OK, we have a pipe and it's pinned down */
891
Al Viroebec73f2013-03-21 12:24:01 -0400892 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400893
894 /* We can only do regular read/write on fifos */
895 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
896
897 switch (filp->f_mode) {
898 case FMODE_READ:
899 /*
900 * O_RDONLY
901 * POSIX.1 says that O_NONBLOCK means return with the FIFO
902 * opened, even when there is no process writing the FIFO.
903 */
Al Virof776c732013-03-12 09:46:27 -0400904 pipe->r_counter++;
905 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400906 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400907
Al Viro599a0ac2013-03-12 09:58:10 -0400908 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400909 if ((filp->f_flags & O_NONBLOCK)) {
910 /* suppress POLLHUP until we have
911 * seen a writer */
912 filp->f_version = pipe->w_counter;
913 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400914 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400915 goto err_rd;
916 }
917 }
918 break;
919
920 case FMODE_WRITE:
921 /*
922 * O_WRONLY
923 * POSIX.1 says that O_NONBLOCK means return -1 with
924 * errno=ENXIO when there is no process reading the FIFO.
925 */
926 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400927 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400928 goto err;
929
Al Virof776c732013-03-12 09:46:27 -0400930 pipe->w_counter++;
931 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400932 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400933
Al Viro599a0ac2013-03-12 09:58:10 -0400934 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400935 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400936 goto err_wr;
937 }
938 break;
939
940 case FMODE_READ | FMODE_WRITE:
941 /*
942 * O_RDWR
943 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
944 * This implementation will NEVER block on a O_RDWR open, since
945 * the process can at least talk to itself.
946 */
Al Virof776c732013-03-12 09:46:27 -0400947
948 pipe->readers++;
949 pipe->writers++;
950 pipe->r_counter++;
951 pipe->w_counter++;
952 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -0400953 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400954 break;
955
956 default:
957 ret = -EINVAL;
958 goto err;
959 }
960
961 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -0400962 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400963 return 0;
964
965err_rd:
966 if (!--pipe->readers)
967 wake_up_interruptible(&pipe->wait);
968 ret = -ERESTARTSYS;
969 goto err;
970
971err_wr:
972 if (!--pipe->writers)
973 wake_up_interruptible(&pipe->wait);
974 ret = -ERESTARTSYS;
975 goto err;
976
977err:
Al Viroebec73f2013-03-21 12:24:01 -0400978 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800979
980 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -0400981 return ret;
982}
983
Al Viro599a0ac2013-03-12 09:58:10 -0400984const struct file_operations pipefifo_fops = {
985 .open = fifo_open,
986 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -0400987 .read = new_sync_read,
988 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -0400989 .write = new_sync_write,
990 .write_iter = pipe_write,
Al Viro599a0ac2013-03-12 09:58:10 -0400991 .poll = pipe_poll,
992 .unlocked_ioctl = pipe_ioctl,
993 .release = pipe_release,
994 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -0400995};
996
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400997/*
Jens Axboe35f3d142010-05-20 10:43:18 +0200998 * Allocate a new array of pipe buffers and copy the info over. Returns the
999 * pipe size if successful, or return -ERROR on error.
1000 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001001static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001002{
1003 struct pipe_buffer *bufs;
1004
1005 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001006 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1007 * expect a lot of shrink+grow operations, just free and allocate
1008 * again like we would do for growing. If the pipe currently
1009 * contains more buffers than arg, then return busy.
1010 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001011 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001012 return -EBUSY;
1013
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001014 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001015 if (unlikely(!bufs))
1016 return -ENOMEM;
1017
1018 /*
1019 * The pipe array wraps around, so just start the new one at zero
1020 * and adjust the indexes.
1021 */
1022 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001023 unsigned int tail;
1024 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001025
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001026 tail = pipe->curbuf + pipe->nrbufs;
1027 if (tail < pipe->buffers)
1028 tail = 0;
1029 else
1030 tail &= (pipe->buffers - 1);
1031
1032 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001033 if (head)
1034 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1035 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001036 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001037 }
1038
Willy Tarreaube65d292016-01-18 16:36:09 +01001039 account_pipe_buffers(pipe, pipe->buffers, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001040 pipe->curbuf = 0;
1041 kfree(pipe->bufs);
1042 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001043 pipe->buffers = nr_pages;
1044 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001045}
1046
Jens Axboeff9da692010-06-03 14:54:39 +02001047/*
1048 * Currently we rely on the pipe array holding a power-of-2 number
1049 * of pages.
1050 */
1051static inline unsigned int round_pipe_size(unsigned int size)
1052{
1053 unsigned long nr_pages;
1054
1055 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1056 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1057}
1058
1059/*
1060 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1061 * will return an error.
1062 */
1063int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1064 size_t *lenp, loff_t *ppos)
1065{
1066 int ret;
1067
1068 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1069 if (ret < 0 || !write)
1070 return ret;
1071
1072 pipe_max_size = round_pipe_size(pipe_max_size);
1073 return ret;
1074}
1075
Linus Torvalds72083642010-11-28 16:27:19 -08001076/*
1077 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1078 * location, so checking ->i_pipe is not enough to verify that this is a
1079 * pipe.
1080 */
1081struct pipe_inode_info *get_pipe_info(struct file *file)
1082{
Al Virode32ec42013-03-21 11:16:56 -04001083 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001084}
1085
Jens Axboe35f3d142010-05-20 10:43:18 +02001086long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1087{
1088 struct pipe_inode_info *pipe;
1089 long ret;
1090
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001091 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001092 if (!pipe)
1093 return -EBADF;
1094
Al Viroebec73f2013-03-21 12:24:01 -04001095 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001096
1097 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001098 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001099 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001100
Jens Axboeff9da692010-06-03 14:54:39 +02001101 size = round_pipe_size(arg);
1102 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001103
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001104 ret = -EINVAL;
1105 if (!nr_pages)
1106 goto out;
1107
Jens Axboeff9da692010-06-03 14:54:39 +02001108 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001109 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001110 goto out;
Willy Tarreaube65d292016-01-18 16:36:09 +01001111 } else if ((too_many_pipe_buffers_hard(pipe->user) ||
1112 too_many_pipe_buffers_soft(pipe->user)) &&
1113 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
1114 ret = -EPERM;
1115 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001116 }
Jens Axboeff9da692010-06-03 14:54:39 +02001117 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001118 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001119 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001120 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001121 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001122 break;
1123 default:
1124 ret = -EINVAL;
1125 break;
1126 }
1127
Julia Lawallcc967be2010-05-26 17:54:39 +02001128out:
Al Viroebec73f2013-03-21 12:24:01 -04001129 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001130 return ret;
1131}
1132
Nick Pigginff0c7d12011-01-07 17:49:50 +11001133static const struct super_operations pipefs_ops = {
1134 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001135 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001136};
1137
Jens Axboe35f3d142010-05-20 10:43:18 +02001138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * pipefs should _never_ be mounted by userland - too much of security hassle,
1140 * no real gain from having the whole whorehouse mounted. So we don't need
1141 * any operations on the root directory. However, we need a non-trivial
1142 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1143 */
Al Viro51139ad2010-07-25 23:47:46 +04001144static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1145 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Al Viroc74a1cb2011-01-12 16:59:34 -05001147 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1148 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151static struct file_system_type pipe_fs_type = {
1152 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001153 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 .kill_sb = kill_anon_super,
1155};
1156
1157static int __init init_pipe_fs(void)
1158{
1159 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!err) {
1162 pipe_mnt = kern_mount(&pipe_fs_type);
1163 if (IS_ERR(pipe_mnt)) {
1164 err = PTR_ERR(pipe_mnt);
1165 unregister_filesystem(&pipe_fs_type);
1166 }
1167 }
1168 return err;
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171fs_initcall(init_pipe_fs);