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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len) |
| 59 | { |
| 60 | unsigned long copy; |
| 61 | |
| 62 | while (len > 0) { |
| 63 | while (!iov->iov_len) |
| 64 | iov++; |
| 65 | copy = min_t(unsigned long, len, iov->iov_len); |
| 66 | |
| 67 | if (copy_from_user(to, iov->iov_base, copy)) |
| 68 | return -EFAULT; |
| 69 | to += copy; |
| 70 | len -= copy; |
| 71 | iov->iov_base += copy; |
| 72 | iov->iov_len -= copy; |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 77 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len) |
| 79 | { |
| 80 | unsigned long copy; |
| 81 | |
| 82 | while (len > 0) { |
| 83 | while (!iov->iov_len) |
| 84 | iov++; |
| 85 | copy = min_t(unsigned long, len, iov->iov_len); |
| 86 | |
| 87 | if (copy_to_user(iov->iov_base, from, copy)) |
| 88 | return -EFAULT; |
| 89 | from += copy; |
| 90 | len -= copy; |
| 91 | iov->iov_base += copy; |
| 92 | iov->iov_len -= copy; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 97 | static void anon_pipe_buf_release(struct pipe_inode_info *pipe, |
| 98 | struct pipe_buffer *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
| 100 | struct page *page = buf->page; |
| 101 | |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 102 | buf->flags &= ~PIPE_BUF_FLAG_STOLEN; |
| 103 | |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 104 | /* |
| 105 | * If nobody else uses this page, and we don't already have a |
| 106 | * temporary page, let's keep track of it as a one-deep |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 107 | * allocation cache. (Otherwise just release our reference to it) |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 108 | */ |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 109 | if (page_count(page) == 1 && !pipe->tmp_page) |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 110 | pipe->tmp_page = page; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 111 | else |
| 112 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 115 | static void * anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe, |
| 116 | struct pipe_buffer *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | return kmap(buf->page); |
| 119 | } |
| 120 | |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 121 | static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe, |
| 122 | struct pipe_buffer *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | kunmap(buf->page); |
| 125 | } |
| 126 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 127 | static int anon_pipe_buf_steal(struct pipe_inode_info *pipe, |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 128 | struct pipe_buffer *buf) |
| 129 | { |
Jens Axboe | 3e7ee3e | 2006-04-02 23:11:04 +0200 | [diff] [blame] | 130 | buf->flags |= PIPE_BUF_FLAG_STOLEN; |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 134 | static void anon_pipe_buf_get(struct pipe_inode_info *info, |
| 135 | struct pipe_buffer *buf) |
| 136 | { |
| 137 | page_cache_get(buf->page); |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | static struct pipe_buf_operations anon_pipe_buf_ops = { |
| 141 | .can_merge = 1, |
| 142 | .map = anon_pipe_buf_map, |
| 143 | .unmap = anon_pipe_buf_unmap, |
| 144 | .release = anon_pipe_buf_release, |
Jens Axboe | 5abc97a | 2006-03-30 15:16:46 +0200 | [diff] [blame] | 145 | .steal = anon_pipe_buf_steal, |
Jens Axboe | 7052449 | 2006-04-11 15:51:17 +0200 | [diff] [blame] | 146 | .get = anon_pipe_buf_get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static ssize_t |
| 150 | pipe_readv(struct file *filp, const struct iovec *_iov, |
| 151 | unsigned long nr_segs, loff_t *ppos) |
| 152 | { |
| 153 | struct inode *inode = filp->f_dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 154 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | int do_wakeup; |
| 156 | ssize_t ret; |
| 157 | struct iovec *iov = (struct iovec *)_iov; |
| 158 | size_t total_len; |
| 159 | |
| 160 | total_len = iov_length(iov, nr_segs); |
| 161 | /* Null read succeeds. */ |
| 162 | if (unlikely(total_len == 0)) |
| 163 | return 0; |
| 164 | |
| 165 | do_wakeup = 0; |
| 166 | ret = 0; |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 167 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 168 | pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | for (;;) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 170 | int bufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | if (bufs) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 172 | int curbuf = pipe->curbuf; |
| 173 | struct pipe_buffer *buf = pipe->bufs + curbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | struct pipe_buf_operations *ops = buf->ops; |
| 175 | void *addr; |
| 176 | size_t chars = buf->len; |
| 177 | int error; |
| 178 | |
| 179 | if (chars > total_len) |
| 180 | chars = total_len; |
| 181 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 182 | addr = ops->map(filp, pipe, buf); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 183 | if (IS_ERR(addr)) { |
| 184 | if (!ret) |
| 185 | ret = PTR_ERR(addr); |
| 186 | break; |
| 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 189 | ops->unmap(pipe, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (unlikely(error)) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 191 | if (!ret) |
| 192 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | ret += chars; |
| 196 | buf->offset += chars; |
| 197 | buf->len -= chars; |
| 198 | if (!buf->len) { |
| 199 | buf->ops = NULL; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 200 | ops->release(pipe, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | curbuf = (curbuf + 1) & (PIPE_BUFFERS-1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 202 | pipe->curbuf = curbuf; |
| 203 | pipe->nrbufs = --bufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | do_wakeup = 1; |
| 205 | } |
| 206 | total_len -= chars; |
| 207 | if (!total_len) |
| 208 | break; /* common path: read succeeded */ |
| 209 | } |
| 210 | if (bufs) /* More to do? */ |
| 211 | continue; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 212 | if (!pipe->writers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | break; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 214 | if (!pipe->waiting_writers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | /* syscall merging: Usually we must not sleep |
| 216 | * if O_NONBLOCK is set, or if we got some data. |
| 217 | * But if a writer sleeps in kernel space, then |
| 218 | * we can wait for that data without violating POSIX. |
| 219 | */ |
| 220 | if (ret) |
| 221 | break; |
| 222 | if (filp->f_flags & O_NONBLOCK) { |
| 223 | ret = -EAGAIN; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if (signal_pending(current)) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 228 | if (!ret) |
| 229 | ret = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | if (do_wakeup) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 233 | wake_up_interruptible_sync(&pipe->wait); |
| 234 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 236 | pipe_wait(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 238 | mutex_unlock(&inode->i_mutex); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 239 | |
| 240 | /* Signal writers asynchronously that there is more room. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (do_wakeup) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 242 | wake_up_interruptible(&pipe->wait); |
| 243 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | if (ret > 0) |
| 246 | file_accessed(filp); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static ssize_t |
| 251 | pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) |
| 252 | { |
| 253 | struct iovec iov = { .iov_base = buf, .iov_len = count }; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return pipe_readv(filp, &iov, 1, ppos); |
| 256 | } |
| 257 | |
| 258 | static ssize_t |
| 259 | pipe_writev(struct file *filp, const struct iovec *_iov, |
| 260 | unsigned long nr_segs, loff_t *ppos) |
| 261 | { |
| 262 | struct inode *inode = filp->f_dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 263 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | ssize_t ret; |
| 265 | int do_wakeup; |
| 266 | struct iovec *iov = (struct iovec *)_iov; |
| 267 | size_t total_len; |
| 268 | ssize_t chars; |
| 269 | |
| 270 | total_len = iov_length(iov, nr_segs); |
| 271 | /* Null write succeeds. */ |
| 272 | if (unlikely(total_len == 0)) |
| 273 | return 0; |
| 274 | |
| 275 | do_wakeup = 0; |
| 276 | ret = 0; |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 277 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 278 | pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 280 | if (!pipe->readers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | send_sig(SIGPIPE, current, 0); |
| 282 | ret = -EPIPE; |
| 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | /* We try to merge small writes */ |
| 287 | chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 288 | if (pipe->nrbufs && chars != 0) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 289 | int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) & |
| 290 | (PIPE_BUFFERS-1); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 291 | struct pipe_buffer *buf = pipe->bufs + lastbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | struct pipe_buf_operations *ops = buf->ops; |
| 293 | int offset = buf->offset + buf->len; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (ops->can_merge && offset + chars <= PAGE_SIZE) { |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 296 | void *addr; |
| 297 | int error; |
| 298 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 299 | addr = ops->map(filp, pipe, buf); |
Jens Axboe | 5274f05 | 2006-03-30 15:15:30 +0200 | [diff] [blame] | 300 | if (IS_ERR(addr)) { |
| 301 | error = PTR_ERR(addr); |
| 302 | goto out; |
| 303 | } |
| 304 | error = pipe_iov_copy_from_user(offset + addr, iov, |
| 305 | chars); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 306 | ops->unmap(pipe, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | ret = error; |
| 308 | do_wakeup = 1; |
| 309 | if (error) |
| 310 | goto out; |
| 311 | buf->len += chars; |
| 312 | total_len -= chars; |
| 313 | ret = chars; |
| 314 | if (!total_len) |
| 315 | goto out; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | for (;;) { |
| 320 | int bufs; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 321 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 322 | if (!pipe->readers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | send_sig(SIGPIPE, current, 0); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 324 | if (!ret) |
| 325 | ret = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | break; |
| 327 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 328 | bufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (bufs < PIPE_BUFFERS) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 330 | int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1); |
| 331 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
| 332 | struct page *page = pipe->tmp_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | int error; |
| 334 | |
| 335 | if (!page) { |
| 336 | page = alloc_page(GFP_HIGHUSER); |
| 337 | if (unlikely(!page)) { |
| 338 | ret = ret ? : -ENOMEM; |
| 339 | break; |
| 340 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 341 | pipe->tmp_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 343 | /* Always wake up, even if the copy fails. Otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | * we lock up (O_NONBLOCK-)readers that sleep due to |
| 345 | * syscall merging. |
| 346 | * FIXME! Is this really true? |
| 347 | */ |
| 348 | do_wakeup = 1; |
| 349 | chars = PAGE_SIZE; |
| 350 | if (chars > total_len) |
| 351 | chars = total_len; |
| 352 | |
| 353 | error = pipe_iov_copy_from_user(kmap(page), iov, chars); |
| 354 | kunmap(page); |
| 355 | if (unlikely(error)) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 356 | if (!ret) |
| 357 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | ret += chars; |
| 361 | |
| 362 | /* Insert it into the buffer array */ |
| 363 | buf->page = page; |
| 364 | buf->ops = &anon_pipe_buf_ops; |
| 365 | buf->offset = 0; |
| 366 | buf->len = chars; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 367 | pipe->nrbufs = ++bufs; |
| 368 | pipe->tmp_page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | total_len -= chars; |
| 371 | if (!total_len) |
| 372 | break; |
| 373 | } |
| 374 | if (bufs < PIPE_BUFFERS) |
| 375 | continue; |
| 376 | if (filp->f_flags & O_NONBLOCK) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 377 | if (!ret) |
| 378 | ret = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | if (signal_pending(current)) { |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 382 | if (!ret) |
| 383 | ret = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | if (do_wakeup) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 387 | wake_up_interruptible_sync(&pipe->wait); |
| 388 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | do_wakeup = 0; |
| 390 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 391 | pipe->waiting_writers++; |
| 392 | pipe_wait(pipe); |
| 393 | pipe->waiting_writers--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | out: |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 396 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (do_wakeup) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 398 | wake_up_interruptible(&pipe->wait); |
| 399 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | if (ret > 0) |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 402 | file_update_time(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static ssize_t |
| 407 | pipe_write(struct file *filp, const char __user *buf, |
| 408 | size_t count, loff_t *ppos) |
| 409 | { |
| 410 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | return pipe_writev(filp, &iov, 1, ppos); |
| 413 | } |
| 414 | |
| 415 | static ssize_t |
| 416 | bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos) |
| 417 | { |
| 418 | return -EBADF; |
| 419 | } |
| 420 | |
| 421 | static ssize_t |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 422 | bad_pipe_w(struct file *filp, const char __user *buf, size_t count, |
| 423 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
| 425 | return -EBADF; |
| 426 | } |
| 427 | |
| 428 | static int |
| 429 | pipe_ioctl(struct inode *pino, struct file *filp, |
| 430 | unsigned int cmd, unsigned long arg) |
| 431 | { |
| 432 | struct inode *inode = filp->f_dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 433 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | int count, buf, nrbufs; |
| 435 | |
| 436 | switch (cmd) { |
| 437 | case FIONREAD: |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 438 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 439 | pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | count = 0; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 441 | buf = pipe->curbuf; |
| 442 | nrbufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | while (--nrbufs >= 0) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 444 | count += pipe->bufs[buf].len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | buf = (buf+1) & (PIPE_BUFFERS-1); |
| 446 | } |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 447 | mutex_unlock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return put_user(count, (int __user *)arg); |
| 450 | default: |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* No kernel lock held - fine */ |
| 456 | static unsigned int |
| 457 | pipe_poll(struct file *filp, poll_table *wait) |
| 458 | { |
| 459 | unsigned int mask; |
| 460 | struct inode *inode = filp->f_dentry->d_inode; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 461 | struct pipe_inode_info *pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | int nrbufs; |
| 463 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 464 | poll_wait(filp, &pipe->wait, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | /* Reading only -- no need for acquiring the semaphore. */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 467 | nrbufs = pipe->nrbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | mask = 0; |
| 469 | if (filp->f_mode & FMODE_READ) { |
| 470 | mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 471 | if (!pipe->writers && filp->f_version != pipe->w_counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | mask |= POLLHUP; |
| 473 | } |
| 474 | |
| 475 | if (filp->f_mode & FMODE_WRITE) { |
| 476 | mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0; |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 477 | /* |
| 478 | * Most Unices do not set POLLERR for FIFOs but on Linux they |
| 479 | * behave exactly like pipes for poll(). |
| 480 | */ |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 481 | if (!pipe->readers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | mask |= POLLERR; |
| 483 | } |
| 484 | |
| 485 | return mask; |
| 486 | } |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | static int |
| 489 | pipe_release(struct inode *inode, int decr, int decw) |
| 490 | { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 491 | struct pipe_inode_info *pipe; |
| 492 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 493 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 494 | pipe = inode->i_pipe; |
| 495 | pipe->readers -= decr; |
| 496 | pipe->writers -= decw; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 497 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 498 | if (!pipe->readers && !pipe->writers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | free_pipe_info(inode); |
| 500 | } else { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 501 | wake_up_interruptible(&pipe->wait); |
| 502 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
| 503 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 505 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int |
| 511 | pipe_read_fasync(int fd, struct file *filp, int on) |
| 512 | { |
| 513 | struct inode *inode = filp->f_dentry->d_inode; |
| 514 | int retval; |
| 515 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 516 | mutex_lock(&inode->i_mutex); |
| 517 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers); |
| 518 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | if (retval < 0) |
| 521 | return retval; |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | static int |
| 528 | pipe_write_fasync(int fd, struct file *filp, int on) |
| 529 | { |
| 530 | struct inode *inode = filp->f_dentry->d_inode; |
| 531 | int retval; |
| 532 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 533 | mutex_lock(&inode->i_mutex); |
| 534 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers); |
| 535 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | if (retval < 0) |
| 538 | return retval; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | static int |
| 545 | pipe_rdwr_fasync(int fd, struct file *filp, int on) |
| 546 | { |
| 547 | struct inode *inode = filp->f_dentry->d_inode; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 548 | struct pipe_inode_info *pipe = inode->i_pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | int retval; |
| 550 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 551 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 553 | retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | if (retval >= 0) |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 556 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 558 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | if (retval < 0) |
| 561 | return retval; |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | static int |
| 568 | pipe_read_release(struct inode *inode, struct file *filp) |
| 569 | { |
| 570 | pipe_read_fasync(-1, filp, 0); |
| 571 | return pipe_release(inode, 1, 0); |
| 572 | } |
| 573 | |
| 574 | static int |
| 575 | pipe_write_release(struct inode *inode, struct file *filp) |
| 576 | { |
| 577 | pipe_write_fasync(-1, filp, 0); |
| 578 | return pipe_release(inode, 0, 1); |
| 579 | } |
| 580 | |
| 581 | static int |
| 582 | pipe_rdwr_release(struct inode *inode, struct file *filp) |
| 583 | { |
| 584 | int decr, decw; |
| 585 | |
| 586 | pipe_rdwr_fasync(-1, filp, 0); |
| 587 | decr = (filp->f_mode & FMODE_READ) != 0; |
| 588 | decw = (filp->f_mode & FMODE_WRITE) != 0; |
| 589 | return pipe_release(inode, decr, decw); |
| 590 | } |
| 591 | |
| 592 | static int |
| 593 | pipe_read_open(struct inode *inode, struct file *filp) |
| 594 | { |
| 595 | /* We could have perhaps used atomic_t, but this and friends |
| 596 | below are the only places. So it doesn't seem worthwhile. */ |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 597 | mutex_lock(&inode->i_mutex); |
| 598 | inode->i_pipe->readers++; |
| 599 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static int |
| 605 | pipe_write_open(struct inode *inode, struct file *filp) |
| 606 | { |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 607 | mutex_lock(&inode->i_mutex); |
| 608 | inode->i_pipe->writers++; |
| 609 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static int |
| 615 | pipe_rdwr_open(struct inode *inode, struct file *filp) |
| 616 | { |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 617 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | if (filp->f_mode & FMODE_READ) |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 619 | inode->i_pipe->readers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | if (filp->f_mode & FMODE_WRITE) |
Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 621 | inode->i_pipe->writers++; |
| 622 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * The file_operations structs are not static because they |
| 629 | * are also used in linux/fs/fifo.c to do operations on FIFOs. |
| 630 | */ |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 631 | const struct file_operations read_fifo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | .llseek = no_llseek, |
| 633 | .read = pipe_read, |
| 634 | .readv = pipe_readv, |
| 635 | .write = bad_pipe_w, |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 636 | .poll = pipe_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | .ioctl = pipe_ioctl, |
| 638 | .open = pipe_read_open, |
| 639 | .release = pipe_read_release, |
| 640 | .fasync = pipe_read_fasync, |
| 641 | }; |
| 642 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 643 | const struct file_operations write_fifo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | .llseek = no_llseek, |
| 645 | .read = bad_pipe_r, |
| 646 | .write = pipe_write, |
| 647 | .writev = pipe_writev, |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 648 | .poll = pipe_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | .ioctl = pipe_ioctl, |
| 650 | .open = pipe_write_open, |
| 651 | .release = pipe_write_release, |
| 652 | .fasync = pipe_write_fasync, |
| 653 | }; |
| 654 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 655 | const struct file_operations rdwr_fifo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | .llseek = no_llseek, |
| 657 | .read = pipe_read, |
| 658 | .readv = pipe_readv, |
| 659 | .write = pipe_write, |
| 660 | .writev = pipe_writev, |
Pekka Enberg | 5e5d7a2 | 2005-09-06 15:17:48 -0700 | [diff] [blame] | 661 | .poll = pipe_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | .ioctl = pipe_ioctl, |
| 663 | .open = pipe_rdwr_open, |
| 664 | .release = pipe_rdwr_release, |
| 665 | .fasync = pipe_rdwr_fasync, |
| 666 | }; |
| 667 | |
Linus Torvalds | a19cbd4 | 2006-03-08 14:03:09 -0800 | [diff] [blame] | 668 | static struct file_operations read_pipe_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | .llseek = no_llseek, |
| 670 | .read = pipe_read, |
| 671 | .readv = pipe_readv, |
| 672 | .write = bad_pipe_w, |
| 673 | .poll = pipe_poll, |
| 674 | .ioctl = pipe_ioctl, |
| 675 | .open = pipe_read_open, |
| 676 | .release = pipe_read_release, |
| 677 | .fasync = pipe_read_fasync, |
| 678 | }; |
| 679 | |
Linus Torvalds | a19cbd4 | 2006-03-08 14:03:09 -0800 | [diff] [blame] | 680 | static struct file_operations write_pipe_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | .llseek = no_llseek, |
| 682 | .read = bad_pipe_r, |
| 683 | .write = pipe_write, |
| 684 | .writev = pipe_writev, |
| 685 | .poll = pipe_poll, |
| 686 | .ioctl = pipe_ioctl, |
| 687 | .open = pipe_write_open, |
| 688 | .release = pipe_write_release, |
| 689 | .fasync = pipe_write_fasync, |
| 690 | }; |
| 691 | |
Linus Torvalds | a19cbd4 | 2006-03-08 14:03:09 -0800 | [diff] [blame] | 692 | static struct file_operations rdwr_pipe_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | .llseek = no_llseek, |
| 694 | .read = pipe_read, |
| 695 | .readv = pipe_readv, |
| 696 | .write = pipe_write, |
| 697 | .writev = pipe_writev, |
| 698 | .poll = pipe_poll, |
| 699 | .ioctl = pipe_ioctl, |
| 700 | .open = pipe_rdwr_open, |
| 701 | .release = pipe_rdwr_release, |
| 702 | .fasync = pipe_rdwr_fasync, |
| 703 | }; |
| 704 | |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 705 | struct pipe_inode_info * alloc_pipe_info(struct inode *inode) |
| 706 | { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 707 | struct pipe_inode_info *pipe; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 708 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 709 | pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL); |
| 710 | if (pipe) { |
| 711 | init_waitqueue_head(&pipe->wait); |
| 712 | pipe->r_counter = pipe->w_counter = 1; |
| 713 | pipe->inode = inode; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 716 | return pipe; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 719 | void __free_pipe_info(struct pipe_inode_info *pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
| 721 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | for (i = 0; i < PIPE_BUFFERS; i++) { |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 724 | struct pipe_buffer *buf = pipe->bufs + i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | if (buf->ops) |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 726 | buf->ops->release(pipe, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 728 | if (pipe->tmp_page) |
| 729 | __free_page(pipe->tmp_page); |
| 730 | kfree(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 733 | void free_pipe_info(struct inode *inode) |
| 734 | { |
| 735 | __free_pipe_info(inode->i_pipe); |
| 736 | inode->i_pipe = NULL; |
| 737 | } |
| 738 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 739 | static struct vfsmount *pipe_mnt __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | static int pipefs_delete_dentry(struct dentry *dentry) |
| 741 | { |
| 742 | return 1; |
| 743 | } |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | static struct dentry_operations pipefs_dentry_operations = { |
| 746 | .d_delete = pipefs_delete_dentry, |
| 747 | }; |
| 748 | |
| 749 | static struct inode * get_pipe_inode(void) |
| 750 | { |
| 751 | struct inode *inode = new_inode(pipe_mnt->mnt_sb); |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 752 | struct pipe_inode_info *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | if (!inode) |
| 755 | goto fail_inode; |
| 756 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 757 | pipe = alloc_pipe_info(inode); |
| 758 | if (!pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | goto fail_iput; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 760 | inode->i_pipe = pipe; |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 761 | |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 762 | pipe->readers = pipe->writers = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | inode->i_fop = &rdwr_pipe_fops; |
| 764 | |
| 765 | /* |
| 766 | * Mark the inode dirty from the very beginning, |
| 767 | * that way it will never be moved to the dirty |
| 768 | * list because "mark_inode_dirty()" will think |
| 769 | * that it already _is_ on the dirty list. |
| 770 | */ |
| 771 | inode->i_state = I_DIRTY; |
| 772 | inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR; |
| 773 | inode->i_uid = current->fsuid; |
| 774 | inode->i_gid = current->fsgid; |
| 775 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 776 | inode->i_blksize = PAGE_SIZE; |
Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return inode; |
| 779 | |
| 780 | fail_iput: |
| 781 | iput(inode); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | fail_inode: |
| 784 | return NULL; |
| 785 | } |
| 786 | |
| 787 | int do_pipe(int *fd) |
| 788 | { |
| 789 | struct qstr this; |
| 790 | char name[32]; |
| 791 | struct dentry *dentry; |
| 792 | struct inode * inode; |
| 793 | struct file *f1, *f2; |
| 794 | int error; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 795 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | error = -ENFILE; |
| 798 | f1 = get_empty_filp(); |
| 799 | if (!f1) |
| 800 | goto no_files; |
| 801 | |
| 802 | f2 = get_empty_filp(); |
| 803 | if (!f2) |
| 804 | goto close_f1; |
| 805 | |
| 806 | inode = get_pipe_inode(); |
| 807 | if (!inode) |
| 808 | goto close_f12; |
| 809 | |
| 810 | error = get_unused_fd(); |
| 811 | if (error < 0) |
| 812 | goto close_f12_inode; |
| 813 | i = error; |
| 814 | |
| 815 | error = get_unused_fd(); |
| 816 | if (error < 0) |
| 817 | goto close_f12_inode_i; |
| 818 | j = error; |
| 819 | |
| 820 | error = -ENOMEM; |
| 821 | sprintf(name, "[%lu]", inode->i_ino); |
| 822 | this.name = name; |
| 823 | this.len = strlen(name); |
| 824 | this.hash = inode->i_ino; /* will go */ |
| 825 | dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this); |
| 826 | if (!dentry) |
| 827 | goto close_f12_inode_i_j; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | dentry->d_op = &pipefs_dentry_operations; |
| 830 | d_add(dentry, inode); |
| 831 | f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt)); |
| 832 | f1->f_dentry = f2->f_dentry = dget(dentry); |
| 833 | f1->f_mapping = f2->f_mapping = inode->i_mapping; |
| 834 | |
| 835 | /* read file */ |
| 836 | f1->f_pos = f2->f_pos = 0; |
| 837 | f1->f_flags = O_RDONLY; |
| 838 | f1->f_op = &read_pipe_fops; |
| 839 | f1->f_mode = FMODE_READ; |
| 840 | f1->f_version = 0; |
| 841 | |
| 842 | /* write file */ |
| 843 | f2->f_flags = O_WRONLY; |
| 844 | f2->f_op = &write_pipe_fops; |
| 845 | f2->f_mode = FMODE_WRITE; |
| 846 | f2->f_version = 0; |
| 847 | |
| 848 | fd_install(i, f1); |
| 849 | fd_install(j, f2); |
| 850 | fd[0] = i; |
| 851 | fd[1] = j; |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | return 0; |
| 854 | |
| 855 | close_f12_inode_i_j: |
| 856 | put_unused_fd(j); |
| 857 | close_f12_inode_i: |
| 858 | put_unused_fd(i); |
| 859 | close_f12_inode: |
| 860 | free_pipe_info(inode); |
| 861 | iput(inode); |
| 862 | close_f12: |
| 863 | put_filp(f2); |
| 864 | close_f1: |
| 865 | put_filp(f1); |
| 866 | no_files: |
| 867 | return error; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * pipefs should _never_ be mounted by userland - too much of security hassle, |
| 872 | * no real gain from having the whole whorehouse mounted. So we don't need |
| 873 | * any operations on the root directory. However, we need a non-trivial |
| 874 | * d_name - pipe: will go nicely and kill the special-casing in procfs. |
| 875 | */ |
| 876 | |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 877 | static struct super_block * |
| 878 | pipefs_get_sb(struct file_system_type *fs_type, int flags, |
| 879 | const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | { |
| 881 | return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC); |
| 882 | } |
| 883 | |
| 884 | static struct file_system_type pipe_fs_type = { |
| 885 | .name = "pipefs", |
| 886 | .get_sb = pipefs_get_sb, |
| 887 | .kill_sb = kill_anon_super, |
| 888 | }; |
| 889 | |
| 890 | static int __init init_pipe_fs(void) |
| 891 | { |
| 892 | int err = register_filesystem(&pipe_fs_type); |
Ingo Molnar | 341b446 | 2006-04-11 13:57:45 +0200 | [diff] [blame] | 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | if (!err) { |
| 895 | pipe_mnt = kern_mount(&pipe_fs_type); |
| 896 | if (IS_ERR(pipe_mnt)) { |
| 897 | err = PTR_ERR(pipe_mnt); |
| 898 | unregister_filesystem(&pipe_fs_type); |
| 899 | } |
| 900 | } |
| 901 | return err; |
| 902 | } |
| 903 | |
| 904 | static void __exit exit_pipe_fs(void) |
| 905 | { |
| 906 | unregister_filesystem(&pipe_fs_type); |
| 907 | mntput(pipe_mnt); |
| 908 | } |
| 909 | |
| 910 | fs_initcall(init_pipe_fs); |
| 911 | module_exit(exit_pipe_fs); |