blob: 3344cb0662fa625d3f157fdac2147c2af41ba6c4 [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;
Ben Hutchingsbfe8a772016-02-13 02:34:52 +0000399 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 if (chars > total_len)
402 chars = total_len;
403
Jens Axboecac36bb2007-06-14 13:10:48 +0200404 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200405 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200406 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200407 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200408 break;
409 }
Jens Axboef84d7512006-05-01 19:59:03 +0200410
Jens Axboef6762b72006-05-01 20:02:05 +0200411 atomic = !iov_fault_in_pages_write(iov, chars);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100412 remaining = chars;
Ben Hutchingsbfe8a772016-02-13 02:34:52 +0000413 offset = buf->offset;
Jens Axboef6762b72006-05-01 20:02:05 +0200414redo:
415 addr = ops->map(pipe, buf, atomic);
Ben Hutchingsbfe8a772016-02-13 02:34:52 +0000416 error = pipe_iov_copy_to_user(iov, addr, &offset,
Ben Hutchings3e223c22015-06-16 22:11:06 +0100417 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200418 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200420 /*
421 * Just retry with the slow path if we failed.
422 */
423 if (atomic) {
424 atomic = 0;
425 goto redo;
426 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200427 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200428 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
430 }
431 ret += chars;
Ben Hutchingsbfe8a772016-02-13 02:34:52 +0000432 buf->offset += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700434
435 /* Was it a packet buffer? Clean up and exit */
436 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
437 total_len = chars;
438 buf->len = 0;
439 }
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!buf->len) {
442 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200443 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200444 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200445 pipe->curbuf = curbuf;
446 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 do_wakeup = 1;
448 }
449 total_len -= chars;
450 if (!total_len)
451 break; /* common path: read succeeded */
452 }
453 if (bufs) /* More to do? */
454 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200455 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200457 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* syscall merging: Usually we must not sleep
459 * if O_NONBLOCK is set, or if we got some data.
460 * But if a writer sleeps in kernel space, then
461 * we can wait for that data without violating POSIX.
462 */
463 if (ret)
464 break;
465 if (filp->f_flags & O_NONBLOCK) {
466 ret = -EAGAIN;
467 break;
468 }
469 }
470 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200471 if (!ret)
472 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 }
475 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800476 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200477 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200479 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200481 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200482
483 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800485 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200486 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 if (ret > 0)
489 file_accessed(filp);
490 return ret;
491}
492
Linus Torvalds98830352012-04-29 13:12:42 -0700493static inline int is_packetized(struct file *file)
494{
495 return (file->f_flags & O_DIRECT) != 0;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700499pipe_write(struct kiocb *iocb, const struct iovec *_iov,
500 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700502 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800503 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200504 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ssize_t ret;
506 int do_wakeup;
507 struct iovec *iov = (struct iovec *)_iov;
508 size_t total_len;
509 ssize_t chars;
510
511 total_len = iov_length(iov, nr_segs);
512 /* Null write succeeds. */
513 if (unlikely(total_len == 0))
514 return 0;
515
516 do_wakeup = 0;
517 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200518 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200519 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Ingo Molnar923f4f22006-04-11 13:53:33 +0200521 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 send_sig(SIGPIPE, current, 0);
523 ret = -EPIPE;
524 goto out;
525 }
526
527 /* We try to merge small writes */
528 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200529 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200530 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200531 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200532 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800533 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200537 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200538 void *addr;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100539 size_t remaining = chars;
Jens Axboe5274f052006-03-30 15:15:30 +0200540
Jens Axboecac36bb2007-06-14 13:10:48 +0200541 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200542 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200543 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200544
Jens Axboef6762b72006-05-01 20:02:05 +0200545 iov_fault_in_pages_read(iov, chars);
546redo1:
547 addr = ops->map(pipe, buf, atomic);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100548 error = pipe_iov_copy_from_user(addr, &offset, iov,
549 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200550 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ret = error;
552 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200553 if (error) {
554 if (atomic) {
555 atomic = 0;
556 goto redo1;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 buf->len += chars;
561 total_len -= chars;
562 ret = chars;
563 if (!total_len)
564 goto out;
565 }
566 }
567
568 for (;;) {
569 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200570
Ingo Molnar923f4f22006-04-11 13:53:33 +0200571 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200573 if (!ret)
574 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
576 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200577 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200578 if (bufs < pipe->buffers) {
579 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200580 struct pipe_buffer *buf = pipe->bufs + newbuf;
581 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200582 char *src;
583 int error, atomic = 1;
Ben Hutchings3e223c22015-06-16 22:11:06 +0100584 int offset = 0;
585 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (!page) {
588 page = alloc_page(GFP_HIGHUSER);
589 if (unlikely(!page)) {
590 ret = ret ? : -ENOMEM;
591 break;
592 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200593 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200595 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * we lock up (O_NONBLOCK-)readers that sleep due to
597 * syscall merging.
598 * FIXME! Is this really true?
599 */
600 do_wakeup = 1;
601 chars = PAGE_SIZE;
602 if (chars > total_len)
603 chars = total_len;
604
Jens Axboef6762b72006-05-01 20:02:05 +0200605 iov_fault_in_pages_read(iov, chars);
Ben Hutchings3e223c22015-06-16 22:11:06 +0100606 remaining = chars;
Jens Axboef6762b72006-05-01 20:02:05 +0200607redo2:
608 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800609 src = kmap_atomic(page);
Jens Axboef6762b72006-05-01 20:02:05 +0200610 else
611 src = kmap(page);
612
Ben Hutchings3e223c22015-06-16 22:11:06 +0100613 error = pipe_iov_copy_from_user(src, &offset, iov,
614 &remaining, atomic);
Jens Axboef6762b72006-05-01 20:02:05 +0200615 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800616 kunmap_atomic(src);
Jens Axboef6762b72006-05-01 20:02:05 +0200617 else
618 kunmap(page);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200621 if (atomic) {
622 atomic = 0;
623 goto redo2;
624 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200625 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200626 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628 }
629 ret += chars;
630
631 /* Insert it into the buffer array */
632 buf->page = page;
633 buf->ops = &anon_pipe_buf_ops;
634 buf->offset = 0;
635 buf->len = chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700636 buf->flags = 0;
637 if (is_packetized(filp)) {
638 buf->ops = &packet_pipe_buf_ops;
639 buf->flags = PIPE_BUF_FLAG_PACKET;
640 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200641 pipe->nrbufs = ++bufs;
642 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 total_len -= chars;
645 if (!total_len)
646 break;
647 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200648 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 continue;
650 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200651 if (!ret)
652 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 }
655 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200656 if (!ret)
657 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 }
660 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800661 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200662 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 do_wakeup = 0;
664 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200665 pipe->waiting_writers++;
666 pipe_wait(pipe);
667 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200670 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800672 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200673 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Josef Bacik295534f2012-03-26 09:59:21 -0400675 if (ret > 0) {
676 int err = file_update_time(filp);
677 if (err)
678 ret = err;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return ret;
681}
682
683static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
685{
686 return -EBADF;
687}
688
689static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200690bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
691 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 return -EBADF;
694}
695
Andi Kleend59d0b12008-02-08 04:21:23 -0800696static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800698 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200699 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int count, buf, nrbufs;
701
702 switch (cmd) {
703 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200704 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200705 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200707 buf = pipe->curbuf;
708 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200710 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200711 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200713 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return put_user(count, (int __user *)arg);
716 default:
717 return -EINVAL;
718 }
719}
720
721/* No kernel lock held - fine */
722static unsigned int
723pipe_poll(struct file *filp, poll_table *wait)
724{
725 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800726 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200727 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int nrbufs;
729
Ingo Molnar923f4f22006-04-11 13:53:33 +0200730 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200733 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 mask = 0;
735 if (filp->f_mode & FMODE_READ) {
736 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200737 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mask |= POLLHUP;
739 }
740
741 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200742 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700743 /*
744 * Most Unices do not set POLLERR for FIFOs but on Linux they
745 * behave exactly like pipes for poll().
746 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200747 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 mask |= POLLERR;
749 }
750
751 return mask;
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754static int
755pipe_release(struct inode *inode, int decr, int decw)
756{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200757 struct pipe_inode_info *pipe;
758
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200759 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200760 pipe = inode->i_pipe;
761 pipe->readers -= decr;
762 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200763
Ingo Molnar923f4f22006-04-11 13:53:33 +0200764 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 free_pipe_info(inode);
766 } else {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800767 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200768 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
769 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200771 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return 0;
774}
775
776static int
777pipe_read_fasync(int fd, struct file *filp, int on)
778{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800779 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int retval;
781
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200782 mutex_lock(&inode->i_mutex);
783 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
784 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700786 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789
790static int
791pipe_write_fasync(int fd, struct file *filp, int on)
792{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800793 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int retval;
795
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200796 mutex_lock(&inode->i_mutex);
797 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
798 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700800 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803
804static int
805pipe_rdwr_fasync(int fd, struct file *filp, int on)
806{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800807 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200808 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int retval;
810
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200811 mutex_lock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200812 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700813 if (retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200814 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700815 if (retval < 0) /* this can happen only if on == T */
816 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
817 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200818 mutex_unlock(&inode->i_mutex);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700819 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822
823static int
824pipe_read_release(struct inode *inode, struct file *filp)
825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return pipe_release(inode, 1, 0);
827}
828
829static int
830pipe_write_release(struct inode *inode, struct file *filp)
831{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return pipe_release(inode, 0, 1);
833}
834
835static int
836pipe_rdwr_release(struct inode *inode, struct file *filp)
837{
838 int decr, decw;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 decr = (filp->f_mode & FMODE_READ) != 0;
841 decw = (filp->f_mode & FMODE_WRITE) != 0;
842 return pipe_release(inode, decr, decw);
843}
844
845static int
846pipe_read_open(struct inode *inode, struct file *filp)
847{
Earl Chewad396022009-10-19 15:55:41 -0700848 int ret = -ENOENT;
849
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200850 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700851
852 if (inode->i_pipe) {
853 ret = 0;
854 inode->i_pipe->readers++;
855 }
856
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200857 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Earl Chewad396022009-10-19 15:55:41 -0700859 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862static int
863pipe_write_open(struct inode *inode, struct file *filp)
864{
Earl Chewad396022009-10-19 15:55:41 -0700865 int ret = -ENOENT;
866
Al Viro9eacc062013-03-12 02:59:49 +0000867 if (!(filp->f_mode & (FMODE_READ|FMODE_WRITE)))
868 return -EINVAL;
869
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200870 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700871
872 if (inode->i_pipe) {
873 ret = 0;
874 inode->i_pipe->writers++;
875 }
876
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200877 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Earl Chewad396022009-10-19 15:55:41 -0700879 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static int
883pipe_rdwr_open(struct inode *inode, struct file *filp)
884{
Earl Chewad396022009-10-19 15:55:41 -0700885 int ret = -ENOENT;
886
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200887 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700888
889 if (inode->i_pipe) {
890 ret = 0;
891 if (filp->f_mode & FMODE_READ)
892 inode->i_pipe->readers++;
893 if (filp->f_mode & FMODE_WRITE)
894 inode->i_pipe->writers++;
895 }
896
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200897 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Earl Chewad396022009-10-19 15:55:41 -0700899 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902/*
903 * The file_operations structs are not static because they
904 * are also used in linux/fs/fifo.c to do operations on FIFOs.
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200905 *
906 * Pipes reuse fifos' file_operations structs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200908const struct file_operations read_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700910 .read = do_sync_read,
911 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .write = bad_pipe_w,
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_read_open,
916 .release = pipe_read_release,
917 .fasync = pipe_read_fasync,
918};
919
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200920const struct file_operations write_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 .llseek = no_llseek,
922 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700923 .write = do_sync_write,
924 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700925 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800926 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 .open = pipe_write_open,
928 .release = pipe_write_release,
929 .fasync = pipe_write_fasync,
930};
931
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200932const struct file_operations rdwr_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700934 .read = do_sync_read,
935 .aio_read = pipe_read,
936 .write = do_sync_write,
937 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800939 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 .open = pipe_rdwr_open,
941 .release = pipe_rdwr_release,
942 .fasync = pipe_rdwr_fasync,
943};
944
Willy Tarreau239bc432016-01-18 16:36:09 +0100945static void account_pipe_buffers(struct pipe_inode_info *pipe,
946 unsigned long old, unsigned long new)
947{
948 atomic_long_add(new - old, &pipe->user->pipe_bufs);
949}
950
951static bool too_many_pipe_buffers_soft(struct user_struct *user)
952{
953 return pipe_user_pages_soft &&
954 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
955}
956
957static bool too_many_pipe_buffers_hard(struct user_struct *user)
958{
959 return pipe_user_pages_hard &&
960 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
961}
962
Ingo Molnar3a326a22006-04-10 15:18:35 +0200963struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
964{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200965 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200966
Ingo Molnar923f4f22006-04-11 13:53:33 +0200967 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
968 if (pipe) {
Willy Tarreau239bc432016-01-18 16:36:09 +0100969 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
970 struct user_struct *user = get_current_user();
971
972 if (!too_many_pipe_buffers_hard(user)) {
973 if (too_many_pipe_buffers_soft(user))
974 pipe_bufs = 1;
975 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
976 }
977
Jens Axboe35f3d142010-05-20 10:43:18 +0200978 if (pipe->bufs) {
979 init_waitqueue_head(&pipe->wait);
980 pipe->r_counter = pipe->w_counter = 1;
981 pipe->inode = inode;
Willy Tarreau239bc432016-01-18 16:36:09 +0100982 pipe->buffers = pipe_bufs;
983 pipe->user = user;
984 account_pipe_buffers(pipe, 0, pipe_bufs);
Jens Axboe35f3d142010-05-20 10:43:18 +0200985 return pipe;
986 }
Willy Tarreau239bc432016-01-18 16:36:09 +0100987 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200988 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200989 }
990
Jens Axboe35f3d142010-05-20 10:43:18 +0200991 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200992}
993
Ingo Molnar923f4f22006-04-11 13:53:33 +0200994void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Willy Tarreau239bc432016-01-18 16:36:09 +0100998 account_pipe_buffers(pipe, pipe->buffers, 0);
999 free_uid(pipe->user);
Jens Axboe35f3d142010-05-20 10:43:18 +02001000 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +02001001 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +02001003 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Ingo Molnar923f4f22006-04-11 13:53:33 +02001005 if (pipe->tmp_page)
1006 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +02001007 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +02001008 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Jens Axboeb92ce552006-04-11 13:52:07 +02001011void free_pipe_info(struct inode *inode)
1012{
1013 __free_pipe_info(inode->i_pipe);
1014 inode->i_pipe = NULL;
1015}
1016
Eric Dumazetfa3536c2006-03-26 01:37:24 -08001017static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +02001018
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001019/*
1020 * pipefs_dname() is called from d_path().
1021 */
1022static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
1023{
1024 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
1025 dentry->d_inode->i_ino);
1026}
1027
Al Viro3ba13d12009-02-20 06:02:22 +00001028static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001029 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030};
1031
1032static struct inode * get_pipe_inode(void)
1033{
Eric Dumazeta209dfc2011-07-26 11:36:34 +02001034 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +02001035 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (!inode)
1038 goto fail_inode;
1039
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001040 inode->i_ino = get_next_ino();
1041
Ingo Molnar923f4f22006-04-11 13:53:33 +02001042 pipe = alloc_pipe_info(inode);
1043 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +02001045 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +02001046
Ingo Molnar923f4f22006-04-11 13:53:33 +02001047 pipe->readers = pipe->writers = 1;
Denys Vlasenkod2d96482008-07-01 14:16:09 +02001048 inode->i_fop = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /*
1051 * Mark the inode dirty from the very beginning,
1052 * that way it will never be moved to the dirty
1053 * list because "mark_inode_dirty()" will think
1054 * that it already _is_ on the dirty list.
1055 */
1056 inode->i_state = I_DIRTY;
1057 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +11001058 inode->i_uid = current_fsuid();
1059 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +02001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return inode;
1063
1064fail_iput:
1065 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +02001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067fail_inode:
1068 return NULL;
1069}
1070
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001071struct file *create_write_pipe(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Andi Kleend6cbd282006-09-30 23:29:26 -07001073 int err;
1074 struct inode *inode;
1075 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +04001076 struct path path;
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001077 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Andi Kleend6cbd282006-09-30 23:29:26 -07001079 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 inode = get_pipe_inode();
1081 if (!inode)
Dave Hansen430e2852008-02-15 14:37:26 -08001082 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Andi Kleend6cbd282006-09-30 23:29:26 -07001084 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +11001085 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +04001086 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -07001087 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04001088 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +02001089
Al Viro2c48b9c2009-08-09 00:52:35 +04001090 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001091
1092 err = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04001093 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
Dave Hansen430e2852008-02-15 14:37:26 -08001094 if (!f)
1095 goto err_dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -07001096 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds98830352012-04-29 13:12:42 -07001098 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Andi Kleend6cbd282006-09-30 23:29:26 -07001099 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Andi Kleend6cbd282006-09-30 23:29:26 -07001101 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dave Hansen430e2852008-02-15 14:37:26 -08001103 err_dentry:
Al Viroed152432008-04-22 19:51:27 -04001104 free_pipe_info(inode);
Al Viro2c48b9c2009-08-09 00:52:35 +04001105 path_put(&path);
Al Viroed152432008-04-22 19:51:27 -04001106 return ERR_PTR(err);
1107
Andi Kleend6cbd282006-09-30 23:29:26 -07001108 err_inode:
1109 free_pipe_info(inode);
1110 iput(inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001111 err:
Andi Kleend6cbd282006-09-30 23:29:26 -07001112 return ERR_PTR(err);
1113}
1114
1115void free_write_pipe(struct file *f)
1116{
Al Viro5ccac882006-12-18 13:31:18 +00001117 free_pipe_info(f->f_dentry->d_inode);
Jan Blunckc8e7f442008-06-09 16:40:35 -07001118 path_put(&f->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001119 put_filp(f);
1120}
1121
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001122struct file *create_read_pipe(struct file *wrf, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001123{
Al Virod2314122009-08-09 01:01:37 +04001124 /* Grab pipe from the writer */
1125 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1126 &read_pipefifo_fops);
Andi Kleend6cbd282006-09-30 23:29:26 -07001127 if (!f)
1128 return ERR_PTR(-ENFILE);
1129
Jan Blunckc8e7f442008-06-09 16:40:35 -07001130 path_get(&wrf->f_path);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001131 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
Andi Kleend6cbd282006-09-30 23:29:26 -07001132
1133 return f;
1134}
1135
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001136int do_pipe_flags(int *fd, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001137{
1138 struct file *fw, *fr;
1139 int error;
1140 int fdw, fdr;
1141
Linus Torvalds98830352012-04-29 13:12:42 -07001142 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001143 return -EINVAL;
1144
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001145 fw = create_write_pipe(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001146 if (IS_ERR(fw))
1147 return PTR_ERR(fw);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001148 fr = create_read_pipe(fw, flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001149 error = PTR_ERR(fr);
1150 if (IS_ERR(fr))
1151 goto err_write_pipe;
1152
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001153 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001154 if (error < 0)
1155 goto err_read_pipe;
1156 fdr = error;
1157
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001158 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001159 if (error < 0)
1160 goto err_fdr;
1161 fdw = error;
1162
Al Viro157cf642008-12-14 04:57:47 -05001163 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001164 fd_install(fdr, fr);
1165 fd_install(fdw, fw);
1166 fd[0] = fdr;
1167 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170
Andi Kleend6cbd282006-09-30 23:29:26 -07001171 err_fdr:
1172 put_unused_fd(fdr);
1173 err_read_pipe:
Jan Blunckc8e7f442008-06-09 16:40:35 -07001174 path_put(&fr->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001175 put_filp(fr);
1176 err_write_pipe:
1177 free_write_pipe(fw);
1178 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001182 * sys_pipe() is the normal C calling standard for creating
1183 * a pipe. It's not the way Unix traditionally does this, though.
1184 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01001185SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001186{
1187 int fd[2];
1188 int error;
1189
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001190 error = do_pipe_flags(fd, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001191 if (!error) {
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001192 if (copy_to_user(fildes, fd, sizeof(fd))) {
1193 sys_close(fd[0]);
1194 sys_close(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001195 error = -EFAULT;
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001196 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001197 }
1198 return error;
1199}
1200
Heiko Carstens2b664212009-01-14 14:14:35 +01001201SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001202{
1203 return sys_pipe2(fildes, 0);
1204}
1205
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001206/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001207 * Allocate a new array of pipe buffers and copy the info over. Returns the
1208 * pipe size if successful, or return -ERROR on error.
1209 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001210static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001211{
1212 struct pipe_buffer *bufs;
1213
1214 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001215 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1216 * expect a lot of shrink+grow operations, just free and allocate
1217 * again like we would do for growing. If the pipe currently
1218 * contains more buffers than arg, then return busy.
1219 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001220 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001221 return -EBUSY;
1222
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001223 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001224 if (unlikely(!bufs))
1225 return -ENOMEM;
1226
1227 /*
1228 * The pipe array wraps around, so just start the new one at zero
1229 * and adjust the indexes.
1230 */
1231 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001232 unsigned int tail;
1233 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001234
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001235 tail = pipe->curbuf + pipe->nrbufs;
1236 if (tail < pipe->buffers)
1237 tail = 0;
1238 else
1239 tail &= (pipe->buffers - 1);
1240
1241 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001242 if (head)
1243 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1244 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001245 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001246 }
1247
Willy Tarreau239bc432016-01-18 16:36:09 +01001248 account_pipe_buffers(pipe, pipe->buffers, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001249 pipe->curbuf = 0;
1250 kfree(pipe->bufs);
1251 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001252 pipe->buffers = nr_pages;
1253 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001254}
1255
Jens Axboeff9da692010-06-03 14:54:39 +02001256/*
1257 * Currently we rely on the pipe array holding a power-of-2 number
1258 * of pages.
1259 */
1260static inline unsigned int round_pipe_size(unsigned int size)
1261{
1262 unsigned long nr_pages;
1263
1264 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1265 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1266}
1267
1268/*
1269 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1270 * will return an error.
1271 */
1272int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1273 size_t *lenp, loff_t *ppos)
1274{
1275 int ret;
1276
1277 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1278 if (ret < 0 || !write)
1279 return ret;
1280
1281 pipe_max_size = round_pipe_size(pipe_max_size);
1282 return ret;
1283}
1284
Linus Torvalds72083642010-11-28 16:27:19 -08001285/*
1286 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1287 * location, so checking ->i_pipe is not enough to verify that this is a
1288 * pipe.
1289 */
1290struct pipe_inode_info *get_pipe_info(struct file *file)
1291{
1292 struct inode *i = file->f_path.dentry->d_inode;
1293
1294 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1295}
1296
Jens Axboe35f3d142010-05-20 10:43:18 +02001297long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1298{
1299 struct pipe_inode_info *pipe;
1300 long ret;
1301
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001302 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001303 if (!pipe)
1304 return -EBADF;
1305
1306 mutex_lock(&pipe->inode->i_mutex);
1307
1308 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001309 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001310 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001311
Jens Axboeff9da692010-06-03 14:54:39 +02001312 size = round_pipe_size(arg);
1313 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001314
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001315 ret = -EINVAL;
1316 if (!nr_pages)
1317 goto out;
1318
Jens Axboeff9da692010-06-03 14:54:39 +02001319 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001320 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001321 goto out;
Willy Tarreau239bc432016-01-18 16:36:09 +01001322 } else if ((too_many_pipe_buffers_hard(pipe->user) ||
1323 too_many_pipe_buffers_soft(pipe->user)) &&
1324 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
1325 ret = -EPERM;
1326 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001327 }
Jens Axboeff9da692010-06-03 14:54:39 +02001328 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001329 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001330 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001331 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001332 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001333 break;
1334 default:
1335 ret = -EINVAL;
1336 break;
1337 }
1338
Julia Lawallcc967be2010-05-26 17:54:39 +02001339out:
Jens Axboe35f3d142010-05-20 10:43:18 +02001340 mutex_unlock(&pipe->inode->i_mutex);
1341 return ret;
1342}
1343
Nick Pigginff0c7d12011-01-07 17:49:50 +11001344static const struct super_operations pipefs_ops = {
1345 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001346 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001347};
1348
Jens Axboe35f3d142010-05-20 10:43:18 +02001349/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * pipefs should _never_ be mounted by userland - too much of security hassle,
1351 * no real gain from having the whole whorehouse mounted. So we don't need
1352 * any operations on the root directory. However, we need a non-trivial
1353 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1354 */
Al Viro51139ad2010-07-25 23:47:46 +04001355static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1356 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Al Viroc74a1cb2011-01-12 16:59:34 -05001358 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1359 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362static struct file_system_type pipe_fs_type = {
1363 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001364 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 .kill_sb = kill_anon_super,
1366};
1367
1368static int __init init_pipe_fs(void)
1369{
1370 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (!err) {
1373 pipe_mnt = kern_mount(&pipe_fs_type);
1374 if (IS_ERR(pipe_mnt)) {
1375 err = PTR_ERR(pipe_mnt);
1376 unregister_filesystem(&pipe_fs_type);
1377 }
1378 }
1379 return err;
1380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382fs_initcall(init_pipe_fs);