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