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