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