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