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 | |
| 28 | /* |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 29 | * 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] | 30 | * be set by root in /proc/sys/fs/pipe-max-size |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 31 | */ |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 32 | unsigned int pipe_max_size = 1048576; |
| 33 | |
| 34 | /* |
| 35 | * Minimum pipe size, as required by POSIX |
| 36 | */ |
| 37 | unsigned int pipe_min_size = PAGE_SIZE; |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 39 | /* Maximum allocatable pages per user. Hard limit is unset by default, soft |
| 40 | * matches default values. |
| 41 | */ |
| 42 | unsigned long pipe_user_pages_hard; |
| 43 | unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR; |
| 44 | |
Jens Axboe | b492e95 | 2010-05-19 21:03:16 +0200 | [diff] [blame] | 45 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * We use a start+len construction, which provides full use of the |
| 47 | * allocated memory. |
| 48 | * -- Florian Coosmann (FGC) |
| 49 | * |
| 50 | * Reads with count = 0 should always return 0. |
| 51 | * -- Julian Bradfield 1999-06-07. |
| 52 | * |
| 53 | * FIFOs and Pipes now generate SIGIO for both readers and writers. |
| 54 | * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16 |
| 55 | * |
| 56 | * pipe_read & write cleanup |
| 57 | * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09 |
| 58 | */ |
| 59 | |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 60 | static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass) |
| 61 | { |
| 62 | if (pipe->inode) |
| 63 | mutex_lock_nested(&pipe->inode->i_mutex, subclass); |
| 64 | } |
| 65 | |
| 66 | void pipe_lock(struct pipe_inode_info *pipe) |
| 67 | { |
| 68 | /* |
| 69 | * pipe_lock() nests non-pipe inode locks (for writing to a file) |
| 70 | */ |
| 71 | pipe_lock_nested(pipe, I_MUTEX_PARENT); |
| 72 | } |
| 73 | EXPORT_SYMBOL(pipe_lock); |
| 74 | |
| 75 | void pipe_unlock(struct pipe_inode_info *pipe) |
| 76 | { |
| 77 | if (pipe->inode) |
| 78 | mutex_unlock(&pipe->inode->i_mutex); |
| 79 | } |
| 80 | EXPORT_SYMBOL(pipe_unlock); |
| 81 | |
| 82 | void pipe_double_lock(struct pipe_inode_info *pipe1, |
| 83 | struct pipe_inode_info *pipe2) |
| 84 | { |
| 85 | BUG_ON(pipe1 == pipe2); |
| 86 | |
| 87 | if (pipe1 < pipe2) { |
| 88 | pipe_lock_nested(pipe1, I_MUTEX_PARENT); |
| 89 | pipe_lock_nested(pipe2, I_MUTEX_CHILD); |
| 90 | } else { |
Peter Zijlstra | 023d43c | 2009-07-21 10:09:23 +0200 | [diff] [blame] | 91 | pipe_lock_nested(pipe2, I_MUTEX_PARENT); |
| 92 | pipe_lock_nested(pipe1, I_MUTEX_CHILD); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* Drop the inode semaphore and wait for a pipe event, atomically */ |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 97 | void pipe_wait(struct pipe_inode_info *pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
| 99 | DEFINE_WAIT(wait); |
| 100 | |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Pipes are system-local resources, so sleeping on them |
| 103 | * is considered a noninteractive wait: |
| 104 | */ |
Mike Galbraith | af92723 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 105 | prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 106 | pipe_unlock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | schedule(); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 108 | finish_wait(&pipe->wait, &wait); |
Miklos Szeredi | 61e0d47 | 2009-04-14 19:48:41 +0200 | [diff] [blame] | 109 | pipe_lock(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 112 | static int |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 113 | pipe_iov_copy_from_user(void *addr, int *offset, struct iovec *iov, |
| 114 | size_t *remaining, int atomic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | unsigned long copy; |
| 117 | |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 118 | while (*remaining > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | while (!iov->iov_len) |
| 120 | iov++; |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 121 | copy = min_t(unsigned long, *remaining, iov->iov_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 123 | if (atomic) { |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 124 | if (__copy_from_user_inatomic(addr + *offset, |
| 125 | iov->iov_base, copy)) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 126 | return -EFAULT; |
| 127 | } else { |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 128 | if (copy_from_user(addr + *offset, |
| 129 | iov->iov_base, copy)) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 130 | return -EFAULT; |
| 131 | } |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 132 | *offset += copy; |
| 133 | *remaining -= copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | iov->iov_base += copy; |
| 135 | iov->iov_len -= copy; |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 140 | static int |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 141 | pipe_iov_copy_to_user(struct iovec *iov, void *addr, int *offset, |
| 142 | size_t *remaining, int atomic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | unsigned long copy; |
| 145 | |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 146 | while (*remaining > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | while (!iov->iov_len) |
| 148 | iov++; |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 149 | copy = min_t(unsigned long, *remaining, iov->iov_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 151 | if (atomic) { |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 152 | if (__copy_to_user_inatomic(iov->iov_base, |
| 153 | addr + *offset, copy)) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 154 | return -EFAULT; |
| 155 | } else { |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 156 | if (copy_to_user(iov->iov_base, |
| 157 | addr + *offset, copy)) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 158 | return -EFAULT; |
| 159 | } |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 160 | *offset += copy; |
| 161 | *remaining -= copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | iov->iov_base += copy; |
| 163 | iov->iov_len -= copy; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 168 | /* |
| 169 | * Attempt to pre-fault in the user memory, so we can use atomic copies. |
| 170 | * Returns the number of bytes not faulted in. |
| 171 | */ |
| 172 | static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len) |
| 173 | { |
| 174 | while (!iov->iov_len) |
| 175 | iov++; |
| 176 | |
| 177 | while (len > 0) { |
| 178 | unsigned long this_len; |
| 179 | |
| 180 | this_len = min_t(unsigned long, len, iov->iov_len); |
| 181 | if (fault_in_pages_writeable(iov->iov_base, this_len)) |
| 182 | break; |
| 183 | |
| 184 | len -= this_len; |
| 185 | iov++; |
| 186 | } |
| 187 | |
| 188 | return len; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Pre-fault in the user memory, so we can use atomic copies. |
| 193 | */ |
| 194 | static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len) |
| 195 | { |
| 196 | while (!iov->iov_len) |
| 197 | iov++; |
| 198 | |
| 199 | while (len > 0) { |
| 200 | unsigned long this_len; |
| 201 | |
| 202 | this_len = min_t(unsigned long, len, iov->iov_len); |
| 203 | fault_in_pages_readable(iov->iov_base, this_len); |
| 204 | len -= this_len; |
| 205 | iov++; |
| 206 | } |
| 207 | } |
| 208 | |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 209 | static void anon_pipe_buf_release(struct pipe_inode_info *pipe, |
| 210 | struct pipe_buffer *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | struct page *page = buf->page; |
| 213 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 214 | /* |
| 215 | * If nobody else uses this page, and we don't already have a |
| 216 | * temporary page, let's keep track of it as a one-deep |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 217 | * allocation cache. (Otherwise just release our reference to it) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 218 | */ |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 219 | if (page_count(page) == 1 && !pipe->tmp_page) |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 220 | pipe->tmp_page = page; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 221 | else |
| 222 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 225 | /** |
| 226 | * generic_pipe_buf_map - virtually map a pipe buffer |
| 227 | * @pipe: the pipe that the buffer belongs to |
| 228 | * @buf: the buffer that should be mapped |
| 229 | * @atomic: whether to use an atomic map |
| 230 | * |
| 231 | * Description: |
| 232 | * This function returns a kernel virtual address mapping for the |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 233 | * 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] | 234 | * and the caller has to be careful not to fault before calling |
| 235 | * the unmap function. |
| 236 | * |
| 237 | * Note that this function occupies KM_USER0 if @atomic != 0. |
| 238 | */ |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 239 | void *generic_pipe_buf_map(struct pipe_inode_info *pipe, |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 240 | struct pipe_buffer *buf, int atomic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 242 | if (atomic) { |
| 243 | buf->flags |= PIPE_BUF_FLAG_ATOMIC; |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 244 | return kmap_atomic(buf->page); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return kmap(buf->page); |
| 248 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 249 | EXPORT_SYMBOL(generic_pipe_buf_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 251 | /** |
| 252 | * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer |
| 253 | * @pipe: the pipe that the buffer belongs to |
| 254 | * @buf: the buffer that should be unmapped |
| 255 | * @map_data: the data that the mapping function returned |
| 256 | * |
| 257 | * Description: |
| 258 | * This function undoes the mapping that ->map() provided. |
| 259 | */ |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 260 | void generic_pipe_buf_unmap(struct pipe_inode_info *pipe, |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 261 | struct pipe_buffer *buf, void *map_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 263 | if (buf->flags & PIPE_BUF_FLAG_ATOMIC) { |
| 264 | buf->flags &= ~PIPE_BUF_FLAG_ATOMIC; |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 265 | kunmap_atomic(map_data); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 266 | } else |
| 267 | kunmap(buf->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 269 | EXPORT_SYMBOL(generic_pipe_buf_unmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 271 | /** |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 272 | * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 273 | * @pipe: the pipe that the buffer belongs to |
| 274 | * @buf: the buffer to attempt to steal |
| 275 | * |
| 276 | * Description: |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 277 | * This function attempts to steal the &struct page attached to |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 278 | * @buf. If successful, this function returns 0 and returns with |
| 279 | * the page locked. The caller may then reuse the page for whatever |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 280 | * he wishes; the typical use is insertion into a different file |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 281 | * page cache. |
| 282 | */ |
Jens Axboe | 330ab71 | 2006-05-02 15:29:57 +0200 | [diff] [blame] | 283 | int generic_pipe_buf_steal(struct pipe_inode_info *pipe, |
| 284 | struct pipe_buffer *buf) |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 285 | { |
Jens Axboe | 46e678c | 2006-04-30 16:36:32 +0200 | [diff] [blame] | 286 | struct page *page = buf->page; |
| 287 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 288 | /* |
| 289 | * A reference of one is golden, that means that the owner of this |
| 290 | * page is the only one holding a reference to it. lock the page |
| 291 | * and return OK. |
| 292 | */ |
Jens Axboe | 46e678c | 2006-04-30 16:36:32 +0200 | [diff] [blame] | 293 | if (page_count(page) == 1) { |
Jens Axboe | 46e678c | 2006-04-30 16:36:32 +0200 | [diff] [blame] | 294 | lock_page(page); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | return 1; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 299 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 300 | EXPORT_SYMBOL(generic_pipe_buf_steal); |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 301 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 302 | /** |
Randy Dunlap | b51d63c | 2008-02-13 15:03:22 -0800 | [diff] [blame] | 303 | * generic_pipe_buf_get - get a reference to a &struct pipe_buffer |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 304 | * @pipe: the pipe that the buffer belongs to |
| 305 | * @buf: the buffer to get a reference to |
| 306 | * |
| 307 | * Description: |
| 308 | * This function grabs an extra reference to @buf. It's used in |
| 309 | * in the tee() system call, when we duplicate the buffers in one |
| 310 | * pipe into another. |
| 311 | */ |
| 312 | 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] | 313 | { |
| 314 | page_cache_get(buf->page); |
| 315 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 316 | EXPORT_SYMBOL(generic_pipe_buf_get); |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 317 | |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 318 | /** |
| 319 | * generic_pipe_buf_confirm - verify contents of the pipe buffer |
Randy Dunlap | 79685b8 | 2007-07-27 08:08:51 +0200 | [diff] [blame] | 320 | * @info: the pipe that the buffer belongs to |
Jens Axboe | 0845718 | 2007-06-12 20:51:32 +0200 | [diff] [blame] | 321 | * @buf: the buffer to confirm |
| 322 | * |
| 323 | * Description: |
| 324 | * This function does nothing, because the generic pipe code uses |
| 325 | * pages that are always good when inserted into the pipe. |
| 326 | */ |
Jens Axboe | cac36bb | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 327 | int generic_pipe_buf_confirm(struct pipe_inode_info *info, |
| 328 | struct pipe_buffer *buf) |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 329 | { |
| 330 | return 0; |
| 331 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 332 | EXPORT_SYMBOL(generic_pipe_buf_confirm); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 333 | |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 334 | /** |
| 335 | * generic_pipe_buf_release - put a reference to a &struct pipe_buffer |
| 336 | * @pipe: the pipe that the buffer belongs to |
| 337 | * @buf: the buffer to put a reference to |
| 338 | * |
| 339 | * Description: |
| 340 | * This function releases a reference to @buf. |
| 341 | */ |
| 342 | void generic_pipe_buf_release(struct pipe_inode_info *pipe, |
| 343 | struct pipe_buffer *buf) |
| 344 | { |
| 345 | page_cache_release(buf->page); |
| 346 | } |
Miklos Szeredi | 51921cb | 2010-05-26 08:44:22 +0200 | [diff] [blame] | 347 | EXPORT_SYMBOL(generic_pipe_buf_release); |
Miklos Szeredi | 6818173 | 2009-05-07 15:37:36 +0200 | [diff] [blame] | 348 | |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 349 | static const struct pipe_buf_operations anon_pipe_buf_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | .can_merge = 1, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 351 | .map = generic_pipe_buf_map, |
| 352 | .unmap = generic_pipe_buf_unmap, |
Jens Axboe | cac36bb | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 353 | .confirm = generic_pipe_buf_confirm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | .release = anon_pipe_buf_release, |
Jens Axboe | 330ab71 | 2006-05-02 15:29:57 +0200 | [diff] [blame] | 355 | .steal = generic_pipe_buf_steal, |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 356 | .get = generic_pipe_buf_get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 359 | static const struct pipe_buf_operations packet_pipe_buf_ops = { |
| 360 | .can_merge = 0, |
| 361 | .map = generic_pipe_buf_map, |
| 362 | .unmap = generic_pipe_buf_unmap, |
| 363 | .confirm = generic_pipe_buf_confirm, |
| 364 | .release = anon_pipe_buf_release, |
| 365 | .steal = generic_pipe_buf_steal, |
| 366 | .get = generic_pipe_buf_get, |
| 367 | }; |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | static ssize_t |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 370 | pipe_read(struct kiocb *iocb, const struct iovec *_iov, |
| 371 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 373 | struct file *filp = iocb->ki_filp; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 374 | struct inode *inode = filp->f_path.dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 375 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | int do_wakeup; |
| 377 | ssize_t ret; |
| 378 | struct iovec *iov = (struct iovec *)_iov; |
| 379 | size_t total_len; |
| 380 | |
| 381 | total_len = iov_length(iov, nr_segs); |
| 382 | /* Null read succeeds. */ |
| 383 | if (unlikely(total_len == 0)) |
| 384 | return 0; |
| 385 | |
| 386 | do_wakeup = 0; |
| 387 | ret = 0; |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 388 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 389 | pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | for (;;) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 391 | int bufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (bufs) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 393 | int curbuf = pipe->curbuf; |
| 394 | struct pipe_buffer *buf = pipe->bufs + curbuf; |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 395 | const struct pipe_buf_operations *ops = buf->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | void *addr; |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 397 | size_t chars = buf->len, remaining; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 398 | int error, atomic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | if (chars > total_len) |
| 401 | chars = total_len; |
| 402 | |
Jens Axboe | cac36bb | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 403 | error = ops->confirm(pipe, buf); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 404 | if (error) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 405 | if (!ret) |
Nicolas Kaiser | e5953cb | 2010-10-21 14:56:00 +0200 | [diff] [blame] | 406 | ret = error; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 407 | break; |
| 408 | } |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 409 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 410 | atomic = !iov_fault_in_pages_write(iov, chars); |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 411 | remaining = chars; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 412 | redo: |
| 413 | addr = ops->map(pipe, buf, atomic); |
Ben Hutchings | 91ec8cc | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 414 | error = pipe_iov_copy_to_user(iov, addr, &buf->offset, |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 415 | &remaining, atomic); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 416 | ops->unmap(pipe, buf, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | if (unlikely(error)) { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 418 | /* |
| 419 | * Just retry with the slow path if we failed. |
| 420 | */ |
| 421 | if (atomic) { |
| 422 | atomic = 0; |
| 423 | goto redo; |
| 424 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 425 | if (!ret) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 426 | ret = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | ret += chars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | buf->len -= chars; |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 431 | |
| 432 | /* Was it a packet buffer? Clean up and exit */ |
| 433 | if (buf->flags & PIPE_BUF_FLAG_PACKET) { |
| 434 | total_len = chars; |
| 435 | buf->len = 0; |
| 436 | } |
| 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (!buf->len) { |
| 439 | buf->ops = NULL; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 440 | ops->release(pipe, buf); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 441 | curbuf = (curbuf + 1) & (pipe->buffers - 1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 442 | pipe->curbuf = curbuf; |
| 443 | pipe->nrbufs = --bufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | do_wakeup = 1; |
| 445 | } |
| 446 | total_len -= chars; |
| 447 | if (!total_len) |
| 448 | break; /* common path: read succeeded */ |
| 449 | } |
| 450 | if (bufs) /* More to do? */ |
| 451 | continue; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 452 | if (!pipe->writers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | break; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 454 | if (!pipe->waiting_writers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* syscall merging: Usually we must not sleep |
| 456 | * if O_NONBLOCK is set, or if we got some data. |
| 457 | * But if a writer sleeps in kernel space, then |
| 458 | * we can wait for that data without violating POSIX. |
| 459 | */ |
| 460 | if (ret) |
| 461 | break; |
| 462 | if (filp->f_flags & O_NONBLOCK) { |
| 463 | ret = -EAGAIN; |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | if (signal_pending(current)) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 468 | if (!ret) |
| 469 | ret = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 473 | wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 474 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 476 | pipe_wait(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 478 | mutex_unlock(&inode->i_mutex); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 479 | |
| 480 | /* Signal writers asynchronously that there is more room. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 482 | wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 483 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | if (ret > 0) |
| 486 | file_accessed(filp); |
| 487 | return ret; |
| 488 | } |
| 489 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 490 | static inline int is_packetized(struct file *file) |
| 491 | { |
| 492 | return (file->f_flags & O_DIRECT) != 0; |
| 493 | } |
| 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | static ssize_t |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 496 | pipe_write(struct kiocb *iocb, const struct iovec *_iov, |
| 497 | unsigned long nr_segs, loff_t ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 499 | struct file *filp = iocb->ki_filp; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 500 | struct inode *inode = filp->f_path.dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 501 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | ssize_t ret; |
| 503 | int do_wakeup; |
| 504 | struct iovec *iov = (struct iovec *)_iov; |
| 505 | size_t total_len; |
| 506 | ssize_t chars; |
| 507 | |
| 508 | total_len = iov_length(iov, nr_segs); |
| 509 | /* Null write succeeds. */ |
| 510 | if (unlikely(total_len == 0)) |
| 511 | return 0; |
| 512 | |
| 513 | do_wakeup = 0; |
| 514 | ret = 0; |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 515 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 516 | pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 518 | if (!pipe->readers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | send_sig(SIGPIPE, current, 0); |
| 520 | ret = -EPIPE; |
| 521 | goto out; |
| 522 | } |
| 523 | |
| 524 | /* We try to merge small writes */ |
| 525 | chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 526 | if (pipe->nrbufs && chars != 0) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 527 | int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) & |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 528 | (pipe->buffers - 1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 529 | struct pipe_buffer *buf = pipe->bufs + lastbuf; |
Eric Dumazet | d4c3cca | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 530 | const struct pipe_buf_operations *ops = buf->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | int offset = buf->offset + buf->len; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (ops->can_merge && offset + chars <= PAGE_SIZE) { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 534 | int error, atomic = 1; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 535 | void *addr; |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 536 | size_t remaining = chars; |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 537 | |
Jens Axboe | cac36bb | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 538 | error = ops->confirm(pipe, buf); |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 539 | if (error) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 540 | goto out; |
Jens Axboe | f84d751 | 2006-05-01 19:59:03 +0200 | [diff] [blame] | 541 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 542 | iov_fault_in_pages_read(iov, chars); |
| 543 | redo1: |
| 544 | addr = ops->map(pipe, buf, atomic); |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 545 | error = pipe_iov_copy_from_user(addr, &offset, iov, |
| 546 | &remaining, atomic); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 547 | ops->unmap(pipe, buf, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | ret = error; |
| 549 | do_wakeup = 1; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 550 | if (error) { |
| 551 | if (atomic) { |
| 552 | atomic = 0; |
| 553 | goto redo1; |
| 554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | goto out; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | buf->len += chars; |
| 558 | total_len -= chars; |
| 559 | ret = chars; |
| 560 | if (!total_len) |
| 561 | goto out; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | for (;;) { |
| 566 | int bufs; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 567 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 568 | if (!pipe->readers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | send_sig(SIGPIPE, current, 0); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 570 | if (!ret) |
| 571 | ret = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | break; |
| 573 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 574 | bufs = pipe->nrbufs; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 575 | if (bufs < pipe->buffers) { |
| 576 | int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 577 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
| 578 | struct page *page = pipe->tmp_page; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 579 | char *src; |
| 580 | int error, atomic = 1; |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 581 | int offset = 0; |
| 582 | size_t remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | if (!page) { |
| 585 | page = alloc_page(GFP_HIGHUSER); |
| 586 | if (unlikely(!page)) { |
| 587 | ret = ret ? : -ENOMEM; |
| 588 | break; |
| 589 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 590 | pipe->tmp_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 592 | /* Always wake up, even if the copy fails. Otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | * we lock up (O_NONBLOCK-)readers that sleep due to |
| 594 | * syscall merging. |
| 595 | * FIXME! Is this really true? |
| 596 | */ |
| 597 | do_wakeup = 1; |
| 598 | chars = PAGE_SIZE; |
| 599 | if (chars > total_len) |
| 600 | chars = total_len; |
| 601 | |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 602 | iov_fault_in_pages_read(iov, chars); |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 603 | remaining = chars; |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 604 | redo2: |
| 605 | if (atomic) |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 606 | src = kmap_atomic(page); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 607 | else |
| 608 | src = kmap(page); |
| 609 | |
Ben Hutchings | 3e223c2 | 2015-06-16 22:11:06 +0100 | [diff] [blame] | 610 | error = pipe_iov_copy_from_user(src, &offset, iov, |
| 611 | &remaining, atomic); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 612 | if (atomic) |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 613 | kunmap_atomic(src); |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 614 | else |
| 615 | kunmap(page); |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | if (unlikely(error)) { |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 618 | if (atomic) { |
| 619 | atomic = 0; |
| 620 | goto redo2; |
| 621 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 622 | if (!ret) |
Jens Axboe | f6762b7 | 2006-05-01 20:02:05 +0200 | [diff] [blame] | 623 | ret = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | ret += chars; |
| 627 | |
| 628 | /* Insert it into the buffer array */ |
| 629 | buf->page = page; |
| 630 | buf->ops = &anon_pipe_buf_ops; |
| 631 | buf->offset = 0; |
| 632 | buf->len = chars; |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 633 | buf->flags = 0; |
| 634 | if (is_packetized(filp)) { |
| 635 | buf->ops = &packet_pipe_buf_ops; |
| 636 | buf->flags = PIPE_BUF_FLAG_PACKET; |
| 637 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 638 | pipe->nrbufs = ++bufs; |
| 639 | pipe->tmp_page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | total_len -= chars; |
| 642 | if (!total_len) |
| 643 | break; |
| 644 | } |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 645 | if (bufs < pipe->buffers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | continue; |
| 647 | if (filp->f_flags & O_NONBLOCK) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 648 | if (!ret) |
| 649 | ret = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | if (signal_pending(current)) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 653 | if (!ret) |
| 654 | ret = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 658 | wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 659 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | do_wakeup = 0; |
| 661 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 662 | pipe->waiting_writers++; |
| 663 | pipe_wait(pipe); |
| 664 | pipe->waiting_writers--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | out: |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 667 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | if (do_wakeup) { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 669 | wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 670 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
Josef Bacik | 295534f | 2012-03-26 09:59:21 -0400 | [diff] [blame] | 672 | if (ret > 0) { |
| 673 | int err = file_update_time(filp); |
| 674 | if (err) |
| 675 | ret = err; |
| 676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos) |
| 682 | { |
| 683 | return -EBADF; |
| 684 | } |
| 685 | |
| 686 | static ssize_t |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 687 | bad_pipe_w(struct file *filp, const char __user *buf, size_t count, |
| 688 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
| 690 | return -EBADF; |
| 691 | } |
| 692 | |
Andi Kleen | d59d0b1 | 2008-02-08 04:21:23 -0800 | [diff] [blame] | 693 | 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] | 694 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 695 | struct inode *inode = filp->f_path.dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 696 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | int count, buf, nrbufs; |
| 698 | |
| 699 | switch (cmd) { |
| 700 | case FIONREAD: |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 701 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 702 | pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | count = 0; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 704 | buf = pipe->curbuf; |
| 705 | nrbufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | while (--nrbufs >= 0) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 707 | count += pipe->bufs[buf].len; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 708 | buf = (buf+1) & (pipe->buffers - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 710 | mutex_unlock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return put_user(count, (int __user *)arg); |
| 713 | default: |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /* No kernel lock held - fine */ |
| 719 | static unsigned int |
| 720 | pipe_poll(struct file *filp, poll_table *wait) |
| 721 | { |
| 722 | unsigned int mask; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 723 | struct inode *inode = filp->f_path.dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 724 | struct pipe_inode_info *pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | int nrbufs; |
| 726 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 727 | poll_wait(filp, &pipe->wait, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | /* Reading only -- no need for acquiring the semaphore. */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 730 | nrbufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | mask = 0; |
| 732 | if (filp->f_mode & FMODE_READ) { |
| 733 | mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 734 | if (!pipe->writers && filp->f_version != pipe->w_counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | mask |= POLLHUP; |
| 736 | } |
| 737 | |
| 738 | if (filp->f_mode & FMODE_WRITE) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 739 | mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0; |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 740 | /* |
| 741 | * Most Unices do not set POLLERR for FIFOs but on Linux they |
| 742 | * behave exactly like pipes for poll(). |
| 743 | */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 744 | if (!pipe->readers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | mask |= POLLERR; |
| 746 | } |
| 747 | |
| 748 | return mask; |
| 749 | } |
| 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | static int |
| 752 | pipe_release(struct inode *inode, int decr, int decw) |
| 753 | { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 754 | struct pipe_inode_info *pipe; |
| 755 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 756 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 757 | pipe = inode->i_pipe; |
| 758 | pipe->readers -= decr; |
| 759 | pipe->writers -= decw; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 760 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 761 | if (!pipe->readers && !pipe->writers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | free_pipe_info(inode); |
| 763 | } else { |
Linus Torvalds | 28e58ee | 2011-01-20 16:21:59 -0800 | [diff] [blame] | 764 | 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] | 765 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
| 766 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 768 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | static int |
| 774 | pipe_read_fasync(int fd, struct file *filp, int on) |
| 775 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 776 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | int retval; |
| 778 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 779 | mutex_lock(&inode->i_mutex); |
| 780 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers); |
| 781 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 783 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
| 787 | static int |
| 788 | pipe_write_fasync(int fd, struct file *filp, int on) |
| 789 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 790 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | int retval; |
| 792 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 793 | mutex_lock(&inode->i_mutex); |
| 794 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers); |
| 795 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 797 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | |
| 801 | static int |
| 802 | pipe_rdwr_fasync(int fd, struct file *filp, int on) |
| 803 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 804 | struct inode *inode = filp->f_path.dentry->d_inode; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 805 | struct pipe_inode_info *pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | int retval; |
| 807 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 808 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 809 | retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); |
Oleg Nesterov | e5bc49b | 2009-03-12 14:31:28 -0700 | [diff] [blame] | 810 | if (retval >= 0) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 811 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
Oleg Nesterov | e5bc49b | 2009-03-12 14:31:28 -0700 | [diff] [blame] | 812 | if (retval < 0) /* this can happen only if on == T */ |
| 813 | fasync_helper(-1, filp, 0, &pipe->fasync_readers); |
| 814 | } |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 815 | mutex_unlock(&inode->i_mutex); |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 816 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | |
| 820 | static int |
| 821 | pipe_read_release(struct inode *inode, struct file *filp) |
| 822 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | return pipe_release(inode, 1, 0); |
| 824 | } |
| 825 | |
| 826 | static int |
| 827 | pipe_write_release(struct inode *inode, struct file *filp) |
| 828 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return pipe_release(inode, 0, 1); |
| 830 | } |
| 831 | |
| 832 | static int |
| 833 | pipe_rdwr_release(struct inode *inode, struct file *filp) |
| 834 | { |
| 835 | int decr, decw; |
| 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | decr = (filp->f_mode & FMODE_READ) != 0; |
| 838 | decw = (filp->f_mode & FMODE_WRITE) != 0; |
| 839 | return pipe_release(inode, decr, decw); |
| 840 | } |
| 841 | |
| 842 | static int |
| 843 | pipe_read_open(struct inode *inode, struct file *filp) |
| 844 | { |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 845 | int ret = -ENOENT; |
| 846 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 847 | mutex_lock(&inode->i_mutex); |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 848 | |
| 849 | if (inode->i_pipe) { |
| 850 | ret = 0; |
| 851 | inode->i_pipe->readers++; |
| 852 | } |
| 853 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 854 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 856 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static int |
| 860 | pipe_write_open(struct inode *inode, struct file *filp) |
| 861 | { |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 862 | int ret = -ENOENT; |
| 863 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 864 | mutex_lock(&inode->i_mutex); |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 865 | |
| 866 | if (inode->i_pipe) { |
| 867 | ret = 0; |
| 868 | inode->i_pipe->writers++; |
| 869 | } |
| 870 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 871 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 873 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static int |
| 877 | pipe_rdwr_open(struct inode *inode, struct file *filp) |
| 878 | { |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 879 | int ret = -ENOENT; |
| 880 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 881 | mutex_lock(&inode->i_mutex); |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 882 | |
| 883 | if (inode->i_pipe) { |
| 884 | ret = 0; |
| 885 | if (filp->f_mode & FMODE_READ) |
| 886 | inode->i_pipe->readers++; |
| 887 | if (filp->f_mode & FMODE_WRITE) |
| 888 | inode->i_pipe->writers++; |
| 889 | } |
| 890 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 891 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Earl Chew | ad39602 | 2009-10-19 15:55:41 -0700 | [diff] [blame] | 893 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* |
| 897 | * The file_operations structs are not static because they |
| 898 | * are also used in linux/fs/fifo.c to do operations on FIFOs. |
Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 899 | * |
| 900 | * Pipes reuse fifos' file_operations structs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | */ |
Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 902 | const struct file_operations read_pipefifo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | .llseek = no_llseek, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 904 | .read = do_sync_read, |
| 905 | .aio_read = pipe_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | .write = bad_pipe_w, |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 907 | .poll = pipe_poll, |
Andi Kleen | d59d0b1 | 2008-02-08 04:21:23 -0800 | [diff] [blame] | 908 | .unlocked_ioctl = pipe_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | .open = pipe_read_open, |
| 910 | .release = pipe_read_release, |
| 911 | .fasync = pipe_read_fasync, |
| 912 | }; |
| 913 | |
Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 914 | const struct file_operations write_pipefifo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | .llseek = no_llseek, |
| 916 | .read = bad_pipe_r, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 917 | .write = do_sync_write, |
| 918 | .aio_write = pipe_write, |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 919 | .poll = pipe_poll, |
Andi Kleen | d59d0b1 | 2008-02-08 04:21:23 -0800 | [diff] [blame] | 920 | .unlocked_ioctl = pipe_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | .open = pipe_write_open, |
| 922 | .release = pipe_write_release, |
| 923 | .fasync = pipe_write_fasync, |
| 924 | }; |
| 925 | |
Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 926 | const struct file_operations rdwr_pipefifo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | .llseek = no_llseek, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 928 | .read = do_sync_read, |
| 929 | .aio_read = pipe_read, |
| 930 | .write = do_sync_write, |
| 931 | .aio_write = pipe_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | .poll = pipe_poll, |
Andi Kleen | d59d0b1 | 2008-02-08 04:21:23 -0800 | [diff] [blame] | 933 | .unlocked_ioctl = pipe_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | .open = pipe_rdwr_open, |
| 935 | .release = pipe_rdwr_release, |
| 936 | .fasync = pipe_rdwr_fasync, |
| 937 | }; |
| 938 | |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 939 | static void account_pipe_buffers(struct pipe_inode_info *pipe, |
| 940 | unsigned long old, unsigned long new) |
| 941 | { |
| 942 | atomic_long_add(new - old, &pipe->user->pipe_bufs); |
| 943 | } |
| 944 | |
| 945 | static bool too_many_pipe_buffers_soft(struct user_struct *user) |
| 946 | { |
| 947 | return pipe_user_pages_soft && |
| 948 | atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft; |
| 949 | } |
| 950 | |
| 951 | static bool too_many_pipe_buffers_hard(struct user_struct *user) |
| 952 | { |
| 953 | return pipe_user_pages_hard && |
| 954 | atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard; |
| 955 | } |
| 956 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 957 | struct pipe_inode_info * alloc_pipe_info(struct inode *inode) |
| 958 | { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 959 | struct pipe_inode_info *pipe; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 960 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 961 | pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL); |
| 962 | if (pipe) { |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 963 | unsigned long pipe_bufs = PIPE_DEF_BUFFERS; |
| 964 | struct user_struct *user = get_current_user(); |
| 965 | |
| 966 | if (!too_many_pipe_buffers_hard(user)) { |
| 967 | if (too_many_pipe_buffers_soft(user)) |
| 968 | pipe_bufs = 1; |
| 969 | pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL); |
| 970 | } |
| 971 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 972 | if (pipe->bufs) { |
| 973 | init_waitqueue_head(&pipe->wait); |
| 974 | pipe->r_counter = pipe->w_counter = 1; |
| 975 | pipe->inode = inode; |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 976 | pipe->buffers = pipe_bufs; |
| 977 | pipe->user = user; |
| 978 | account_pipe_buffers(pipe, 0, pipe_bufs); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 979 | return pipe; |
| 980 | } |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 981 | free_uid(user); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 982 | kfree(pipe); |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 985 | return NULL; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 988 | void __free_pipe_info(struct pipe_inode_info *pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | { |
| 990 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 992 | account_pipe_buffers(pipe, pipe->buffers, 0); |
| 993 | free_uid(pipe->user); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 994 | for (i = 0; i < pipe->buffers; i++) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 995 | struct pipe_buffer *buf = pipe->bufs + i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | if (buf->ops) |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 997 | buf->ops->release(pipe, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 999 | if (pipe->tmp_page) |
| 1000 | __free_page(pipe->tmp_page); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1001 | kfree(pipe->bufs); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 1002 | kfree(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1005 | void free_pipe_info(struct inode *inode) |
| 1006 | { |
| 1007 | __free_pipe_info(inode->i_pipe); |
| 1008 | inode->i_pipe = NULL; |
| 1009 | } |
| 1010 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 1011 | static struct vfsmount *pipe_mnt __read_mostly; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 1012 | |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * pipefs_dname() is called from d_path(). |
| 1015 | */ |
| 1016 | static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen) |
| 1017 | { |
| 1018 | return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]", |
| 1019 | dentry->d_inode->i_ino); |
| 1020 | } |
| 1021 | |
Al Viro | 3ba13d1 | 2009-02-20 06:02:22 +0000 | [diff] [blame] | 1022 | static const struct dentry_operations pipefs_dentry_operations = { |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 1023 | .d_dname = pipefs_dname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | }; |
| 1025 | |
| 1026 | static struct inode * get_pipe_inode(void) |
| 1027 | { |
Eric Dumazet | a209dfc | 2011-07-26 11:36:34 +0200 | [diff] [blame] | 1028 | struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 1029 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | if (!inode) |
| 1032 | goto fail_inode; |
| 1033 | |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 1034 | inode->i_ino = get_next_ino(); |
| 1035 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 1036 | pipe = alloc_pipe_info(inode); |
| 1037 | if (!pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | goto fail_iput; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 1039 | inode->i_pipe = pipe; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 1040 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 1041 | pipe->readers = pipe->writers = 1; |
Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 1042 | inode->i_fop = &rdwr_pipefifo_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
| 1044 | /* |
| 1045 | * Mark the inode dirty from the very beginning, |
| 1046 | * that way it will never be moved to the dirty |
| 1047 | * list because "mark_inode_dirty()" will think |
| 1048 | * that it already _is_ on the dirty list. |
| 1049 | */ |
| 1050 | inode->i_state = I_DIRTY; |
| 1051 | inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR; |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 1052 | inode->i_uid = current_fsuid(); |
| 1053 | inode->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 1055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return inode; |
| 1057 | |
| 1058 | fail_iput: |
| 1059 | iput(inode); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | fail_inode: |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
Ulrich Drepper | be61a86 | 2008-07-23 21:29:40 -0700 | [diff] [blame] | 1065 | struct file *create_write_pipe(int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | { |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1067 | int err; |
| 1068 | struct inode *inode; |
| 1069 | struct file *f; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 1070 | struct path path; |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 1071 | struct qstr name = { .name = "" }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1073 | err = -ENFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | inode = get_pipe_inode(); |
| 1075 | if (!inode) |
Dave Hansen | 430e285 | 2008-02-15 14:37:26 -0800 | [diff] [blame] | 1076 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1078 | err = -ENOMEM; |
Nick Piggin | 4b93688 | 2011-01-07 17:50:07 +1100 | [diff] [blame] | 1079 | path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name); |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 1080 | if (!path.dentry) |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1081 | goto err_inode; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 1082 | path.mnt = mntget(pipe_mnt); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 1083 | |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 1084 | d_instantiate(path.dentry, inode); |
Dave Hansen | 430e285 | 2008-02-15 14:37:26 -0800 | [diff] [blame] | 1085 | |
| 1086 | err = -ENFILE; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 1087 | f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops); |
Dave Hansen | 430e285 | 2008-02-15 14:37:26 -0800 | [diff] [blame] | 1088 | if (!f) |
| 1089 | goto err_dentry; |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1090 | f->f_mapping = inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 1092 | f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1093 | f->f_version = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1095 | return f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
Dave Hansen | 430e285 | 2008-02-15 14:37:26 -0800 | [diff] [blame] | 1097 | err_dentry: |
Al Viro | ed15243 | 2008-04-22 19:51:27 -0400 | [diff] [blame] | 1098 | free_pipe_info(inode); |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 1099 | path_put(&path); |
Al Viro | ed15243 | 2008-04-22 19:51:27 -0400 | [diff] [blame] | 1100 | return ERR_PTR(err); |
| 1101 | |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1102 | err_inode: |
| 1103 | free_pipe_info(inode); |
| 1104 | iput(inode); |
Dave Hansen | 430e285 | 2008-02-15 14:37:26 -0800 | [diff] [blame] | 1105 | err: |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1106 | return ERR_PTR(err); |
| 1107 | } |
| 1108 | |
| 1109 | void free_write_pipe(struct file *f) |
| 1110 | { |
Al Viro | 5ccac88 | 2006-12-18 13:31:18 +0000 | [diff] [blame] | 1111 | free_pipe_info(f->f_dentry->d_inode); |
Jan Blunck | c8e7f44 | 2008-06-09 16:40:35 -0700 | [diff] [blame] | 1112 | path_put(&f->f_path); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1113 | put_filp(f); |
| 1114 | } |
| 1115 | |
Ulrich Drepper | be61a86 | 2008-07-23 21:29:40 -0700 | [diff] [blame] | 1116 | struct file *create_read_pipe(struct file *wrf, int flags) |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1117 | { |
Al Viro | d231412 | 2009-08-09 01:01:37 +0400 | [diff] [blame] | 1118 | /* Grab pipe from the writer */ |
| 1119 | struct file *f = alloc_file(&wrf->f_path, FMODE_READ, |
| 1120 | &read_pipefifo_fops); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1121 | if (!f) |
| 1122 | return ERR_PTR(-ENFILE); |
| 1123 | |
Jan Blunck | c8e7f44 | 2008-06-09 16:40:35 -0700 | [diff] [blame] | 1124 | path_get(&wrf->f_path); |
Ulrich Drepper | be61a86 | 2008-07-23 21:29:40 -0700 | [diff] [blame] | 1125 | f->f_flags = O_RDONLY | (flags & O_NONBLOCK); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1126 | |
| 1127 | return f; |
| 1128 | } |
| 1129 | |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 1130 | int do_pipe_flags(int *fd, int flags) |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1131 | { |
| 1132 | struct file *fw, *fr; |
| 1133 | int error; |
| 1134 | int fdw, fdr; |
| 1135 | |
Linus Torvalds | 9883035 | 2012-04-29 13:12:42 -0700 | [diff] [blame] | 1136 | if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT)) |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 1137 | return -EINVAL; |
| 1138 | |
Ulrich Drepper | be61a86 | 2008-07-23 21:29:40 -0700 | [diff] [blame] | 1139 | fw = create_write_pipe(flags); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1140 | if (IS_ERR(fw)) |
| 1141 | return PTR_ERR(fw); |
Ulrich Drepper | be61a86 | 2008-07-23 21:29:40 -0700 | [diff] [blame] | 1142 | fr = create_read_pipe(fw, flags); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1143 | error = PTR_ERR(fr); |
| 1144 | if (IS_ERR(fr)) |
| 1145 | goto err_write_pipe; |
| 1146 | |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 1147 | error = get_unused_fd_flags(flags); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1148 | if (error < 0) |
| 1149 | goto err_read_pipe; |
| 1150 | fdr = error; |
| 1151 | |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 1152 | error = get_unused_fd_flags(flags); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1153 | if (error < 0) |
| 1154 | goto err_fdr; |
| 1155 | fdw = error; |
| 1156 | |
Al Viro | 157cf64 | 2008-12-14 04:57:47 -0500 | [diff] [blame] | 1157 | audit_fd_pair(fdr, fdw); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1158 | fd_install(fdr, fr); |
| 1159 | fd_install(fdw, fw); |
| 1160 | fd[0] = fdr; |
| 1161 | fd[1] = fdw; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 1162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | return 0; |
| 1164 | |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1165 | err_fdr: |
| 1166 | put_unused_fd(fdr); |
| 1167 | err_read_pipe: |
Jan Blunck | c8e7f44 | 2008-06-09 16:40:35 -0700 | [diff] [blame] | 1168 | path_put(&fr->f_path); |
Andi Kleen | d6cbd28 | 2006-09-30 23:29:26 -0700 | [diff] [blame] | 1169 | put_filp(fr); |
| 1170 | err_write_pipe: |
| 1171 | free_write_pipe(fw); |
| 1172 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 1176 | * sys_pipe() is the normal C calling standard for creating |
| 1177 | * a pipe. It's not the way Unix traditionally does this, though. |
| 1178 | */ |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 1179 | SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags) |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 1180 | { |
| 1181 | int fd[2]; |
| 1182 | int error; |
| 1183 | |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 1184 | error = do_pipe_flags(fd, flags); |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 1185 | if (!error) { |
Ulrich Drepper | ba719ba | 2008-05-06 20:42:38 -0700 | [diff] [blame] | 1186 | if (copy_to_user(fildes, fd, sizeof(fd))) { |
| 1187 | sys_close(fd[0]); |
| 1188 | sys_close(fd[1]); |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 1189 | error = -EFAULT; |
Ulrich Drepper | ba719ba | 2008-05-06 20:42:38 -0700 | [diff] [blame] | 1190 | } |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 1191 | } |
| 1192 | return error; |
| 1193 | } |
| 1194 | |
Heiko Carstens | 2b66421 | 2009-01-14 14:14:35 +0100 | [diff] [blame] | 1195 | SYSCALL_DEFINE1(pipe, int __user *, fildes) |
Ulrich Drepper | ed8cae8 | 2008-07-23 21:29:30 -0700 | [diff] [blame] | 1196 | { |
| 1197 | return sys_pipe2(fildes, 0); |
| 1198 | } |
| 1199 | |
Ulrich Drepper | d35c7b0 | 2008-05-03 15:10:37 -0400 | [diff] [blame] | 1200 | /* |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1201 | * Allocate a new array of pipe buffers and copy the info over. Returns the |
| 1202 | * pipe size if successful, or return -ERROR on error. |
| 1203 | */ |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1204 | 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] | 1205 | { |
| 1206 | struct pipe_buffer *bufs; |
| 1207 | |
| 1208 | /* |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1209 | * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't |
| 1210 | * expect a lot of shrink+grow operations, just free and allocate |
| 1211 | * again like we would do for growing. If the pipe currently |
| 1212 | * contains more buffers than arg, then return busy. |
| 1213 | */ |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1214 | if (nr_pages < pipe->nrbufs) |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1215 | return -EBUSY; |
| 1216 | |
Sasha Levin | 2ccd4f4 | 2012-01-12 17:17:40 -0800 | [diff] [blame] | 1217 | bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1218 | if (unlikely(!bufs)) |
| 1219 | return -ENOMEM; |
| 1220 | |
| 1221 | /* |
| 1222 | * The pipe array wraps around, so just start the new one at zero |
| 1223 | * and adjust the indexes. |
| 1224 | */ |
| 1225 | if (pipe->nrbufs) { |
Miklos Szeredi | 1d862f4 | 2010-06-08 16:28:45 +0200 | [diff] [blame] | 1226 | unsigned int tail; |
| 1227 | unsigned int head; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1228 | |
Miklos Szeredi | 1d862f4 | 2010-06-08 16:28:45 +0200 | [diff] [blame] | 1229 | tail = pipe->curbuf + pipe->nrbufs; |
| 1230 | if (tail < pipe->buffers) |
| 1231 | tail = 0; |
| 1232 | else |
| 1233 | tail &= (pipe->buffers - 1); |
| 1234 | |
| 1235 | head = pipe->nrbufs - tail; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1236 | if (head) |
| 1237 | memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer)); |
| 1238 | if (tail) |
Miklos Szeredi | 1d862f4 | 2010-06-08 16:28:45 +0200 | [diff] [blame] | 1239 | memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer)); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 1242 | account_pipe_buffers(pipe, pipe->buffers, nr_pages); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1243 | pipe->curbuf = 0; |
| 1244 | kfree(pipe->bufs); |
| 1245 | pipe->bufs = bufs; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1246 | pipe->buffers = nr_pages; |
| 1247 | return nr_pages * PAGE_SIZE; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1250 | /* |
| 1251 | * Currently we rely on the pipe array holding a power-of-2 number |
| 1252 | * of pages. |
| 1253 | */ |
| 1254 | static inline unsigned int round_pipe_size(unsigned int size) |
| 1255 | { |
| 1256 | unsigned long nr_pages; |
| 1257 | |
| 1258 | nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1259 | return roundup_pow_of_two(nr_pages) << PAGE_SHIFT; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax |
| 1264 | * will return an error. |
| 1265 | */ |
| 1266 | int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf, |
| 1267 | size_t *lenp, loff_t *ppos) |
| 1268 | { |
| 1269 | int ret; |
| 1270 | |
| 1271 | ret = proc_dointvec_minmax(table, write, buf, lenp, ppos); |
| 1272 | if (ret < 0 || !write) |
| 1273 | return ret; |
| 1274 | |
| 1275 | pipe_max_size = round_pipe_size(pipe_max_size); |
| 1276 | return ret; |
| 1277 | } |
| 1278 | |
Linus Torvalds | 7208364 | 2010-11-28 16:27:19 -0800 | [diff] [blame] | 1279 | /* |
| 1280 | * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same |
| 1281 | * location, so checking ->i_pipe is not enough to verify that this is a |
| 1282 | * pipe. |
| 1283 | */ |
| 1284 | struct pipe_inode_info *get_pipe_info(struct file *file) |
| 1285 | { |
| 1286 | struct inode *i = file->f_path.dentry->d_inode; |
| 1287 | |
| 1288 | return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL; |
| 1289 | } |
| 1290 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1291 | long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1292 | { |
| 1293 | struct pipe_inode_info *pipe; |
| 1294 | long ret; |
| 1295 | |
Linus Torvalds | c66fb34 | 2010-11-28 14:09:57 -0800 | [diff] [blame] | 1296 | pipe = get_pipe_info(file); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1297 | if (!pipe) |
| 1298 | return -EBADF; |
| 1299 | |
| 1300 | mutex_lock(&pipe->inode->i_mutex); |
| 1301 | |
| 1302 | switch (cmd) { |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1303 | case F_SETPIPE_SZ: { |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1304 | unsigned int size, nr_pages; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1305 | |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1306 | size = round_pipe_size(arg); |
| 1307 | nr_pages = size >> PAGE_SHIFT; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1308 | |
Miklos Szeredi | 6db40cf | 2010-06-09 09:27:57 +0200 | [diff] [blame] | 1309 | ret = -EINVAL; |
| 1310 | if (!nr_pages) |
| 1311 | goto out; |
| 1312 | |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1313 | if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) { |
Jens Axboe | b4ca761 | 2010-06-01 12:42:12 +0200 | [diff] [blame] | 1314 | ret = -EPERM; |
Julia Lawall | cc967be | 2010-05-26 17:54:39 +0200 | [diff] [blame] | 1315 | goto out; |
Willy Tarreau | 239bc43 | 2016-01-18 16:36:09 +0100 | [diff] [blame] | 1316 | } else if ((too_many_pipe_buffers_hard(pipe->user) || |
| 1317 | too_many_pipe_buffers_soft(pipe->user)) && |
| 1318 | !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 1319 | ret = -EPERM; |
| 1320 | goto out; |
Julia Lawall | cc967be | 2010-05-26 17:54:39 +0200 | [diff] [blame] | 1321 | } |
Jens Axboe | ff9da69 | 2010-06-03 14:54:39 +0200 | [diff] [blame] | 1322 | ret = pipe_set_size(pipe, nr_pages); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1323 | break; |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1324 | } |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1325 | case F_GETPIPE_SZ: |
Jens Axboe | b9598db | 2010-05-24 19:34:43 +0200 | [diff] [blame] | 1326 | ret = pipe->buffers * PAGE_SIZE; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1327 | break; |
| 1328 | default: |
| 1329 | ret = -EINVAL; |
| 1330 | break; |
| 1331 | } |
| 1332 | |
Julia Lawall | cc967be | 2010-05-26 17:54:39 +0200 | [diff] [blame] | 1333 | out: |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1334 | mutex_unlock(&pipe->inode->i_mutex); |
| 1335 | return ret; |
| 1336 | } |
| 1337 | |
Nick Piggin | ff0c7d1 | 2011-01-07 17:49:50 +1100 | [diff] [blame] | 1338 | static const struct super_operations pipefs_ops = { |
| 1339 | .destroy_inode = free_inode_nonrcu, |
Pavel Emelyanov | d70ef97 | 2011-10-31 17:10:04 -0700 | [diff] [blame] | 1340 | .statfs = simple_statfs, |
Nick Piggin | ff0c7d1 | 2011-01-07 17:49:50 +1100 | [diff] [blame] | 1341 | }; |
| 1342 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1343 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | * pipefs should _never_ be mounted by userland - too much of security hassle, |
| 1345 | * no real gain from having the whole whorehouse mounted. So we don't need |
| 1346 | * any operations on the root directory. However, we need a non-trivial |
| 1347 | * d_name - pipe: will go nicely and kill the special-casing in procfs. |
| 1348 | */ |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1349 | static struct dentry *pipefs_mount(struct file_system_type *fs_type, |
| 1350 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | { |
Al Viro | c74a1cb | 2011-01-12 16:59:34 -0500 | [diff] [blame] | 1352 | return mount_pseudo(fs_type, "pipe:", &pipefs_ops, |
| 1353 | &pipefs_dentry_operations, PIPEFS_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | static struct file_system_type pipe_fs_type = { |
| 1357 | .name = "pipefs", |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1358 | .mount = pipefs_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | .kill_sb = kill_anon_super, |
| 1360 | }; |
| 1361 | |
| 1362 | static int __init init_pipe_fs(void) |
| 1363 | { |
| 1364 | int err = register_filesystem(&pipe_fs_type); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | if (!err) { |
| 1367 | pipe_mnt = kern_mount(&pipe_fs_type); |
| 1368 | if (IS_ERR(pipe_mnt)) { |
| 1369 | err = PTR_ERR(pipe_mnt); |
| 1370 | unregister_filesystem(&pipe_fs_type); |
| 1371 | } |
| 1372 | } |
| 1373 | return err; |
| 1374 | } |
| 1375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | fs_initcall(init_pipe_fs); |