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