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