blob: edd1c636bf1eaf8709385669e5706db59db76911 [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
Willy Tarreau239bc432016-01-18 16:36:09 +010039/* Maximum allocatable pages per user. Hard limit is unset by default, soft
40 * matches default values.
41 */
42unsigned long pipe_user_pages_hard;
43unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
44
Jens Axboeb492e952010-05-19 21:03:16 +020045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * We use a start+len construction, which provides full use of the
47 * allocated memory.
48 * -- Florian Coosmann (FGC)
49 *
50 * Reads with count = 0 should always return 0.
51 * -- Julian Bradfield 1999-06-07.
52 *
53 * FIFOs and Pipes now generate SIGIO for both readers and writers.
54 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
55 *
56 * pipe_read & write cleanup
57 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
58 */
59
Miklos Szeredi61e0d472009-04-14 19:48:41 +020060static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
61{
62 if (pipe->inode)
63 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
64}
65
66void pipe_lock(struct pipe_inode_info *pipe)
67{
68 /*
69 * pipe_lock() nests non-pipe inode locks (for writing to a file)
70 */
71 pipe_lock_nested(pipe, I_MUTEX_PARENT);
72}
73EXPORT_SYMBOL(pipe_lock);
74
75void pipe_unlock(struct pipe_inode_info *pipe)
76{
77 if (pipe->inode)
78 mutex_unlock(&pipe->inode->i_mutex);
79}
80EXPORT_SYMBOL(pipe_unlock);
81
82void pipe_double_lock(struct pipe_inode_info *pipe1,
83 struct pipe_inode_info *pipe2)
84{
85 BUG_ON(pipe1 == pipe2);
86
87 if (pipe1 < pipe2) {
88 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
89 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
90 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +020091 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
92 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020093 }
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020097void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 DEFINE_WAIT(wait);
100
Ingo Molnard79fc0f2005-09-10 00:26:12 -0700101 /*
102 * Pipes are system-local resources, so sleeping on them
103 * is considered a noninteractive wait:
104 */
Mike Galbraithaf927232007-10-15 17:00:13 +0200105 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200106 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200108 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200109 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Arjan van de Ven858119e2006-01-14 13:20:43 -0800112static int
Ben Hutchings3e223c22015-06-16 22:11:06 +0100113pipe_iov_copy_from_user(void *addr, int *offset, struct iovec *iov,
114 size_t *remaining, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 unsigned long copy;
117
Ben Hutchings3e223c22015-06-16 22:11:06 +0100118 while (*remaining > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 while (!iov->iov_len)
120 iov++;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100121 copy = min_t(unsigned long, *remaining, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jens Axboef6762b72006-05-01 20:02:05 +0200123 if (atomic) {
Ben Hutchings3e223c22015-06-16 22:11:06 +0100124 if (__copy_from_user_inatomic(addr + *offset,
125 iov->iov_base, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200126 return -EFAULT;
127 } else {
Ben Hutchings3e223c22015-06-16 22:11:06 +0100128 if (copy_from_user(addr + *offset,
129 iov->iov_base, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200130 return -EFAULT;
131 }
Ben Hutchings3e223c22015-06-16 22:11:06 +0100132 *offset += copy;
133 *remaining -= copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 iov->iov_base += copy;
135 iov->iov_len -= copy;
136 }
137 return 0;
138}
139
Arjan van de Ven858119e2006-01-14 13:20:43 -0800140static int
Ben Hutchings3e223c22015-06-16 22:11:06 +0100141pipe_iov_copy_to_user(struct iovec *iov, void *addr, int *offset,
142 size_t *remaining, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 unsigned long copy;
145
Ben Hutchings3e223c22015-06-16 22:11:06 +0100146 while (*remaining > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 while (!iov->iov_len)
148 iov++;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100149 copy = min_t(unsigned long, *remaining, iov->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jens Axboef6762b72006-05-01 20:02:05 +0200151 if (atomic) {
Ben Hutchings3e223c22015-06-16 22:11:06 +0100152 if (__copy_to_user_inatomic(iov->iov_base,
153 addr + *offset, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200154 return -EFAULT;
155 } else {
Ben Hutchings3e223c22015-06-16 22:11:06 +0100156 if (copy_to_user(iov->iov_base,
157 addr + *offset, copy))
Jens Axboef6762b72006-05-01 20:02:05 +0200158 return -EFAULT;
159 }
Ben Hutchings3e223c22015-06-16 22:11:06 +0100160 *offset += copy;
161 *remaining -= copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 iov->iov_base += copy;
163 iov->iov_len -= copy;
164 }
165 return 0;
166}
167
Jens Axboef6762b72006-05-01 20:02:05 +0200168/*
169 * Attempt to pre-fault in the user memory, so we can use atomic copies.
170 * Returns the number of bytes not faulted in.
171 */
172static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
173{
174 while (!iov->iov_len)
175 iov++;
176
177 while (len > 0) {
178 unsigned long this_len;
179
180 this_len = min_t(unsigned long, len, iov->iov_len);
181 if (fault_in_pages_writeable(iov->iov_base, this_len))
182 break;
183
184 len -= this_len;
185 iov++;
186 }
187
188 return len;
189}
190
191/*
192 * Pre-fault in the user memory, so we can use atomic copies.
193 */
194static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
195{
196 while (!iov->iov_len)
197 iov++;
198
199 while (len > 0) {
200 unsigned long this_len;
201
202 this_len = min_t(unsigned long, len, iov->iov_len);
203 fault_in_pages_readable(iov->iov_base, this_len);
204 len -= this_len;
205 iov++;
206 }
207}
208
Ingo Molnar341b4462006-04-11 13:57:45 +0200209static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
210 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct page *page = buf->page;
213
Jens Axboe5274f052006-03-30 15:15:30 +0200214 /*
215 * If nobody else uses this page, and we don't already have a
216 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200217 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200218 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200219 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200220 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200221 else
222 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Jens Axboe08457182007-06-12 20:51:32 +0200225/**
226 * generic_pipe_buf_map - virtually map a pipe buffer
227 * @pipe: the pipe that the buffer belongs to
228 * @buf: the buffer that should be mapped
229 * @atomic: whether to use an atomic map
230 *
231 * Description:
232 * This function returns a kernel virtual address mapping for the
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800233 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
Jens Axboe08457182007-06-12 20:51:32 +0200234 * and the caller has to be careful not to fault before calling
235 * the unmap function.
236 *
237 * Note that this function occupies KM_USER0 if @atomic != 0.
238 */
Jens Axboef84d7512006-05-01 19:59:03 +0200239void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200240 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Jens Axboef6762b72006-05-01 20:02:05 +0200242 if (atomic) {
243 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800244 return kmap_atomic(buf->page);
Jens Axboef6762b72006-05-01 20:02:05 +0200245 }
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return kmap(buf->page);
248}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200249EXPORT_SYMBOL(generic_pipe_buf_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Jens Axboe08457182007-06-12 20:51:32 +0200251/**
252 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
253 * @pipe: the pipe that the buffer belongs to
254 * @buf: the buffer that should be unmapped
255 * @map_data: the data that the mapping function returned
256 *
257 * Description:
258 * This function undoes the mapping that ->map() provided.
259 */
Jens Axboef84d7512006-05-01 19:59:03 +0200260void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200261 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Jens Axboef6762b72006-05-01 20:02:05 +0200263 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
264 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800265 kunmap_atomic(map_data);
Jens Axboef6762b72006-05-01 20:02:05 +0200266 } else
267 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200269EXPORT_SYMBOL(generic_pipe_buf_unmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jens Axboe08457182007-06-12 20:51:32 +0200271/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800272 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200273 * @pipe: the pipe that the buffer belongs to
274 * @buf: the buffer to attempt to steal
275 *
276 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800277 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200278 * @buf. If successful, this function returns 0 and returns with
279 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800280 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200281 * page cache.
282 */
Jens Axboe330ab712006-05-02 15:29:57 +0200283int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
284 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200285{
Jens Axboe46e678c2006-04-30 16:36:32 +0200286 struct page *page = buf->page;
287
Jens Axboe08457182007-06-12 20:51:32 +0200288 /*
289 * A reference of one is golden, that means that the owner of this
290 * page is the only one holding a reference to it. lock the page
291 * and return OK.
292 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200293 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200294 lock_page(page);
295 return 0;
296 }
297
298 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200299}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200300EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200301
Jens Axboe08457182007-06-12 20:51:32 +0200302/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800303 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200304 * @pipe: the pipe that the buffer belongs to
305 * @buf: the buffer to get a reference to
306 *
307 * Description:
308 * This function grabs an extra reference to @buf. It's used in
309 * in the tee() system call, when we duplicate the buffers in one
310 * pipe into another.
311 */
312void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200313{
314 page_cache_get(buf->page);
315}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200316EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200317
Jens Axboe08457182007-06-12 20:51:32 +0200318/**
319 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200320 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200321 * @buf: the buffer to confirm
322 *
323 * Description:
324 * This function does nothing, because the generic pipe code uses
325 * pages that are always good when inserted into the pipe.
326 */
Jens Axboecac36bb2007-06-14 13:10:48 +0200327int generic_pipe_buf_confirm(struct pipe_inode_info *info,
328 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200329{
330 return 0;
331}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200332EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200333
Miklos Szeredi68181732009-05-07 15:37:36 +0200334/**
335 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
336 * @pipe: the pipe that the buffer belongs to
337 * @buf: the buffer to put a reference to
338 *
339 * Description:
340 * This function releases a reference to @buf.
341 */
342void generic_pipe_buf_release(struct pipe_inode_info *pipe,
343 struct pipe_buffer *buf)
344{
345 page_cache_release(buf->page);
346}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200347EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200348
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800349static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200351 .map = generic_pipe_buf_map,
352 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb2007-06-14 13:10:48 +0200353 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200355 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200356 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
Linus Torvalds98830352012-04-29 13:12:42 -0700359static const struct pipe_buf_operations packet_pipe_buf_ops = {
360 .can_merge = 0,
361 .map = generic_pipe_buf_map,
362 .unmap = generic_pipe_buf_unmap,
363 .confirm = generic_pipe_buf_confirm,
364 .release = anon_pipe_buf_release,
365 .steal = generic_pipe_buf_steal,
366 .get = generic_pipe_buf_get,
367};
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700370pipe_read(struct kiocb *iocb, const struct iovec *_iov,
371 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700373 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800374 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200375 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int do_wakeup;
377 ssize_t ret;
378 struct iovec *iov = (struct iovec *)_iov;
379 size_t total_len;
380
381 total_len = iov_length(iov, nr_segs);
382 /* Null read succeeds. */
383 if (unlikely(total_len == 0))
384 return 0;
385
386 do_wakeup = 0;
387 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200388 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200389 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200391 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200393 int curbuf = pipe->curbuf;
394 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800395 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 void *addr;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100397 size_t chars = buf->len, remaining;
Jens Axboef6762b72006-05-01 20:02:05 +0200398 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (chars > total_len)
401 chars = total_len;
402
Jens Axboecac36bb2007-06-14 13:10:48 +0200403 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200404 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200405 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200406 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200407 break;
408 }
Jens Axboef84d7512006-05-01 19:59:03 +0200409
Jens Axboef6762b72006-05-01 20:02:05 +0200410 atomic = !iov_fault_in_pages_write(iov, chars);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100411 remaining = chars;
Jens Axboef6762b72006-05-01 20:02:05 +0200412redo:
413 addr = ops->map(pipe, buf, atomic);
Ben Hutchings91ec8cc2015-06-16 22:11:06 +0100414 error = pipe_iov_copy_to_user(iov, addr, &buf->offset,
Ben Hutchings3e223c22015-06-16 22:11:06 +0100415 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200416 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200418 /*
419 * Just retry with the slow path if we failed.
420 */
421 if (atomic) {
422 atomic = 0;
423 goto redo;
424 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200425 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200426 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428 }
429 ret += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700431
432 /* Was it a packet buffer? Clean up and exit */
433 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
434 total_len = chars;
435 buf->len = 0;
436 }
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!buf->len) {
439 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200440 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200441 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200442 pipe->curbuf = curbuf;
443 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 do_wakeup = 1;
445 }
446 total_len -= chars;
447 if (!total_len)
448 break; /* common path: read succeeded */
449 }
450 if (bufs) /* More to do? */
451 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200452 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200454 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* syscall merging: Usually we must not sleep
456 * if O_NONBLOCK is set, or if we got some data.
457 * But if a writer sleeps in kernel space, then
458 * we can wait for that data without violating POSIX.
459 */
460 if (ret)
461 break;
462 if (filp->f_flags & O_NONBLOCK) {
463 ret = -EAGAIN;
464 break;
465 }
466 }
467 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200468 if (!ret)
469 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 }
472 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800473 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200474 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200476 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200478 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200479
480 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800482 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200483 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 if (ret > 0)
486 file_accessed(filp);
487 return ret;
488}
489
Linus Torvalds98830352012-04-29 13:12:42 -0700490static inline int is_packetized(struct file *file)
491{
492 return (file->f_flags & O_DIRECT) != 0;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700496pipe_write(struct kiocb *iocb, const struct iovec *_iov,
497 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700499 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800500 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200501 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 ssize_t ret;
503 int do_wakeup;
504 struct iovec *iov = (struct iovec *)_iov;
505 size_t total_len;
506 ssize_t chars;
507
508 total_len = iov_length(iov, nr_segs);
509 /* Null write succeeds. */
510 if (unlikely(total_len == 0))
511 return 0;
512
513 do_wakeup = 0;
514 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200515 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200516 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Ingo Molnar923f4f22006-04-11 13:53:33 +0200518 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 send_sig(SIGPIPE, current, 0);
520 ret = -EPIPE;
521 goto out;
522 }
523
524 /* We try to merge small writes */
525 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200526 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200527 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200528 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200529 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800530 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200534 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200535 void *addr;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100536 size_t remaining = chars;
Jens Axboe5274f052006-03-30 15:15:30 +0200537
Jens Axboecac36bb2007-06-14 13:10:48 +0200538 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200539 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200540 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200541
Jens Axboef6762b72006-05-01 20:02:05 +0200542 iov_fault_in_pages_read(iov, chars);
543redo1:
544 addr = ops->map(pipe, buf, atomic);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100545 error = pipe_iov_copy_from_user(addr, &offset, iov,
546 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200547 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ret = error;
549 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200550 if (error) {
551 if (atomic) {
552 atomic = 0;
553 goto redo1;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 buf->len += chars;
558 total_len -= chars;
559 ret = chars;
560 if (!total_len)
561 goto out;
562 }
563 }
564
565 for (;;) {
566 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200567
Ingo Molnar923f4f22006-04-11 13:53:33 +0200568 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200570 if (!ret)
571 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200574 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200575 if (bufs < pipe->buffers) {
576 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200577 struct pipe_buffer *buf = pipe->bufs + newbuf;
578 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200579 char *src;
580 int error, atomic = 1;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100581 int offset = 0;
582 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (!page) {
585 page = alloc_page(GFP_HIGHUSER);
586 if (unlikely(!page)) {
587 ret = ret ? : -ENOMEM;
588 break;
589 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200590 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200592 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * we lock up (O_NONBLOCK-)readers that sleep due to
594 * syscall merging.
595 * FIXME! Is this really true?
596 */
597 do_wakeup = 1;
598 chars = PAGE_SIZE;
599 if (chars > total_len)
600 chars = total_len;
601
Jens Axboef6762b72006-05-01 20:02:05 +0200602 iov_fault_in_pages_read(iov, chars);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100603 remaining = chars;
Jens Axboef6762b72006-05-01 20:02:05 +0200604redo2:
605 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800606 src = kmap_atomic(page);
Jens Axboef6762b72006-05-01 20:02:05 +0200607 else
608 src = kmap(page);
609
Ben Hutchings3e223c22015-06-16 22:11:06 +0100610 error = pipe_iov_copy_from_user(src, &offset, iov,
611 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200612 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800613 kunmap_atomic(src);
Jens Axboef6762b72006-05-01 20:02:05 +0200614 else
615 kunmap(page);
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200618 if (atomic) {
619 atomic = 0;
620 goto redo2;
621 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200622 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200623 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625 }
626 ret += chars;
627
628 /* Insert it into the buffer array */
629 buf->page = page;
630 buf->ops = &anon_pipe_buf_ops;
631 buf->offset = 0;
632 buf->len = chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700633 buf->flags = 0;
634 if (is_packetized(filp)) {
635 buf->ops = &packet_pipe_buf_ops;
636 buf->flags = PIPE_BUF_FLAG_PACKET;
637 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200638 pipe->nrbufs = ++bufs;
639 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 total_len -= chars;
642 if (!total_len)
643 break;
644 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200645 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 continue;
647 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200648 if (!ret)
649 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651 }
652 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200653 if (!ret)
654 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 }
657 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800658 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200659 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 do_wakeup = 0;
661 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200662 pipe->waiting_writers++;
663 pipe_wait(pipe);
664 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200667 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800669 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200670 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Josef Bacik295534f2012-03-26 09:59:21 -0400672 if (ret > 0) {
673 int err = file_update_time(filp);
674 if (err)
675 ret = err;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return ret;
678}
679
680static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
682{
683 return -EBADF;
684}
685
686static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200687bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
688 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 return -EBADF;
691}
692
Andi Kleend59d0b12008-02-08 04:21:23 -0800693static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800695 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200696 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int count, buf, nrbufs;
698
699 switch (cmd) {
700 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200701 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200702 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200704 buf = pipe->curbuf;
705 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200707 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200708 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200710 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return put_user(count, (int __user *)arg);
713 default:
714 return -EINVAL;
715 }
716}
717
718/* No kernel lock held - fine */
719static unsigned int
720pipe_poll(struct file *filp, poll_table *wait)
721{
722 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800723 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200724 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int nrbufs;
726
Ingo Molnar923f4f22006-04-11 13:53:33 +0200727 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200730 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 mask = 0;
732 if (filp->f_mode & FMODE_READ) {
733 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200734 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 mask |= POLLHUP;
736 }
737
738 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200739 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700740 /*
741 * Most Unices do not set POLLERR for FIFOs but on Linux they
742 * behave exactly like pipes for poll().
743 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200744 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 mask |= POLLERR;
746 }
747
748 return mask;
749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static int
752pipe_release(struct inode *inode, int decr, int decw)
753{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200754 struct pipe_inode_info *pipe;
755
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200756 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200757 pipe = inode->i_pipe;
758 pipe->readers -= decr;
759 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200760
Ingo Molnar923f4f22006-04-11 13:53:33 +0200761 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 free_pipe_info(inode);
763 } else {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800764 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200765 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
766 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200768 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 return 0;
771}
772
773static int
774pipe_read_fasync(int fd, struct file *filp, int on)
775{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800776 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int retval;
778
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200779 mutex_lock(&inode->i_mutex);
780 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
781 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700783 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786
787static int
788pipe_write_fasync(int fd, struct file *filp, int on)
789{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800790 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int retval;
792
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200793 mutex_lock(&inode->i_mutex);
794 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
795 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700797 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800
801static int
802pipe_rdwr_fasync(int fd, struct file *filp, int on)
803{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800804 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200805 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int retval;
807
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200808 mutex_lock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200809 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700810 if (retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200811 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700812 if (retval < 0) /* this can happen only if on == T */
813 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
814 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200815 mutex_unlock(&inode->i_mutex);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700816 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819
820static int
821pipe_read_release(struct inode *inode, struct file *filp)
822{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return pipe_release(inode, 1, 0);
824}
825
826static int
827pipe_write_release(struct inode *inode, struct file *filp)
828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return pipe_release(inode, 0, 1);
830}
831
832static int
833pipe_rdwr_release(struct inode *inode, struct file *filp)
834{
835 int decr, decw;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 decr = (filp->f_mode & FMODE_READ) != 0;
838 decw = (filp->f_mode & FMODE_WRITE) != 0;
839 return pipe_release(inode, decr, decw);
840}
841
842static int
843pipe_read_open(struct inode *inode, struct file *filp)
844{
Earl Chewad396022009-10-19 15:55:41 -0700845 int ret = -ENOENT;
846
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200847 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700848
849 if (inode->i_pipe) {
850 ret = 0;
851 inode->i_pipe->readers++;
852 }
853
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200854 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Earl Chewad396022009-10-19 15:55:41 -0700856 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859static int
860pipe_write_open(struct inode *inode, struct file *filp)
861{
Earl Chewad396022009-10-19 15:55:41 -0700862 int ret = -ENOENT;
863
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200864 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700865
866 if (inode->i_pipe) {
867 ret = 0;
868 inode->i_pipe->writers++;
869 }
870
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200871 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Earl Chewad396022009-10-19 15:55:41 -0700873 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876static int
877pipe_rdwr_open(struct inode *inode, struct file *filp)
878{
Earl Chewad396022009-10-19 15:55:41 -0700879 int ret = -ENOENT;
880
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200881 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700882
883 if (inode->i_pipe) {
884 ret = 0;
885 if (filp->f_mode & FMODE_READ)
886 inode->i_pipe->readers++;
887 if (filp->f_mode & FMODE_WRITE)
888 inode->i_pipe->writers++;
889 }
890
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200891 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Earl Chewad396022009-10-19 15:55:41 -0700893 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897 * The file_operations structs are not static because they
898 * are also used in linux/fs/fifo.c to do operations on FIFOs.
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200899 *
900 * Pipes reuse fifos' file_operations structs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200902const struct file_operations read_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700904 .read = do_sync_read,
905 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700907 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800908 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 .open = pipe_read_open,
910 .release = pipe_read_release,
911 .fasync = pipe_read_fasync,
912};
913
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200914const struct file_operations write_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 .llseek = no_llseek,
916 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700917 .write = do_sync_write,
918 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700919 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800920 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 .open = pipe_write_open,
922 .release = pipe_write_release,
923 .fasync = pipe_write_fasync,
924};
925
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200926const struct file_operations rdwr_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700928 .read = do_sync_read,
929 .aio_read = pipe_read,
930 .write = do_sync_write,
931 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800933 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 .open = pipe_rdwr_open,
935 .release = pipe_rdwr_release,
936 .fasync = pipe_rdwr_fasync,
937};
938
Willy Tarreau239bc432016-01-18 16:36:09 +0100939static void account_pipe_buffers(struct pipe_inode_info *pipe,
940 unsigned long old, unsigned long new)
941{
942 atomic_long_add(new - old, &pipe->user->pipe_bufs);
943}
944
945static bool too_many_pipe_buffers_soft(struct user_struct *user)
946{
947 return pipe_user_pages_soft &&
948 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
949}
950
951static bool too_many_pipe_buffers_hard(struct user_struct *user)
952{
953 return pipe_user_pages_hard &&
954 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
955}
956
Ingo Molnar3a326a22006-04-10 15:18:35 +0200957struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
958{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200959 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200960
Ingo Molnar923f4f22006-04-11 13:53:33 +0200961 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
962 if (pipe) {
Willy Tarreau239bc432016-01-18 16:36:09 +0100963 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
964 struct user_struct *user = get_current_user();
965
966 if (!too_many_pipe_buffers_hard(user)) {
967 if (too_many_pipe_buffers_soft(user))
968 pipe_bufs = 1;
969 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
970 }
971
Jens Axboe35f3d142010-05-20 10:43:18 +0200972 if (pipe->bufs) {
973 init_waitqueue_head(&pipe->wait);
974 pipe->r_counter = pipe->w_counter = 1;
975 pipe->inode = inode;
Willy Tarreau239bc432016-01-18 16:36:09 +0100976 pipe->buffers = pipe_bufs;
977 pipe->user = user;
978 account_pipe_buffers(pipe, 0, pipe_bufs);
Jens Axboe35f3d142010-05-20 10:43:18 +0200979 return pipe;
980 }
Willy Tarreau239bc432016-01-18 16:36:09 +0100981 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200982 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200983 }
984
Jens Axboe35f3d142010-05-20 10:43:18 +0200985 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200986}
987
Ingo Molnar923f4f22006-04-11 13:53:33 +0200988void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Willy Tarreau239bc432016-01-18 16:36:09 +0100992 account_pipe_buffers(pipe, pipe->buffers, 0);
993 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200994 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200995 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200997 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200999 if (pipe->tmp_page)
1000 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +02001001 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +02001002 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Jens Axboeb92ce552006-04-11 13:52:07 +02001005void free_pipe_info(struct inode *inode)
1006{
1007 __free_pipe_info(inode->i_pipe);
1008 inode->i_pipe = NULL;
1009}
1010
Eric Dumazetfa3536c2006-03-26 01:37:24 -08001011static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +02001012
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001013/*
1014 * pipefs_dname() is called from d_path().
1015 */
1016static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
1017{
1018 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
1019 dentry->d_inode->i_ino);
1020}
1021
Al Viro3ba13d12009-02-20 06:02:22 +00001022static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001023 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024};
1025
1026static struct inode * get_pipe_inode(void)
1027{
Eric Dumazeta209dfc2011-07-26 11:36:34 +02001028 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +02001029 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (!inode)
1032 goto fail_inode;
1033
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001034 inode->i_ino = get_next_ino();
1035
Ingo Molnar923f4f22006-04-11 13:53:33 +02001036 pipe = alloc_pipe_info(inode);
1037 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +02001039 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +02001040
Ingo Molnar923f4f22006-04-11 13:53:33 +02001041 pipe->readers = pipe->writers = 1;
Denys Vlasenkod2d96482008-07-01 14:16:09 +02001042 inode->i_fop = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /*
1045 * Mark the inode dirty from the very beginning,
1046 * that way it will never be moved to the dirty
1047 * list because "mark_inode_dirty()" will think
1048 * that it already _is_ on the dirty list.
1049 */
1050 inode->i_state = I_DIRTY;
1051 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +11001052 inode->i_uid = current_fsuid();
1053 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +02001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return inode;
1057
1058fail_iput:
1059 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +02001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061fail_inode:
1062 return NULL;
1063}
1064
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001065struct file *create_write_pipe(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Andi Kleend6cbd282006-09-30 23:29:26 -07001067 int err;
1068 struct inode *inode;
1069 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +04001070 struct path path;
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001071 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Andi Kleend6cbd282006-09-30 23:29:26 -07001073 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 inode = get_pipe_inode();
1075 if (!inode)
Dave Hansen430e2852008-02-15 14:37:26 -08001076 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Andi Kleend6cbd282006-09-30 23:29:26 -07001078 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +11001079 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +04001080 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -07001081 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04001082 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +02001083
Al Viro2c48b9c2009-08-09 00:52:35 +04001084 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001085
1086 err = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04001087 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
Dave Hansen430e2852008-02-15 14:37:26 -08001088 if (!f)
1089 goto err_dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -07001090 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds98830352012-04-29 13:12:42 -07001092 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Andi Kleend6cbd282006-09-30 23:29:26 -07001093 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Andi Kleend6cbd282006-09-30 23:29:26 -07001095 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Dave Hansen430e2852008-02-15 14:37:26 -08001097 err_dentry:
Al Viroed152432008-04-22 19:51:27 -04001098 free_pipe_info(inode);
Al Viro2c48b9c2009-08-09 00:52:35 +04001099 path_put(&path);
Al Viroed152432008-04-22 19:51:27 -04001100 return ERR_PTR(err);
1101
Andi Kleend6cbd282006-09-30 23:29:26 -07001102 err_inode:
1103 free_pipe_info(inode);
1104 iput(inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001105 err:
Andi Kleend6cbd282006-09-30 23:29:26 -07001106 return ERR_PTR(err);
1107}
1108
1109void free_write_pipe(struct file *f)
1110{
Al Viro5ccac882006-12-18 13:31:18 +00001111 free_pipe_info(f->f_dentry->d_inode);
Jan Blunckc8e7f442008-06-09 16:40:35 -07001112 path_put(&f->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001113 put_filp(f);
1114}
1115
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001116struct file *create_read_pipe(struct file *wrf, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001117{
Al Virod2314122009-08-09 01:01:37 +04001118 /* Grab pipe from the writer */
1119 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1120 &read_pipefifo_fops);
Andi Kleend6cbd282006-09-30 23:29:26 -07001121 if (!f)
1122 return ERR_PTR(-ENFILE);
1123
Jan Blunckc8e7f442008-06-09 16:40:35 -07001124 path_get(&wrf->f_path);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001125 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
Andi Kleend6cbd282006-09-30 23:29:26 -07001126
1127 return f;
1128}
1129
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001130int do_pipe_flags(int *fd, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001131{
1132 struct file *fw, *fr;
1133 int error;
1134 int fdw, fdr;
1135
Linus Torvalds98830352012-04-29 13:12:42 -07001136 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001137 return -EINVAL;
1138
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001139 fw = create_write_pipe(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001140 if (IS_ERR(fw))
1141 return PTR_ERR(fw);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001142 fr = create_read_pipe(fw, flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001143 error = PTR_ERR(fr);
1144 if (IS_ERR(fr))
1145 goto err_write_pipe;
1146
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001147 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001148 if (error < 0)
1149 goto err_read_pipe;
1150 fdr = error;
1151
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001152 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001153 if (error < 0)
1154 goto err_fdr;
1155 fdw = error;
1156
Al Viro157cf642008-12-14 04:57:47 -05001157 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001158 fd_install(fdr, fr);
1159 fd_install(fdw, fw);
1160 fd[0] = fdr;
1161 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return 0;
1164
Andi Kleend6cbd282006-09-30 23:29:26 -07001165 err_fdr:
1166 put_unused_fd(fdr);
1167 err_read_pipe:
Jan Blunckc8e7f442008-06-09 16:40:35 -07001168 path_put(&fr->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001169 put_filp(fr);
1170 err_write_pipe:
1171 free_write_pipe(fw);
1172 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001176 * sys_pipe() is the normal C calling standard for creating
1177 * a pipe. It's not the way Unix traditionally does this, though.
1178 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01001179SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001180{
1181 int fd[2];
1182 int error;
1183
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001184 error = do_pipe_flags(fd, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001185 if (!error) {
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001186 if (copy_to_user(fildes, fd, sizeof(fd))) {
1187 sys_close(fd[0]);
1188 sys_close(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001189 error = -EFAULT;
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001190 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001191 }
1192 return error;
1193}
1194
Heiko Carstens2b664212009-01-14 14:14:35 +01001195SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001196{
1197 return sys_pipe2(fildes, 0);
1198}
1199
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001200/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001201 * Allocate a new array of pipe buffers and copy the info over. Returns the
1202 * pipe size if successful, or return -ERROR on error.
1203 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001204static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001205{
1206 struct pipe_buffer *bufs;
1207
1208 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001209 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1210 * expect a lot of shrink+grow operations, just free and allocate
1211 * again like we would do for growing. If the pipe currently
1212 * contains more buffers than arg, then return busy.
1213 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001214 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001215 return -EBUSY;
1216
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001217 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001218 if (unlikely(!bufs))
1219 return -ENOMEM;
1220
1221 /*
1222 * The pipe array wraps around, so just start the new one at zero
1223 * and adjust the indexes.
1224 */
1225 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001226 unsigned int tail;
1227 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001228
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001229 tail = pipe->curbuf + pipe->nrbufs;
1230 if (tail < pipe->buffers)
1231 tail = 0;
1232 else
1233 tail &= (pipe->buffers - 1);
1234
1235 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001236 if (head)
1237 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1238 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001239 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001240 }
1241
Willy Tarreau239bc432016-01-18 16:36:09 +01001242 account_pipe_buffers(pipe, pipe->buffers, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001243 pipe->curbuf = 0;
1244 kfree(pipe->bufs);
1245 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001246 pipe->buffers = nr_pages;
1247 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001248}
1249
Jens Axboeff9da692010-06-03 14:54:39 +02001250/*
1251 * Currently we rely on the pipe array holding a power-of-2 number
1252 * of pages.
1253 */
1254static inline unsigned int round_pipe_size(unsigned int size)
1255{
1256 unsigned long nr_pages;
1257
1258 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1259 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1260}
1261
1262/*
1263 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1264 * will return an error.
1265 */
1266int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1267 size_t *lenp, loff_t *ppos)
1268{
1269 int ret;
1270
1271 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1272 if (ret < 0 || !write)
1273 return ret;
1274
1275 pipe_max_size = round_pipe_size(pipe_max_size);
1276 return ret;
1277}
1278
Linus Torvalds72083642010-11-28 16:27:19 -08001279/*
1280 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1281 * location, so checking ->i_pipe is not enough to verify that this is a
1282 * pipe.
1283 */
1284struct pipe_inode_info *get_pipe_info(struct file *file)
1285{
1286 struct inode *i = file->f_path.dentry->d_inode;
1287
1288 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1289}
1290
Jens Axboe35f3d142010-05-20 10:43:18 +02001291long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1292{
1293 struct pipe_inode_info *pipe;
1294 long ret;
1295
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001296 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001297 if (!pipe)
1298 return -EBADF;
1299
1300 mutex_lock(&pipe->inode->i_mutex);
1301
1302 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001303 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001304 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001305
Jens Axboeff9da692010-06-03 14:54:39 +02001306 size = round_pipe_size(arg);
1307 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001308
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001309 ret = -EINVAL;
1310 if (!nr_pages)
1311 goto out;
1312
Jens Axboeff9da692010-06-03 14:54:39 +02001313 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001314 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001315 goto out;
Willy Tarreau239bc432016-01-18 16:36:09 +01001316 } else if ((too_many_pipe_buffers_hard(pipe->user) ||
1317 too_many_pipe_buffers_soft(pipe->user)) &&
1318 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
1319 ret = -EPERM;
1320 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001321 }
Jens Axboeff9da692010-06-03 14:54:39 +02001322 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001323 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001324 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001325 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001326 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001327 break;
1328 default:
1329 ret = -EINVAL;
1330 break;
1331 }
1332
Julia Lawallcc967be2010-05-26 17:54:39 +02001333out:
Jens Axboe35f3d142010-05-20 10:43:18 +02001334 mutex_unlock(&pipe->inode->i_mutex);
1335 return ret;
1336}
1337
Nick Pigginff0c7d12011-01-07 17:49:50 +11001338static const struct super_operations pipefs_ops = {
1339 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001340 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001341};
1342
Jens Axboe35f3d142010-05-20 10:43:18 +02001343/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * pipefs should _never_ be mounted by userland - too much of security hassle,
1345 * no real gain from having the whole whorehouse mounted. So we don't need
1346 * any operations on the root directory. However, we need a non-trivial
1347 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1348 */
Al Viro51139ad2010-07-25 23:47:46 +04001349static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1350 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Al Viroc74a1cb2011-01-12 16:59:34 -05001352 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1353 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356static struct file_system_type pipe_fs_type = {
1357 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001358 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 .kill_sb = kill_anon_super,
1360};
1361
1362static int __init init_pipe_fs(void)
1363{
1364 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!err) {
1367 pipe_mnt = kern_mount(&pipe_fs_type);
1368 if (IS_ERR(pipe_mnt)) {
1369 err = PTR_ERR(pipe_mnt);
1370 unregister_filesystem(&pipe_fs_type);
1371 }
1372 }
1373 return err;
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376fs_initcall(init_pipe_fs);