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