[PATCH] pipe.c/fifo.c code cleanups
[safe/jmp/linux-2.6] / fs / pipe.c
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>
18 #include <linux/pagemap.h>
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 */
39 void pipe_wait(struct pipe_inode_info *pipe)
40 {
41         DEFINE_WAIT(wait);
42
43         /*
44          * Pipes are system-local resources, so sleeping on them
45          * is considered a noninteractive wait:
46          */
47         prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE|TASK_NONINTERACTIVE);
48         if (pipe->inode)
49                 mutex_unlock(&pipe->inode->i_mutex);
50         schedule();
51         finish_wait(&pipe->wait, &wait);
52         if (pipe->inode)
53                 mutex_lock(&pipe->inode->i_mutex);
54 }
55
56 static int
57 pipe_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
76 static int
77 pipe_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
96 static void anon_pipe_buf_release(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
97 {
98         struct page *page = buf->page;
99
100         buf->flags &= ~PIPE_BUF_FLAG_STOLEN;
101
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          */
107         if (page_count(page) == 1 && !pipe->tmp_page) {
108                 pipe->tmp_page = page;
109                 return;
110         }
111
112         /*
113          * Otherwise just release our reference to it
114          */
115         page_cache_release(page);
116 }
117
118 static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe, struct pipe_buffer *buf)
119 {
120         return kmap(buf->page);
121 }
122
123 static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
124 {
125         kunmap(buf->page);
126 }
127
128 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
129                                struct pipe_buffer *buf)
130 {
131         buf->flags |= PIPE_BUF_FLAG_STOLEN;
132         return 0;
133 }
134
135 static 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,
140         .steal = anon_pipe_buf_steal,
141 };
142
143 static ssize_t
144 pipe_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;
148         struct pipe_inode_info *pipe;
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;
161         mutex_lock(&inode->i_mutex);
162         pipe = inode->i_pipe;
163         for (;;) {
164                 int bufs = pipe->nrbufs;
165                 if (bufs) {
166                         int curbuf = pipe->curbuf;
167                         struct pipe_buffer *buf = pipe->bufs + curbuf;
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
176                         addr = ops->map(filp, pipe, buf);
177                         if (IS_ERR(addr)) {
178                                 if (!ret)
179                                         ret = PTR_ERR(addr);
180                                 break;
181                         }
182                         error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
183                         ops->unmap(pipe, buf);
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;
193                                 ops->release(pipe, buf);
194                                 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
195                                 pipe->curbuf = curbuf;
196                                 pipe->nrbufs = --bufs;
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;
205                 if (!pipe->writers)
206                         break;
207                 if (!pipe->waiting_writers) {
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) {
225                         wake_up_interruptible_sync(&pipe->wait);
226                         kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
227                 }
228                 pipe_wait(pipe);
229         }
230         mutex_unlock(&inode->i_mutex);
231         /* Signal writers asynchronously that there is more room.  */
232         if (do_wakeup) {
233                 wake_up_interruptible(&pipe->wait);
234                 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
235         }
236         if (ret > 0)
237                 file_accessed(filp);
238         return ret;
239 }
240
241 static ssize_t
242 pipe_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
248 static ssize_t
249 pipe_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;
253         struct pipe_inode_info *pipe;
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;
267         mutex_lock(&inode->i_mutex);
268         pipe = inode->i_pipe;
269
270         if (!pipe->readers) {
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 */
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;
281                 struct pipe_buf_operations *ops = buf->ops;
282                 int offset = buf->offset + buf->len;
283                 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
284                         void *addr;
285                         int error;
286
287                         addr = ops->map(filp, pipe, buf);
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);
294                         ops->unmap(pipe, buf);
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;
309                 if (!pipe->readers) {
310                         send_sig(SIGPIPE, current, 0);
311                         if (!ret) ret = -EPIPE;
312                         break;
313                 }
314                 bufs = pipe->nrbufs;
315                 if (bufs < PIPE_BUFFERS) {
316                         int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
317                         struct pipe_buffer *buf = pipe->bufs + newbuf;
318                         struct page *page = pipe->tmp_page;
319                         int error;
320
321                         if (!page) {
322                                 page = alloc_page(GFP_HIGHUSER);
323                                 if (unlikely(!page)) {
324                                         ret = ret ? : -ENOMEM;
325                                         break;
326                                 }
327                                 pipe->tmp_page = page;
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;
352                         pipe->nrbufs = ++bufs;
353                         pipe->tmp_page = NULL;
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) {
370                         wake_up_interruptible_sync(&pipe->wait);
371                         kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
372                         do_wakeup = 0;
373                 }
374                 pipe->waiting_writers++;
375                 pipe_wait(pipe);
376                 pipe->waiting_writers--;
377         }
378 out:
379         mutex_unlock(&inode->i_mutex);
380         if (do_wakeup) {
381                 wake_up_interruptible(&pipe->wait);
382                 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
383         }
384         if (ret > 0)
385                 file_update_time(filp);
386         return ret;
387 }
388
389 static ssize_t
390 pipe_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
397 static ssize_t
398 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
399 {
400         return -EBADF;
401 }
402
403 static ssize_t
404 bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
405 {
406         return -EBADF;
407 }
408
409 static int
410 pipe_ioctl(struct inode *pino, struct file *filp,
411            unsigned int cmd, unsigned long arg)
412 {
413         struct inode *inode = filp->f_dentry->d_inode;
414         struct pipe_inode_info *pipe;
415         int count, buf, nrbufs;
416
417         switch (cmd) {
418                 case FIONREAD:
419                         mutex_lock(&inode->i_mutex);
420                         pipe = inode->i_pipe;
421                         count = 0;
422                         buf = pipe->curbuf;
423                         nrbufs = pipe->nrbufs;
424                         while (--nrbufs >= 0) {
425                                 count += pipe->bufs[buf].len;
426                                 buf = (buf+1) & (PIPE_BUFFERS-1);
427                         }
428                         mutex_unlock(&inode->i_mutex);
429
430                         return put_user(count, (int __user *)arg);
431                 default:
432                         return -EINVAL;
433         }
434 }
435
436 /* No kernel lock held - fine */
437 static unsigned int
438 pipe_poll(struct file *filp, poll_table *wait)
439 {
440         unsigned int mask;
441         struct inode *inode = filp->f_dentry->d_inode;
442         struct pipe_inode_info *pipe = inode->i_pipe;
443         int nrbufs;
444
445         poll_wait(filp, &pipe->wait, wait);
446
447         /* Reading only -- no need for acquiring the semaphore.  */
448         nrbufs = pipe->nrbufs;
449         mask = 0;
450         if (filp->f_mode & FMODE_READ) {
451                 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
452                 if (!pipe->writers && filp->f_version != pipe->w_counter)
453                         mask |= POLLHUP;
454         }
455
456         if (filp->f_mode & FMODE_WRITE) {
457                 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
458                 /*
459                  * Most Unices do not set POLLERR for FIFOs but on Linux they
460                  * behave exactly like pipes for poll().
461                  */
462                 if (!pipe->readers)
463                         mask |= POLLERR;
464         }
465
466         return mask;
467 }
468
469 static int
470 pipe_release(struct inode *inode, int decr, int decw)
471 {
472         struct pipe_inode_info *pipe;
473
474         mutex_lock(&inode->i_mutex);
475         pipe = inode->i_pipe;
476         pipe->readers -= decr;
477         pipe->writers -= decw;
478         if (!pipe->readers && !pipe->writers) {
479                 free_pipe_info(inode);
480         } else {
481                 wake_up_interruptible(&pipe->wait);
482                 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
483                 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
484         }
485         mutex_unlock(&inode->i_mutex);
486
487         return 0;
488 }
489
490 static int
491 pipe_read_fasync(int fd, struct file *filp, int on)
492 {
493         struct inode *inode = filp->f_dentry->d_inode;
494         int retval;
495
496         mutex_lock(&inode->i_mutex);
497         retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
498         mutex_unlock(&inode->i_mutex);
499
500         if (retval < 0)
501                 return retval;
502
503         return 0;
504 }
505
506
507 static int
508 pipe_write_fasync(int fd, struct file *filp, int on)
509 {
510         struct inode *inode = filp->f_dentry->d_inode;
511         int retval;
512
513         mutex_lock(&inode->i_mutex);
514         retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
515         mutex_unlock(&inode->i_mutex);
516
517         if (retval < 0)
518                 return retval;
519
520         return 0;
521 }
522
523
524 static int
525 pipe_rdwr_fasync(int fd, struct file *filp, int on)
526 {
527         struct inode *inode = filp->f_dentry->d_inode;
528         int retval;
529
530         mutex_lock(&inode->i_mutex);
531
532         retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
533
534         if (retval >= 0)
535                 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
536
537         mutex_unlock(&inode->i_mutex);
538
539         if (retval < 0)
540                 return retval;
541
542         return 0;
543 }
544
545
546 static int
547 pipe_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
553 static int
554 pipe_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
560 static int
561 pipe_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
571 static int
572 pipe_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.  */
576         mutex_lock(&inode->i_mutex);
577         inode->i_pipe->readers++;
578         mutex_unlock(&inode->i_mutex);
579
580         return 0;
581 }
582
583 static int
584 pipe_write_open(struct inode *inode, struct file *filp)
585 {
586         mutex_lock(&inode->i_mutex);
587         inode->i_pipe->writers++;
588         mutex_unlock(&inode->i_mutex);
589
590         return 0;
591 }
592
593 static int
594 pipe_rdwr_open(struct inode *inode, struct file *filp)
595 {
596         mutex_lock(&inode->i_mutex);
597         if (filp->f_mode & FMODE_READ)
598                 inode->i_pipe->readers++;
599         if (filp->f_mode & FMODE_WRITE)
600                 inode->i_pipe->writers++;
601         mutex_unlock(&inode->i_mutex);
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  */
610 const struct file_operations read_fifo_fops = {
611         .llseek         = no_llseek,
612         .read           = pipe_read,
613         .readv          = pipe_readv,
614         .write          = bad_pipe_w,
615         .poll           = pipe_poll,
616         .ioctl          = pipe_ioctl,
617         .open           = pipe_read_open,
618         .release        = pipe_read_release,
619         .fasync         = pipe_read_fasync,
620 };
621
622 const struct file_operations write_fifo_fops = {
623         .llseek         = no_llseek,
624         .read           = bad_pipe_r,
625         .write          = pipe_write,
626         .writev         = pipe_writev,
627         .poll           = pipe_poll,
628         .ioctl          = pipe_ioctl,
629         .open           = pipe_write_open,
630         .release        = pipe_write_release,
631         .fasync         = pipe_write_fasync,
632 };
633
634 const struct file_operations rdwr_fifo_fops = {
635         .llseek         = no_llseek,
636         .read           = pipe_read,
637         .readv          = pipe_readv,
638         .write          = pipe_write,
639         .writev         = pipe_writev,
640         .poll           = pipe_poll,
641         .ioctl          = pipe_ioctl,
642         .open           = pipe_rdwr_open,
643         .release        = pipe_rdwr_release,
644         .fasync         = pipe_rdwr_fasync,
645 };
646
647 static struct file_operations read_pipe_fops = {
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
659 static struct file_operations write_pipe_fops = {
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
671 static struct file_operations rdwr_pipe_fops = {
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
684 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
685 {
686         struct pipe_inode_info *pipe;
687
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;
693         }
694
695         return pipe;
696 }
697
698 void __free_pipe_info(struct pipe_inode_info *pipe)
699 {
700         int i;
701
702         for (i = 0; i < PIPE_BUFFERS; i++) {
703                 struct pipe_buffer *buf = pipe->bufs + i;
704                 if (buf->ops)
705                         buf->ops->release(pipe, buf);
706         }
707         if (pipe->tmp_page)
708                 __free_page(pipe->tmp_page);
709         kfree(pipe);
710 }
711
712 void free_pipe_info(struct inode *inode)
713 {
714         __free_pipe_info(inode->i_pipe);
715         inode->i_pipe = NULL;
716 }
717
718 static struct vfsmount *pipe_mnt __read_mostly;
719 static int pipefs_delete_dentry(struct dentry *dentry)
720 {
721         return 1;
722 }
723 static struct dentry_operations pipefs_dentry_operations = {
724         .d_delete       = pipefs_delete_dentry,
725 };
726
727 static struct inode * get_pipe_inode(void)
728 {
729         struct inode *inode = new_inode(pipe_mnt->mnt_sb);
730         struct pipe_inode_info *pipe;
731
732         if (!inode)
733                 goto fail_inode;
734
735         pipe = alloc_pipe_info(inode);
736         if (!pipe)
737                 goto fail_iput;
738         inode->i_pipe = pipe;
739
740         pipe->readers = pipe->writers = 1;
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;
755
756         return inode;
757
758 fail_iput:
759         iput(inode);
760 fail_inode:
761         return NULL;
762 }
763
764 int 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
830 close_f12_inode_i_j:
831         put_unused_fd(j);
832 close_f12_inode_i:
833         put_unused_fd(i);
834 close_f12_inode:
835         free_pipe_info(inode);
836         iput(inode);
837 close_f12:
838         put_filp(f2);
839 close_f1:
840         put_filp(f1);
841 no_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
852 static 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
858 static struct file_system_type pipe_fs_type = {
859         .name           = "pipefs",
860         .get_sb         = pipefs_get_sb,
861         .kill_sb        = kill_anon_super,
862 };
863
864 static 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
877 static void __exit exit_pipe_fs(void)
878 {
879         unregister_filesystem(&pipe_fs_type);
880         mntput(pipe_mnt);
881 }
882
883 fs_initcall(init_pipe_fs);
884 module_exit(exit_pipe_fs);