blob: abaa9234d27b194a88232d87018ae2c9199a50f8 [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
Al Viro599a0ac2013-03-12 09:58:10 -040028#include "internal.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
Jens Axboeb492e952010-05-19 21:03:16 +020031 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020032 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020033 */
Jens Axboeff9da692010-06-03 14:54:39 +020034unsigned int pipe_max_size = 1048576;
35
36/*
37 * Minimum pipe size, as required by POSIX
38 */
39unsigned int pipe_min_size = PAGE_SIZE;
Jens Axboeb492e952010-05-19 21:03:16 +020040
41/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * We use a start+len construction, which provides full use of the
43 * allocated memory.
44 * -- Florian Coosmann (FGC)
45 *
46 * Reads with count = 0 should always return 0.
47 * -- Julian Bradfield 1999-06-07.
48 *
49 * FIFOs and Pipes now generate SIGIO for both readers and writers.
50 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
51 *
52 * pipe_read & write cleanup
53 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
54 */
55
Miklos Szeredi61e0d472009-04-14 19:48:41 +020056static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
57{
58 if (pipe->inode)
59 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
60}
61
62void pipe_lock(struct pipe_inode_info *pipe)
63{
64 /*
65 * pipe_lock() nests non-pipe inode locks (for writing to a file)
66 */
67 pipe_lock_nested(pipe, I_MUTEX_PARENT);
68}
69EXPORT_SYMBOL(pipe_lock);
70
71void pipe_unlock(struct pipe_inode_info *pipe)
72{
73 if (pipe->inode)
74 mutex_unlock(&pipe->inode->i_mutex);
75}
76EXPORT_SYMBOL(pipe_unlock);
77
78void pipe_double_lock(struct pipe_inode_info *pipe1,
79 struct pipe_inode_info *pipe2)
80{
81 BUG_ON(pipe1 == pipe2);
82
83 if (pipe1 < pipe2) {
84 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
85 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
86 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +020087 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
88 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020089 }
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020093void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 DEFINE_WAIT(wait);
96
Ingo Molnard79fc0f2005-09-10 00:26:12 -070097 /*
98 * Pipes are system-local resources, so sleeping on them
99 * is considered a noninteractive wait:
100 */
Mike Galbraithaf927232007-10-15 17:00:13 +0200101 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200102 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200104 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200105 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Arjan van de Ven858119e2006-01-14 13:20:43 -0800108static int
Jens Axboef6762b72006-05-01 20:02:05 +0200109pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
110 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 unsigned long copy;
113
114 while (len > 0) {
115 while (!iov->iov_len)
116 iov++;
117 copy = min_t(unsigned long, len, iov->iov_len);
118
Jens Axboef6762b72006-05-01 20:02:05 +0200119 if (atomic) {
120 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
121 return -EFAULT;
122 } else {
123 if (copy_from_user(to, iov->iov_base, copy))
124 return -EFAULT;
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 to += copy;
127 len -= copy;
128 iov->iov_base += copy;
129 iov->iov_len -= copy;
130 }
131 return 0;
132}
133
Arjan van de Ven858119e2006-01-14 13:20:43 -0800134static int
Jens Axboef6762b72006-05-01 20:02:05 +0200135pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
136 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 unsigned long copy;
139
140 while (len > 0) {
141 while (!iov->iov_len)
142 iov++;
143 copy = min_t(unsigned long, len, iov->iov_len);
144
Jens Axboef6762b72006-05-01 20:02:05 +0200145 if (atomic) {
146 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
147 return -EFAULT;
148 } else {
149 if (copy_to_user(iov->iov_base, from, copy))
150 return -EFAULT;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 from += copy;
153 len -= copy;
154 iov->iov_base += copy;
155 iov->iov_len -= copy;
156 }
157 return 0;
158}
159
Jens Axboef6762b72006-05-01 20:02:05 +0200160/*
161 * Attempt to pre-fault in the user memory, so we can use atomic copies.
162 * Returns the number of bytes not faulted in.
163 */
164static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
165{
166 while (!iov->iov_len)
167 iov++;
168
169 while (len > 0) {
170 unsigned long this_len;
171
172 this_len = min_t(unsigned long, len, iov->iov_len);
173 if (fault_in_pages_writeable(iov->iov_base, this_len))
174 break;
175
176 len -= this_len;
177 iov++;
178 }
179
180 return len;
181}
182
183/*
184 * Pre-fault in the user memory, so we can use atomic copies.
185 */
186static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
187{
188 while (!iov->iov_len)
189 iov++;
190
191 while (len > 0) {
192 unsigned long this_len;
193
194 this_len = min_t(unsigned long, len, iov->iov_len);
195 fault_in_pages_readable(iov->iov_base, this_len);
196 len -= this_len;
197 iov++;
198 }
199}
200
Ingo Molnar341b4462006-04-11 13:57:45 +0200201static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
202 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct page *page = buf->page;
205
Jens Axboe5274f052006-03-30 15:15:30 +0200206 /*
207 * If nobody else uses this page, and we don't already have a
208 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200209 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200210 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200211 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200212 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200213 else
214 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Jens Axboe08457182007-06-12 20:51:32 +0200217/**
218 * generic_pipe_buf_map - virtually map a pipe buffer
219 * @pipe: the pipe that the buffer belongs to
220 * @buf: the buffer that should be mapped
221 * @atomic: whether to use an atomic map
222 *
223 * Description:
224 * This function returns a kernel virtual address mapping for the
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800225 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
Jens Axboe08457182007-06-12 20:51:32 +0200226 * and the caller has to be careful not to fault before calling
227 * the unmap function.
228 *
Cong Wang2164d332012-06-23 11:33:51 +0800229 * Note that this function calls kmap_atomic() if @atomic != 0.
Jens Axboe08457182007-06-12 20:51:32 +0200230 */
Jens Axboef84d7512006-05-01 19:59:03 +0200231void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200232 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Jens Axboef6762b72006-05-01 20:02:05 +0200234 if (atomic) {
235 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800236 return kmap_atomic(buf->page);
Jens Axboef6762b72006-05-01 20:02:05 +0200237 }
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return kmap(buf->page);
240}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200241EXPORT_SYMBOL(generic_pipe_buf_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jens Axboe08457182007-06-12 20:51:32 +0200243/**
244 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
245 * @pipe: the pipe that the buffer belongs to
246 * @buf: the buffer that should be unmapped
247 * @map_data: the data that the mapping function returned
248 *
249 * Description:
250 * This function undoes the mapping that ->map() provided.
251 */
Jens Axboef84d7512006-05-01 19:59:03 +0200252void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200253 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Jens Axboef6762b72006-05-01 20:02:05 +0200255 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
256 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800257 kunmap_atomic(map_data);
Jens Axboef6762b72006-05-01 20:02:05 +0200258 } else
259 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200261EXPORT_SYMBOL(generic_pipe_buf_unmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jens Axboe08457182007-06-12 20:51:32 +0200263/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800264 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200265 * @pipe: the pipe that the buffer belongs to
266 * @buf: the buffer to attempt to steal
267 *
268 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800269 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200270 * @buf. If successful, this function returns 0 and returns with
271 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800272 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200273 * page cache.
274 */
Jens Axboe330ab712006-05-02 15:29:57 +0200275int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
276 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200277{
Jens Axboe46e678c2006-04-30 16:36:32 +0200278 struct page *page = buf->page;
279
Jens Axboe08457182007-06-12 20:51:32 +0200280 /*
281 * A reference of one is golden, that means that the owner of this
282 * page is the only one holding a reference to it. lock the page
283 * and return OK.
284 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200285 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200286 lock_page(page);
287 return 0;
288 }
289
290 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200291}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200292EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200293
Jens Axboe08457182007-06-12 20:51:32 +0200294/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800295 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200296 * @pipe: the pipe that the buffer belongs to
297 * @buf: the buffer to get a reference to
298 *
299 * Description:
300 * This function grabs an extra reference to @buf. It's used in
301 * in the tee() system call, when we duplicate the buffers in one
302 * pipe into another.
303 */
304void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200305{
306 page_cache_get(buf->page);
307}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200308EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200309
Jens Axboe08457182007-06-12 20:51:32 +0200310/**
311 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200312 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200313 * @buf: the buffer to confirm
314 *
315 * Description:
316 * This function does nothing, because the generic pipe code uses
317 * pages that are always good when inserted into the pipe.
318 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200319int generic_pipe_buf_confirm(struct pipe_inode_info *info,
320 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200321{
322 return 0;
323}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200324EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200325
Miklos Szeredi68181732009-05-07 15:37:36 +0200326/**
327 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
328 * @pipe: the pipe that the buffer belongs to
329 * @buf: the buffer to put a reference to
330 *
331 * Description:
332 * This function releases a reference to @buf.
333 */
334void generic_pipe_buf_release(struct pipe_inode_info *pipe,
335 struct pipe_buffer *buf)
336{
337 page_cache_release(buf->page);
338}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200339EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200340
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800341static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200343 .map = generic_pipe_buf_map,
344 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb02007-06-14 13:10:48 +0200345 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200347 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200348 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
Linus Torvalds9883035a2012-04-29 13:12:42 -0700351static const struct pipe_buf_operations packet_pipe_buf_ops = {
352 .can_merge = 0,
353 .map = generic_pipe_buf_map,
354 .unmap = generic_pipe_buf_unmap,
355 .confirm = generic_pipe_buf_confirm,
356 .release = anon_pipe_buf_release,
357 .steal = generic_pipe_buf_steal,
358 .get = generic_pipe_buf_get,
359};
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700362pipe_read(struct kiocb *iocb, const struct iovec *_iov,
363 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700365 struct file *filp = iocb->ki_filp;
Al Viro18c03cf2013-03-21 02:16:30 -0400366 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int do_wakeup;
368 ssize_t ret;
369 struct iovec *iov = (struct iovec *)_iov;
370 size_t total_len;
371
372 total_len = iov_length(iov, nr_segs);
373 /* Null read succeeds. */
374 if (unlikely(total_len == 0))
375 return 0;
376
377 do_wakeup = 0;
378 ret = 0;
Al Viro18c03cf2013-03-21 02:16:30 -0400379 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200381 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200383 int curbuf = pipe->curbuf;
384 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800385 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 void *addr;
387 size_t chars = buf->len;
Jens Axboef6762b72006-05-01 20:02:05 +0200388 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (chars > total_len)
391 chars = total_len;
392
Jens Axboecac36bb02007-06-14 13:10:48 +0200393 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200394 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200395 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200396 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200397 break;
398 }
Jens Axboef84d7512006-05-01 19:59:03 +0200399
Jens Axboef6762b72006-05-01 20:02:05 +0200400 atomic = !iov_fault_in_pages_write(iov, chars);
401redo:
402 addr = ops->map(pipe, buf, atomic);
403 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
404 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200406 /*
407 * Just retry with the slow path if we failed.
408 */
409 if (atomic) {
410 atomic = 0;
411 goto redo;
412 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200413 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200414 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416 }
417 ret += chars;
418 buf->offset += chars;
419 buf->len -= chars;
Linus Torvalds9883035a2012-04-29 13:12:42 -0700420
421 /* Was it a packet buffer? Clean up and exit */
422 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
423 total_len = chars;
424 buf->len = 0;
425 }
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!buf->len) {
428 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200429 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200430 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200431 pipe->curbuf = curbuf;
432 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 do_wakeup = 1;
434 }
435 total_len -= chars;
436 if (!total_len)
437 break; /* common path: read succeeded */
438 }
439 if (bufs) /* More to do? */
440 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200441 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200443 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* syscall merging: Usually we must not sleep
445 * if O_NONBLOCK is set, or if we got some data.
446 * But if a writer sleeps in kernel space, then
447 * we can wait for that data without violating POSIX.
448 */
449 if (ret)
450 break;
451 if (filp->f_flags & O_NONBLOCK) {
452 ret = -EAGAIN;
453 break;
454 }
455 }
456 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200457 if (!ret)
458 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 }
461 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800462 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200463 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200465 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Al Viro18c03cf2013-03-21 02:16:30 -0400467 pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200468
469 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800471 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200472 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 if (ret > 0)
475 file_accessed(filp);
476 return ret;
477}
478
Linus Torvalds9883035a2012-04-29 13:12:42 -0700479static inline int is_packetized(struct file *file)
480{
481 return (file->f_flags & O_DIRECT) != 0;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700485pipe_write(struct kiocb *iocb, const struct iovec *_iov,
486 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700488 struct file *filp = iocb->ki_filp;
Al Viro18c03cf2013-03-21 02:16:30 -0400489 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ssize_t ret;
491 int do_wakeup;
492 struct iovec *iov = (struct iovec *)_iov;
493 size_t total_len;
494 ssize_t chars;
495
496 total_len = iov_length(iov, nr_segs);
497 /* Null write succeeds. */
498 if (unlikely(total_len == 0))
499 return 0;
500
501 do_wakeup = 0;
502 ret = 0;
Al Viro18c03cf2013-03-21 02:16:30 -0400503 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Ingo Molnar923f4f22006-04-11 13:53:33 +0200505 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 send_sig(SIGPIPE, current, 0);
507 ret = -EPIPE;
508 goto out;
509 }
510
511 /* We try to merge small writes */
512 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200513 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200514 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200515 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200516 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800517 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200521 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200522 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200523
Jens Axboecac36bb02007-06-14 13:10:48 +0200524 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200525 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200526 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200527
Jens Axboef6762b72006-05-01 20:02:05 +0200528 iov_fault_in_pages_read(iov, chars);
529redo1:
530 addr = ops->map(pipe, buf, atomic);
Jens Axboe5274f052006-03-30 15:15:30 +0200531 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200532 chars, atomic);
533 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ret = error;
535 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200536 if (error) {
537 if (atomic) {
538 atomic = 0;
539 goto redo1;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 buf->len += chars;
544 total_len -= chars;
545 ret = chars;
546 if (!total_len)
547 goto out;
548 }
549 }
550
551 for (;;) {
552 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200553
Ingo Molnar923f4f22006-04-11 13:53:33 +0200554 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200556 if (!ret)
557 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200560 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200561 if (bufs < pipe->buffers) {
562 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200563 struct pipe_buffer *buf = pipe->bufs + newbuf;
564 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200565 char *src;
566 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (!page) {
569 page = alloc_page(GFP_HIGHUSER);
570 if (unlikely(!page)) {
571 ret = ret ? : -ENOMEM;
572 break;
573 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200574 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200576 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * we lock up (O_NONBLOCK-)readers that sleep due to
578 * syscall merging.
579 * FIXME! Is this really true?
580 */
581 do_wakeup = 1;
582 chars = PAGE_SIZE;
583 if (chars > total_len)
584 chars = total_len;
585
Jens Axboef6762b72006-05-01 20:02:05 +0200586 iov_fault_in_pages_read(iov, chars);
587redo2:
588 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800589 src = kmap_atomic(page);
Jens Axboef6762b72006-05-01 20:02:05 +0200590 else
591 src = kmap(page);
592
593 error = pipe_iov_copy_from_user(src, iov, chars,
594 atomic);
595 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800596 kunmap_atomic(src);
Jens Axboef6762b72006-05-01 20:02:05 +0200597 else
598 kunmap(page);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200601 if (atomic) {
602 atomic = 0;
603 goto redo2;
604 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200605 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200606 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608 }
609 ret += chars;
610
611 /* Insert it into the buffer array */
612 buf->page = page;
613 buf->ops = &anon_pipe_buf_ops;
614 buf->offset = 0;
615 buf->len = chars;
Linus Torvalds9883035a2012-04-29 13:12:42 -0700616 buf->flags = 0;
617 if (is_packetized(filp)) {
618 buf->ops = &packet_pipe_buf_ops;
619 buf->flags = PIPE_BUF_FLAG_PACKET;
620 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200621 pipe->nrbufs = ++bufs;
622 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 total_len -= chars;
625 if (!total_len)
626 break;
627 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200628 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 continue;
630 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200631 if (!ret)
632 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 }
635 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200636 if (!ret)
637 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639 }
640 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800641 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200642 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 do_wakeup = 0;
644 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200645 pipe->waiting_writers++;
646 pipe_wait(pipe);
647 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649out:
Al Viro18c03cf2013-03-21 02:16:30 -0400650 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800652 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200653 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Josef Bacikc3b2da32012-03-26 09:59:21 -0400655 if (ret > 0) {
656 int err = file_update_time(filp);
657 if (err)
658 ret = err;
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return ret;
661}
662
Andi Kleend59d0b12008-02-08 04:21:23 -0800663static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Al Viro18c03cf2013-03-21 02:16:30 -0400665 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int count, buf, nrbufs;
667
668 switch (cmd) {
669 case FIONREAD:
Al Viro18c03cf2013-03-21 02:16:30 -0400670 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200672 buf = pipe->curbuf;
673 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200675 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200676 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Al Viro18c03cf2013-03-21 02:16:30 -0400678 pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return put_user(count, (int __user *)arg);
681 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100682 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684}
685
686/* No kernel lock held - fine */
687static unsigned int
688pipe_poll(struct file *filp, poll_table *wait)
689{
690 unsigned int mask;
Al Viro18c03cf2013-03-21 02:16:30 -0400691 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int nrbufs;
693
Ingo Molnar923f4f22006-04-11 13:53:33 +0200694 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200697 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 mask = 0;
699 if (filp->f_mode & FMODE_READ) {
700 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200701 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 mask |= POLLHUP;
703 }
704
705 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200706 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700707 /*
708 * Most Unices do not set POLLERR for FIFOs but on Linux they
709 * behave exactly like pipes for poll().
710 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200711 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 mask |= POLLERR;
713 }
714
715 return mask;
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static int
Al Viro599a0ac2013-03-12 09:58:10 -0400719pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Al Viroba5bb142013-03-21 02:21:19 -0400721 struct pipe_inode_info *pipe = inode->i_pipe;
722 int kill = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200723
Al Viroba5bb142013-03-21 02:21:19 -0400724 pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400725 if (file->f_mode & FMODE_READ)
726 pipe->readers--;
727 if (file->f_mode & FMODE_WRITE)
728 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200729
Al Viroba5bb142013-03-21 02:21:19 -0400730 if (pipe->readers || pipe->writers) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800731 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200732 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
733 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Al Viroba5bb142013-03-21 02:21:19 -0400735 spin_lock(&inode->i_lock);
736 if (!--pipe->files) {
737 inode->i_pipe = NULL;
738 kill = 1;
739 }
740 spin_unlock(&inode->i_lock);
741 pipe_unlock(pipe);
742
743 if (kill)
744 __free_pipe_info(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 return 0;
747}
748
749static int
Al Viro599a0ac2013-03-12 09:58:10 -0400750pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Al Viro18c03cf2013-03-21 02:16:30 -0400752 struct pipe_inode_info *pipe = file_inode(filp)->i_pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400753 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Al Viro18c03cf2013-03-21 02:16:30 -0400755 pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400756 if (filp->f_mode & FMODE_READ)
757 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
758 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200759 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400760 if (retval < 0 && (filp->f_mode & FMODE_READ))
761 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700762 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
763 }
Al Viro18c03cf2013-03-21 02:16:30 -0400764 pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700765 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Ingo Molnar3a326a22006-04-10 15:18:35 +0200768struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
769{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200770 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200771
Ingo Molnar923f4f22006-04-11 13:53:33 +0200772 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
773 if (pipe) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200774 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
775 if (pipe->bufs) {
776 init_waitqueue_head(&pipe->wait);
777 pipe->r_counter = pipe->w_counter = 1;
778 pipe->inode = inode;
779 pipe->buffers = PIPE_DEF_BUFFERS;
780 return pipe;
781 }
782 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200783 }
784
Jens Axboe35f3d142010-05-20 10:43:18 +0200785 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200786}
787
Ingo Molnar923f4f22006-04-11 13:53:33 +0200788void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Jens Axboe35f3d142010-05-20 10:43:18 +0200792 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200793 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200795 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200797 if (pipe->tmp_page)
798 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200799 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200800 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Jens Axboeb92ce552006-04-11 13:52:07 +0200803void free_pipe_info(struct inode *inode)
804{
805 __free_pipe_info(inode->i_pipe);
806 inode->i_pipe = NULL;
807}
808
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800809static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200810
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700811/*
812 * pipefs_dname() is called from d_path().
813 */
814static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
815{
816 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
817 dentry->d_inode->i_ino);
818}
819
Al Viro3ba13d12009-02-20 06:02:22 +0000820static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700821 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822};
823
824static struct inode * get_pipe_inode(void)
825{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200826 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200827 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (!inode)
830 goto fail_inode;
831
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400832 inode->i_ino = get_next_ino();
833
Ingo Molnar923f4f22006-04-11 13:53:33 +0200834 pipe = alloc_pipe_info(inode);
835 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200837
Al Viroba5bb142013-03-21 02:21:19 -0400838 inode->i_pipe = pipe;
839 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200840 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400841 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /*
844 * Mark the inode dirty from the very beginning,
845 * that way it will never be moved to the dirty
846 * list because "mark_inode_dirty()" will think
847 * that it already _is_ on the dirty list.
848 */
849 inode->i_state = I_DIRTY;
850 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100851 inode->i_uid = current_fsuid();
852 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return inode;
856
857fail_iput:
858 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860fail_inode:
861 return NULL;
862}
863
Al Viroe4fad8e2012-07-21 15:33:25 +0400864int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Andi Kleend6cbd282006-09-30 23:29:26 -0700866 int err;
Al Viroe4fad8e2012-07-21 15:33:25 +0400867 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700868 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +0400869 struct path path;
Al Viroe4fad8e2012-07-21 15:33:25 +0400870 static struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400873 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Andi Kleend6cbd282006-09-30 23:29:26 -0700875 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +1100876 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +0400877 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -0700878 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +0400879 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +0200880
Al Viro2c48b9c2009-08-09 00:52:35 +0400881 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -0800882
883 err = -ENFILE;
Al Viro599a0ac2013-03-12 09:58:10 -0400884 f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
Anatol Pomozov39b652522012-09-12 20:11:55 -0700885 if (IS_ERR(f))
Dave Hansen430e2852008-02-15 14:37:26 -0800886 goto err_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds9883035a2012-04-29 13:12:42 -0700888 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Al Viro599a0ac2013-03-12 09:58:10 -0400890 res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
Anatol Pomozov39b652522012-09-12 20:11:55 -0700891 if (IS_ERR(res[0]))
Al Viroe4fad8e2012-07-21 15:33:25 +0400892 goto err_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Al Viroe4fad8e2012-07-21 15:33:25 +0400894 path_get(&path);
895 res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
896 res[1] = f;
897 return 0;
898
899err_file:
900 put_filp(f);
901err_dentry:
Al Viroed152432008-04-22 19:51:27 -0400902 free_pipe_info(inode);
Al Viro2c48b9c2009-08-09 00:52:35 +0400903 path_put(&path);
Al Viroe4fad8e2012-07-21 15:33:25 +0400904 return err;
Al Viroed152432008-04-22 19:51:27 -0400905
Al Viroe4fad8e2012-07-21 15:33:25 +0400906err_inode:
Andi Kleend6cbd282006-09-30 23:29:26 -0700907 free_pipe_info(inode);
908 iput(inode);
Al Viroe4fad8e2012-07-21 15:33:25 +0400909 return err;
Andi Kleend6cbd282006-09-30 23:29:26 -0700910}
911
Al Viro5b249b12012-08-19 12:17:29 -0400912static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700913{
Andi Kleend6cbd282006-09-30 23:29:26 -0700914 int error;
915 int fdw, fdr;
916
Linus Torvalds9883035a2012-04-29 13:12:42 -0700917 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700918 return -EINVAL;
919
Al Viroe4fad8e2012-07-21 15:33:25 +0400920 error = create_pipe_files(files, flags);
921 if (error)
922 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700923
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700924 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700925 if (error < 0)
926 goto err_read_pipe;
927 fdr = error;
928
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700929 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700930 if (error < 0)
931 goto err_fdr;
932 fdw = error;
933
Al Viro157cf642008-12-14 04:57:47 -0500934 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700935 fd[0] = fdr;
936 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938
Andi Kleend6cbd282006-09-30 23:29:26 -0700939 err_fdr:
940 put_unused_fd(fdr);
941 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400942 fput(files[0]);
943 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700944 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Al Viro5b249b12012-08-19 12:17:29 -0400947int do_pipe_flags(int *fd, int flags)
948{
949 struct file *files[2];
950 int error = __do_pipe_flags(fd, files, flags);
951 if (!error) {
952 fd_install(fd[0], files[0]);
953 fd_install(fd[1], files[1]);
954 }
955 return error;
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400959 * sys_pipe() is the normal C calling standard for creating
960 * a pipe. It's not the way Unix traditionally does this, though.
961 */
Heiko Carstensd4e82042009-01-14 14:14:34 +0100962SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400963{
Al Viro5b249b12012-08-19 12:17:29 -0400964 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400965 int fd[2];
966 int error;
967
Al Viro5b249b12012-08-19 12:17:29 -0400968 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400969 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400970 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
971 fput(files[0]);
972 fput(files[1]);
973 put_unused_fd(fd[0]);
974 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400975 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400976 } else {
977 fd_install(fd[0], files[0]);
978 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700979 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400980 }
981 return error;
982}
983
Heiko Carstens2b664212009-01-14 14:14:35 +0100984SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700985{
986 return sys_pipe2(fildes, 0);
987}
988
Al Virofc7478a2013-03-21 02:07:59 -0400989static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400990{
991 int cur = *cnt;
992
993 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400994 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400995 if (signal_pending(current))
996 break;
997 }
998 return cur == *cnt ? -ERESTARTSYS : 0;
999}
1000
Al Virofc7478a2013-03-21 02:07:59 -04001001static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -04001002{
Al Virofc7478a2013-03-21 02:07:59 -04001003 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -04001004}
1005
1006static int fifo_open(struct inode *inode, struct file *filp)
1007{
1008 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -04001009 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Viroba5bb142013-03-21 02:21:19 -04001010 int kill = 0;
Al Virof776c732013-03-12 09:46:27 -04001011 int ret;
1012
Al Viroba5bb142013-03-21 02:21:19 -04001013 filp->f_version = 0;
1014
1015 spin_lock(&inode->i_lock);
1016 if (inode->i_pipe) {
1017 pipe = inode->i_pipe;
1018 pipe->files++;
1019 spin_unlock(&inode->i_lock);
1020 } else {
1021 spin_unlock(&inode->i_lock);
Al Virof776c732013-03-12 09:46:27 -04001022 pipe = alloc_pipe_info(inode);
1023 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -04001024 return -ENOMEM;
1025 pipe->files = 1;
1026 spin_lock(&inode->i_lock);
1027 if (unlikely(inode->i_pipe)) {
1028 inode->i_pipe->files++;
1029 spin_unlock(&inode->i_lock);
1030 __free_pipe_info(pipe);
1031 pipe = inode->i_pipe;
1032 } else {
1033 inode->i_pipe = pipe;
1034 spin_unlock(&inode->i_lock);
1035 }
Al Virof776c732013-03-12 09:46:27 -04001036 }
Al Viroba5bb142013-03-21 02:21:19 -04001037 /* OK, we have a pipe and it's pinned down */
1038
1039 pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001040
1041 /* We can only do regular read/write on fifos */
1042 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
1043
1044 switch (filp->f_mode) {
1045 case FMODE_READ:
1046 /*
1047 * O_RDONLY
1048 * POSIX.1 says that O_NONBLOCK means return with the FIFO
1049 * opened, even when there is no process writing the FIFO.
1050 */
Al Virof776c732013-03-12 09:46:27 -04001051 pipe->r_counter++;
1052 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -04001053 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001054
Al Viro599a0ac2013-03-12 09:58:10 -04001055 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -04001056 if ((filp->f_flags & O_NONBLOCK)) {
1057 /* suppress POLLHUP until we have
1058 * seen a writer */
1059 filp->f_version = pipe->w_counter;
1060 } else {
Al Virofc7478a2013-03-21 02:07:59 -04001061 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -04001062 goto err_rd;
1063 }
1064 }
1065 break;
1066
1067 case FMODE_WRITE:
1068 /*
1069 * O_WRONLY
1070 * POSIX.1 says that O_NONBLOCK means return -1 with
1071 * errno=ENXIO when there is no process reading the FIFO.
1072 */
1073 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -04001074 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -04001075 goto err;
1076
Al Virof776c732013-03-12 09:46:27 -04001077 pipe->w_counter++;
1078 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -04001079 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001080
Al Viro599a0ac2013-03-12 09:58:10 -04001081 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -04001082 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -04001083 goto err_wr;
1084 }
1085 break;
1086
1087 case FMODE_READ | FMODE_WRITE:
1088 /*
1089 * O_RDWR
1090 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1091 * This implementation will NEVER block on a O_RDWR open, since
1092 * the process can at least talk to itself.
1093 */
Al Virof776c732013-03-12 09:46:27 -04001094
1095 pipe->readers++;
1096 pipe->writers++;
1097 pipe->r_counter++;
1098 pipe->w_counter++;
1099 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -04001100 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001101 break;
1102
1103 default:
1104 ret = -EINVAL;
1105 goto err;
1106 }
1107
1108 /* Ok! */
Al Viroba5bb142013-03-21 02:21:19 -04001109 pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001110 return 0;
1111
1112err_rd:
1113 if (!--pipe->readers)
1114 wake_up_interruptible(&pipe->wait);
1115 ret = -ERESTARTSYS;
1116 goto err;
1117
1118err_wr:
1119 if (!--pipe->writers)
1120 wake_up_interruptible(&pipe->wait);
1121 ret = -ERESTARTSYS;
1122 goto err;
1123
1124err:
Al Viroba5bb142013-03-21 02:21:19 -04001125 spin_lock(&inode->i_lock);
1126 if (!--pipe->files) {
1127 inode->i_pipe = NULL;
1128 kill = 1;
1129 }
1130 spin_unlock(&inode->i_lock);
1131 pipe_unlock(pipe);
1132 if (kill)
1133 __free_pipe_info(pipe);
Al Virof776c732013-03-12 09:46:27 -04001134 return ret;
1135}
1136
Al Viro599a0ac2013-03-12 09:58:10 -04001137const struct file_operations pipefifo_fops = {
1138 .open = fifo_open,
1139 .llseek = no_llseek,
1140 .read = do_sync_read,
1141 .aio_read = pipe_read,
1142 .write = do_sync_write,
1143 .aio_write = pipe_write,
1144 .poll = pipe_poll,
1145 .unlocked_ioctl = pipe_ioctl,
1146 .release = pipe_release,
1147 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001148};
1149
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001150/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001151 * Allocate a new array of pipe buffers and copy the info over. Returns the
1152 * pipe size if successful, or return -ERROR on error.
1153 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001154static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001155{
1156 struct pipe_buffer *bufs;
1157
1158 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001159 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1160 * expect a lot of shrink+grow operations, just free and allocate
1161 * again like we would do for growing. If the pipe currently
1162 * contains more buffers than arg, then return busy.
1163 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001164 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001165 return -EBUSY;
1166
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001167 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001168 if (unlikely(!bufs))
1169 return -ENOMEM;
1170
1171 /*
1172 * The pipe array wraps around, so just start the new one at zero
1173 * and adjust the indexes.
1174 */
1175 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001176 unsigned int tail;
1177 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001178
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001179 tail = pipe->curbuf + pipe->nrbufs;
1180 if (tail < pipe->buffers)
1181 tail = 0;
1182 else
1183 tail &= (pipe->buffers - 1);
1184
1185 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001186 if (head)
1187 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1188 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001189 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001190 }
1191
1192 pipe->curbuf = 0;
1193 kfree(pipe->bufs);
1194 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001195 pipe->buffers = nr_pages;
1196 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001197}
1198
Jens Axboeff9da692010-06-03 14:54:39 +02001199/*
1200 * Currently we rely on the pipe array holding a power-of-2 number
1201 * of pages.
1202 */
1203static inline unsigned int round_pipe_size(unsigned int size)
1204{
1205 unsigned long nr_pages;
1206
1207 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1208 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1209}
1210
1211/*
1212 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1213 * will return an error.
1214 */
1215int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1216 size_t *lenp, loff_t *ppos)
1217{
1218 int ret;
1219
1220 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1221 if (ret < 0 || !write)
1222 return ret;
1223
1224 pipe_max_size = round_pipe_size(pipe_max_size);
1225 return ret;
1226}
1227
Linus Torvalds72083642010-11-28 16:27:19 -08001228/*
1229 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1230 * location, so checking ->i_pipe is not enough to verify that this is a
1231 * pipe.
1232 */
1233struct pipe_inode_info *get_pipe_info(struct file *file)
1234{
Al Viro496ad9a2013-01-23 17:07:38 -05001235 struct inode *i = file_inode(file);
Linus Torvalds72083642010-11-28 16:27:19 -08001236
1237 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1238}
1239
Jens Axboe35f3d142010-05-20 10:43:18 +02001240long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1241{
1242 struct pipe_inode_info *pipe;
1243 long ret;
1244
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001245 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001246 if (!pipe)
1247 return -EBADF;
1248
Al Viro18c03cf2013-03-21 02:16:30 -04001249 pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001250
1251 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001252 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001253 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001254
Jens Axboeff9da692010-06-03 14:54:39 +02001255 size = round_pipe_size(arg);
1256 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001257
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001258 ret = -EINVAL;
1259 if (!nr_pages)
1260 goto out;
1261
Jens Axboeff9da692010-06-03 14:54:39 +02001262 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001263 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001264 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001265 }
Jens Axboeff9da692010-06-03 14:54:39 +02001266 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001267 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001268 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001269 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001270 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001271 break;
1272 default:
1273 ret = -EINVAL;
1274 break;
1275 }
1276
Julia Lawallcc967be2010-05-26 17:54:39 +02001277out:
Al Viro18c03cf2013-03-21 02:16:30 -04001278 pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001279 return ret;
1280}
1281
Nick Pigginff0c7d12011-01-07 17:49:50 +11001282static const struct super_operations pipefs_ops = {
1283 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001284 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001285};
1286
Jens Axboe35f3d142010-05-20 10:43:18 +02001287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * pipefs should _never_ be mounted by userland - too much of security hassle,
1289 * no real gain from having the whole whorehouse mounted. So we don't need
1290 * any operations on the root directory. However, we need a non-trivial
1291 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1292 */
Al Viro51139ad2010-07-25 23:47:46 +04001293static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1294 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Al Viroc74a1cb2011-01-12 16:59:34 -05001296 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1297 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300static struct file_system_type pipe_fs_type = {
1301 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001302 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .kill_sb = kill_anon_super,
1304};
1305
1306static int __init init_pipe_fs(void)
1307{
1308 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (!err) {
1311 pipe_mnt = kern_mount(&pipe_fs_type);
1312 if (IS_ERR(pipe_mnt)) {
1313 err = PTR_ERR(pipe_mnt);
1314 unregister_filesystem(&pipe_fs_type);
1315 }
1316 }
1317 return err;
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320fs_initcall(init_pipe_fs);