Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 14 | #include <linux/log2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mount.h> |
Muthu Kumar | b502bd1 | 2012-03-23 15:01:50 -0700 | [diff] [blame] | 16 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/pipe_fs_i.h> |
| 18 | #include <linux/uio.h> |
| 19 | #include <linux/highmem.h> |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 20 | #include <linux/pagemap.h> |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 21 | #include <linux/audit.h> |
Ulrich Drepper | ba719ba | 2008-05-06 20:42:38 -0700 | [diff] [blame] | 22 | #include <linux/syscalls.h> |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 23 | #include <linux/fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <asm/ioctls.h> |
| 27 | |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 28 | #include "internal.h" |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /* |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 31 | * The max size that a non-root user is allowed to grow the pipe. Can |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 32 | * be set by root in /proc/sys/fs/pipe-max-size |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 33 | */ |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 34 | unsigned int pipe_max_size = 1048576; |
| 35 | |
| 36 | /* |
| 37 | * Minimum pipe size, as required by POSIX |
| 38 | */ |
| 39 | unsigned int pipe_min_size = PAGE_SIZE; |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 40 | |
| 41 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * 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 Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 56 | static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass) |
| 57 | { |
| 58 | if (pipe->inode) |
Al Viro | 72b0d9a | 2013-03-21 02:32:24 -0400 | [diff] [blame] | 59 | mutex_lock_nested(&pipe->mutex, subclass); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void 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 | } |
| 69 | EXPORT_SYMBOL(pipe_lock); |
| 70 | |
| 71 | void pipe_unlock(struct pipe_inode_info *pipe) |
| 72 | { |
| 73 | if (pipe->inode) |
Al Viro | 72b0d9a | 2013-03-21 02:32:24 -0400 | [diff] [blame] | 74 | mutex_unlock(&pipe->mutex); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 75 | } |
| 76 | EXPORT_SYMBOL(pipe_unlock); |
| 77 | |
| 78 | void 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 Zijlstra | 023d43c | 2009-07-21 10:09:23 +0200 | [diff] [blame] | 87 | pipe_lock_nested(pipe2, I_MUTEX_PARENT); |
| 88 | pipe_lock_nested(pipe1, I_MUTEX_CHILD); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* Drop the inode semaphore and wait for a pipe event, atomically */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 93 | void pipe_wait(struct pipe_inode_info *pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | DEFINE_WAIT(wait); |
| 96 | |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Pipes are system-local resources, so sleeping on them |
| 99 | * is considered a noninteractive wait: |
| 100 | */ |
Mike Galbraith | af92723 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 101 | prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 102 | pipe_unlock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | schedule(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 104 | finish_wait(&pipe->wait, &wait); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 105 | pipe_lock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 108 | static int |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 109 | pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len, |
| 110 | int atomic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 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 Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 119 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | to += copy; |
| 127 | len -= copy; |
| 128 | iov->iov_base += copy; |
| 129 | iov->iov_len -= copy; |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 134 | static int |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 135 | pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len, |
| 136 | int atomic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 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 Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 145 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | from += copy; |
| 153 | len -= copy; |
| 154 | iov->iov_base += copy; |
| 155 | iov->iov_len -= copy; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 160 | /* |
| 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 | */ |
| 164 | static 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 | */ |
| 186 | static 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 Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 201 | static void anon_pipe_buf_release(struct pipe_inode_info *pipe, |
| 202 | struct pipe_buffer *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | struct page *page = buf->page; |
| 205 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 206 | /* |
| 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 Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 209 | * allocation cache. (Otherwise just release our reference to it) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 210 | */ |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 211 | if (page_count(page) == 1 && !pipe->tmp_page) |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 212 | pipe->tmp_page = page; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 213 | else |
| 214 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 217 | /** |
| 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 Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 225 | * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 226 | * and the caller has to be careful not to fault before calling |
| 227 | * the unmap function. |
| 228 | * |
Cong Wang | 2164d33 | 2012-06-23 11:33:51 +0800 | [diff] [blame] | 229 | * Note that this function calls kmap_atomic() if @atomic != 0. |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 230 | */ |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 231 | void *generic_pipe_buf_map(struct pipe_inode_info *pipe, |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 232 | struct pipe_buffer *buf, int atomic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 234 | if (atomic) { |
| 235 | buf->flags |= PIPE_BUF_FLAG_ATOMIC; |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 236 | return kmap_atomic(buf->page); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return kmap(buf->page); |
| 240 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 241 | EXPORT_SYMBOL(generic_pipe_buf_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 243 | /** |
| 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 Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 252 | void generic_pipe_buf_unmap(struct pipe_inode_info *pipe, |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 253 | struct pipe_buffer *buf, void *map_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 255 | if (buf->flags & PIPE_BUF_FLAG_ATOMIC) { |
| 256 | buf->flags &= ~PIPE_BUF_FLAG_ATOMIC; |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 257 | kunmap_atomic(map_data); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 258 | } else |
| 259 | kunmap(buf->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 261 | EXPORT_SYMBOL(generic_pipe_buf_unmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 263 | /** |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 264 | * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 265 | * @pipe: the pipe that the buffer belongs to |
| 266 | * @buf: the buffer to attempt to steal |
| 267 | * |
| 268 | * Description: |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 269 | * This function attempts to steal the &struct page attached to |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 270 | * @buf. If successful, this function returns 0 and returns with |
| 271 | * the page locked. The caller may then reuse the page for whatever |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 272 | * he wishes; the typical use is insertion into a different file |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 273 | * page cache. |
| 274 | */ |
Jens Axboe | 330ab71 | 2006-05-02 15:29:57 +0200 | [diff] [blame] | 275 | int generic_pipe_buf_steal(struct pipe_inode_info *pipe, |
| 276 | struct pipe_buffer *buf) |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 277 | { |
Jens Axboe | 46e678c | 2006-04-30 16:36:32 +0200 | [diff] [blame] | 278 | struct page *page = buf->page; |
| 279 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 280 | /* |
| 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 Axboe | 46e678c | 2006-04-30 16:36:32 +0200 | [diff] [blame] | 285 | if (page_count(page) == 1) { |
Jens Axboe | 46e678c | 2006-04-30 16:36:32 +0200 | [diff] [blame] | 286 | lock_page(page); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | return 1; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 291 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 292 | EXPORT_SYMBOL(generic_pipe_buf_steal); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 293 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 294 | /** |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 295 | * generic_pipe_buf_get - get a reference to a &struct pipe_buffer |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 296 | * @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 | */ |
| 304 | void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 305 | { |
| 306 | page_cache_get(buf->page); |
| 307 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 308 | EXPORT_SYMBOL(generic_pipe_buf_get); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 309 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 310 | /** |
| 311 | * generic_pipe_buf_confirm - verify contents of the pipe buffer |
Randy Dunlap | 79685b8 | 2007-07-27 08:08:51 +0200 | [diff] [blame] | 312 | * @info: the pipe that the buffer belongs to |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 313 | * @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 Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 319 | int generic_pipe_buf_confirm(struct pipe_inode_info *info, |
| 320 | struct pipe_buffer *buf) |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 321 | { |
| 322 | return 0; |
| 323 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 324 | EXPORT_SYMBOL(generic_pipe_buf_confirm); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 325 | |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 326 | /** |
| 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 | */ |
| 334 | void generic_pipe_buf_release(struct pipe_inode_info *pipe, |
| 335 | struct pipe_buffer *buf) |
| 336 | { |
| 337 | page_cache_release(buf->page); |
| 338 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 339 | EXPORT_SYMBOL(generic_pipe_buf_release); |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 340 | |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 341 | static const struct pipe_buf_operations anon_pipe_buf_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | .can_merge = 1, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 343 | .map = generic_pipe_buf_map, |
| 344 | .unmap = generic_pipe_buf_unmap, |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 345 | .confirm = generic_pipe_buf_confirm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | .release = anon_pipe_buf_release, |
Jens Axboe | 330ab71 | 2006-05-02 15:29:57 +0200 | [diff] [blame] | 347 | .steal = generic_pipe_buf_steal, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 348 | .get = generic_pipe_buf_get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 351 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | static ssize_t |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 362 | pipe_read(struct kiocb *iocb, const struct iovec *_iov, |
| 363 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 365 | struct file *filp = iocb->ki_filp; |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 366 | struct pipe_inode_info *pipe = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | 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 Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 379 | pipe_lock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | for (;;) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 381 | int bufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if (bufs) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 383 | int curbuf = pipe->curbuf; |
| 384 | struct pipe_buffer *buf = pipe->bufs + curbuf; |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 385 | const struct pipe_buf_operations *ops = buf->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | void *addr; |
| 387 | size_t chars = buf->len; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 388 | int error, atomic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | if (chars > total_len) |
| 391 | chars = total_len; |
| 392 | |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 393 | error = ops->confirm(pipe, buf); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 394 | if (error) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 395 | if (!ret) |
Nicolas Kaiser | e5953cb | 2010-10-21 14:56:00 +0200 | [diff] [blame] | 396 | ret = error; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 397 | break; |
| 398 | } |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 399 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 400 | atomic = !iov_fault_in_pages_write(iov, chars); |
| 401 | redo: |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (unlikely(error)) { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 406 | /* |
| 407 | * Just retry with the slow path if we failed. |
| 408 | */ |
| 409 | if (atomic) { |
| 410 | atomic = 0; |
| 411 | goto redo; |
| 412 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 413 | if (!ret) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 414 | ret = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | break; |
| 416 | } |
| 417 | ret += chars; |
| 418 | buf->offset += chars; |
| 419 | buf->len -= chars; |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 420 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (!buf->len) { |
| 428 | buf->ops = NULL; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 429 | ops->release(pipe, buf); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 430 | curbuf = (curbuf + 1) & (pipe->buffers - 1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 431 | pipe->curbuf = curbuf; |
| 432 | pipe->nrbufs = --bufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | 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 Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 441 | if (!pipe->writers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | break; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 443 | if (!pipe->waiting_writers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | /* 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 Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 457 | if (!ret) |
| 458 | ret = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 462 | wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 463 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 465 | pipe_wait(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 467 | pipe_unlock(pipe); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 468 | |
| 469 | /* Signal writers asynchronously that there is more room. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 471 | wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 472 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | if (ret > 0) |
| 475 | file_accessed(filp); |
| 476 | return ret; |
| 477 | } |
| 478 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 479 | static inline int is_packetized(struct file *file) |
| 480 | { |
| 481 | return (file->f_flags & O_DIRECT) != 0; |
| 482 | } |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | static ssize_t |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 485 | pipe_write(struct kiocb *iocb, const struct iovec *_iov, |
| 486 | unsigned long nr_segs, loff_t ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 488 | struct file *filp = iocb->ki_filp; |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 489 | struct pipe_inode_info *pipe = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | 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 Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 503 | pipe_lock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 505 | if (!pipe->readers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | 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 Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 513 | if (pipe->nrbufs && chars != 0) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 514 | int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) & |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 515 | (pipe->buffers - 1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 516 | struct pipe_buffer *buf = pipe->bufs + lastbuf; |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 517 | const struct pipe_buf_operations *ops = buf->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | int offset = buf->offset + buf->len; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | if (ops->can_merge && offset + chars <= PAGE_SIZE) { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 521 | int error, atomic = 1; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 522 | void *addr; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 523 | |
Jens Axboe | cac36bb0 | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 524 | error = ops->confirm(pipe, buf); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 525 | if (error) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 526 | goto out; |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 527 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 528 | iov_fault_in_pages_read(iov, chars); |
| 529 | redo1: |
| 530 | addr = ops->map(pipe, buf, atomic); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 531 | error = pipe_iov_copy_from_user(offset + addr, iov, |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 532 | chars, atomic); |
| 533 | ops->unmap(pipe, buf, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | ret = error; |
| 535 | do_wakeup = 1; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 536 | if (error) { |
| 537 | if (atomic) { |
| 538 | atomic = 0; |
| 539 | goto redo1; |
| 540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | goto out; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | 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 Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 553 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 554 | if (!pipe->readers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | send_sig(SIGPIPE, current, 0); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 556 | if (!ret) |
| 557 | ret = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | break; |
| 559 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 560 | bufs = pipe->nrbufs; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 561 | if (bufs < pipe->buffers) { |
| 562 | int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 563 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
| 564 | struct page *page = pipe->tmp_page; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 565 | char *src; |
| 566 | int error, atomic = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
| 568 | if (!page) { |
| 569 | page = alloc_page(GFP_HIGHUSER); |
| 570 | if (unlikely(!page)) { |
| 571 | ret = ret ? : -ENOMEM; |
| 572 | break; |
| 573 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 574 | pipe->tmp_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 576 | /* Always wake up, even if the copy fails. Otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | * 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 Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 586 | iov_fault_in_pages_read(iov, chars); |
| 587 | redo2: |
| 588 | if (atomic) |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 589 | src = kmap_atomic(page); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 590 | else |
| 591 | src = kmap(page); |
| 592 | |
| 593 | error = pipe_iov_copy_from_user(src, iov, chars, |
| 594 | atomic); |
| 595 | if (atomic) |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 596 | kunmap_atomic(src); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 597 | else |
| 598 | kunmap(page); |
| 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | if (unlikely(error)) { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 601 | if (atomic) { |
| 602 | atomic = 0; |
| 603 | goto redo2; |
| 604 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 605 | if (!ret) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 606 | ret = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | 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 Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 616 | buf->flags = 0; |
| 617 | if (is_packetized(filp)) { |
| 618 | buf->ops = &packet_pipe_buf_ops; |
| 619 | buf->flags = PIPE_BUF_FLAG_PACKET; |
| 620 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 621 | pipe->nrbufs = ++bufs; |
| 622 | pipe->tmp_page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | total_len -= chars; |
| 625 | if (!total_len) |
| 626 | break; |
| 627 | } |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 628 | if (bufs < pipe->buffers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | continue; |
| 630 | if (filp->f_flags & O_NONBLOCK) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 631 | if (!ret) |
| 632 | ret = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | if (signal_pending(current)) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 636 | if (!ret) |
| 637 | ret = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | break; |
| 639 | } |
| 640 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 641 | wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 642 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | do_wakeup = 0; |
| 644 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 645 | pipe->waiting_writers++; |
| 646 | pipe_wait(pipe); |
| 647 | pipe->waiting_writers--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | out: |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 650 | pipe_unlock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 652 | wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 653 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
Josef Bacik | c3b2da3 | 2012-03-26 09:59:21 -0400 | [diff] [blame] | 655 | if (ret > 0) { |
| 656 | int err = file_update_time(filp); |
| 657 | if (err) |
| 658 | ret = err; |
| 659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | return ret; |
| 661 | } |
| 662 | |
Andi Kleen | d59d0b1 | 2008-02-08 04:21:23 -0800 | [diff] [blame] | 663 | static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 665 | struct pipe_inode_info *pipe = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | int count, buf, nrbufs; |
| 667 | |
| 668 | switch (cmd) { |
| 669 | case FIONREAD: |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 670 | pipe_lock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | count = 0; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 672 | buf = pipe->curbuf; |
| 673 | nrbufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | while (--nrbufs >= 0) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 675 | count += pipe->bufs[buf].len; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 676 | buf = (buf+1) & (pipe->buffers - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 678 | pipe_unlock(pipe); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | return put_user(count, (int __user *)arg); |
| 681 | default: |
Will Deacon | 46ce341 | 2012-05-25 11:39:13 +0100 | [diff] [blame] | 682 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | /* No kernel lock held - fine */ |
| 687 | static unsigned int |
| 688 | pipe_poll(struct file *filp, poll_table *wait) |
| 689 | { |
| 690 | unsigned int mask; |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 691 | struct pipe_inode_info *pipe = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | int nrbufs; |
| 693 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 694 | poll_wait(filp, &pipe->wait, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
| 696 | /* Reading only -- no need for acquiring the semaphore. */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 697 | nrbufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | mask = 0; |
| 699 | if (filp->f_mode & FMODE_READ) { |
| 700 | mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 701 | if (!pipe->writers && filp->f_version != pipe->w_counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | mask |= POLLHUP; |
| 703 | } |
| 704 | |
| 705 | if (filp->f_mode & FMODE_WRITE) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 706 | mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0; |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 707 | /* |
| 708 | * Most Unices do not set POLLERR for FIFOs but on Linux they |
| 709 | * behave exactly like pipes for poll(). |
| 710 | */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 711 | if (!pipe->readers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | mask |= POLLERR; |
| 713 | } |
| 714 | |
| 715 | return mask; |
| 716 | } |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | static int |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 719 | pipe_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 721 | struct pipe_inode_info *pipe = inode->i_pipe; |
| 722 | int kill = 0; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 723 | |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 724 | pipe_lock(pipe); |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 725 | if (file->f_mode & FMODE_READ) |
| 726 | pipe->readers--; |
| 727 | if (file->f_mode & FMODE_WRITE) |
| 728 | pipe->writers--; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 729 | |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 730 | if (pipe->readers || pipe->writers) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 731 | wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 732 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
| 733 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 735 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 750 | pipe_fasync(int fd, struct file *filp, int on) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 752 | struct pipe_inode_info *pipe = filp->private_data; |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 753 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 755 | pipe_lock(pipe); |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 756 | 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 Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 759 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 760 | if (retval < 0 && (filp->f_mode & FMODE_READ)) |
| 761 | /* this can happen only if on == T */ |
Oleg Nesterov | e5bc49b | 2009-03-12 14:31:28 -0700 | [diff] [blame] | 762 | fasync_helper(-1, filp, 0, &pipe->fasync_readers); |
| 763 | } |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 764 | pipe_unlock(pipe); |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 765 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 768 | struct pipe_inode_info * alloc_pipe_info(struct inode *inode) |
| 769 | { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 770 | struct pipe_inode_info *pipe; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 771 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 772 | pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL); |
| 773 | if (pipe) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 774 | 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; |
Al Viro | 72b0d9a | 2013-03-21 02:32:24 -0400 | [diff] [blame] | 780 | mutex_init(&pipe->mutex); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 781 | return pipe; |
| 782 | } |
| 783 | kfree(pipe); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 784 | } |
| 785 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 786 | return NULL; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 787 | } |
| 788 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 789 | void __free_pipe_info(struct pipe_inode_info *pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
| 791 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 793 | for (i = 0; i < pipe->buffers; i++) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 794 | struct pipe_buffer *buf = pipe->bufs + i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | if (buf->ops) |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 796 | buf->ops->release(pipe, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 798 | if (pipe->tmp_page) |
| 799 | __free_page(pipe->tmp_page); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 800 | kfree(pipe->bufs); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 801 | kfree(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 804 | void free_pipe_info(struct inode *inode) |
| 805 | { |
| 806 | __free_pipe_info(inode->i_pipe); |
| 807 | inode->i_pipe = NULL; |
| 808 | } |
| 809 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 810 | static struct vfsmount *pipe_mnt __read_mostly; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 811 | |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 812 | /* |
| 813 | * pipefs_dname() is called from d_path(). |
| 814 | */ |
| 815 | static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen) |
| 816 | { |
| 817 | return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]", |
| 818 | dentry->d_inode->i_ino); |
| 819 | } |
| 820 | |
Al Viro | 3ba13d1 | 2009-02-20 06:02:22 +0000 | [diff] [blame] | 821 | static const struct dentry_operations pipefs_dentry_operations = { |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 822 | .d_dname = pipefs_dname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | }; |
| 824 | |
| 825 | static struct inode * get_pipe_inode(void) |
| 826 | { |
Eric Dumazet | a209dfc | 2011-07-26 11:36:34 +0200 | [diff] [blame] | 827 | struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 828 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
| 830 | if (!inode) |
| 831 | goto fail_inode; |
| 832 | |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 833 | inode->i_ino = get_next_ino(); |
| 834 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 835 | pipe = alloc_pipe_info(inode); |
| 836 | if (!pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | goto fail_iput; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 838 | |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 839 | inode->i_pipe = pipe; |
| 840 | pipe->files = 2; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 841 | pipe->readers = pipe->writers = 1; |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 842 | inode->i_fop = &pipefifo_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
| 844 | /* |
| 845 | * Mark the inode dirty from the very beginning, |
| 846 | * that way it will never be moved to the dirty |
| 847 | * list because "mark_inode_dirty()" will think |
| 848 | * that it already _is_ on the dirty list. |
| 849 | */ |
| 850 | inode->i_state = I_DIRTY; |
| 851 | inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR; |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 852 | inode->i_uid = current_fsuid(); |
| 853 | inode->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | return inode; |
| 857 | |
| 858 | fail_iput: |
| 859 | iput(inode); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | fail_inode: |
| 862 | return NULL; |
| 863 | } |
| 864 | |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 865 | int create_pipe_files(struct file **res, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 867 | int err; |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 868 | struct inode *inode = get_pipe_inode(); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 869 | struct file *f; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 870 | struct path path; |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 871 | static struct qstr name = { .name = "" }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | if (!inode) |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 874 | return -ENFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 876 | err = -ENOMEM; |
Nick Piggin | 4b93688 | 2011-01-07 17:50:07 +1100 | [diff] [blame] | 877 | path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name); |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 878 | if (!path.dentry) |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 879 | goto err_inode; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 880 | path.mnt = mntget(pipe_mnt); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 881 | |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 882 | d_instantiate(path.dentry, inode); |
Dave Hansen | 430e285 | 2008-02-15 14:37:26 -0800 | [diff] [blame] | 883 | |
| 884 | err = -ENFILE; |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 885 | f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops); |
Anatol Pomozov | 39b6525 | 2012-09-12 20:11:55 -0700 | [diff] [blame] | 886 | if (IS_ERR(f)) |
Dave Hansen | 430e285 | 2008-02-15 14:37:26 -0800 | [diff] [blame] | 887 | goto err_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 889 | f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)); |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 890 | f->private_data = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 892 | res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops); |
Anatol Pomozov | 39b6525 | 2012-09-12 20:11:55 -0700 | [diff] [blame] | 893 | if (IS_ERR(res[0])) |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 894 | goto err_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 896 | path_get(&path); |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 897 | res[0]->private_data = inode->i_pipe; |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 898 | res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK); |
| 899 | res[1] = f; |
| 900 | return 0; |
| 901 | |
| 902 | err_file: |
| 903 | put_filp(f); |
| 904 | err_dentry: |
Al Viro | ed15243 | 2008-04-22 19:51:27 -0400 | [diff] [blame] | 905 | free_pipe_info(inode); |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 906 | path_put(&path); |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 907 | return err; |
Al Viro | ed15243 | 2008-04-22 19:51:27 -0400 | [diff] [blame] | 908 | |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 909 | err_inode: |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 910 | free_pipe_info(inode); |
| 911 | iput(inode); |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 912 | return err; |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Al Viro | 5b249b1 | 2012-08-19 12:17:29 -0400 | [diff] [blame] | 915 | static int __do_pipe_flags(int *fd, struct file **files, int flags) |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 916 | { |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 917 | int error; |
| 918 | int fdw, fdr; |
| 919 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 920 | if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT)) |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 921 | return -EINVAL; |
| 922 | |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 923 | error = create_pipe_files(files, flags); |
| 924 | if (error) |
| 925 | return error; |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 926 | |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 927 | error = get_unused_fd_flags(flags); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 928 | if (error < 0) |
| 929 | goto err_read_pipe; |
| 930 | fdr = error; |
| 931 | |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 932 | error = get_unused_fd_flags(flags); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 933 | if (error < 0) |
| 934 | goto err_fdr; |
| 935 | fdw = error; |
| 936 | |
Al Viro | 157cf64 | 2008-12-14 04:57:47 -0500 | [diff] [blame] | 937 | audit_fd_pair(fdr, fdw); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 938 | fd[0] = fdr; |
| 939 | fd[1] = fdw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return 0; |
| 941 | |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 942 | err_fdr: |
| 943 | put_unused_fd(fdr); |
| 944 | err_read_pipe: |
Al Viro | e4fad8e | 2012-07-21 15:33:25 +0400 | [diff] [blame] | 945 | fput(files[0]); |
| 946 | fput(files[1]); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 947 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Al Viro | 5b249b1 | 2012-08-19 12:17:29 -0400 | [diff] [blame] | 950 | int do_pipe_flags(int *fd, int flags) |
| 951 | { |
| 952 | struct file *files[2]; |
| 953 | int error = __do_pipe_flags(fd, files, flags); |
| 954 | if (!error) { |
| 955 | fd_install(fd[0], files[0]); |
| 956 | fd_install(fd[1], files[1]); |
| 957 | } |
| 958 | return error; |
| 959 | } |
| 960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | /* |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 962 | * sys_pipe() is the normal C calling standard for creating |
| 963 | * a pipe. It's not the way Unix traditionally does this, though. |
| 964 | */ |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 965 | SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags) |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 966 | { |
Al Viro | 5b249b1 | 2012-08-19 12:17:29 -0400 | [diff] [blame] | 967 | struct file *files[2]; |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 968 | int fd[2]; |
| 969 | int error; |
| 970 | |
Al Viro | 5b249b1 | 2012-08-19 12:17:29 -0400 | [diff] [blame] | 971 | error = __do_pipe_flags(fd, files, flags); |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 972 | if (!error) { |
Al Viro | 5b249b1 | 2012-08-19 12:17:29 -0400 | [diff] [blame] | 973 | if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) { |
| 974 | fput(files[0]); |
| 975 | fput(files[1]); |
| 976 | put_unused_fd(fd[0]); |
| 977 | put_unused_fd(fd[1]); |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 978 | error = -EFAULT; |
Al Viro | 5b249b1 | 2012-08-19 12:17:29 -0400 | [diff] [blame] | 979 | } else { |
| 980 | fd_install(fd[0], files[0]); |
| 981 | fd_install(fd[1], files[1]); |
Ulrich Drepper | ba719ba | 2008-05-06 20:42:38 -0700 | [diff] [blame] | 982 | } |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 983 | } |
| 984 | return error; |
| 985 | } |
| 986 | |
Heiko Carstens | 2b66421 | 2009-01-14 14:14:35 +0100 | [diff] [blame] | 987 | SYSCALL_DEFINE1(pipe, int __user *, fildes) |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 988 | { |
| 989 | return sys_pipe2(fildes, 0); |
| 990 | } |
| 991 | |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 992 | static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt) |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 993 | { |
| 994 | int cur = *cnt; |
| 995 | |
| 996 | while (cur == *cnt) { |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 997 | pipe_wait(pipe); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 998 | if (signal_pending(current)) |
| 999 | break; |
| 1000 | } |
| 1001 | return cur == *cnt ? -ERESTARTSYS : 0; |
| 1002 | } |
| 1003 | |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 1004 | static void wake_up_partner(struct pipe_inode_info *pipe) |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1005 | { |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 1006 | wake_up_interruptible(&pipe->wait); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static int fifo_open(struct inode *inode, struct file *filp) |
| 1010 | { |
| 1011 | struct pipe_inode_info *pipe; |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 1012 | bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC; |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 1013 | int kill = 0; |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1014 | int ret; |
| 1015 | |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 1016 | filp->f_version = 0; |
| 1017 | |
| 1018 | spin_lock(&inode->i_lock); |
| 1019 | if (inode->i_pipe) { |
| 1020 | pipe = inode->i_pipe; |
| 1021 | pipe->files++; |
| 1022 | spin_unlock(&inode->i_lock); |
| 1023 | } else { |
| 1024 | spin_unlock(&inode->i_lock); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1025 | pipe = alloc_pipe_info(inode); |
| 1026 | if (!pipe) |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 1027 | return -ENOMEM; |
| 1028 | pipe->files = 1; |
| 1029 | spin_lock(&inode->i_lock); |
| 1030 | if (unlikely(inode->i_pipe)) { |
| 1031 | inode->i_pipe->files++; |
| 1032 | spin_unlock(&inode->i_lock); |
| 1033 | __free_pipe_info(pipe); |
| 1034 | pipe = inode->i_pipe; |
| 1035 | } else { |
| 1036 | inode->i_pipe = pipe; |
| 1037 | spin_unlock(&inode->i_lock); |
| 1038 | } |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1039 | } |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 1040 | filp->private_data = pipe; |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 1041 | /* OK, we have a pipe and it's pinned down */ |
| 1042 | |
| 1043 | pipe_lock(pipe); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1044 | |
| 1045 | /* We can only do regular read/write on fifos */ |
| 1046 | filp->f_mode &= (FMODE_READ | FMODE_WRITE); |
| 1047 | |
| 1048 | switch (filp->f_mode) { |
| 1049 | case FMODE_READ: |
| 1050 | /* |
| 1051 | * O_RDONLY |
| 1052 | * POSIX.1 says that O_NONBLOCK means return with the FIFO |
| 1053 | * opened, even when there is no process writing the FIFO. |
| 1054 | */ |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1055 | pipe->r_counter++; |
| 1056 | if (pipe->readers++ == 0) |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 1057 | wake_up_partner(pipe); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1058 | |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 1059 | if (!is_pipe && !pipe->writers) { |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1060 | if ((filp->f_flags & O_NONBLOCK)) { |
| 1061 | /* suppress POLLHUP until we have |
| 1062 | * seen a writer */ |
| 1063 | filp->f_version = pipe->w_counter; |
| 1064 | } else { |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 1065 | if (wait_for_partner(pipe, &pipe->w_counter)) |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1066 | goto err_rd; |
| 1067 | } |
| 1068 | } |
| 1069 | break; |
| 1070 | |
| 1071 | case FMODE_WRITE: |
| 1072 | /* |
| 1073 | * O_WRONLY |
| 1074 | * POSIX.1 says that O_NONBLOCK means return -1 with |
| 1075 | * errno=ENXIO when there is no process reading the FIFO. |
| 1076 | */ |
| 1077 | ret = -ENXIO; |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 1078 | if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers) |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1079 | goto err; |
| 1080 | |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1081 | pipe->w_counter++; |
| 1082 | if (!pipe->writers++) |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 1083 | wake_up_partner(pipe); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1084 | |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 1085 | if (!is_pipe && !pipe->readers) { |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 1086 | if (wait_for_partner(pipe, &pipe->r_counter)) |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1087 | goto err_wr; |
| 1088 | } |
| 1089 | break; |
| 1090 | |
| 1091 | case FMODE_READ | FMODE_WRITE: |
| 1092 | /* |
| 1093 | * O_RDWR |
| 1094 | * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set. |
| 1095 | * This implementation will NEVER block on a O_RDWR open, since |
| 1096 | * the process can at least talk to itself. |
| 1097 | */ |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1098 | |
| 1099 | pipe->readers++; |
| 1100 | pipe->writers++; |
| 1101 | pipe->r_counter++; |
| 1102 | pipe->w_counter++; |
| 1103 | if (pipe->readers == 1 || pipe->writers == 1) |
Al Viro | fc7478a | 2013-03-21 02:07:59 -0400 | [diff] [blame] | 1104 | wake_up_partner(pipe); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1105 | break; |
| 1106 | |
| 1107 | default: |
| 1108 | ret = -EINVAL; |
| 1109 | goto err; |
| 1110 | } |
| 1111 | |
| 1112 | /* Ok! */ |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 1113 | pipe_unlock(pipe); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1114 | return 0; |
| 1115 | |
| 1116 | err_rd: |
| 1117 | if (!--pipe->readers) |
| 1118 | wake_up_interruptible(&pipe->wait); |
| 1119 | ret = -ERESTARTSYS; |
| 1120 | goto err; |
| 1121 | |
| 1122 | err_wr: |
| 1123 | if (!--pipe->writers) |
| 1124 | wake_up_interruptible(&pipe->wait); |
| 1125 | ret = -ERESTARTSYS; |
| 1126 | goto err; |
| 1127 | |
| 1128 | err: |
Al Viro | ba5bb14 | 2013-03-21 02:21:19 -0400 | [diff] [blame] | 1129 | spin_lock(&inode->i_lock); |
| 1130 | if (!--pipe->files) { |
| 1131 | inode->i_pipe = NULL; |
| 1132 | kill = 1; |
| 1133 | } |
| 1134 | spin_unlock(&inode->i_lock); |
| 1135 | pipe_unlock(pipe); |
| 1136 | if (kill) |
| 1137 | __free_pipe_info(pipe); |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1138 | return ret; |
| 1139 | } |
| 1140 | |
Al Viro | 599a0ac | 2013-03-12 09:58:10 -0400 | [diff] [blame] | 1141 | const struct file_operations pipefifo_fops = { |
| 1142 | .open = fifo_open, |
| 1143 | .llseek = no_llseek, |
| 1144 | .read = do_sync_read, |
| 1145 | .aio_read = pipe_read, |
| 1146 | .write = do_sync_write, |
| 1147 | .aio_write = pipe_write, |
| 1148 | .poll = pipe_poll, |
| 1149 | .unlocked_ioctl = pipe_ioctl, |
| 1150 | .release = pipe_release, |
| 1151 | .fasync = pipe_fasync, |
Al Viro | f776c73 | 2013-03-12 09:46:27 -0400 | [diff] [blame] | 1152 | }; |
| 1153 | |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 1154 | /* |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1155 | * Allocate a new array of pipe buffers and copy the info over. Returns the |
| 1156 | * pipe size if successful, or return -ERROR on error. |
| 1157 | */ |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1158 | static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1159 | { |
| 1160 | struct pipe_buffer *bufs; |
| 1161 | |
| 1162 | /* |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1163 | * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't |
| 1164 | * expect a lot of shrink+grow operations, just free and allocate |
| 1165 | * again like we would do for growing. If the pipe currently |
| 1166 | * contains more buffers than arg, then return busy. |
| 1167 | */ |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1168 | if (nr_pages < pipe->nrbufs) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1169 | return -EBUSY; |
| 1170 | |
Sasha Levin | 2ccd4f4 | 2012-01-12 17:17:40 -0800 | [diff] [blame] | 1171 | bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1172 | if (unlikely(!bufs)) |
| 1173 | return -ENOMEM; |
| 1174 | |
| 1175 | /* |
| 1176 | * The pipe array wraps around, so just start the new one at zero |
| 1177 | * and adjust the indexes. |
| 1178 | */ |
| 1179 | if (pipe->nrbufs) { |
Miklos Szeredi | 1d862f4 | 2010-06-08 16:28:45 +0200 | [diff] [blame] | 1180 | unsigned int tail; |
| 1181 | unsigned int head; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1182 | |
Miklos Szeredi | 1d862f4 | 2010-06-08 16:28:45 +0200 | [diff] [blame] | 1183 | tail = pipe->curbuf + pipe->nrbufs; |
| 1184 | if (tail < pipe->buffers) |
| 1185 | tail = 0; |
| 1186 | else |
| 1187 | tail &= (pipe->buffers - 1); |
| 1188 | |
| 1189 | head = pipe->nrbufs - tail; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1190 | if (head) |
| 1191 | memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer)); |
| 1192 | if (tail) |
Miklos Szeredi | 1d862f4 | 2010-06-08 16:28:45 +0200 | [diff] [blame] | 1193 | memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer)); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | pipe->curbuf = 0; |
| 1197 | kfree(pipe->bufs); |
| 1198 | pipe->bufs = bufs; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1199 | pipe->buffers = nr_pages; |
| 1200 | return nr_pages * PAGE_SIZE; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1203 | /* |
| 1204 | * Currently we rely on the pipe array holding a power-of-2 number |
| 1205 | * of pages. |
| 1206 | */ |
| 1207 | static inline unsigned int round_pipe_size(unsigned int size) |
| 1208 | { |
| 1209 | unsigned long nr_pages; |
| 1210 | |
| 1211 | nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1212 | return roundup_pow_of_two(nr_pages) << PAGE_SHIFT; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax |
| 1217 | * will return an error. |
| 1218 | */ |
| 1219 | int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf, |
| 1220 | size_t *lenp, loff_t *ppos) |
| 1221 | { |
| 1222 | int ret; |
| 1223 | |
| 1224 | ret = proc_dointvec_minmax(table, write, buf, lenp, ppos); |
| 1225 | if (ret < 0 || !write) |
| 1226 | return ret; |
| 1227 | |
| 1228 | pipe_max_size = round_pipe_size(pipe_max_size); |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
Linus Torvalds | 7208364 | 2010-11-28 16:27:19 -0800 | [diff] [blame] | 1232 | /* |
| 1233 | * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same |
| 1234 | * location, so checking ->i_pipe is not enough to verify that this is a |
| 1235 | * pipe. |
| 1236 | */ |
| 1237 | struct pipe_inode_info *get_pipe_info(struct file *file) |
| 1238 | { |
Al Viro | de32ec4 | 2013-03-21 11:16:56 -0400 | [diff] [blame^] | 1239 | return file->f_op == &pipefifo_fops ? file->private_data : NULL; |
Linus Torvalds | 7208364 | 2010-11-28 16:27:19 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1242 | long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1243 | { |
| 1244 | struct pipe_inode_info *pipe; |
| 1245 | long ret; |
| 1246 | |
Linus Torvalds | c66fb34 | 2010-11-28 14:09:57 -0800 | [diff] [blame] | 1247 | pipe = get_pipe_info(file); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1248 | if (!pipe) |
| 1249 | return -EBADF; |
| 1250 | |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 1251 | pipe_lock(pipe); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1252 | |
| 1253 | switch (cmd) { |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1254 | case F_SETPIPE_SZ: { |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1255 | unsigned int size, nr_pages; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1256 | |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1257 | size = round_pipe_size(arg); |
| 1258 | nr_pages = size >> PAGE_SHIFT; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1259 | |
Miklos Szeredi | 6db40cf | 2010-06-09 09:27:57 +0200 | [diff] [blame] | 1260 | ret = -EINVAL; |
| 1261 | if (!nr_pages) |
| 1262 | goto out; |
| 1263 | |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1264 | if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) { |
Jens Axboe | b4ca761 | 2010-06-01 12:42:12 +0200 | [diff] [blame] | 1265 | ret = -EPERM; |
Julia Lawall | cc967be | 2010-05-26 17:54:39 +0200 | [diff] [blame] | 1266 | goto out; |
Julia Lawall | cc967be | 2010-05-26 17:54:39 +0200 | [diff] [blame] | 1267 | } |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1268 | ret = pipe_set_size(pipe, nr_pages); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1269 | break; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1270 | } |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1271 | case F_GETPIPE_SZ: |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1272 | ret = pipe->buffers * PAGE_SIZE; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1273 | break; |
| 1274 | default: |
| 1275 | ret = -EINVAL; |
| 1276 | break; |
| 1277 | } |
| 1278 | |
Julia Lawall | cc967be | 2010-05-26 17:54:39 +0200 | [diff] [blame] | 1279 | out: |
Al Viro | 18c03cf | 2013-03-21 02:16:30 -0400 | [diff] [blame] | 1280 | pipe_unlock(pipe); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1281 | return ret; |
| 1282 | } |
| 1283 | |
Nick Piggin | ff0c7d1 | 2011-01-07 17:49:50 +1100 | [diff] [blame] | 1284 | static const struct super_operations pipefs_ops = { |
| 1285 | .destroy_inode = free_inode_nonrcu, |
Pavel Emelyanov | d70ef97 | 2011-10-31 17:10:04 -0700 | [diff] [blame] | 1286 | .statfs = simple_statfs, |
Nick Piggin | ff0c7d1 | 2011-01-07 17:49:50 +1100 | [diff] [blame] | 1287 | }; |
| 1288 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1289 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | * pipefs should _never_ be mounted by userland - too much of security hassle, |
| 1291 | * no real gain from having the whole whorehouse mounted. So we don't need |
| 1292 | * any operations on the root directory. However, we need a non-trivial |
| 1293 | * d_name - pipe: will go nicely and kill the special-casing in procfs. |
| 1294 | */ |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1295 | static struct dentry *pipefs_mount(struct file_system_type *fs_type, |
| 1296 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | { |
Al Viro | c74a1cb | 2011-01-12 16:59:34 -0500 | [diff] [blame] | 1298 | return mount_pseudo(fs_type, "pipe:", &pipefs_ops, |
| 1299 | &pipefs_dentry_operations, PIPEFS_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | static struct file_system_type pipe_fs_type = { |
| 1303 | .name = "pipefs", |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1304 | .mount = pipefs_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | .kill_sb = kill_anon_super, |
| 1306 | }; |
| 1307 | |
| 1308 | static int __init init_pipe_fs(void) |
| 1309 | { |
| 1310 | int err = register_filesystem(&pipe_fs_type); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | if (!err) { |
| 1313 | pipe_mnt = kern_mount(&pipe_fs_type); |
| 1314 | if (IS_ERR(pipe_mnt)) { |
| 1315 | err = PTR_ERR(pipe_mnt); |
| 1316 | unregister_filesystem(&pipe_fs_type); |
| 1317 | } |
| 1318 | } |
| 1319 | return err; |
| 1320 | } |
| 1321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | fs_initcall(init_pipe_fs); |