blob: 25feaa3faac068eba842438f9171f78ff189d7f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/pipe.c
3 *
4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
5 */
6
7#include <linux/mm.h>
8#include <linux/file.h>
9#include <linux/poll.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/fs.h>
Jens Axboe35f3d142010-05-20 10:43:18 +020014#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mount.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070016#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/pipe_fs_i.h>
18#include <linux/uio.h>
19#include <linux/highmem.h>
Jens Axboe5274f052006-03-30 15:15:30 +020020#include <linux/pagemap.h>
Al Virodb349502007-02-07 01:48:00 -050021#include <linux/audit.h>
Ulrich Drepperba719ba2008-05-06 20:42:38 -070022#include <linux/syscalls.h>
Jens Axboeb492e952010-05-19 21:03:16 +020023#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26#include <asm/ioctls.h>
27
28/*
Jens Axboeb492e952010-05-19 21:03:16 +020029 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020030 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020031 */
Jens Axboeff9da692010-06-03 14:54:39 +020032unsigned int pipe_max_size = 1048576;
33
34/*
35 * Minimum pipe size, as required by POSIX
36 */
37unsigned int pipe_min_size = PAGE_SIZE;
Jens Axboeb492e952010-05-19 21:03:16 +020038
39/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * We use a start+len construction, which provides full use of the
41 * allocated memory.
42 * -- Florian Coosmann (FGC)
43 *
44 * Reads with count = 0 should always return 0.
45 * -- Julian Bradfield 1999-06-07.
46 *
47 * FIFOs and Pipes now generate SIGIO for both readers and writers.
48 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
49 *
50 * pipe_read & write cleanup
51 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
52 */
53
Miklos Szeredi61e0d472009-04-14 19:48:41 +020054static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
55{
56 if (pipe->inode)
57 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
58}
59
60void pipe_lock(struct pipe_inode_info *pipe)
61{
62 /*
63 * pipe_lock() nests non-pipe inode locks (for writing to a file)
64 */
65 pipe_lock_nested(pipe, I_MUTEX_PARENT);
66}
67EXPORT_SYMBOL(pipe_lock);
68
69void pipe_unlock(struct pipe_inode_info *pipe)
70{
71 if (pipe->inode)
72 mutex_unlock(&pipe->inode->i_mutex);
73}
74EXPORT_SYMBOL(pipe_unlock);
75
76void pipe_double_lock(struct pipe_inode_info *pipe1,
77 struct pipe_inode_info *pipe2)
78{
79 BUG_ON(pipe1 == pipe2);
80
81 if (pipe1 < pipe2) {
82 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
83 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
84 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +020085 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
86 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020087 }
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +020091void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 DEFINE_WAIT(wait);
94
Ingo Molnard79fc0f2005-09-10 00:26:12 -070095 /*
96 * Pipes are system-local resources, so sleeping on them
97 * is considered a noninteractive wait:
98 */
Mike Galbraithaf927232007-10-15 17:00:13 +020099 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200100 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200102 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200103 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Arjan van de Ven858119e2006-01-14 13:20:43 -0800106static int
Jens Axboef6762b72006-05-01 20:02:05 +0200107pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
108 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 unsigned long copy;
111
112 while (len > 0) {
113 while (!iov->iov_len)
114 iov++;
115 copy = min_t(unsigned long, len, iov->iov_len);
116
Jens Axboef6762b72006-05-01 20:02:05 +0200117 if (atomic) {
118 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
119 return -EFAULT;
120 } else {
121 if (copy_from_user(to, iov->iov_base, copy))
122 return -EFAULT;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 to += copy;
125 len -= copy;
126 iov->iov_base += copy;
127 iov->iov_len -= copy;
128 }
129 return 0;
130}
131
Arjan van de Ven858119e2006-01-14 13:20:43 -0800132static int
Jens Axboef6762b72006-05-01 20:02:05 +0200133pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
134 int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 unsigned long copy;
137
138 while (len > 0) {
139 while (!iov->iov_len)
140 iov++;
141 copy = min_t(unsigned long, len, iov->iov_len);
142
Jens Axboef6762b72006-05-01 20:02:05 +0200143 if (atomic) {
144 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
145 return -EFAULT;
146 } else {
147 if (copy_to_user(iov->iov_base, from, copy))
148 return -EFAULT;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 from += copy;
151 len -= copy;
152 iov->iov_base += copy;
153 iov->iov_len -= copy;
154 }
155 return 0;
156}
157
Jens Axboef6762b72006-05-01 20:02:05 +0200158/*
159 * Attempt to pre-fault in the user memory, so we can use atomic copies.
160 * Returns the number of bytes not faulted in.
161 */
162static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
163{
164 while (!iov->iov_len)
165 iov++;
166
167 while (len > 0) {
168 unsigned long this_len;
169
170 this_len = min_t(unsigned long, len, iov->iov_len);
171 if (fault_in_pages_writeable(iov->iov_base, this_len))
172 break;
173
174 len -= this_len;
175 iov++;
176 }
177
178 return len;
179}
180
181/*
182 * Pre-fault in the user memory, so we can use atomic copies.
183 */
184static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
185{
186 while (!iov->iov_len)
187 iov++;
188
189 while (len > 0) {
190 unsigned long this_len;
191
192 this_len = min_t(unsigned long, len, iov->iov_len);
193 fault_in_pages_readable(iov->iov_base, this_len);
194 len -= this_len;
195 iov++;
196 }
197}
198
Ingo Molnar341b4462006-04-11 13:57:45 +0200199static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
200 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct page *page = buf->page;
203
Jens Axboe5274f052006-03-30 15:15:30 +0200204 /*
205 * If nobody else uses this page, and we don't already have a
206 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200207 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200208 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200209 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200210 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200211 else
212 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Jens Axboe08457182007-06-12 20:51:32 +0200215/**
216 * generic_pipe_buf_map - virtually map a pipe buffer
217 * @pipe: the pipe that the buffer belongs to
218 * @buf: the buffer that should be mapped
219 * @atomic: whether to use an atomic map
220 *
221 * Description:
222 * This function returns a kernel virtual address mapping for the
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800223 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
Jens Axboe08457182007-06-12 20:51:32 +0200224 * and the caller has to be careful not to fault before calling
225 * the unmap function.
226 *
227 * Note that this function occupies KM_USER0 if @atomic != 0.
228 */
Jens Axboef84d7512006-05-01 19:59:03 +0200229void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200230 struct pipe_buffer *buf, int atomic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Jens Axboef6762b72006-05-01 20:02:05 +0200232 if (atomic) {
233 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800234 return kmap_atomic(buf->page);
Jens Axboef6762b72006-05-01 20:02:05 +0200235 }
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return kmap(buf->page);
238}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200239EXPORT_SYMBOL(generic_pipe_buf_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jens Axboe08457182007-06-12 20:51:32 +0200241/**
242 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
243 * @pipe: the pipe that the buffer belongs to
244 * @buf: the buffer that should be unmapped
245 * @map_data: the data that the mapping function returned
246 *
247 * Description:
248 * This function undoes the mapping that ->map() provided.
249 */
Jens Axboef84d7512006-05-01 19:59:03 +0200250void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
Jens Axboef6762b72006-05-01 20:02:05 +0200251 struct pipe_buffer *buf, void *map_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Jens Axboef6762b72006-05-01 20:02:05 +0200253 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
254 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800255 kunmap_atomic(map_data);
Jens Axboef6762b72006-05-01 20:02:05 +0200256 } else
257 kunmap(buf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200259EXPORT_SYMBOL(generic_pipe_buf_unmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jens Axboe08457182007-06-12 20:51:32 +0200261/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800262 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200263 * @pipe: the pipe that the buffer belongs to
264 * @buf: the buffer to attempt to steal
265 *
266 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800267 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200268 * @buf. If successful, this function returns 0 and returns with
269 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800270 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200271 * page cache.
272 */
Jens Axboe330ab712006-05-02 15:29:57 +0200273int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
274 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200275{
Jens Axboe46e678c2006-04-30 16:36:32 +0200276 struct page *page = buf->page;
277
Jens Axboe08457182007-06-12 20:51:32 +0200278 /*
279 * A reference of one is golden, that means that the owner of this
280 * page is the only one holding a reference to it. lock the page
281 * and return OK.
282 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200283 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200284 lock_page(page);
285 return 0;
286 }
287
288 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200289}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200290EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200291
Jens Axboe08457182007-06-12 20:51:32 +0200292/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800293 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200294 * @pipe: the pipe that the buffer belongs to
295 * @buf: the buffer to get a reference to
296 *
297 * Description:
298 * This function grabs an extra reference to @buf. It's used in
299 * in the tee() system call, when we duplicate the buffers in one
300 * pipe into another.
301 */
302void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200303{
304 page_cache_get(buf->page);
305}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200306EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200307
Jens Axboe08457182007-06-12 20:51:32 +0200308/**
309 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200310 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200311 * @buf: the buffer to confirm
312 *
313 * Description:
314 * This function does nothing, because the generic pipe code uses
315 * pages that are always good when inserted into the pipe.
316 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200317int generic_pipe_buf_confirm(struct pipe_inode_info *info,
318 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200319{
320 return 0;
321}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200322EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200323
Miklos Szeredi68181732009-05-07 15:37:36 +0200324/**
325 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
326 * @pipe: the pipe that the buffer belongs to
327 * @buf: the buffer to put a reference to
328 *
329 * Description:
330 * This function releases a reference to @buf.
331 */
332void generic_pipe_buf_release(struct pipe_inode_info *pipe,
333 struct pipe_buffer *buf)
334{
335 page_cache_release(buf->page);
336}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200337EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200338
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800339static const struct pipe_buf_operations anon_pipe_buf_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .can_merge = 1,
Jens Axboef84d7512006-05-01 19:59:03 +0200341 .map = generic_pipe_buf_map,
342 .unmap = generic_pipe_buf_unmap,
Jens Axboecac36bb02007-06-14 13:10:48 +0200343 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 .release = anon_pipe_buf_release,
Jens Axboe330ab712006-05-02 15:29:57 +0200345 .steal = generic_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200346 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
349static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700350pipe_read(struct kiocb *iocb, const struct iovec *_iov,
351 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700353 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800354 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200355 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int do_wakeup;
357 ssize_t ret;
358 struct iovec *iov = (struct iovec *)_iov;
359 size_t total_len;
360
361 total_len = iov_length(iov, nr_segs);
362 /* Null read succeeds. */
363 if (unlikely(total_len == 0))
364 return 0;
365
366 do_wakeup = 0;
367 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200368 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200369 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200371 int bufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (bufs) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200373 int curbuf = pipe->curbuf;
374 struct pipe_buffer *buf = pipe->bufs + curbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800375 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 void *addr;
377 size_t chars = buf->len;
Jens Axboef6762b72006-05-01 20:02:05 +0200378 int error, atomic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (chars > total_len)
381 chars = total_len;
382
Jens Axboecac36bb02007-06-14 13:10:48 +0200383 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200384 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200385 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200386 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200387 break;
388 }
Jens Axboef84d7512006-05-01 19:59:03 +0200389
Jens Axboef6762b72006-05-01 20:02:05 +0200390 atomic = !iov_fault_in_pages_write(iov, chars);
391redo:
392 addr = ops->map(pipe, buf, atomic);
393 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
394 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200396 /*
397 * Just retry with the slow path if we failed.
398 */
399 if (atomic) {
400 atomic = 0;
401 goto redo;
402 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200403 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200404 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406 }
407 ret += chars;
408 buf->offset += chars;
409 buf->len -= chars;
410 if (!buf->len) {
411 buf->ops = NULL;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200412 ops->release(pipe, buf);
Jens Axboe35f3d142010-05-20 10:43:18 +0200413 curbuf = (curbuf + 1) & (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200414 pipe->curbuf = curbuf;
415 pipe->nrbufs = --bufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 do_wakeup = 1;
417 }
418 total_len -= chars;
419 if (!total_len)
420 break; /* common path: read succeeded */
421 }
422 if (bufs) /* More to do? */
423 continue;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200424 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200426 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* syscall merging: Usually we must not sleep
428 * if O_NONBLOCK is set, or if we got some data.
429 * But if a writer sleeps in kernel space, then
430 * we can wait for that data without violating POSIX.
431 */
432 if (ret)
433 break;
434 if (filp->f_flags & O_NONBLOCK) {
435 ret = -EAGAIN;
436 break;
437 }
438 }
439 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200440 if (!ret)
441 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443 }
444 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800445 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200446 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200448 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200450 mutex_unlock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200451
452 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800454 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200455 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 if (ret > 0)
458 file_accessed(filp);
459 return ret;
460}
461
462static ssize_t
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700463pipe_write(struct kiocb *iocb, const struct iovec *_iov,
464 unsigned long nr_segs, loff_t ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700466 struct file *filp = iocb->ki_filp;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800467 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200468 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ssize_t ret;
470 int do_wakeup;
471 struct iovec *iov = (struct iovec *)_iov;
472 size_t total_len;
473 ssize_t chars;
474
475 total_len = iov_length(iov, nr_segs);
476 /* Null write succeeds. */
477 if (unlikely(total_len == 0))
478 return 0;
479
480 do_wakeup = 0;
481 ret = 0;
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200482 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200483 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Ingo Molnar923f4f22006-04-11 13:53:33 +0200485 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 send_sig(SIGPIPE, current, 0);
487 ret = -EPIPE;
488 goto out;
489 }
490
491 /* We try to merge small writes */
492 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200493 if (pipe->nrbufs && chars != 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200494 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
Jens Axboe35f3d142010-05-20 10:43:18 +0200495 (pipe->buffers - 1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200496 struct pipe_buffer *buf = pipe->bufs + lastbuf;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800497 const struct pipe_buf_operations *ops = buf->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
Jens Axboef6762b72006-05-01 20:02:05 +0200501 int error, atomic = 1;
Jens Axboe5274f052006-03-30 15:15:30 +0200502 void *addr;
Jens Axboe5274f052006-03-30 15:15:30 +0200503
Jens Axboecac36bb02007-06-14 13:10:48 +0200504 error = ops->confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200505 if (error)
Jens Axboe5274f052006-03-30 15:15:30 +0200506 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200507
Jens Axboef6762b72006-05-01 20:02:05 +0200508 iov_fault_in_pages_read(iov, chars);
509redo1:
510 addr = ops->map(pipe, buf, atomic);
Jens Axboe5274f052006-03-30 15:15:30 +0200511 error = pipe_iov_copy_from_user(offset + addr, iov,
Jens Axboef6762b72006-05-01 20:02:05 +0200512 chars, atomic);
513 ops->unmap(pipe, buf, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ret = error;
515 do_wakeup = 1;
Jens Axboef6762b72006-05-01 20:02:05 +0200516 if (error) {
517 if (atomic) {
518 atomic = 0;
519 goto redo1;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 buf->len += chars;
524 total_len -= chars;
525 ret = chars;
526 if (!total_len)
527 goto out;
528 }
529 }
530
531 for (;;) {
532 int bufs;
Ingo Molnar341b4462006-04-11 13:57:45 +0200533
Ingo Molnar923f4f22006-04-11 13:53:33 +0200534 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200536 if (!ret)
537 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
539 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200540 bufs = pipe->nrbufs;
Jens Axboe35f3d142010-05-20 10:43:18 +0200541 if (bufs < pipe->buffers) {
542 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200543 struct pipe_buffer *buf = pipe->bufs + newbuf;
544 struct page *page = pipe->tmp_page;
Jens Axboef6762b72006-05-01 20:02:05 +0200545 char *src;
546 int error, atomic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (!page) {
549 page = alloc_page(GFP_HIGHUSER);
550 if (unlikely(!page)) {
551 ret = ret ? : -ENOMEM;
552 break;
553 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200554 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200556 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * we lock up (O_NONBLOCK-)readers that sleep due to
558 * syscall merging.
559 * FIXME! Is this really true?
560 */
561 do_wakeup = 1;
562 chars = PAGE_SIZE;
563 if (chars > total_len)
564 chars = total_len;
565
Jens Axboef6762b72006-05-01 20:02:05 +0200566 iov_fault_in_pages_read(iov, chars);
567redo2:
568 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800569 src = kmap_atomic(page);
Jens Axboef6762b72006-05-01 20:02:05 +0200570 else
571 src = kmap(page);
572
573 error = pipe_iov_copy_from_user(src, iov, chars,
574 atomic);
575 if (atomic)
Cong Wange8e3c3d2011-11-25 23:14:27 +0800576 kunmap_atomic(src);
Jens Axboef6762b72006-05-01 20:02:05 +0200577 else
578 kunmap(page);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (unlikely(error)) {
Jens Axboef6762b72006-05-01 20:02:05 +0200581 if (atomic) {
582 atomic = 0;
583 goto redo2;
584 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200585 if (!ret)
Jens Axboef6762b72006-05-01 20:02:05 +0200586 ret = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 break;
588 }
589 ret += chars;
590
591 /* Insert it into the buffer array */
592 buf->page = page;
593 buf->ops = &anon_pipe_buf_ops;
594 buf->offset = 0;
595 buf->len = chars;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200596 pipe->nrbufs = ++bufs;
597 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 total_len -= chars;
600 if (!total_len)
601 break;
602 }
Jens Axboe35f3d142010-05-20 10:43:18 +0200603 if (bufs < pipe->buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 continue;
605 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200606 if (!ret)
607 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
609 }
610 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200611 if (!ret)
612 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 }
615 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800616 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200617 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 do_wakeup = 0;
619 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200620 pipe->waiting_writers++;
621 pipe_wait(pipe);
622 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624out:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200625 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (do_wakeup) {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800627 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200628 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 if (ret > 0)
Christoph Hellwig870f4812006-01-09 20:52:01 -0800631 file_update_time(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return ret;
633}
634
635static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
637{
638 return -EBADF;
639}
640
641static ssize_t
Ingo Molnar341b4462006-04-11 13:57:45 +0200642bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
643 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 return -EBADF;
646}
647
Andi Kleend59d0b12008-02-08 04:21:23 -0800648static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800650 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200651 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int count, buf, nrbufs;
653
654 switch (cmd) {
655 case FIONREAD:
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200656 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200657 pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 count = 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200659 buf = pipe->curbuf;
660 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 while (--nrbufs >= 0) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200662 count += pipe->bufs[buf].len;
Jens Axboe35f3d142010-05-20 10:43:18 +0200663 buf = (buf+1) & (pipe->buffers - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200665 mutex_unlock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return put_user(count, (int __user *)arg);
668 default:
669 return -EINVAL;
670 }
671}
672
673/* No kernel lock held - fine */
674static unsigned int
675pipe_poll(struct file *filp, poll_table *wait)
676{
677 unsigned int mask;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800678 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200679 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int nrbufs;
681
Ingo Molnar923f4f22006-04-11 13:53:33 +0200682 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* Reading only -- no need for acquiring the semaphore. */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200685 nrbufs = pipe->nrbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mask = 0;
687 if (filp->f_mode & FMODE_READ) {
688 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200689 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 mask |= POLLHUP;
691 }
692
693 if (filp->f_mode & FMODE_WRITE) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200694 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700695 /*
696 * Most Unices do not set POLLERR for FIFOs but on Linux they
697 * behave exactly like pipes for poll().
698 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200699 if (!pipe->readers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 mask |= POLLERR;
701 }
702
703 return mask;
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706static int
707pipe_release(struct inode *inode, int decr, int decw)
708{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200709 struct pipe_inode_info *pipe;
710
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200711 mutex_lock(&inode->i_mutex);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200712 pipe = inode->i_pipe;
713 pipe->readers -= decr;
714 pipe->writers -= decw;
Ingo Molnar341b4462006-04-11 13:57:45 +0200715
Ingo Molnar923f4f22006-04-11 13:53:33 +0200716 if (!pipe->readers && !pipe->writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 free_pipe_info(inode);
718 } else {
Linus Torvalds28e58ee2011-01-20 16:21:59 -0800719 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200720 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
721 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200723 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 return 0;
726}
727
728static int
729pipe_read_fasync(int fd, struct file *filp, int on)
730{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800731 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int retval;
733
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200734 mutex_lock(&inode->i_mutex);
735 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
736 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700738 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741
742static int
743pipe_write_fasync(int fd, struct file *filp, int on)
744{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800745 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int retval;
747
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200748 mutex_lock(&inode->i_mutex);
749 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
750 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700752 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755
756static int
757pipe_rdwr_fasync(int fd, struct file *filp, int on)
758{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800759 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar341b4462006-04-11 13:57:45 +0200760 struct pipe_inode_info *pipe = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int retval;
762
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200763 mutex_lock(&inode->i_mutex);
Ingo Molnar341b4462006-04-11 13:57:45 +0200764 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700765 if (retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200766 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700767 if (retval < 0) /* this can happen only if on == T */
768 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
769 }
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200770 mutex_unlock(&inode->i_mutex);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700771 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774
775static int
776pipe_read_release(struct inode *inode, struct file *filp)
777{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return pipe_release(inode, 1, 0);
779}
780
781static int
782pipe_write_release(struct inode *inode, struct file *filp)
783{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return pipe_release(inode, 0, 1);
785}
786
787static int
788pipe_rdwr_release(struct inode *inode, struct file *filp)
789{
790 int decr, decw;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 decr = (filp->f_mode & FMODE_READ) != 0;
793 decw = (filp->f_mode & FMODE_WRITE) != 0;
794 return pipe_release(inode, decr, decw);
795}
796
797static int
798pipe_read_open(struct inode *inode, struct file *filp)
799{
Earl Chewad396022009-10-19 15:55:41 -0700800 int ret = -ENOENT;
801
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200802 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700803
804 if (inode->i_pipe) {
805 ret = 0;
806 inode->i_pipe->readers++;
807 }
808
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200809 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Earl Chewad396022009-10-19 15:55:41 -0700811 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814static int
815pipe_write_open(struct inode *inode, struct file *filp)
816{
Earl Chewad396022009-10-19 15:55:41 -0700817 int ret = -ENOENT;
818
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200819 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700820
821 if (inode->i_pipe) {
822 ret = 0;
823 inode->i_pipe->writers++;
824 }
825
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200826 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Earl Chewad396022009-10-19 15:55:41 -0700828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831static int
832pipe_rdwr_open(struct inode *inode, struct file *filp)
833{
Earl Chewad396022009-10-19 15:55:41 -0700834 int ret = -ENOENT;
835
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200836 mutex_lock(&inode->i_mutex);
Earl Chewad396022009-10-19 15:55:41 -0700837
838 if (inode->i_pipe) {
839 ret = 0;
840 if (filp->f_mode & FMODE_READ)
841 inode->i_pipe->readers++;
842 if (filp->f_mode & FMODE_WRITE)
843 inode->i_pipe->writers++;
844 }
845
Ingo Molnar9aeedfc2006-04-11 13:53:10 +0200846 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Earl Chewad396022009-10-19 15:55:41 -0700848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
852 * The file_operations structs are not static because they
853 * are also used in linux/fs/fifo.c to do operations on FIFOs.
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200854 *
855 * Pipes reuse fifos' file_operations structs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200857const struct file_operations read_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700859 .read = do_sync_read,
860 .aio_read = pipe_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 .write = bad_pipe_w,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700862 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800863 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 .open = pipe_read_open,
865 .release = pipe_read_release,
866 .fasync = pipe_read_fasync,
867};
868
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200869const struct file_operations write_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 .llseek = no_llseek,
871 .read = bad_pipe_r,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700872 .write = do_sync_write,
873 .aio_write = pipe_write,
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700874 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800875 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 .open = pipe_write_open,
877 .release = pipe_write_release,
878 .fasync = pipe_write_fasync,
879};
880
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200881const struct file_operations rdwr_pipefifo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700883 .read = do_sync_read,
884 .aio_read = pipe_read,
885 .write = do_sync_write,
886 .aio_write = pipe_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 .poll = pipe_poll,
Andi Kleend59d0b12008-02-08 04:21:23 -0800888 .unlocked_ioctl = pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 .open = pipe_rdwr_open,
890 .release = pipe_rdwr_release,
891 .fasync = pipe_rdwr_fasync,
892};
893
Ingo Molnar3a326a22006-04-10 15:18:35 +0200894struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
895{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200896 struct pipe_inode_info *pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200897
Ingo Molnar923f4f22006-04-11 13:53:33 +0200898 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
899 if (pipe) {
Jens Axboe35f3d142010-05-20 10:43:18 +0200900 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
901 if (pipe->bufs) {
902 init_waitqueue_head(&pipe->wait);
903 pipe->r_counter = pipe->w_counter = 1;
904 pipe->inode = inode;
905 pipe->buffers = PIPE_DEF_BUFFERS;
906 return pipe;
907 }
908 kfree(pipe);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200909 }
910
Jens Axboe35f3d142010-05-20 10:43:18 +0200911 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200912}
913
Ingo Molnar923f4f22006-04-11 13:53:33 +0200914void __free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Jens Axboe35f3d142010-05-20 10:43:18 +0200918 for (i = 0; i < pipe->buffers; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200919 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (buf->ops)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200921 buf->ops->release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200923 if (pipe->tmp_page)
924 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200925 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200926 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Jens Axboeb92ce552006-04-11 13:52:07 +0200929void free_pipe_info(struct inode *inode)
930{
931 __free_pipe_info(inode->i_pipe);
932 inode->i_pipe = NULL;
933}
934
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800935static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200936
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700937/*
938 * pipefs_dname() is called from d_path().
939 */
940static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
941{
942 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
943 dentry->d_inode->i_ino);
944}
945
Al Viro3ba13d12009-02-20 06:02:22 +0000946static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700947 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948};
949
950static struct inode * get_pipe_inode(void)
951{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200952 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200953 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (!inode)
956 goto fail_inode;
957
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400958 inode->i_ino = get_next_ino();
959
Ingo Molnar923f4f22006-04-11 13:53:33 +0200960 pipe = alloc_pipe_info(inode);
961 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto fail_iput;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200963 inode->i_pipe = pipe;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200964
Ingo Molnar923f4f22006-04-11 13:53:33 +0200965 pipe->readers = pipe->writers = 1;
Denys Vlasenkod2d96482008-07-01 14:16:09 +0200966 inode->i_fop = &rdwr_pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /*
969 * Mark the inode dirty from the very beginning,
970 * that way it will never be moved to the dirty
971 * list because "mark_inode_dirty()" will think
972 * that it already _is_ on the dirty list.
973 */
974 inode->i_state = I_DIRTY;
975 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100976 inode->i_uid = current_fsuid();
977 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return inode;
981
982fail_iput:
983 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985fail_inode:
986 return NULL;
987}
988
Ulrich Drepperbe61a862008-07-23 21:29:40 -0700989struct file *create_write_pipe(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Andi Kleend6cbd282006-09-30 23:29:26 -0700991 int err;
992 struct inode *inode;
993 struct file *f;
Al Viro2c48b9c2009-08-09 00:52:35 +0400994 struct path path;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700995 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Andi Kleend6cbd282006-09-30 23:29:26 -0700997 err = -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 inode = get_pipe_inode();
999 if (!inode)
Dave Hansen430e2852008-02-15 14:37:26 -08001000 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Andi Kleend6cbd282006-09-30 23:29:26 -07001002 err = -ENOMEM;
Nick Piggin4b936882011-01-07 17:50:07 +11001003 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +04001004 if (!path.dentry)
Andi Kleend6cbd282006-09-30 23:29:26 -07001005 goto err_inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04001006 path.mnt = mntget(pipe_mnt);
Ingo Molnar341b4462006-04-11 13:57:45 +02001007
Al Viro2c48b9c2009-08-09 00:52:35 +04001008 d_instantiate(path.dentry, inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001009
1010 err = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04001011 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
Dave Hansen430e2852008-02-15 14:37:26 -08001012 if (!f)
1013 goto err_dentry;
Andi Kleend6cbd282006-09-30 23:29:26 -07001014 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001016 f->f_flags = O_WRONLY | (flags & O_NONBLOCK);
Andi Kleend6cbd282006-09-30 23:29:26 -07001017 f->f_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Andi Kleend6cbd282006-09-30 23:29:26 -07001019 return f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Dave Hansen430e2852008-02-15 14:37:26 -08001021 err_dentry:
Al Viroed152432008-04-22 19:51:27 -04001022 free_pipe_info(inode);
Al Viro2c48b9c2009-08-09 00:52:35 +04001023 path_put(&path);
Al Viroed152432008-04-22 19:51:27 -04001024 return ERR_PTR(err);
1025
Andi Kleend6cbd282006-09-30 23:29:26 -07001026 err_inode:
1027 free_pipe_info(inode);
1028 iput(inode);
Dave Hansen430e2852008-02-15 14:37:26 -08001029 err:
Andi Kleend6cbd282006-09-30 23:29:26 -07001030 return ERR_PTR(err);
1031}
1032
1033void free_write_pipe(struct file *f)
1034{
Al Viro5ccac88e2006-12-18 13:31:18 +00001035 free_pipe_info(f->f_dentry->d_inode);
Jan Blunckc8e7f442008-06-09 16:40:35 -07001036 path_put(&f->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001037 put_filp(f);
1038}
1039
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001040struct file *create_read_pipe(struct file *wrf, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001041{
Al Virod2314122009-08-09 01:01:37 +04001042 /* Grab pipe from the writer */
1043 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1044 &read_pipefifo_fops);
Andi Kleend6cbd282006-09-30 23:29:26 -07001045 if (!f)
1046 return ERR_PTR(-ENFILE);
1047
Jan Blunckc8e7f442008-06-09 16:40:35 -07001048 path_get(&wrf->f_path);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001049 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
Andi Kleend6cbd282006-09-30 23:29:26 -07001050
1051 return f;
1052}
1053
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001054int do_pipe_flags(int *fd, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -07001055{
1056 struct file *fw, *fr;
1057 int error;
1058 int fdw, fdr;
1059
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001060 if (flags & ~(O_CLOEXEC | O_NONBLOCK))
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001061 return -EINVAL;
1062
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001063 fw = create_write_pipe(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001064 if (IS_ERR(fw))
1065 return PTR_ERR(fw);
Ulrich Drepperbe61a862008-07-23 21:29:40 -07001066 fr = create_read_pipe(fw, flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001067 error = PTR_ERR(fr);
1068 if (IS_ERR(fr))
1069 goto err_write_pipe;
1070
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001071 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001072 if (error < 0)
1073 goto err_read_pipe;
1074 fdr = error;
1075
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001076 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -07001077 if (error < 0)
1078 goto err_fdr;
1079 fdw = error;
1080
Al Viro157cf642008-12-14 04:57:47 -05001081 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -07001082 fd_install(fdr, fr);
1083 fd_install(fdw, fw);
1084 fd[0] = fdr;
1085 fd[1] = fdw;
Ingo Molnar341b4462006-04-11 13:57:45 +02001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return 0;
1088
Andi Kleend6cbd282006-09-30 23:29:26 -07001089 err_fdr:
1090 put_unused_fd(fdr);
1091 err_read_pipe:
Jan Blunckc8e7f442008-06-09 16:40:35 -07001092 path_put(&fr->f_path);
Andi Kleend6cbd282006-09-30 23:29:26 -07001093 put_filp(fr);
1094 err_write_pipe:
1095 free_write_pipe(fw);
1096 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001100 * sys_pipe() is the normal C calling standard for creating
1101 * a pipe. It's not the way Unix traditionally does this, though.
1102 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01001103SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001104{
1105 int fd[2];
1106 int error;
1107
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001108 error = do_pipe_flags(fd, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001109 if (!error) {
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001110 if (copy_to_user(fildes, fd, sizeof(fd))) {
1111 sys_close(fd[0]);
1112 sys_close(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001113 error = -EFAULT;
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001114 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001115 }
1116 return error;
1117}
1118
Heiko Carstens2b664212009-01-14 14:14:35 +01001119SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001120{
1121 return sys_pipe2(fildes, 0);
1122}
1123
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001124/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001125 * Allocate a new array of pipe buffers and copy the info over. Returns the
1126 * pipe size if successful, or return -ERROR on error.
1127 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001128static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
Jens Axboe35f3d142010-05-20 10:43:18 +02001129{
1130 struct pipe_buffer *bufs;
1131
1132 /*
Jens Axboe35f3d142010-05-20 10:43:18 +02001133 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1134 * expect a lot of shrink+grow operations, just free and allocate
1135 * again like we would do for growing. If the pipe currently
1136 * contains more buffers than arg, then return busy.
1137 */
Jens Axboeb9598db2010-05-24 19:34:43 +02001138 if (nr_pages < pipe->nrbufs)
Jens Axboe35f3d142010-05-20 10:43:18 +02001139 return -EBUSY;
1140
Sasha Levin2ccd4f42012-01-12 17:17:40 -08001141 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
Jens Axboe35f3d142010-05-20 10:43:18 +02001142 if (unlikely(!bufs))
1143 return -ENOMEM;
1144
1145 /*
1146 * The pipe array wraps around, so just start the new one at zero
1147 * and adjust the indexes.
1148 */
1149 if (pipe->nrbufs) {
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001150 unsigned int tail;
1151 unsigned int head;
Jens Axboe35f3d142010-05-20 10:43:18 +02001152
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001153 tail = pipe->curbuf + pipe->nrbufs;
1154 if (tail < pipe->buffers)
1155 tail = 0;
1156 else
1157 tail &= (pipe->buffers - 1);
1158
1159 head = pipe->nrbufs - tail;
Jens Axboe35f3d142010-05-20 10:43:18 +02001160 if (head)
1161 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1162 if (tail)
Miklos Szeredi1d862f42010-06-08 16:28:45 +02001163 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
Jens Axboe35f3d142010-05-20 10:43:18 +02001164 }
1165
1166 pipe->curbuf = 0;
1167 kfree(pipe->bufs);
1168 pipe->bufs = bufs;
Jens Axboeb9598db2010-05-24 19:34:43 +02001169 pipe->buffers = nr_pages;
1170 return nr_pages * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001171}
1172
Jens Axboeff9da692010-06-03 14:54:39 +02001173/*
1174 * Currently we rely on the pipe array holding a power-of-2 number
1175 * of pages.
1176 */
1177static inline unsigned int round_pipe_size(unsigned int size)
1178{
1179 unsigned long nr_pages;
1180
1181 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1182 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1183}
1184
1185/*
1186 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1187 * will return an error.
1188 */
1189int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1190 size_t *lenp, loff_t *ppos)
1191{
1192 int ret;
1193
1194 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1195 if (ret < 0 || !write)
1196 return ret;
1197
1198 pipe_max_size = round_pipe_size(pipe_max_size);
1199 return ret;
1200}
1201
Linus Torvalds72083642010-11-28 16:27:19 -08001202/*
1203 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1204 * location, so checking ->i_pipe is not enough to verify that this is a
1205 * pipe.
1206 */
1207struct pipe_inode_info *get_pipe_info(struct file *file)
1208{
1209 struct inode *i = file->f_path.dentry->d_inode;
1210
1211 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1212}
1213
Jens Axboe35f3d142010-05-20 10:43:18 +02001214long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1215{
1216 struct pipe_inode_info *pipe;
1217 long ret;
1218
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001219 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001220 if (!pipe)
1221 return -EBADF;
1222
1223 mutex_lock(&pipe->inode->i_mutex);
1224
1225 switch (cmd) {
Jens Axboeb9598db2010-05-24 19:34:43 +02001226 case F_SETPIPE_SZ: {
Jens Axboeff9da692010-06-03 14:54:39 +02001227 unsigned int size, nr_pages;
Jens Axboeb9598db2010-05-24 19:34:43 +02001228
Jens Axboeff9da692010-06-03 14:54:39 +02001229 size = round_pipe_size(arg);
1230 nr_pages = size >> PAGE_SHIFT;
Jens Axboeb9598db2010-05-24 19:34:43 +02001231
Miklos Szeredi6db40cf2010-06-09 09:27:57 +02001232 ret = -EINVAL;
1233 if (!nr_pages)
1234 goto out;
1235
Jens Axboeff9da692010-06-03 14:54:39 +02001236 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
Jens Axboeb4ca7612010-06-01 12:42:12 +02001237 ret = -EPERM;
Julia Lawallcc967be2010-05-26 17:54:39 +02001238 goto out;
Julia Lawallcc967be2010-05-26 17:54:39 +02001239 }
Jens Axboeff9da692010-06-03 14:54:39 +02001240 ret = pipe_set_size(pipe, nr_pages);
Jens Axboe35f3d142010-05-20 10:43:18 +02001241 break;
Jens Axboeb9598db2010-05-24 19:34:43 +02001242 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001243 case F_GETPIPE_SZ:
Jens Axboeb9598db2010-05-24 19:34:43 +02001244 ret = pipe->buffers * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001245 break;
1246 default:
1247 ret = -EINVAL;
1248 break;
1249 }
1250
Julia Lawallcc967be2010-05-26 17:54:39 +02001251out:
Jens Axboe35f3d142010-05-20 10:43:18 +02001252 mutex_unlock(&pipe->inode->i_mutex);
1253 return ret;
1254}
1255
Nick Pigginff0c7d12011-01-07 17:49:50 +11001256static const struct super_operations pipefs_ops = {
1257 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001258 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001259};
1260
Jens Axboe35f3d142010-05-20 10:43:18 +02001261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * pipefs should _never_ be mounted by userland - too much of security hassle,
1263 * no real gain from having the whole whorehouse mounted. So we don't need
1264 * any operations on the root directory. However, we need a non-trivial
1265 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1266 */
Al Viro51139ad2010-07-25 23:47:46 +04001267static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1268 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Al Viroc74a1cb2011-01-12 16:59:34 -05001270 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1271 &pipefs_dentry_operations, PIPEFS_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274static struct file_system_type pipe_fs_type = {
1275 .name = "pipefs",
Al Viro51139ad2010-07-25 23:47:46 +04001276 .mount = pipefs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 .kill_sb = kill_anon_super,
1278};
1279
1280static int __init init_pipe_fs(void)
1281{
1282 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (!err) {
1285 pipe_mnt = kern_mount(&pipe_fs_type);
1286 if (IS_ERR(pipe_mnt)) {
1287 err = PTR_ERR(pipe_mnt);
1288 unregister_filesystem(&pipe_fs_type);
1289 }
1290 }
1291 return err;
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294fs_initcall(init_pipe_fs);