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