blob: 1da3cc6dd4d02e99bdd5e43220739d3621e80a2b [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 Hutchings3e223c22015-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 Hutchings3e223c22015-06-16 22:11:06 +0100112 while (*remaining > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 while (!iov->iov_len)
114 iov++;
Ben Hutchings3e223c22015-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 Hutchings3e223c22015-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 Hutchings3e223c22015-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 Hutchings3e223c22015-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 Hutchings3e223c22015-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 Hutchings3e223c22015-06-16 22:11:06 +0100140 while (*remaining > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 while (!iov->iov_len)
142 iov++;
Ben Hutchings3e223c22015-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 Hutchings3e223c22015-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 Hutchings3e223c22015-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 Hutchings3e223c22015-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 Hutchings3e223c22015-06-16 22:11:06 +0100391 size_t chars = buf->len, remaining;
Jens Axboef6762b72006-05-01 20:02:05 +0200392 int error, atomic;
Ben Hutchings37bfabf2016-02-13 02:34:52 +0000393 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (chars > total_len)
396 chars = total_len;
397
Jens Axboecac36bb2007-06-14 13:10:48 +0200398 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200399 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200400 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200401 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200402 break;
403 }
Jens Axboef84d7512006-05-01 19:59:03 +0200404
Jens Axboef6762b72006-05-01 20:02:05 +0200405 atomic = !iov_fault_in_pages_write(iov, chars);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100406 remaining = chars;
Ben Hutchings37bfabf2016-02-13 02:34:52 +0000407 offset = buf->offset;
Jens Axboef6762b72006-05-01 20:02:05 +0200408redo:
409 addr = ops->map(pipe, buf, atomic);
Ben Hutchings37bfabf2016-02-13 02:34:52 +0000410 error = pipe_iov_copy_to_user(iov, addr, &offset,
Ben Hutchings3e223c22015-06-16 22:11:06 +0100411 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200412 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200414 /*
415 * Just retry with the slow path if we failed.
416 */
417 if (atomic) {
418 atomic = 0;
419 goto redo;
420 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200421 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200422 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
424 }
425 ret += chars;
Ben Hutchings37bfabf2016-02-13 02:34:52 +0000426 buf->offset += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700428
429 /* Was it a packet buffer? Clean up and exit */
430 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
431 total_len = chars;
432 buf->len = 0;
433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!buf->len) {
436 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200437 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200438 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200439 pipe->curbuf = curbuf;
440 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 do_wakeup = 1;
442 }
443 total_len -= chars;
444 if (!total_len)
445 break; /* common path: read succeeded */
446 }
447 if (bufs) /* More to do? */
448 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200449 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200451 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* syscall merging: Usually we must not sleep
453 * if O_NONBLOCK is set, or if we got some data.
454 * But if a writer sleeps in kernel space, then
455 * we can wait for that data without violating POSIX.
456 */
457 if (ret)
458 break;
459 if (filp->f_flags & O_NONBLOCK) {
460 ret = -EAGAIN;
461 break;
462 }
463 }
464 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200465 if (!ret)
466 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 }
469 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800470 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200471 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200473 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200475 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200476
477 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800479 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200480 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 if (ret > 0)
483 file_accessed(filp);
484 return ret;
485}
486
Linus Torvalds98830352012-04-29 13:12:42 -0700487static inline int is_packetized(struct file *file)
488{
489 return (file->f_flags & O_DIRECT) != 0;
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700493pipe_write(struct kiocb *iocb, const struct iovec *_iov,
494 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700496 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800497 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200498 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 ssize_t ret;
500 int do_wakeup;
501 struct iovec *iov = (struct iovec *)_iov;
502 size_t total_len;
503 ssize_t chars;
504
505 total_len = iov_length(iov, nr_segs);
506 /* Null write succeeds. */
507 if (unlikely(total_len == 0))
508 return 0;
509
510 do_wakeup = 0;
511 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200512 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200513 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ingo Molnar923f4f22006-04-11 13:53:33 +0200515 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 send_sig(SIGPIPE, current, 0);
517 ret = -EPIPE;
518 goto out;
519 }
520
521 /* We try to merge small writes */
522 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200523 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200524 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200525 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200526 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800527 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200531 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200532 void *addr;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100533 size_t remaining = chars;
Jens Axboe5274f052006-03-30 15:15:30 +0200534
Jens Axboecac36bb2007-06-14 13:10:48 +0200535 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200536 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200537 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200538
Jens Axboef6762b72006-05-01 20:02:05 +0200539 iov_fault_in_pages_read(iov, chars);
540redo1:
541 addr = ops->map(pipe, buf, atomic);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100542 error = pipe_iov_copy_from_user(addr, &offset, iov,
543 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200544 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 ret = error;
546 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200547 if (error) {
548 if (atomic) {
549 atomic = 0;
550 goto redo1;
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 buf->len += chars;
555 total_len -= chars;
556 ret = chars;
557 if (!total_len)
558 goto out;
559 }
560 }
561
562 for (;;) {
563 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200564
Ingo Molnar923f4f22006-04-11 13:53:33 +0200565 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200567 if (!ret)
568 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200571 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200572 if (bufs < pipe->buffers) {
573 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200574 struct pipe_buffer *buf = pipe->bufs + newbuf;
575 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200576 char *src;
577 int error, atomic = 1;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100578 int offset = 0;
579 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (!page) {
582 page = alloc_page(GFP_HIGHUSER);
583 if (unlikely(!page)) {
584 ret = ret ? : -ENOMEM;
585 break;
586 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200587 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200589 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * we lock up (O_NONBLOCK-)readers that sleep due to
591 * syscall merging.
592 * FIXME! Is this really true?
593 */
594 do_wakeup = 1;
595 chars = PAGE_SIZE;
596 if (chars > total_len)
597 chars = total_len;
598
Jens Axboef6762b72006-05-01 20:02:05 +0200599 iov_fault_in_pages_read(iov, chars);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100600 remaining = chars;
Jens Axboef6762b72006-05-01 20:02:05 +0200601redo2:
602 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800603 src = kmap_atomic(page);
Jens Axboef6762b72006-05-01 20:02:05 +0200604 else
605 src = kmap(page);
606
Ben Hutchings3e223c22015-06-16 22:11:06 +0100607 error = pipe_iov_copy_from_user(src, &offset, iov,
608 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200609 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800610 kunmap_atomic(src);
Jens Axboef6762b72006-05-01 20:02:05 +0200611 else
612 kunmap(page);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200615 if (atomic) {
616 atomic = 0;
617 goto redo2;
618 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200619 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200620 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
622 }
623 ret += chars;
624
625 /* Insert it into the buffer array */
626 buf->page = page;
627 buf->ops = &anon_pipe_buf_ops;
628 buf->offset = 0;
629 buf->len = chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700630 buf->flags = 0;
631 if (is_packetized(filp)) {
632 buf->ops = &packet_pipe_buf_ops;
633 buf->flags = PIPE_BUF_FLAG_PACKET;
634 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200635 pipe->nrbufs = ++bufs;
636 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 total_len -= chars;
639 if (!total_len)
640 break;
641 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200642 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 continue;
644 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200645 if (!ret)
646 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
648 }
649 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200650 if (!ret)
651 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 break;
653 }
654 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800655 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200656 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 do_wakeup = 0;
658 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200659 pipe->waiting_writers++;
660 pipe_wait(pipe);
661 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200664 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800666 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200667 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800670 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return ret;
672}
673
674static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
676{
677 return -EBADF;
678}
679
680static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200681bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
682 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 return -EBADF;
685}
686
Andi Kleend59d0b12008-02-08 04:21:23 -0800687static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800689 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200690 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int count, buf, nrbufs;
692
693 switch (cmd) {
694 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200695 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200696 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200698 buf = pipe->curbuf;
699 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200701 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200702 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200704 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return put_user(count, (int __user *)arg);
707 default:
708 return -EINVAL;
709 }
710}
711
712/* No kernel lock held - fine */
713static unsigned int
714pipe_poll(struct file *filp, poll_table *wait)
715{
716 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800717 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200718 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int nrbufs;
720
Ingo Molnar923f4f22006-04-11 13:53:33 +0200721 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200724 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 mask = 0;
726 if (filp->f_mode & FMODE_READ) {
727 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200728 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mask |= POLLHUP;
730 }
731
732 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200733 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700734 /*
735 * Most Unices do not set POLLERR for FIFOs but on Linux they
736 * behave exactly like pipes for poll().
737 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200738 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 mask |= POLLERR;
740 }
741
742 return mask;
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static int
746pipe_release(struct inode *inode, int decr, int decw)
747{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200748 struct pipe_inode_info *pipe;
749
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200750 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200751 pipe = inode->i_pipe;
752 pipe->readers -= decr;
753 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200754
Ingo Molnar923f4f22006-04-11 13:53:33 +0200755 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 free_pipe_info(inode);
757 } else {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800758 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200759 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
760 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200762 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return 0;
765}
766
767static int
768pipe_read_fasync(int fd, struct file *filp, int on)
769{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800770 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int retval;
772
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200773 mutex_lock(&inode->i_mutex);
774 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
775 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700777 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780
781static int
782pipe_write_fasync(int fd, struct file *filp, int on)
783{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800784 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int retval;
786
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200787 mutex_lock(&inode->i_mutex);
788 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
789 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700791 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794
795static int
796pipe_rdwr_fasync(int fd, struct file *filp, int on)
797{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800798 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200799 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int retval;
801
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200802 mutex_lock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200803 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700804 if (retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200805 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700806 if (retval < 0) /* this can happen only if on == T */
807 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
808 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200809 mutex_unlock(&inode->i_mutex);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700810 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813
814static int
815pipe_read_release(struct inode *inode, struct file *filp)
816{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return pipe_release(inode, 1, 0);
818}
819
820static int
821pipe_write_release(struct inode *inode, struct file *filp)
822{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return pipe_release(inode, 0, 1);
824}
825
826static int
827pipe_rdwr_release(struct inode *inode, struct file *filp)
828{
829 int decr, decw;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 decr = (filp->f_mode & FMODE_READ) != 0;
832 decw = (filp->f_mode & FMODE_WRITE) != 0;
833 return pipe_release(inode, decr, decw);
834}
835
836static int
837pipe_read_open(struct inode *inode, struct file *filp)
838{
Earl Chewad396022009-10-19 15:55:41 -0700839 int ret = -ENOENT;
840
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200841 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700842
843 if (inode->i_pipe) {
844 ret = 0;
845 inode->i_pipe->readers++;
846 }
847
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200848 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Earl Chewad396022009-10-19 15:55:41 -0700850 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853static int
854pipe_write_open(struct inode *inode, struct file *filp)
855{
Earl Chewad396022009-10-19 15:55:41 -0700856 int ret = -ENOENT;
857
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200858 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700859
860 if (inode->i_pipe) {
861 ret = 0;
862 inode->i_pipe->writers++;
863 }
864
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200865 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Earl Chewad396022009-10-19 15:55:41 -0700867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870static int
871pipe_rdwr_open(struct inode *inode, struct file *filp)
872{
Earl Chewad396022009-10-19 15:55:41 -0700873 int ret = -ENOENT;
874
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200875 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700876
877 if (inode->i_pipe) {
878 ret = 0;
879 if (filp->f_mode & FMODE_READ)
880 inode->i_pipe->readers++;
881 if (filp->f_mode & FMODE_WRITE)
882 inode->i_pipe->writers++;
883 }
884
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200885 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Earl Chewad396022009-10-19 15:55:41 -0700887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890/*
891 * The file_operations structs are not static because they
892 * are also used in linux/fs/fifo.c to do operations on FIFOs.
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200893 *
894 * Pipes reuse fifos' file_operations structs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200896const struct file_operations read_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700898 .read = do_sync_read,
899 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700901 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800902 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .open = pipe_read_open,
904 .release = pipe_read_release,
905 .fasync = pipe_read_fasync,
906};
907
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200908const struct file_operations write_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 .llseek = no_llseek,
910 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700911 .write = do_sync_write,
912 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700913 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800914 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 .open = pipe_write_open,
916 .release = pipe_write_release,
917 .fasync = pipe_write_fasync,
918};
919
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200920const struct file_operations rdwr_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700922 .read = do_sync_read,
923 .aio_read = pipe_read,
924 .write = do_sync_write,
925 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800927 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 .open = pipe_rdwr_open,
929 .release = pipe_rdwr_release,
930 .fasync = pipe_rdwr_fasync,
931};
932
Ingo Molnar3a326a22006-04-10 15:18:35 +0200933struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
934{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200935 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200936
Ingo Molnar923f4f22006-04-11 13:53:33 +0200937 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
938 if (pipe) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200939 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
940 if (pipe->bufs) {
941 init_waitqueue_head(&pipe->wait);
942 pipe->r_counter = pipe->w_counter = 1;
943 pipe->inode = inode;
944 pipe->buffers = PIPE_DEF_BUFFERS;
945 return pipe;
946 }
947 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200948 }
949
Jens Axboe35f3d142010-05-20 10:43:18 +0200950 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200951}
952
Ingo Molnar923f4f22006-04-11 13:53:33 +0200953void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Jens Axboe35f3d142010-05-20 10:43:18 +0200957 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200958 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200960 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200962 if (pipe->tmp_page)
963 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200964 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200965 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Jens Axboeb92ce552006-04-11 13:52:07 +0200968void free_pipe_info(struct inode *inode)
969{
970 __free_pipe_info(inode->i_pipe);
971 inode->i_pipe = NULL;
972}
973
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800974static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200975
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700976/*
977 * pipefs_dname() is called from d_path().
978 */
979static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
980{
981 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
982 dentry->d_inode->i_ino);
983}
984
Al Viro3ba13d12009-02-20 06:02:22 +0000985static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700986 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987};
988
989static struct inode * get_pipe_inode(void)
990{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200991 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200992 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (!inode)
995 goto fail_inode;
996
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400997 inode->i_ino = get_next_ino();
998
Ingo Molnar923f4f22006-04-11 13:53:33 +0200999 pipe = alloc_pipe_info(inode);
1000 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +02001002 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +02001003
Ingo Molnar923f4f22006-04-11 13:53:33 +02001004 pipe->readers = pipe->writers = 1;
Denys Vlasenkod2d96482008-07-01 14:16:09 +02001005 inode->i_fop = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
1008 * Mark the inode dirty from the very beginning,
1009 * that way it will never be moved to the dirty
1010 * list because "mark_inode_dirty()" will think
1011 * that it already _is_ on the dirty list.
1012 */
1013 inode->i_state = I_DIRTY;
1014 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +11001015 inode->i_uid = current_fsuid();
1016 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +02001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return inode;
1020
1021fail_iput:
1022 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +02001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024fail_inode:
1025 return NULL;
1026}
1027
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001028struct file *create_write_pipe(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Andi Kleend6cbd282006-09-30 23:29:26 -07001030 int err;
1031 struct inode *inode;
1032 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +04001033 struct path path;
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001034 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Andi Kleend6cbd282006-09-30 23:29:26 -07001036 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 inode = get_pipe_inode();
1038 if (!inode)
Dave Hansen430e2852008-02-15 14:37:26 -08001039 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Andi Kleend6cbd282006-09-30 23:29:26 -07001041 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +11001042 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +04001043 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -07001044 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04001045 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +02001046
Al Viro2c48b9c2009-08-09 00:52:35 +04001047 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001048
1049 err = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04001050 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
Dave Hansen430e2852008-02-15 14:37:26 -08001051 if (!f)
1052 goto err_dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -07001053 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Linus Torvalds98830352012-04-29 13:12:42 -07001055 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Andi Kleend6cbd282006-09-30 23:29:26 -07001056 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Andi Kleend6cbd282006-09-30 23:29:26 -07001058 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Dave Hansen430e2852008-02-15 14:37:26 -08001060 err_dentry:
Al Viroed152432008-04-22 19:51:27 -04001061 free_pipe_info(inode);
Al Viro2c48b9c2009-08-09 00:52:35 +04001062 path_put(&path);
Al Viroed152432008-04-22 19:51:27 -04001063 return ERR_PTR(err);
1064
Andi Kleend6cbd282006-09-30 23:29:26 -07001065 err_inode:
1066 free_pipe_info(inode);
1067 iput(inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001068 err:
Andi Kleend6cbd282006-09-30 23:29:26 -07001069 return ERR_PTR(err);
1070}
1071
1072void free_write_pipe(struct file *f)
1073{
Al Viro5ccac882006-12-18 13:31:18 +00001074 free_pipe_info(f->f_dentry->d_inode);
Jan Blunckc8e7f442008-06-09 16:40:35 -07001075 path_put(&f->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001076 put_filp(f);
1077}
1078
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001079struct file *create_read_pipe(struct file *wrf, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001080{
Al Virod2314122009-08-09 01:01:37 +04001081 /* Grab pipe from the writer */
1082 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1083 &read_pipefifo_fops);
Andi Kleend6cbd282006-09-30 23:29:26 -07001084 if (!f)
1085 return ERR_PTR(-ENFILE);
1086
Jan Blunckc8e7f442008-06-09 16:40:35 -07001087 path_get(&wrf->f_path);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001088 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
Andi Kleend6cbd282006-09-30 23:29:26 -07001089
1090 return f;
1091}
1092
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001093int do_pipe_flags(int *fd, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001094{
1095 struct file *fw, *fr;
1096 int error;
1097 int fdw, fdr;
1098
Linus Torvalds98830352012-04-29 13:12:42 -07001099 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001100 return -EINVAL;
1101
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001102 fw = create_write_pipe(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001103 if (IS_ERR(fw))
1104 return PTR_ERR(fw);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001105 fr = create_read_pipe(fw, flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001106 error = PTR_ERR(fr);
1107 if (IS_ERR(fr))
1108 goto err_write_pipe;
1109
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001110 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001111 if (error < 0)
1112 goto err_read_pipe;
1113 fdr = error;
1114
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001115 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001116 if (error < 0)
1117 goto err_fdr;
1118 fdw = error;
1119
Al Viro157cf642008-12-14 04:57:47 -05001120 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001121 fd_install(fdr, fr);
1122 fd_install(fdw, fw);
1123 fd[0] = fdr;
1124 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return 0;
1127
Andi Kleend6cbd282006-09-30 23:29:26 -07001128 err_fdr:
1129 put_unused_fd(fdr);
1130 err_read_pipe:
Jan Blunckc8e7f442008-06-09 16:40:35 -07001131 path_put(&fr->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001132 put_filp(fr);
1133 err_write_pipe:
1134 free_write_pipe(fw);
1135 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001139 * sys_pipe() is the normal C calling standard for creating
1140 * a pipe. It's not the way Unix traditionally does this, though.
1141 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01001142SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001143{
1144 int fd[2];
1145 int error;
1146
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001147 error = do_pipe_flags(fd, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001148 if (!error) {
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001149 if (copy_to_user(fildes, fd, sizeof(fd))) {
1150 sys_close(fd[0]);
1151 sys_close(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001152 error = -EFAULT;
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001153 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001154 }
1155 return error;
1156}
1157
Heiko Carstens2b664212009-01-14 14:14:35 +01001158SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001159{
1160 return sys_pipe2(fildes, 0);
1161}
1162
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001163/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001164 * Allocate a new array of pipe buffers and copy the info over. Returns the
1165 * pipe size if successful, or return -ERROR on error.
1166 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001167static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001168{
1169 struct pipe_buffer *bufs;
1170
1171 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001172 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1173 * expect a lot of shrink+grow operations, just free and allocate
1174 * again like we would do for growing. If the pipe currently
1175 * contains more buffers than arg, then return busy.
1176 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001177 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001178 return -EBUSY;
1179
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001180 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001181 if (unlikely(!bufs))
1182 return -ENOMEM;
1183
1184 /*
1185 * The pipe array wraps around, so just start the new one at zero
1186 * and adjust the indexes.
1187 */
1188 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001189 unsigned int tail;
1190 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001191
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001192 tail = pipe->curbuf + pipe->nrbufs;
1193 if (tail < pipe->buffers)
1194 tail = 0;
1195 else
1196 tail &= (pipe->buffers - 1);
1197
1198 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001199 if (head)
1200 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1201 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001202 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001203 }
1204
1205 pipe->curbuf = 0;
1206 kfree(pipe->bufs);
1207 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001208 pipe->buffers = nr_pages;
1209 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001210}
1211
Jens Axboeff9da692010-06-03 14:54:39 +02001212/*
1213 * Currently we rely on the pipe array holding a power-of-2 number
1214 * of pages.
1215 */
1216static inline unsigned int round_pipe_size(unsigned int size)
1217{
1218 unsigned long nr_pages;
1219
1220 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1221 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1222}
1223
1224/*
1225 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1226 * will return an error.
1227 */
1228int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1229 size_t *lenp, loff_t *ppos)
1230{
1231 int ret;
1232
1233 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1234 if (ret < 0 || !write)
1235 return ret;
1236
1237 pipe_max_size = round_pipe_size(pipe_max_size);
1238 return ret;
1239}
1240
Linus Torvalds72083642010-11-28 16:27:19 -08001241/*
1242 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1243 * location, so checking ->i_pipe is not enough to verify that this is a
1244 * pipe.
1245 */
1246struct pipe_inode_info *get_pipe_info(struct file *file)
1247{
1248 struct inode *i = file->f_path.dentry->d_inode;
1249
1250 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1251}
1252
Jens Axboe35f3d142010-05-20 10:43:18 +02001253long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1254{
1255 struct pipe_inode_info *pipe;
1256 long ret;
1257
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001258 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001259 if (!pipe)
1260 return -EBADF;
1261
1262 mutex_lock(&pipe->inode->i_mutex);
1263
1264 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001265 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001266 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001267
Jens Axboeff9da692010-06-03 14:54:39 +02001268 size = round_pipe_size(arg);
1269 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001270
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001271 ret = -EINVAL;
1272 if (!nr_pages)
1273 goto out;
1274
Jens Axboeff9da692010-06-03 14:54:39 +02001275 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001276 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001277 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001278 }
Jens Axboeff9da692010-06-03 14:54:39 +02001279 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001280 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001281 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001282 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001283 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001284 break;
1285 default:
1286 ret = -EINVAL;
1287 break;
1288 }
1289
Julia Lawallcc967be2010-05-26 17:54:39 +02001290out:
Jens Axboe35f3d142010-05-20 10:43:18 +02001291 mutex_unlock(&pipe->inode->i_mutex);
1292 return ret;
1293}
1294
Nick Pigginff0c7d12011-01-07 17:49:50 +11001295static const struct super_operations pipefs_ops = {
1296 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001297 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001298};
1299
Jens Axboe35f3d142010-05-20 10:43:18 +02001300/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * pipefs should _never_ be mounted by userland - too much of security hassle,
1302 * no real gain from having the whole whorehouse mounted. So we don't need
1303 * any operations on the root directory. However, we need a non-trivial
1304 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1305 */
Al Viro51139ad2010-07-25 23:47:46 +04001306static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1307 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Al Viroc74a1cb2011-01-12 16:59:34 -05001309 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1310 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313static struct file_system_type pipe_fs_type = {
1314 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001315 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 .kill_sb = kill_anon_super,
1317};
1318
1319static int __init init_pipe_fs(void)
1320{
1321 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!err) {
1324 pipe_mnt = kern_mount(&pipe_fs_type);
1325 if (IS_ERR(pipe_mnt)) {
1326 err = PTR_ERR(pipe_mnt);
1327 unregister_filesystem(&pipe_fs_type);
1328 }
1329 }
1330 return err;
1331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333fs_initcall(init_pipe_fs);