blob: 034bffac3f9724c6121f4635ba9740d61e106d06 [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
42/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * We use a start+len construction, which provides full use of the
44 * allocated memory.
45 * -- Florian Coosmann (FGC)
46 *
47 * Reads with count = 0 should always return 0.
48 * -- Julian Bradfield 1999-06-07.
49 *
50 * FIFOs and Pipes now generate SIGIO for both readers and writers.
51 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
52 *
53 * pipe_read & write cleanup
54 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
55 */
56
Miklos Szeredi61e0d472009-04-14 19:48:41 +020057static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
58{
Al Viro6447a3c2013-03-21 11:01:38 -040059 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040060 mutex_lock_nested(&pipe->mutex, subclass);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020061}
62
63void pipe_lock(struct pipe_inode_info *pipe)
64{
65 /*
66 * pipe_lock() nests non-pipe inode locks (for writing to a file)
67 */
68 pipe_lock_nested(pipe, I_MUTEX_PARENT);
69}
70EXPORT_SYMBOL(pipe_lock);
71
72void pipe_unlock(struct pipe_inode_info *pipe)
73{
Al Viro6447a3c2013-03-21 11:01:38 -040074 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040075 mutex_unlock(&pipe->mutex);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020076}
77EXPORT_SYMBOL(pipe_unlock);
78
Al Viroebec73f2013-03-21 12:24:01 -040079static inline void __pipe_lock(struct pipe_inode_info *pipe)
80{
81 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
82}
83
84static inline void __pipe_unlock(struct pipe_inode_info *pipe)
85{
86 mutex_unlock(&pipe->mutex);
87}
88
Miklos Szeredi61e0d472009-04-14 19:48:41 +020089void pipe_double_lock(struct pipe_inode_info *pipe1,
90 struct pipe_inode_info *pipe2)
91{
92 BUG_ON(pipe1 == pipe2);
93
94 if (pipe1 < pipe2) {
95 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
96 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
97 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +020098 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
99 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200100 }
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200104void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 DEFINE_WAIT(wait);
107
Ingo Molnard79fc0f2005-09-10 00:26:12 -0700108 /*
109 * Pipes are system-local resources, so sleeping on them
110 * is considered a noninteractive wait:
111 */
Mike Galbraithaf927232007-10-15 17:00:13 +0200112 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200113 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200115 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200116 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Arjan van de Ven858119e2006-01-14 13:20:43 -0800119static int
Jens Axboef6762b72006-05-01 20:02:05 +0200120pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
121 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 unsigned long copy;
124
125 while (len > 0) {
126 while (!iov->iov_len)
127 iov++;
128 copy = min_t(unsigned long, len, iov->iov_len);
129
Jens Axboef6762b72006-05-01 20:02:05 +0200130 if (atomic) {
131 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
132 return -EFAULT;
133 } else {
134 if (copy_from_user(to, iov->iov_base, copy))
135 return -EFAULT;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 to += copy;
138 len -= copy;
139 iov->iov_base += copy;
140 iov->iov_len -= copy;
141 }
142 return 0;
143}
144
Jens Axboef6762b72006-05-01 20:02:05 +0200145/*
146 * Pre-fault in the user memory, so we can use atomic copies.
147 */
148static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
149{
150 while (!iov->iov_len)
151 iov++;
152
153 while (len > 0) {
154 unsigned long this_len;
155
156 this_len = min_t(unsigned long, len, iov->iov_len);
157 fault_in_pages_readable(iov->iov_base, this_len);
158 len -= this_len;
159 iov++;
160 }
161}
162
Ingo Molnar341b4462006-04-11 13:57:45 +0200163static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
164 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct page *page = buf->page;
167
Jens Axboe5274f052006-03-30 15:15:30 +0200168 /*
169 * If nobody else uses this page, and we don't already have a
170 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200171 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200172 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200173 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200174 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200175 else
176 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Jens Axboe08457182007-06-12 20:51:32 +0200179/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800180 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200181 * @pipe: the pipe that the buffer belongs to
182 * @buf: the buffer to attempt to steal
183 *
184 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800185 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200186 * @buf. If successful, this function returns 0 and returns with
187 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800188 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200189 * page cache.
190 */
Jens Axboe330ab712006-05-02 15:29:57 +0200191int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
192 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200193{
Jens Axboe46e678c2006-04-30 16:36:32 +0200194 struct page *page = buf->page;
195
Jens Axboe08457182007-06-12 20:51:32 +0200196 /*
197 * A reference of one is golden, that means that the owner of this
198 * page is the only one holding a reference to it. lock the page
199 * and return OK.
200 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200201 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200202 lock_page(page);
203 return 0;
204 }
205
206 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200207}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200208EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200209
Jens Axboe08457182007-06-12 20:51:32 +0200210/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800211 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200212 * @pipe: the pipe that the buffer belongs to
213 * @buf: the buffer to get a reference to
214 *
215 * Description:
216 * This function grabs an extra reference to @buf. It's used in
217 * in the tee() system call, when we duplicate the buffers in one
218 * pipe into another.
219 */
220void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200221{
222 page_cache_get(buf->page);
223}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200224EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200225
Jens Axboe08457182007-06-12 20:51:32 +0200226/**
227 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200228 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200229 * @buf: the buffer to confirm
230 *
231 * Description:
232 * This function does nothing, because the generic pipe code uses
233 * pages that are always good when inserted into the pipe.
234 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200235int generic_pipe_buf_confirm(struct pipe_inode_info *info,
236 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200237{
238 return 0;
239}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200240EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200241
Miklos Szeredi68181732009-05-07 15:37:36 +0200242/**
243 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
244 * @pipe: the pipe that the buffer belongs to
245 * @buf: the buffer to put a reference to
246 *
247 * Description:
248 * This function releases a reference to @buf.
249 */
250void generic_pipe_buf_release(struct pipe_inode_info *pipe,
251 struct pipe_buffer *buf)
252{
253 page_cache_release(buf->page);
254}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200255EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200256
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800257static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .can_merge = 1,
Jens Axboecac36bb02007-06-14 13:10:48 +0200259 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200261 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200262 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
Linus Torvalds98830352012-04-29 13:12:42 -0700265static const struct pipe_buf_operations packet_pipe_buf_ops = {
266 .can_merge = 0,
Linus Torvalds98830352012-04-29 13:12:42 -0700267 .confirm = generic_pipe_buf_confirm,
268 .release = anon_pipe_buf_release,
269 .steal = generic_pipe_buf_steal,
270 .get = generic_pipe_buf_get,
271};
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700274pipe_read(struct kiocb *iocb, const struct iovec *_iov,
275 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700277 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400278 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int do_wakeup;
280 ssize_t ret;
281 struct iovec *iov = (struct iovec *)_iov;
282 size_t total_len;
Al Viro637b58c2014-02-03 19:11:42 -0500283 struct iov_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 total_len = iov_length(iov, nr_segs);
286 /* Null read succeeds. */
287 if (unlikely(total_len == 0))
288 return 0;
289
Al Viro637b58c2014-02-03 19:11:42 -0500290 iov_iter_init(&iter, iov, nr_segs, total_len, 0);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 do_wakeup = 0;
293 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400294 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200296 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200298 int curbuf = pipe->curbuf;
299 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800300 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500302 size_t written;
303 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (chars > total_len)
306 chars = total_len;
307
Jens Axboecac36bb02007-06-14 13:10:48 +0200308 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200309 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200310 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200311 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200312 break;
313 }
Jens Axboef84d7512006-05-01 19:59:03 +0200314
Al Viro637b58c2014-02-03 19:11:42 -0500315 written = copy_page_to_iter(buf->page, buf->offset, chars, &iter);
316 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200317 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500318 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320 }
321 ret += chars;
322 buf->offset += chars;
323 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700324
325 /* Was it a packet buffer? Clean up and exit */
326 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
327 total_len = chars;
328 buf->len = 0;
329 }
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (!buf->len) {
332 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200333 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200334 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200335 pipe->curbuf = curbuf;
336 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 do_wakeup = 1;
338 }
339 total_len -= chars;
340 if (!total_len)
341 break; /* common path: read succeeded */
342 }
343 if (bufs) /* More to do? */
344 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200345 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200347 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* syscall merging: Usually we must not sleep
349 * if O_NONBLOCK is set, or if we got some data.
350 * But if a writer sleeps in kernel space, then
351 * we can wait for that data without violating POSIX.
352 */
353 if (ret)
354 break;
355 if (filp->f_flags & O_NONBLOCK) {
356 ret = -EAGAIN;
357 break;
358 }
359 }
360 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200361 if (!ret)
362 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 }
365 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800366 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200367 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200369 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Al Viroebec73f2013-03-21 12:24:01 -0400371 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200372
373 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800375 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200376 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 if (ret > 0)
379 file_accessed(filp);
380 return ret;
381}
382
Linus Torvalds98830352012-04-29 13:12:42 -0700383static inline int is_packetized(struct file *file)
384{
385 return (file->f_flags & O_DIRECT) != 0;
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700389pipe_write(struct kiocb *iocb, const struct iovec *_iov,
390 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700392 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400393 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ssize_t ret;
395 int do_wakeup;
396 struct iovec *iov = (struct iovec *)_iov;
397 size_t total_len;
398 ssize_t chars;
399
400 total_len = iov_length(iov, nr_segs);
401 /* Null write succeeds. */
402 if (unlikely(total_len == 0))
403 return 0;
404
405 do_wakeup = 0;
406 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400407 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Ingo Molnar923f4f22006-04-11 13:53:33 +0200409 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 send_sig(SIGPIPE, current, 0);
411 ret = -EPIPE;
412 goto out;
413 }
414
415 /* We try to merge small writes */
416 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200417 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200418 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200419 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200420 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800421 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200425 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200426 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200427
Jens Axboecac36bb02007-06-14 13:10:48 +0200428 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200429 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200430 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200431
Jens Axboef6762b72006-05-01 20:02:05 +0200432 iov_fault_in_pages_read(iov, chars);
433redo1:
Al Virofbb32752014-02-02 21:09:54 -0500434 if (atomic)
435 addr = kmap_atomic(buf->page);
436 else
437 addr = kmap(buf->page);
Jens Axboe5274f052006-03-30 15:15:30 +0200438 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200439 chars, atomic);
Al Virofbb32752014-02-02 21:09:54 -0500440 if (atomic)
441 kunmap_atomic(addr);
442 else
443 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 ret = error;
445 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200446 if (error) {
447 if (atomic) {
448 atomic = 0;
449 goto redo1;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 buf->len += chars;
454 total_len -= chars;
455 ret = chars;
456 if (!total_len)
457 goto out;
458 }
459 }
460
461 for (;;) {
462 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200463
Ingo Molnar923f4f22006-04-11 13:53:33 +0200464 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200466 if (!ret)
467 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200470 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200471 if (bufs < pipe->buffers) {
472 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200473 struct pipe_buffer *buf = pipe->bufs + newbuf;
474 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200475 char *src;
476 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (!page) {
479 page = alloc_page(GFP_HIGHUSER);
480 if (unlikely(!page)) {
481 ret = ret ? : -ENOMEM;
482 break;
483 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200484 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200486 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * we lock up (O_NONBLOCK-)readers that sleep due to
488 * syscall merging.
489 * FIXME! Is this really true?
490 */
491 do_wakeup = 1;
492 chars = PAGE_SIZE;
493 if (chars > total_len)
494 chars = total_len;
495
Jens Axboef6762b72006-05-01 20:02:05 +0200496 iov_fault_in_pages_read(iov, chars);
497redo2:
498 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800499 src = kmap_atomic(page);
Jens Axboef6762b72006-05-01 20:02:05 +0200500 else
501 src = kmap(page);
502
503 error = pipe_iov_copy_from_user(src, iov, chars,
504 atomic);
505 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800506 kunmap_atomic(src);
Jens Axboef6762b72006-05-01 20:02:05 +0200507 else
508 kunmap(page);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200511 if (atomic) {
512 atomic = 0;
513 goto redo2;
514 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200515 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200516 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518 }
519 ret += chars;
520
521 /* Insert it into the buffer array */
522 buf->page = page;
523 buf->ops = &anon_pipe_buf_ops;
524 buf->offset = 0;
525 buf->len = chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700526 buf->flags = 0;
527 if (is_packetized(filp)) {
528 buf->ops = &packet_pipe_buf_ops;
529 buf->flags = PIPE_BUF_FLAG_PACKET;
530 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200531 pipe->nrbufs = ++bufs;
532 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 total_len -= chars;
535 if (!total_len)
536 break;
537 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200538 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 continue;
540 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200541 if (!ret)
542 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544 }
545 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200546 if (!ret)
547 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 }
550 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800551 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200552 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 do_wakeup = 0;
554 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200555 pipe->waiting_writers++;
556 pipe_wait(pipe);
557 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559out:
Al Viroebec73f2013-03-21 12:24:01 -0400560 __pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800562 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200563 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800565 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400566 int err = file_update_time(filp);
567 if (err)
568 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800569 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return ret;
572}
573
Andi Kleend59d0b12008-02-08 04:21:23 -0800574static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Al Virode32ec42013-03-21 11:16:56 -0400576 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int count, buf, nrbufs;
578
579 switch (cmd) {
580 case FIONREAD:
Al Viroebec73f2013-03-21 12:24:01 -0400581 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200583 buf = pipe->curbuf;
584 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200586 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200587 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Al Viroebec73f2013-03-21 12:24:01 -0400589 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return put_user(count, (int __user *)arg);
592 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100593 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595}
596
597/* No kernel lock held - fine */
598static unsigned int
599pipe_poll(struct file *filp, poll_table *wait)
600{
601 unsigned int mask;
Al Virode32ec42013-03-21 11:16:56 -0400602 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int nrbufs;
604
Ingo Molnar923f4f22006-04-11 13:53:33 +0200605 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200608 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 mask = 0;
610 if (filp->f_mode & FMODE_READ) {
611 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200612 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 mask |= POLLHUP;
614 }
615
616 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200617 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700618 /*
619 * Most Unices do not set POLLERR for FIFOs but on Linux they
620 * behave exactly like pipes for poll().
621 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200622 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 mask |= POLLERR;
624 }
625
626 return mask;
627}
628
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800629static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
630{
631 int kill = 0;
632
633 spin_lock(&inode->i_lock);
634 if (!--pipe->files) {
635 inode->i_pipe = NULL;
636 kill = 1;
637 }
638 spin_unlock(&inode->i_lock);
639
640 if (kill)
641 free_pipe_info(pipe);
642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static int
Al Viro599a0ac2013-03-12 09:58:10 -0400645pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800647 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200648
Al Viroebec73f2013-03-21 12:24:01 -0400649 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400650 if (file->f_mode & FMODE_READ)
651 pipe->readers--;
652 if (file->f_mode & FMODE_WRITE)
653 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200654
Al Viroba5bb142013-03-21 02:21:19 -0400655 if (pipe->readers || pipe->writers) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800656 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200657 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
658 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Al Viroebec73f2013-03-21 12:24:01 -0400660 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400661
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800662 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664}
665
666static int
Al Viro599a0ac2013-03-12 09:58:10 -0400667pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Al Virode32ec42013-03-21 11:16:56 -0400669 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400670 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Al Viroebec73f2013-03-21 12:24:01 -0400672 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400673 if (filp->f_mode & FMODE_READ)
674 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
675 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200676 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400677 if (retval < 0 && (filp->f_mode & FMODE_READ))
678 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700679 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
680 }
Al Viroebec73f2013-03-21 12:24:01 -0400681 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700682 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Al Viro7bee1302013-03-21 11:04:15 -0400685struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200686{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200687 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200688
Ingo Molnar923f4f22006-04-11 13:53:33 +0200689 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
690 if (pipe) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200691 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
692 if (pipe->bufs) {
693 init_waitqueue_head(&pipe->wait);
694 pipe->r_counter = pipe->w_counter = 1;
Jens Axboe35f3d142010-05-20 10:43:18 +0200695 pipe->buffers = PIPE_DEF_BUFFERS;
Al Viro72b0d9a2013-03-21 02:32:24 -0400696 mutex_init(&pipe->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +0200697 return pipe;
698 }
699 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200700 }
701
Jens Axboe35f3d142010-05-20 10:43:18 +0200702 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200703}
704
Al Viro4b8a8f12013-03-21 11:06:46 -0400705void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jens Axboe35f3d142010-05-20 10:43:18 +0200709 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200710 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200712 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200714 if (pipe->tmp_page)
715 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200716 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200717 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800720static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200721
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700722/*
723 * pipefs_dname() is called from d_path().
724 */
725static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
726{
727 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
728 dentry->d_inode->i_ino);
729}
730
Al Viro3ba13d12009-02-20 06:02:22 +0000731static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700732 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733};
734
735static struct inode * get_pipe_inode(void)
736{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200737 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200738 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (!inode)
741 goto fail_inode;
742
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400743 inode->i_ino = get_next_ino();
744
Al Viro7bee1302013-03-21 11:04:15 -0400745 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200746 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200748
Al Viroba5bb142013-03-21 02:21:19 -0400749 inode->i_pipe = pipe;
750 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200751 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400752 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /*
755 * Mark the inode dirty from the very beginning,
756 * that way it will never be moved to the dirty
757 * list because "mark_inode_dirty()" will think
758 * that it already _is_ on the dirty list.
759 */
760 inode->i_state = I_DIRTY;
761 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100762 inode->i_uid = current_fsuid();
763 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return inode;
767
768fail_iput:
769 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771fail_inode:
772 return NULL;
773}
774
Al Viroe4fad8e2012-07-21 15:33:25 +0400775int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Andi Kleend6cbd282006-09-30 23:29:26 -0700777 int err;
Al Viroe4fad8e2012-07-21 15:33:25 +0400778 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700779 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +0400780 struct path path;
Al Viroe4fad8e2012-07-21 15:33:25 +0400781 static struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400784 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Andi Kleend6cbd282006-09-30 23:29:26 -0700786 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +1100787 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +0400788 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700789 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +0400790 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +0200791
Al Viro2c48b9c2009-08-09 00:52:35 +0400792 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800793
794 err = -ENFILE;
Al Viro599a0ac2013-03-12 09:58:10 -0400795 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700796 if (IS_ERR(f))
Dave Hansen430e2852008-02-15 14:37:26 -0800797 goto err_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Linus Torvalds98830352012-04-29 13:12:42 -0700799 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Al Virode32ec42013-03-21 11:16:56 -0400800 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Al Viro599a0ac2013-03-12 09:58:10 -0400802 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700803 if (IS_ERR(res[0]))
Al Viroe4fad8e2012-07-21 15:33:25 +0400804 goto err_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Al Viroe4fad8e2012-07-21 15:33:25 +0400806 path_get(&path);
Al Virode32ec42013-03-21 11:16:56 -0400807 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400808 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
809 res[1] = f;
810 return 0;
811
812err_file:
813 put_filp(f);
814err_dentry:
Al Viro4b8a8f12013-03-21 11:06:46 -0400815 free_pipe_info(inode->i_pipe);
Al Viro2c48b9c2009-08-09 00:52:35 +0400816 path_put(&path);
Al Viroe4fad8e2012-07-21 15:33:25 +0400817 return err;
Al Viroed152432008-04-22 19:51:27 -0400818
Al Viroe4fad8e2012-07-21 15:33:25 +0400819err_inode:
Al Viro4b8a8f12013-03-21 11:06:46 -0400820 free_pipe_info(inode->i_pipe);
Andi Kleend6cbd282006-09-30 23:29:26 -0700821 iput(inode);
Al Viroe4fad8e2012-07-21 15:33:25 +0400822 return err;
Andi Kleend6cbd282006-09-30 23:29:26 -0700823}
824
Al Viro5b249b12012-08-19 12:17:29 -0400825static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700826{
Andi Kleend6cbd282006-09-30 23:29:26 -0700827 int error;
828 int fdw, fdr;
829
Linus Torvalds98830352012-04-29 13:12:42 -0700830 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700831 return -EINVAL;
832
Al Viroe4fad8e2012-07-21 15:33:25 +0400833 error = create_pipe_files(files, flags);
834 if (error)
835 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700836
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700837 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700838 if (error < 0)
839 goto err_read_pipe;
840 fdr = error;
841
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700842 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700843 if (error < 0)
844 goto err_fdr;
845 fdw = error;
846
Al Viro157cf642008-12-14 04:57:47 -0500847 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700848 fd[0] = fdr;
849 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return 0;
851
Andi Kleend6cbd282006-09-30 23:29:26 -0700852 err_fdr:
853 put_unused_fd(fdr);
854 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400855 fput(files[0]);
856 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700857 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Al Viro5b249b12012-08-19 12:17:29 -0400860int do_pipe_flags(int *fd, int flags)
861{
862 struct file *files[2];
863 int error = __do_pipe_flags(fd, files, flags);
864 if (!error) {
865 fd_install(fd[0], files[0]);
866 fd_install(fd[1], files[1]);
867 }
868 return error;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400872 * sys_pipe() is the normal C calling standard for creating
873 * a pipe. It's not the way Unix traditionally does this, though.
874 */
Heiko Carstensd4e82042009-01-14 14:14:34 +0100875SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400876{
Al Viro5b249b12012-08-19 12:17:29 -0400877 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400878 int fd[2];
879 int error;
880
Al Viro5b249b12012-08-19 12:17:29 -0400881 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400882 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400883 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
884 fput(files[0]);
885 fput(files[1]);
886 put_unused_fd(fd[0]);
887 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400888 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400889 } else {
890 fd_install(fd[0], files[0]);
891 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700892 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400893 }
894 return error;
895}
896
Heiko Carstens2b664212009-01-14 14:14:35 +0100897SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700898{
899 return sys_pipe2(fildes, 0);
900}
901
Al Virofc7478a2013-03-21 02:07:59 -0400902static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400903{
904 int cur = *cnt;
905
906 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400907 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400908 if (signal_pending(current))
909 break;
910 }
911 return cur == *cnt ? -ERESTARTSYS : 0;
912}
913
Al Virofc7478a2013-03-21 02:07:59 -0400914static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400915{
Al Virofc7478a2013-03-21 02:07:59 -0400916 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400917}
918
919static int fifo_open(struct inode *inode, struct file *filp)
920{
921 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400922 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400923 int ret;
924
Al Viroba5bb142013-03-21 02:21:19 -0400925 filp->f_version = 0;
926
927 spin_lock(&inode->i_lock);
928 if (inode->i_pipe) {
929 pipe = inode->i_pipe;
930 pipe->files++;
931 spin_unlock(&inode->i_lock);
932 } else {
933 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400934 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400935 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400936 return -ENOMEM;
937 pipe->files = 1;
938 spin_lock(&inode->i_lock);
939 if (unlikely(inode->i_pipe)) {
940 inode->i_pipe->files++;
941 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400942 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400943 pipe = inode->i_pipe;
944 } else {
945 inode->i_pipe = pipe;
946 spin_unlock(&inode->i_lock);
947 }
Al Virof776c732013-03-12 09:46:27 -0400948 }
Al Virode32ec42013-03-21 11:16:56 -0400949 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400950 /* OK, we have a pipe and it's pinned down */
951
Al Viroebec73f2013-03-21 12:24:01 -0400952 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400953
954 /* We can only do regular read/write on fifos */
955 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
956
957 switch (filp->f_mode) {
958 case FMODE_READ:
959 /*
960 * O_RDONLY
961 * POSIX.1 says that O_NONBLOCK means return with the FIFO
962 * opened, even when there is no process writing the FIFO.
963 */
Al Virof776c732013-03-12 09:46:27 -0400964 pipe->r_counter++;
965 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400966 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400967
Al Viro599a0ac2013-03-12 09:58:10 -0400968 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400969 if ((filp->f_flags & O_NONBLOCK)) {
970 /* suppress POLLHUP until we have
971 * seen a writer */
972 filp->f_version = pipe->w_counter;
973 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400974 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400975 goto err_rd;
976 }
977 }
978 break;
979
980 case FMODE_WRITE:
981 /*
982 * O_WRONLY
983 * POSIX.1 says that O_NONBLOCK means return -1 with
984 * errno=ENXIO when there is no process reading the FIFO.
985 */
986 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400987 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400988 goto err;
989
Al Virof776c732013-03-12 09:46:27 -0400990 pipe->w_counter++;
991 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400992 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400993
Al Viro599a0ac2013-03-12 09:58:10 -0400994 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400995 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400996 goto err_wr;
997 }
998 break;
999
1000 case FMODE_READ | FMODE_WRITE:
1001 /*
1002 * O_RDWR
1003 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1004 * This implementation will NEVER block on a O_RDWR open, since
1005 * the process can at least talk to itself.
1006 */
Al Virof776c732013-03-12 09:46:27 -04001007
1008 pipe->readers++;
1009 pipe->writers++;
1010 pipe->r_counter++;
1011 pipe->w_counter++;
1012 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -04001013 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001014 break;
1015
1016 default:
1017 ret = -EINVAL;
1018 goto err;
1019 }
1020
1021 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -04001022 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001023 return 0;
1024
1025err_rd:
1026 if (!--pipe->readers)
1027 wake_up_interruptible(&pipe->wait);
1028 ret = -ERESTARTSYS;
1029 goto err;
1030
1031err_wr:
1032 if (!--pipe->writers)
1033 wake_up_interruptible(&pipe->wait);
1034 ret = -ERESTARTSYS;
1035 goto err;
1036
1037err:
Al Viroebec73f2013-03-21 12:24:01 -04001038 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -08001039
1040 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -04001041 return ret;
1042}
1043
Al Viro599a0ac2013-03-12 09:58:10 -04001044const struct file_operations pipefifo_fops = {
1045 .open = fifo_open,
1046 .llseek = no_llseek,
1047 .read = do_sync_read,
1048 .aio_read = pipe_read,
1049 .write = do_sync_write,
1050 .aio_write = pipe_write,
1051 .poll = pipe_poll,
1052 .unlocked_ioctl = pipe_ioctl,
1053 .release = pipe_release,
1054 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001055};
1056
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001057/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001058 * Allocate a new array of pipe buffers and copy the info over. Returns the
1059 * pipe size if successful, or return -ERROR on error.
1060 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001061static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001062{
1063 struct pipe_buffer *bufs;
1064
1065 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001066 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1067 * expect a lot of shrink+grow operations, just free and allocate
1068 * again like we would do for growing. If the pipe currently
1069 * contains more buffers than arg, then return busy.
1070 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001071 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001072 return -EBUSY;
1073
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001074 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001075 if (unlikely(!bufs))
1076 return -ENOMEM;
1077
1078 /*
1079 * The pipe array wraps around, so just start the new one at zero
1080 * and adjust the indexes.
1081 */
1082 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001083 unsigned int tail;
1084 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001085
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001086 tail = pipe->curbuf + pipe->nrbufs;
1087 if (tail < pipe->buffers)
1088 tail = 0;
1089 else
1090 tail &= (pipe->buffers - 1);
1091
1092 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001093 if (head)
1094 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1095 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001096 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001097 }
1098
1099 pipe->curbuf = 0;
1100 kfree(pipe->bufs);
1101 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001102 pipe->buffers = nr_pages;
1103 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001104}
1105
Jens Axboeff9da692010-06-03 14:54:39 +02001106/*
1107 * Currently we rely on the pipe array holding a power-of-2 number
1108 * of pages.
1109 */
1110static inline unsigned int round_pipe_size(unsigned int size)
1111{
1112 unsigned long nr_pages;
1113
1114 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1115 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1116}
1117
1118/*
1119 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1120 * will return an error.
1121 */
1122int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1123 size_t *lenp, loff_t *ppos)
1124{
1125 int ret;
1126
1127 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1128 if (ret < 0 || !write)
1129 return ret;
1130
1131 pipe_max_size = round_pipe_size(pipe_max_size);
1132 return ret;
1133}
1134
Linus Torvalds72083642010-11-28 16:27:19 -08001135/*
1136 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1137 * location, so checking ->i_pipe is not enough to verify that this is a
1138 * pipe.
1139 */
1140struct pipe_inode_info *get_pipe_info(struct file *file)
1141{
Al Virode32ec42013-03-21 11:16:56 -04001142 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001143}
1144
Jens Axboe35f3d142010-05-20 10:43:18 +02001145long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1146{
1147 struct pipe_inode_info *pipe;
1148 long ret;
1149
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001150 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001151 if (!pipe)
1152 return -EBADF;
1153
Al Viroebec73f2013-03-21 12:24:01 -04001154 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001155
1156 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001157 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001158 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001159
Jens Axboeff9da692010-06-03 14:54:39 +02001160 size = round_pipe_size(arg);
1161 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001162
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001163 ret = -EINVAL;
1164 if (!nr_pages)
1165 goto out;
1166
Jens Axboeff9da692010-06-03 14:54:39 +02001167 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001168 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001169 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001170 }
Jens Axboeff9da692010-06-03 14:54:39 +02001171 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001172 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001173 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001174 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001175 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001176 break;
1177 default:
1178 ret = -EINVAL;
1179 break;
1180 }
1181
Julia Lawallcc967be2010-05-26 17:54:39 +02001182out:
Al Viroebec73f2013-03-21 12:24:01 -04001183 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001184 return ret;
1185}
1186
Nick Pigginff0c7d12011-01-07 17:49:50 +11001187static const struct super_operations pipefs_ops = {
1188 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001189 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001190};
1191
Jens Axboe35f3d142010-05-20 10:43:18 +02001192/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * pipefs should _never_ be mounted by userland - too much of security hassle,
1194 * no real gain from having the whole whorehouse mounted. So we don't need
1195 * any operations on the root directory. However, we need a non-trivial
1196 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1197 */
Al Viro51139ad2010-07-25 23:47:46 +04001198static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1199 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Al Viroc74a1cb2011-01-12 16:59:34 -05001201 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1202 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205static struct file_system_type pipe_fs_type = {
1206 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001207 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 .kill_sb = kill_anon_super,
1209};
1210
1211static int __init init_pipe_fs(void)
1212{
1213 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (!err) {
1216 pipe_mnt = kern_mount(&pipe_fs_type);
1217 if (IS_ERR(pipe_mnt)) {
1218 err = PTR_ERR(pipe_mnt);
1219 unregister_filesystem(&pipe_fs_type);
1220 }
1221 }
1222 return err;
1223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225fs_initcall(init_pipe_fs);