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