blob: 074d15fe3bc2148bfa0b16f69ca029166eeec9c4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/pipe.c
3 *
4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
5 */
6
7#include <linux/mm.h>
8#include <linux/file.h>
9#include <linux/poll.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/fs.h>
Jens Axboe35f3d142010-05-20 10:43:18 +020014#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mount.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070016#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/pipe_fs_i.h>
18#include <linux/uio.h>
19#include <linux/highmem.h>
Jens Axboe5274f052006-03-30 15:15:30 +020020#include <linux/pagemap.h>
Al Virodb349502007-02-07 01:48:00 -050021#include <linux/audit.h>
Ulrich Drepperba719ba2008-05-06 20:42:38 -070022#include <linux/syscalls.h>
Jens Axboeb492e952010-05-19 21:03:16 +020023#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26#include <asm/ioctls.h>
27
28/*
Jens Axboeb492e952010-05-19 21:03:16 +020029 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020030 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020031 */
Jens Axboeff9da692010-06-03 14:54:39 +020032unsigned int pipe_max_size = 1048576;
33
34/*
35 * Minimum pipe size, as required by POSIX
36 */
37unsigned int pipe_min_size = PAGE_SIZE;
Jens Axboeb492e952010-05-19 21:03:16 +020038
39/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * We use a start+len construction, which provides full use of the
41 * allocated memory.
42 * -- Florian Coosmann (FGC)
43 *
44 * Reads with count = 0 should always return 0.
45 * -- Julian Bradfield 1999-06-07.
46 *
47 * FIFOs and Pipes now generate SIGIO for both readers and writers.
48 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
49 *
50 * pipe_read & write cleanup
51 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
52 */
53
Miklos Szeredi61e0d472009-04-14 19:48:41 +020054static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
55{
56 if (pipe->inode)
57 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
58}
59
60void pipe_lock(struct pipe_inode_info *pipe)
61{
62 /*
63 * pipe_lock() nests non-pipe inode locks (for writing to a file)
64 */
65 pipe_lock_nested(pipe, I_MUTEX_PARENT);
66}
67EXPORT_SYMBOL(pipe_lock);
68
69void pipe_unlock(struct pipe_inode_info *pipe)
70{
71 if (pipe->inode)
72 mutex_unlock(&pipe->inode->i_mutex);
73}
74EXPORT_SYMBOL(pipe_unlock);
75
76void pipe_double_lock(struct pipe_inode_info *pipe1,
77 struct pipe_inode_info *pipe2)
78{
79 BUG_ON(pipe1 == pipe2);
80
81 if (pipe1 < pipe2) {
82 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
83 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
84 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +020085 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
86 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020087 }
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020091void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 DEFINE_WAIT(wait);
94
Ingo Molnard79fc0f2005-09-10 00:26:12 -070095 /*
96 * Pipes are system-local resources, so sleeping on them
97 * is considered a noninteractive wait:
98 */
Mike Galbraithaf927232007-10-15 17:00:13 +020099 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200100 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200102 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200103 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Arjan van de Ven858119e2006-01-14 13:20:43 -0800106static int
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100107pipe_iov_copy_from_user(void *addr, int *offset, struct iovec *iov,
108 size_t *remaining, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 unsigned long copy;
111
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100112 while (*remaining > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 while (!iov->iov_len)
114 iov++;
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100115 copy = min_t(unsigned long, *remaining, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Jens Axboef6762b72006-05-01 20:02:05 +0200117 if (atomic) {
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100118 if (__copy_from_user_inatomic(addr + *offset,
119 iov->iov_base, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200120 return -EFAULT;
121 } else {
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100122 if (copy_from_user(addr + *offset,
123 iov->iov_base, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200124 return -EFAULT;
125 }
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100126 *offset += copy;
127 *remaining -= copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 iov->iov_base += copy;
129 iov->iov_len -= copy;
130 }
131 return 0;
132}
133
Arjan van de Ven858119e2006-01-14 13:20:43 -0800134static int
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100135pipe_iov_copy_to_user(struct iovec *iov, void *addr, int *offset,
136 size_t *remaining, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 unsigned long copy;
139
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100140 while (*remaining > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 while (!iov->iov_len)
142 iov++;
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100143 copy = min_t(unsigned long, *remaining, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jens Axboef6762b72006-05-01 20:02:05 +0200145 if (atomic) {
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100146 if (__copy_to_user_inatomic(iov->iov_base,
147 addr + *offset, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200148 return -EFAULT;
149 } else {
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100150 if (copy_to_user(iov->iov_base,
151 addr + *offset, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200152 return -EFAULT;
153 }
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100154 *offset += copy;
155 *remaining -= copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 iov->iov_base += copy;
157 iov->iov_len -= copy;
158 }
159 return 0;
160}
161
Jens Axboef6762b72006-05-01 20:02:05 +0200162/*
163 * Attempt to pre-fault in the user memory, so we can use atomic copies.
164 * Returns the number of bytes not faulted in.
165 */
166static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
167{
168 while (!iov->iov_len)
169 iov++;
170
171 while (len > 0) {
172 unsigned long this_len;
173
174 this_len = min_t(unsigned long, len, iov->iov_len);
175 if (fault_in_pages_writeable(iov->iov_base, this_len))
176 break;
177
178 len -= this_len;
179 iov++;
180 }
181
182 return len;
183}
184
185/*
186 * Pre-fault in the user memory, so we can use atomic copies.
187 */
188static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
189{
190 while (!iov->iov_len)
191 iov++;
192
193 while (len > 0) {
194 unsigned long this_len;
195
196 this_len = min_t(unsigned long, len, iov->iov_len);
197 fault_in_pages_readable(iov->iov_base, this_len);
198 len -= this_len;
199 iov++;
200 }
201}
202
Ingo Molnar341b4462006-04-11 13:57:45 +0200203static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
204 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct page *page = buf->page;
207
Jens Axboe5274f052006-03-30 15:15:30 +0200208 /*
209 * If nobody else uses this page, and we don't already have a
210 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200211 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200212 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200213 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200214 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200215 else
216 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Jens Axboe08457182007-06-12 20:51:32 +0200219/**
220 * generic_pipe_buf_map - virtually map a pipe buffer
221 * @pipe: the pipe that the buffer belongs to
222 * @buf: the buffer that should be mapped
223 * @atomic: whether to use an atomic map
224 *
225 * Description:
226 * This function returns a kernel virtual address mapping for the
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800227 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
Jens Axboe08457182007-06-12 20:51:32 +0200228 * and the caller has to be careful not to fault before calling
229 * the unmap function.
230 *
231 * Note that this function occupies KM_USER0 if @atomic != 0.
232 */
Jens Axboef84d7512006-05-01 19:59:03 +0200233void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200234 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Jens Axboef6762b72006-05-01 20:02:05 +0200236 if (atomic) {
237 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800238 return kmap_atomic(buf->page);
Jens Axboef6762b72006-05-01 20:02:05 +0200239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return kmap(buf->page);
242}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200243EXPORT_SYMBOL(generic_pipe_buf_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jens Axboe08457182007-06-12 20:51:32 +0200245/**
246 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
247 * @pipe: the pipe that the buffer belongs to
248 * @buf: the buffer that should be unmapped
249 * @map_data: the data that the mapping function returned
250 *
251 * Description:
252 * This function undoes the mapping that ->map() provided.
253 */
Jens Axboef84d7512006-05-01 19:59:03 +0200254void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200255 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Jens Axboef6762b72006-05-01 20:02:05 +0200257 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
258 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800259 kunmap_atomic(map_data);
Jens Axboef6762b72006-05-01 20:02:05 +0200260 } else
261 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200263EXPORT_SYMBOL(generic_pipe_buf_unmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Jens Axboe08457182007-06-12 20:51:32 +0200265/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800266 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200267 * @pipe: the pipe that the buffer belongs to
268 * @buf: the buffer to attempt to steal
269 *
270 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800271 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200272 * @buf. If successful, this function returns 0 and returns with
273 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800274 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200275 * page cache.
276 */
Jens Axboe330ab712006-05-02 15:29:57 +0200277int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
278 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200279{
Jens Axboe46e678c2006-04-30 16:36:32 +0200280 struct page *page = buf->page;
281
Jens Axboe08457182007-06-12 20:51:32 +0200282 /*
283 * A reference of one is golden, that means that the owner of this
284 * page is the only one holding a reference to it. lock the page
285 * and return OK.
286 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200287 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200288 lock_page(page);
289 return 0;
290 }
291
292 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200293}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200294EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200295
Jens Axboe08457182007-06-12 20:51:32 +0200296/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800297 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200298 * @pipe: the pipe that the buffer belongs to
299 * @buf: the buffer to get a reference to
300 *
301 * Description:
302 * This function grabs an extra reference to @buf. It's used in
303 * in the tee() system call, when we duplicate the buffers in one
304 * pipe into another.
305 */
306void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200307{
308 page_cache_get(buf->page);
309}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200310EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200311
Jens Axboe08457182007-06-12 20:51:32 +0200312/**
313 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200314 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200315 * @buf: the buffer to confirm
316 *
317 * Description:
318 * This function does nothing, because the generic pipe code uses
319 * pages that are always good when inserted into the pipe.
320 */
Jens Axboecac36bb2007-06-14 13:10:48 +0200321int generic_pipe_buf_confirm(struct pipe_inode_info *info,
322 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200323{
324 return 0;
325}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200326EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200327
Miklos Szeredi68181732009-05-07 15:37:36 +0200328/**
329 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
330 * @pipe: the pipe that the buffer belongs to
331 * @buf: the buffer to put a reference to
332 *
333 * Description:
334 * This function releases a reference to @buf.
335 */
336void generic_pipe_buf_release(struct pipe_inode_info *pipe,
337 struct pipe_buffer *buf)
338{
339 page_cache_release(buf->page);
340}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200341EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200342
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800343static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200345 .map = generic_pipe_buf_map,
346 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb2007-06-14 13:10:48 +0200347 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200349 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200350 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351};
352
Linus Torvalds98830352012-04-29 13:12:42 -0700353static const struct pipe_buf_operations packet_pipe_buf_ops = {
354 .can_merge = 0,
355 .map = generic_pipe_buf_map,
356 .unmap = generic_pipe_buf_unmap,
357 .confirm = generic_pipe_buf_confirm,
358 .release = anon_pipe_buf_release,
359 .steal = generic_pipe_buf_steal,
360 .get = generic_pipe_buf_get,
361};
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700364pipe_read(struct kiocb *iocb, const struct iovec *_iov,
365 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700367 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800368 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200369 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int do_wakeup;
371 ssize_t ret;
372 struct iovec *iov = (struct iovec *)_iov;
373 size_t total_len;
374
375 total_len = iov_length(iov, nr_segs);
376 /* Null read succeeds. */
377 if (unlikely(total_len == 0))
378 return 0;
379
380 do_wakeup = 0;
381 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200382 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200383 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200385 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200387 int curbuf = pipe->curbuf;
388 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800389 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 void *addr;
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100391 size_t chars = buf->len, remaining;
Jens Axboef6762b72006-05-01 20:02:05 +0200392 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (chars > total_len)
395 chars = total_len;
396
Jens Axboecac36bb2007-06-14 13:10:48 +0200397 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200398 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200399 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200400 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200401 break;
402 }
Jens Axboef84d7512006-05-01 19:59:03 +0200403
Jens Axboef6762b72006-05-01 20:02:05 +0200404 atomic = !iov_fault_in_pages_write(iov, chars);
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100405 remaining = chars;
Jens Axboef6762b72006-05-01 20:02:05 +0200406redo:
407 addr = ops->map(pipe, buf, atomic);
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100408 error = pipe_iov_copy_to_user(iov, addr, &buf->offset,
409 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200410 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200412 /*
413 * Just retry with the slow path if we failed.
414 */
415 if (atomic) {
416 atomic = 0;
417 goto redo;
418 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200419 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200420 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422 }
423 ret += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700425
426 /* Was it a packet buffer? Clean up and exit */
427 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
428 total_len = chars;
429 buf->len = 0;
430 }
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!buf->len) {
433 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200434 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200435 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200436 pipe->curbuf = curbuf;
437 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 do_wakeup = 1;
439 }
440 total_len -= chars;
441 if (!total_len)
442 break; /* common path: read succeeded */
443 }
444 if (bufs) /* More to do? */
445 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200446 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200448 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* syscall merging: Usually we must not sleep
450 * if O_NONBLOCK is set, or if we got some data.
451 * But if a writer sleeps in kernel space, then
452 * we can wait for that data without violating POSIX.
453 */
454 if (ret)
455 break;
456 if (filp->f_flags & O_NONBLOCK) {
457 ret = -EAGAIN;
458 break;
459 }
460 }
461 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200462 if (!ret)
463 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465 }
466 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800467 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200468 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200470 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200472 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200473
474 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800476 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200477 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 if (ret > 0)
480 file_accessed(filp);
481 return ret;
482}
483
Linus Torvalds98830352012-04-29 13:12:42 -0700484static inline int is_packetized(struct file *file)
485{
486 return (file->f_flags & O_DIRECT) != 0;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700490pipe_write(struct kiocb *iocb, const struct iovec *_iov,
491 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700493 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800494 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200495 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 ssize_t ret;
497 int do_wakeup;
498 struct iovec *iov = (struct iovec *)_iov;
499 size_t total_len;
500 ssize_t chars;
501
502 total_len = iov_length(iov, nr_segs);
503 /* Null write succeeds. */
504 if (unlikely(total_len == 0))
505 return 0;
506
507 do_wakeup = 0;
508 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200509 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200510 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Ingo Molnar923f4f22006-04-11 13:53:33 +0200512 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 send_sig(SIGPIPE, current, 0);
514 ret = -EPIPE;
515 goto out;
516 }
517
518 /* We try to merge small writes */
519 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200520 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200521 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200522 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200523 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800524 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200528 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200529 void *addr;
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100530 size_t remaining = chars;
Jens Axboe5274f052006-03-30 15:15:30 +0200531
Jens Axboecac36bb2007-06-14 13:10:48 +0200532 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200533 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200534 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200535
Jens Axboef6762b72006-05-01 20:02:05 +0200536 iov_fault_in_pages_read(iov, chars);
537redo1:
538 addr = ops->map(pipe, buf, atomic);
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100539 error = pipe_iov_copy_from_user(addr, &offset, iov,
540 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200541 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ret = error;
543 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200544 if (error) {
545 if (atomic) {
546 atomic = 0;
547 goto redo1;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 buf->len += chars;
552 total_len -= chars;
553 ret = chars;
554 if (!total_len)
555 goto out;
556 }
557 }
558
559 for (;;) {
560 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200561
Ingo Molnar923f4f22006-04-11 13:53:33 +0200562 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200564 if (!ret)
565 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200568 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200569 if (bufs < pipe->buffers) {
570 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200571 struct pipe_buffer *buf = pipe->bufs + newbuf;
572 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200573 char *src;
574 int error, atomic = 1;
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100575 int offset = 0;
576 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (!page) {
579 page = alloc_page(GFP_HIGHUSER);
580 if (unlikely(!page)) {
581 ret = ret ? : -ENOMEM;
582 break;
583 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200584 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200586 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * we lock up (O_NONBLOCK-)readers that sleep due to
588 * syscall merging.
589 * FIXME! Is this really true?
590 */
591 do_wakeup = 1;
592 chars = PAGE_SIZE;
593 if (chars > total_len)
594 chars = total_len;
595
Jens Axboef6762b72006-05-01 20:02:05 +0200596 iov_fault_in_pages_read(iov, chars);
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100597 remaining = chars;
Jens Axboef6762b72006-05-01 20:02:05 +0200598redo2:
599 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800600 src = kmap_atomic(page);
Jens Axboef6762b72006-05-01 20:02:05 +0200601 else
602 src = kmap(page);
603
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100604 error = pipe_iov_copy_from_user(src, &offset, iov,
605 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200606 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800607 kunmap_atomic(src);
Jens Axboef6762b72006-05-01 20:02:05 +0200608 else
609 kunmap(page);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200612 if (atomic) {
613 atomic = 0;
614 goto redo2;
615 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200616 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200617 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
619 }
620 ret += chars;
621
622 /* Insert it into the buffer array */
623 buf->page = page;
624 buf->ops = &anon_pipe_buf_ops;
625 buf->offset = 0;
626 buf->len = chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700627 buf->flags = 0;
628 if (is_packetized(filp)) {
629 buf->ops = &packet_pipe_buf_ops;
630 buf->flags = PIPE_BUF_FLAG_PACKET;
631 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200632 pipe->nrbufs = ++bufs;
633 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 total_len -= chars;
636 if (!total_len)
637 break;
638 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200639 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 continue;
641 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200642 if (!ret)
643 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645 }
646 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200647 if (!ret)
648 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 break;
650 }
651 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800652 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200653 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 do_wakeup = 0;
655 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200656 pipe->waiting_writers++;
657 pipe_wait(pipe);
658 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200661 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800663 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200664 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800667 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return ret;
669}
670
671static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
673{
674 return -EBADF;
675}
676
677static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200678bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
679 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 return -EBADF;
682}
683
Andi Kleend59d0b12008-02-08 04:21:23 -0800684static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800686 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200687 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int count, buf, nrbufs;
689
690 switch (cmd) {
691 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200692 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200693 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200695 buf = pipe->curbuf;
696 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200698 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200699 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200701 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return put_user(count, (int __user *)arg);
704 default:
705 return -EINVAL;
706 }
707}
708
709/* No kernel lock held - fine */
710static unsigned int
711pipe_poll(struct file *filp, poll_table *wait)
712{
713 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800714 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200715 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int nrbufs;
717
Ingo Molnar923f4f22006-04-11 13:53:33 +0200718 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200721 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 mask = 0;
723 if (filp->f_mode & FMODE_READ) {
724 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200725 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mask |= POLLHUP;
727 }
728
729 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200730 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700731 /*
732 * Most Unices do not set POLLERR for FIFOs but on Linux they
733 * behave exactly like pipes for poll().
734 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200735 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 mask |= POLLERR;
737 }
738
739 return mask;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742static int
743pipe_release(struct inode *inode, int decr, int decw)
744{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200745 struct pipe_inode_info *pipe;
746
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200747 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200748 pipe = inode->i_pipe;
749 pipe->readers -= decr;
750 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200751
Ingo Molnar923f4f22006-04-11 13:53:33 +0200752 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 free_pipe_info(inode);
754 } else {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800755 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200756 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
757 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200759 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 return 0;
762}
763
764static int
765pipe_read_fasync(int fd, struct file *filp, int on)
766{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800767 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int retval;
769
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200770 mutex_lock(&inode->i_mutex);
771 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
772 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700774 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777
778static int
779pipe_write_fasync(int fd, struct file *filp, int on)
780{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800781 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int retval;
783
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200784 mutex_lock(&inode->i_mutex);
785 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
786 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700788 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791
792static int
793pipe_rdwr_fasync(int fd, struct file *filp, int on)
794{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800795 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200796 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int retval;
798
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200799 mutex_lock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200800 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700801 if (retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200802 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700803 if (retval < 0) /* this can happen only if on == T */
804 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
805 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200806 mutex_unlock(&inode->i_mutex);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700807 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810
811static int
812pipe_read_release(struct inode *inode, struct file *filp)
813{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return pipe_release(inode, 1, 0);
815}
816
817static int
818pipe_write_release(struct inode *inode, struct file *filp)
819{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return pipe_release(inode, 0, 1);
821}
822
823static int
824pipe_rdwr_release(struct inode *inode, struct file *filp)
825{
826 int decr, decw;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 decr = (filp->f_mode & FMODE_READ) != 0;
829 decw = (filp->f_mode & FMODE_WRITE) != 0;
830 return pipe_release(inode, decr, decw);
831}
832
833static int
834pipe_read_open(struct inode *inode, struct file *filp)
835{
Earl Chewad396022009-10-19 15:55:41 -0700836 int ret = -ENOENT;
837
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200838 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700839
840 if (inode->i_pipe) {
841 ret = 0;
842 inode->i_pipe->readers++;
843 }
844
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200845 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Earl Chewad396022009-10-19 15:55:41 -0700847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static int
851pipe_write_open(struct inode *inode, struct file *filp)
852{
Earl Chewad396022009-10-19 15:55:41 -0700853 int ret = -ENOENT;
854
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200855 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700856
857 if (inode->i_pipe) {
858 ret = 0;
859 inode->i_pipe->writers++;
860 }
861
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200862 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Earl Chewad396022009-10-19 15:55:41 -0700864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867static int
868pipe_rdwr_open(struct inode *inode, struct file *filp)
869{
Earl Chewad396022009-10-19 15:55:41 -0700870 int ret = -ENOENT;
871
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200872 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700873
874 if (inode->i_pipe) {
875 ret = 0;
876 if (filp->f_mode & FMODE_READ)
877 inode->i_pipe->readers++;
878 if (filp->f_mode & FMODE_WRITE)
879 inode->i_pipe->writers++;
880 }
881
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200882 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Earl Chewad396022009-10-19 15:55:41 -0700884 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887/*
888 * The file_operations structs are not static because they
889 * are also used in linux/fs/fifo.c to do operations on FIFOs.
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200890 *
891 * Pipes reuse fifos' file_operations structs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200893const struct file_operations read_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700895 .read = do_sync_read,
896 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700898 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800899 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .open = pipe_read_open,
901 .release = pipe_read_release,
902 .fasync = pipe_read_fasync,
903};
904
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200905const struct file_operations write_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .llseek = no_llseek,
907 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700908 .write = do_sync_write,
909 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700910 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800911 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .open = pipe_write_open,
913 .release = pipe_write_release,
914 .fasync = pipe_write_fasync,
915};
916
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200917const struct file_operations rdwr_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700919 .read = do_sync_read,
920 .aio_read = pipe_read,
921 .write = do_sync_write,
922 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800924 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 .open = pipe_rdwr_open,
926 .release = pipe_rdwr_release,
927 .fasync = pipe_rdwr_fasync,
928};
929
Ingo Molnar3a326a22006-04-10 15:18:35 +0200930struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
931{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200932 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200933
Ingo Molnar923f4f22006-04-11 13:53:33 +0200934 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
935 if (pipe) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200936 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
937 if (pipe->bufs) {
938 init_waitqueue_head(&pipe->wait);
939 pipe->r_counter = pipe->w_counter = 1;
940 pipe->inode = inode;
941 pipe->buffers = PIPE_DEF_BUFFERS;
942 return pipe;
943 }
944 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200945 }
946
Jens Axboe35f3d142010-05-20 10:43:18 +0200947 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200948}
949
Ingo Molnar923f4f22006-04-11 13:53:33 +0200950void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Jens Axboe35f3d142010-05-20 10:43:18 +0200954 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200955 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200957 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200959 if (pipe->tmp_page)
960 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200961 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200962 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Jens Axboeb92ce552006-04-11 13:52:07 +0200965void free_pipe_info(struct inode *inode)
966{
967 __free_pipe_info(inode->i_pipe);
968 inode->i_pipe = NULL;
969}
970
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800971static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200972
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700973/*
974 * pipefs_dname() is called from d_path().
975 */
976static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
977{
978 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
979 dentry->d_inode->i_ino);
980}
981
Al Viro3ba13d12009-02-20 06:02:22 +0000982static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700983 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984};
985
986static struct inode * get_pipe_inode(void)
987{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200988 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200989 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (!inode)
992 goto fail_inode;
993
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400994 inode->i_ino = get_next_ino();
995
Ingo Molnar923f4f22006-04-11 13:53:33 +0200996 pipe = alloc_pipe_info(inode);
997 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200999 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +02001000
Ingo Molnar923f4f22006-04-11 13:53:33 +02001001 pipe->readers = pipe->writers = 1;
Denys Vlasenkod2d96482008-07-01 14:16:09 +02001002 inode->i_fop = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /*
1005 * Mark the inode dirty from the very beginning,
1006 * that way it will never be moved to the dirty
1007 * list because "mark_inode_dirty()" will think
1008 * that it already _is_ on the dirty list.
1009 */
1010 inode->i_state = I_DIRTY;
1011 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +11001012 inode->i_uid = current_fsuid();
1013 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +02001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return inode;
1017
1018fail_iput:
1019 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +02001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021fail_inode:
1022 return NULL;
1023}
1024
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001025struct file *create_write_pipe(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Andi Kleend6cbd282006-09-30 23:29:26 -07001027 int err;
1028 struct inode *inode;
1029 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +04001030 struct path path;
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001031 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Andi Kleend6cbd282006-09-30 23:29:26 -07001033 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 inode = get_pipe_inode();
1035 if (!inode)
Dave Hansen430e2852008-02-15 14:37:26 -08001036 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Andi Kleend6cbd282006-09-30 23:29:26 -07001038 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +11001039 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +04001040 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -07001041 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04001042 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +02001043
Al Viro2c48b9c2009-08-09 00:52:35 +04001044 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001045
1046 err = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04001047 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
Dave Hansen430e2852008-02-15 14:37:26 -08001048 if (!f)
1049 goto err_dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -07001050 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linus Torvalds98830352012-04-29 13:12:42 -07001052 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Andi Kleend6cbd282006-09-30 23:29:26 -07001053 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Andi Kleend6cbd282006-09-30 23:29:26 -07001055 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Hansen430e2852008-02-15 14:37:26 -08001057 err_dentry:
Al Viroed152432008-04-22 19:51:27 -04001058 free_pipe_info(inode);
Al Viro2c48b9c2009-08-09 00:52:35 +04001059 path_put(&path);
Al Viroed152432008-04-22 19:51:27 -04001060 return ERR_PTR(err);
1061
Andi Kleend6cbd282006-09-30 23:29:26 -07001062 err_inode:
1063 free_pipe_info(inode);
1064 iput(inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001065 err:
Andi Kleend6cbd282006-09-30 23:29:26 -07001066 return ERR_PTR(err);
1067}
1068
1069void free_write_pipe(struct file *f)
1070{
Al Viro5ccac882006-12-18 13:31:18 +00001071 free_pipe_info(f->f_dentry->d_inode);
Jan Blunckc8e7f442008-06-09 16:40:35 -07001072 path_put(&f->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001073 put_filp(f);
1074}
1075
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001076struct file *create_read_pipe(struct file *wrf, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001077{
Al Virod2314122009-08-09 01:01:37 +04001078 /* Grab pipe from the writer */
1079 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1080 &read_pipefifo_fops);
Andi Kleend6cbd282006-09-30 23:29:26 -07001081 if (!f)
1082 return ERR_PTR(-ENFILE);
1083
Jan Blunckc8e7f442008-06-09 16:40:35 -07001084 path_get(&wrf->f_path);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001085 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
Andi Kleend6cbd282006-09-30 23:29:26 -07001086
1087 return f;
1088}
1089
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001090int do_pipe_flags(int *fd, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001091{
1092 struct file *fw, *fr;
1093 int error;
1094 int fdw, fdr;
1095
Linus Torvalds98830352012-04-29 13:12:42 -07001096 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001097 return -EINVAL;
1098
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001099 fw = create_write_pipe(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001100 if (IS_ERR(fw))
1101 return PTR_ERR(fw);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001102 fr = create_read_pipe(fw, flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001103 error = PTR_ERR(fr);
1104 if (IS_ERR(fr))
1105 goto err_write_pipe;
1106
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001107 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001108 if (error < 0)
1109 goto err_read_pipe;
1110 fdr = error;
1111
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001112 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001113 if (error < 0)
1114 goto err_fdr;
1115 fdw = error;
1116
Al Viro157cf642008-12-14 04:57:47 -05001117 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001118 fd_install(fdr, fr);
1119 fd_install(fdw, fw);
1120 fd[0] = fdr;
1121 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124
Andi Kleend6cbd282006-09-30 23:29:26 -07001125 err_fdr:
1126 put_unused_fd(fdr);
1127 err_read_pipe:
Jan Blunckc8e7f442008-06-09 16:40:35 -07001128 path_put(&fr->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001129 put_filp(fr);
1130 err_write_pipe:
1131 free_write_pipe(fw);
1132 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001136 * sys_pipe() is the normal C calling standard for creating
1137 * a pipe. It's not the way Unix traditionally does this, though.
1138 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01001139SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001140{
1141 int fd[2];
1142 int error;
1143
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001144 error = do_pipe_flags(fd, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001145 if (!error) {
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001146 if (copy_to_user(fildes, fd, sizeof(fd))) {
1147 sys_close(fd[0]);
1148 sys_close(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001149 error = -EFAULT;
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001150 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001151 }
1152 return error;
1153}
1154
Heiko Carstens2b664212009-01-14 14:14:35 +01001155SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001156{
1157 return sys_pipe2(fildes, 0);
1158}
1159
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001160/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001161 * Allocate a new array of pipe buffers and copy the info over. Returns the
1162 * pipe size if successful, or return -ERROR on error.
1163 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001164static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001165{
1166 struct pipe_buffer *bufs;
1167
1168 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001169 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1170 * expect a lot of shrink+grow operations, just free and allocate
1171 * again like we would do for growing. If the pipe currently
1172 * contains more buffers than arg, then return busy.
1173 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001174 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001175 return -EBUSY;
1176
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001177 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001178 if (unlikely(!bufs))
1179 return -ENOMEM;
1180
1181 /*
1182 * The pipe array wraps around, so just start the new one at zero
1183 * and adjust the indexes.
1184 */
1185 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001186 unsigned int tail;
1187 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001188
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001189 tail = pipe->curbuf + pipe->nrbufs;
1190 if (tail < pipe->buffers)
1191 tail = 0;
1192 else
1193 tail &= (pipe->buffers - 1);
1194
1195 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001196 if (head)
1197 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1198 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001199 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001200 }
1201
1202 pipe->curbuf = 0;
1203 kfree(pipe->bufs);
1204 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001205 pipe->buffers = nr_pages;
1206 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001207}
1208
Jens Axboeff9da692010-06-03 14:54:39 +02001209/*
1210 * Currently we rely on the pipe array holding a power-of-2 number
1211 * of pages.
1212 */
1213static inline unsigned int round_pipe_size(unsigned int size)
1214{
1215 unsigned long nr_pages;
1216
1217 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1218 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1219}
1220
1221/*
1222 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1223 * will return an error.
1224 */
1225int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1226 size_t *lenp, loff_t *ppos)
1227{
1228 int ret;
1229
1230 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1231 if (ret < 0 || !write)
1232 return ret;
1233
1234 pipe_max_size = round_pipe_size(pipe_max_size);
1235 return ret;
1236}
1237
Linus Torvalds72083642010-11-28 16:27:19 -08001238/*
1239 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1240 * location, so checking ->i_pipe is not enough to verify that this is a
1241 * pipe.
1242 */
1243struct pipe_inode_info *get_pipe_info(struct file *file)
1244{
1245 struct inode *i = file->f_path.dentry->d_inode;
1246
1247 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1248}
1249
Jens Axboe35f3d142010-05-20 10:43:18 +02001250long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1251{
1252 struct pipe_inode_info *pipe;
1253 long ret;
1254
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001255 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001256 if (!pipe)
1257 return -EBADF;
1258
1259 mutex_lock(&pipe->inode->i_mutex);
1260
1261 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001262 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001263 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001264
Jens Axboeff9da692010-06-03 14:54:39 +02001265 size = round_pipe_size(arg);
1266 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001267
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001268 ret = -EINVAL;
1269 if (!nr_pages)
1270 goto out;
1271
Jens Axboeff9da692010-06-03 14:54:39 +02001272 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001273 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001274 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001275 }
Jens Axboeff9da692010-06-03 14:54:39 +02001276 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001277 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001278 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001279 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001280 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001281 break;
1282 default:
1283 ret = -EINVAL;
1284 break;
1285 }
1286
Julia Lawallcc967be2010-05-26 17:54:39 +02001287out:
Jens Axboe35f3d142010-05-20 10:43:18 +02001288 mutex_unlock(&pipe->inode->i_mutex);
1289 return ret;
1290}
1291
Nick Pigginff0c7d12011-01-07 17:49:50 +11001292static const struct super_operations pipefs_ops = {
1293 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001294 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001295};
1296
Jens Axboe35f3d142010-05-20 10:43:18 +02001297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 * pipefs should _never_ be mounted by userland - too much of security hassle,
1299 * no real gain from having the whole whorehouse mounted. So we don't need
1300 * any operations on the root directory. However, we need a non-trivial
1301 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1302 */
Al Viro51139ad2010-07-25 23:47:46 +04001303static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1304 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Al Viroc74a1cb2011-01-12 16:59:34 -05001306 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1307 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310static struct file_system_type pipe_fs_type = {
1311 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001312 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 .kill_sb = kill_anon_super,
1314};
1315
1316static int __init init_pipe_fs(void)
1317{
1318 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!err) {
1321 pipe_mnt = kern_mount(&pipe_fs_type);
1322 if (IS_ERR(pipe_mnt)) {
1323 err = PTR_ERR(pipe_mnt);
1324 unregister_filesystem(&pipe_fs_type);
1325 }
1326 }
1327 return err;
1328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330fs_initcall(init_pipe_fs);