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