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